blob: 2b1b4544756de0e1b9d0703f0aa1c3b2152a9fd9 [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 {
Ivan Lozano8ef256d2025-02-05 21:15:03 +0000380 if cc.CanUseSdk(mod) {
381 return String(mod.Properties.Sdk_version) != ""
382 }
Colin Crossc511bc52020-04-07 16:50:32 +0000383 return false
384}
385
Ivan Lozanod7586b62021-04-01 09:49:36 -0400386func (mod *Module) RelativeInstallPath() string {
387 if mod.compiler != nil {
388 return mod.compiler.relativeInstallPath()
389 }
390 return ""
391}
392
Ivan Lozano52767be2019-10-18 14:49:46 -0700393func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500394 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700395}
396
Jiyong Park7d55b612021-06-11 17:22:09 +0900397func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400398 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900399}
400
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400401func (mod *Module) SubName() string {
402 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700403}
404
Ivan Lozanof1868af2022-04-12 13:08:36 -0400405func (mod *Module) IsVndkPrebuiltLibrary() bool {
406 // Rust modules do not provide VNDK prebuilts
407 return false
408}
409
410func (mod *Module) IsVendorPublicLibrary() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000411 // Rust modules do not currently support vendor_public_library
412 return false
Ivan Lozanof1868af2022-04-12 13:08:36 -0400413}
414
415func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
416 // Rust modules to not provide Sdk variants
417 return false
418}
419
Colin Cross127bb8b2020-12-16 16:46:01 -0800420func (c *Module) IsVndkPrivate() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000421 // Rust modules do not currently support VNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800422 return false
423}
424
425func (c *Module) IsLlndk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000426 // Rust modules do not currently support LLNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800427 return false
428}
429
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400430func (mod *Module) KernelHeadersDecorator() bool {
431 return false
432}
433
Colin Cross1f3f1302021-04-26 18:37:44 -0700434func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000435 // Rust modules do not currently support LLNDK variants
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400436 return false
437}
438
Colin Cross5271fea2021-04-27 13:06:04 -0700439func (m *Module) NeedsVendorPublicLibraryVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000440 // Rust modules do not currently support vendor_public_library
Colin Cross5271fea2021-04-27 13:06:04 -0700441 return false
442}
443
Ivan Lozanod7586b62021-04-01 09:49:36 -0400444func (mod *Module) HasLlndkStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000445 // Rust modules do not currently support LLNDK stubs
Ivan Lozanod7586b62021-04-01 09:49:36 -0400446 return false
447}
448
Ivan Lozano52767be2019-10-18 14:49:46 -0700449func (mod *Module) SdkVersion() string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000450 return String(mod.Properties.Sdk_version)
Ivan Lozano52767be2019-10-18 14:49:46 -0700451}
452
Colin Crossc511bc52020-04-07 16:50:32 +0000453func (mod *Module) AlwaysSdk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000454 return mod.Properties.AlwaysSdk
Colin Crossc511bc52020-04-07 16:50:32 +0000455}
456
Jiyong Park2286afd2020-06-16 21:58:53 +0900457func (mod *Module) IsSdkVariant() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000458 return mod.Properties.IsSdkVariant
Jiyong Park2286afd2020-06-16 21:58:53 +0900459}
460
Colin Cross1348ce32020-10-01 13:37:16 -0700461func (mod *Module) SplitPerApiLevel() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000462 return cc.CanUseSdk(mod) && mod.IsCrt()
Colin Cross1348ce32020-10-01 13:37:16 -0700463}
464
Sasha Smundaka76acba2022-04-18 20:12:56 -0700465func (mod *Module) XrefRustFiles() android.Paths {
466 return mod.kytheFiles
467}
468
Ivan Lozanoffee3342019-08-27 12:03:00 -0700469type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400470 Dylibs []string
471 Rlibs []string
472 Rustlibs []string
473 Stdlibs []string
474 ProcMacros []string
475 SharedLibs []string
476 StaticLibs []string
477 WholeStaticLibs []string
478 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400480 // Used for data dependencies adjacent to tests
481 DataLibs []string
482 DataBins []string
483
Colin Crossfe605e12022-01-23 20:46:16 -0800484 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485}
486
487type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700488 DyLibs RustLibraries
489 RLibs RustLibraries
490 SharedLibs android.Paths
491 SharedLibDeps android.Paths
492 StaticLibs android.Paths
493 ProcMacros RustLibraries
494 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000495 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500496
497 // depFlags and depLinkFlags are rustc and linker (clang) flags.
498 depFlags []string
499 depLinkFlags []string
500
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400501 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500502 // Both of these are exported and propagate to dependencies.
Ivan Lozano1f10f682024-11-08 16:16:50 +0000503 linkDirs []string
504 rustLibObjects []string
505 staticLibObjects []string
506 wholeStaticLibObjects []string
507 sharedLibObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700508
Ivan Lozano0a468a42024-05-13 21:03:34 -0400509 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
510 // cc_library_static dependants of rlibs.
511 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
512 exportedLinkDirs []string
513
Ivan Lozano45901ed2020-07-24 16:05:01 -0400514 // Used by bindgen modules which call clang
515 depClangFlags []string
516 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400517 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400518 depSystemIncludePaths android.Paths
519
Colin Crossfe605e12022-01-23 20:46:16 -0800520 CrtBegin android.Paths
521 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700522
523 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500524 SrcDeps android.Paths
525 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700526
527 directImplementationDeps android.Paths
528 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700529}
530
531type RustLibraries []RustLibrary
532
533type RustLibrary struct {
534 Path android.Path
535 CrateName string
536}
537
Matthew Maurerbb3add12020-06-25 09:34:12 -0700538type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000539 exportLinkDirs(...string)
Ivan Lozano1f10f682024-11-08 16:16:50 +0000540 exportRustLibs(...string)
541 exportStaticLibs(...string)
542 exportWholeStaticLibs(...string)
543 exportSharedLibs(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700544}
545
Sasha Smundaka76acba2022-04-18 20:12:56 -0700546type xref interface {
547 XrefRustFiles() android.Paths
548}
549
Matthew Maurerbb3add12020-06-25 09:34:12 -0700550type flagExporter struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000551 linkDirs []string
552 ccLinkDirs []string
553 rustLibPaths []string
554 staticLibObjects []string
555 sharedLibObjects []string
556 wholeStaticLibObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700557}
558
Wen-yi Chu41326c12023-09-22 03:58:59 +0000559func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
560 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700561}
562
Ivan Lozano1f10f682024-11-08 16:16:50 +0000563func (flagExporter *flagExporter) exportRustLibs(flags ...string) {
564 flagExporter.rustLibPaths = android.FirstUniqueStrings(append(flagExporter.rustLibPaths, flags...))
565}
566
567func (flagExporter *flagExporter) exportStaticLibs(flags ...string) {
568 flagExporter.staticLibObjects = android.FirstUniqueStrings(append(flagExporter.staticLibObjects, flags...))
569}
570
571func (flagExporter *flagExporter) exportSharedLibs(flags ...string) {
572 flagExporter.sharedLibObjects = android.FirstUniqueStrings(append(flagExporter.sharedLibObjects, flags...))
573}
574
575func (flagExporter *flagExporter) exportWholeStaticLibs(flags ...string) {
576 flagExporter.wholeStaticLibObjects = android.FirstUniqueStrings(append(flagExporter.wholeStaticLibObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400577}
578
Colin Cross0de8a1e2020-09-18 14:15:30 -0700579func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800580 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Ivan Lozano1f10f682024-11-08 16:16:50 +0000581 LinkDirs: flagExporter.linkDirs,
582 RustLibObjects: flagExporter.rustLibPaths,
583 StaticLibObjects: flagExporter.staticLibObjects,
584 WholeStaticLibObjects: flagExporter.wholeStaticLibObjects,
585 SharedLibPaths: flagExporter.sharedLibObjects,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700586 })
587}
588
Matthew Maurerbb3add12020-06-25 09:34:12 -0700589var _ exportedFlagsProducer = (*flagExporter)(nil)
590
591func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700592 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700593}
594
Colin Cross0de8a1e2020-09-18 14:15:30 -0700595type FlagExporterInfo struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000596 Flags []string
597 LinkDirs []string
598 RustLibObjects []string
599 StaticLibObjects []string
600 WholeStaticLibObjects []string
601 SharedLibPaths []string
Colin Cross0de8a1e2020-09-18 14:15:30 -0700602}
603
Colin Crossbc7d76c2023-12-12 16:39:03 -0800604var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700605
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400606func (mod *Module) isCoverageVariant() bool {
607 return mod.coverage.Properties.IsCoverageVariant
608}
609
610var _ cc.Coverage = (*Module)(nil)
611
Colin Crosse1a85552024-06-14 12:17:37 -0700612func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400613 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
614}
615
Ivan Lozanod7586b62021-04-01 09:49:36 -0400616func (mod *Module) VndkVersion() string {
617 return mod.Properties.VndkVersion
618}
619
Ivan Lozano0a468a42024-05-13 21:03:34 -0400620func (mod *Module) ExportedCrateLinkDirs() []string {
621 return mod.exportedLinkDirs
622}
623
Ivan Lozanod7586b62021-04-01 09:49:36 -0400624func (mod *Module) PreventInstall() bool {
625 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400626}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000627func (c *Module) ForceDisableSanitizers() {
628 c.sanitize.Properties.ForceDisable = true
629}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400630
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400631func (mod *Module) MarkAsCoverageVariant(coverage bool) {
632 mod.coverage.Properties.IsCoverageVariant = coverage
633}
634
635func (mod *Module) EnableCoverageIfNeeded() {
636 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700637}
638
639func defaultsFactory() android.Module {
640 return DefaultsFactory()
641}
642
643type Defaults struct {
644 android.ModuleBase
645 android.DefaultsModuleBase
646}
647
648func DefaultsFactory(props ...interface{}) android.Module {
649 module := &Defaults{}
650
651 module.AddProperties(props...)
652 module.AddProperties(
653 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800654 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500655 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100656 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400657 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658 &BaseCompilerProperties{},
659 &BinaryCompilerProperties{},
660 &LibraryCompilerProperties{},
661 &ProcMacroCompilerProperties{},
662 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400663 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700664 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400665 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400666 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200667 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500668 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000669 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670 )
671
672 android.InitDefaultsModule(module)
673 return module
674}
675
676func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700677 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678}
679
Ivan Lozano183a3212019-10-18 14:18:45 -0700680func (mod *Module) CcLibrary() bool {
681 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500682 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700683 return true
684 }
685 }
686 return false
687}
688
689func (mod *Module) CcLibraryInterface() bool {
690 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400691 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
692 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000693 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700694 return true
695 }
696 }
697 return false
698}
699
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400700func (mod *Module) RustLibraryInterface() bool {
701 if mod.compiler != nil {
702 if _, ok := mod.compiler.(libraryInterface); ok {
703 return true
704 }
705 }
706 return false
707}
708
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500709func (mod *Module) IsFuzzModule() bool {
710 if _, ok := mod.compiler.(*fuzzDecorator); ok {
711 return true
712 }
713 return false
714}
715
716func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
717 return mod.FuzzModule
718}
719
720func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
721 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
722 return fuzzer.fuzzPackagedModule
723 }
724 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
725}
726
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000727func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500728 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
729 return fuzzer.sharedLibraries
730 }
731 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
732}
733
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400734func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400735 if mod.compiler != nil {
736 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400737 }
738 return nil
739}
740
Ivan Lozano183a3212019-10-18 14:18:45 -0700741func (mod *Module) SetStatic() {
742 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700743 if library, ok := mod.compiler.(libraryInterface); ok {
744 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700745 return
746 }
747 }
748 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
749}
750
751func (mod *Module) SetShared() {
752 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700753 if library, ok := mod.compiler.(libraryInterface); ok {
754 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700755 return
756 }
757 }
758 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
759}
760
Ivan Lozano183a3212019-10-18 14:18:45 -0700761func (mod *Module) BuildStaticVariant() bool {
762 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700763 if library, ok := mod.compiler.(libraryInterface); ok {
764 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700765 }
766 }
767 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
768}
769
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400770func (mod *Module) BuildRlibVariant() bool {
771 if mod.compiler != nil {
772 if library, ok := mod.compiler.(libraryInterface); ok {
773 return library.buildRlib()
774 }
775 }
776 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
777}
778
Ivan Lozano183a3212019-10-18 14:18:45 -0700779func (mod *Module) BuildSharedVariant() bool {
780 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700781 if library, ok := mod.compiler.(libraryInterface); ok {
782 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700783 }
784 }
785 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
786}
787
Ivan Lozano183a3212019-10-18 14:18:45 -0700788func (mod *Module) Module() android.Module {
789 return mod
790}
791
Ivan Lozano183a3212019-10-18 14:18:45 -0700792func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400793 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700794}
795
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400796func (mod *Module) CoverageFiles() android.Paths {
797 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800798 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400799 }
800 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
801}
802
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400803// Rust does not produce gcno files, and therefore does not produce a coverage archive.
804func (mod *Module) CoverageOutputFile() android.OptionalPath {
805 return android.OptionalPath{}
806}
807
808func (mod *Module) IsNdk(config android.Config) bool {
809 return false
810}
811
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000812func (mod *Module) IsStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000813 if lib, ok := mod.compiler.(libraryInterface); ok {
814 return lib.BuildStubs()
815 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000816 return false
817}
818
Spandan Das10c41362024-12-03 01:33:09 +0000819func (mod *Module) HasStubsVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000820 if lib, ok := mod.compiler.(libraryInterface); ok {
821 return lib.HasStubsVariants()
822 }
Spandan Das10c41362024-12-03 01:33:09 +0000823 return false
824}
825
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000826func (mod *Module) ApexSdkVersion() android.ApiLevel {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000827 return mod.apexSdkVersion
828}
829
830func (mod *Module) RustApexExclude() bool {
831 return mod.ApexExclude()
832}
833
834func (mod *Module) getSharedFlags() *cc.SharedFlags {
835 shared := &mod.sharedFlags
836 if shared.FlagsMap == nil {
837 shared.NumSharedFlags = 0
838 shared.FlagsMap = make(map[string]string)
839 }
840 return shared
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000841}
842
843func (mod *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000844 name := mod.BaseModuleName()
845 if versioned, ok := mod.compiler.(cc.VersionedInterface); ok {
846 name = versioned.ImplementationModuleName(name)
847 }
848 return name
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000849}
850
851func (mod *Module) Multilib() string {
852 return mod.Arch().ArchType.Multilib
853}
854
855func (mod *Module) IsCrt() bool {
856 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400857 return false
858}
859
Jiyong Park459feca2020-12-15 11:02:21 +0900860func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900861 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900862 return false
863 }
864
Jiyong Park459feca2020-12-15 11:02:21 +0900865 // The apex variant is not installable because it is included in the APEX and won't appear
866 // in the system partition as a standalone file.
867 if !apexInfo.IsForPlatform() {
868 return false
869 }
870
Jiyong Parke54f07e2021-04-07 15:08:04 +0900871 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900872}
873
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500874func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800875 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
876 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500877}
878
Ivan Lozano183a3212019-10-18 14:18:45 -0700879var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000880var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700881
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882func (mod *Module) Init() android.Module {
883 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500884 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885
Yi Kong46c6e592022-01-20 22:55:00 +0800886 if mod.afdo != nil {
887 mod.AddProperties(mod.afdo.props()...)
888 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 if mod.compiler != nil {
890 mod.AddProperties(mod.compiler.compilerProps()...)
891 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400892 if mod.coverage != nil {
893 mod.AddProperties(mod.coverage.props()...)
894 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200895 if mod.clippy != nil {
896 mod.AddProperties(mod.clippy.props()...)
897 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400898 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700899 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400900 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500901 if mod.sanitize != nil {
902 mod.AddProperties(mod.sanitize.props()...)
903 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400904
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900906 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907
908 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909 return mod
910}
911
912func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
913 return &Module{
914 hod: hod,
915 multilib: multilib,
916 }
917}
918func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
919 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800920 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400921 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200922 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500923 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924 return module
925}
926
927type ModuleContext interface {
928 android.ModuleContext
929 ModuleContextIntf
930}
931
932type BaseModuleContext interface {
933 android.BaseModuleContext
934 ModuleContextIntf
935}
936
937type DepsContext interface {
938 android.BottomUpMutatorContext
939 ModuleContextIntf
940}
941
942type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200943 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945}
946
947type depsContext struct {
948 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949}
950
951type moduleContext struct {
952 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953}
954
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200955type baseModuleContext struct {
956 android.BaseModuleContext
957}
958
959func (ctx *moduleContext) RustModule() *Module {
960 return ctx.Module().(*Module)
961}
962
963func (ctx *moduleContext) toolchain() config.Toolchain {
964 return ctx.RustModule().toolchain(ctx)
965}
966
967func (ctx *depsContext) RustModule() *Module {
968 return ctx.Module().(*Module)
969}
970
971func (ctx *depsContext) toolchain() config.Toolchain {
972 return ctx.RustModule().toolchain(ctx)
973}
974
975func (ctx *baseModuleContext) RustModule() *Module {
976 return ctx.Module().(*Module)
977}
978
979func (ctx *baseModuleContext) toolchain() config.Toolchain {
980 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400981}
982
983func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700984 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
985 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
986 return false
987 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400988 return mod.compiler != nil && mod.compiler.nativeCoverage()
989}
990
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000991func (mod *Module) SetStl(s string) {
992 // STL is a CC concept; do nothing for Rust
993}
994
995func (mod *Module) SetSdkVersion(s string) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000996 mod.Properties.Sdk_version = StringPtr(s)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000997}
998
999func (mod *Module) SetMinSdkVersion(s string) {
1000 mod.Properties.Min_sdk_version = StringPtr(s)
1001}
1002
1003func (mod *Module) VersionedInterface() cc.VersionedInterface {
1004 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
1005 return mod.compiler.(cc.VersionedInterface)
1006 }
1007 return nil
1008}
1009
Ivan Lozanod7586b62021-04-01 09:49:36 -04001010func (mod *Module) EverInstallable() bool {
1011 return mod.compiler != nil &&
1012 // Check to see whether the module is actually ever installable.
1013 mod.compiler.everInstallable()
1014}
1015
1016func (mod *Module) Installable() *bool {
1017 return mod.Properties.Installable
1018}
1019
Ivan Lozano872d5792022-03-23 17:31:39 -04001020func (mod *Module) ProcMacro() bool {
1021 if pm, ok := mod.compiler.(procMacroInterface); ok {
1022 return pm.ProcMacro()
1023 }
1024 return false
1025}
1026
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
1028 if mod.cachedToolchain == nil {
1029 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
1030 }
1031 return mod.cachedToolchain
1032}
1033
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +02001034func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
1035 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
1036}
1037
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1039}
1040
1041func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1042 ctx := &moduleContext{
1043 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001045
Colin Crossff694a82023-12-13 15:54:49 -08001046 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +09001047 if !apexInfo.IsForPlatform() {
1048 mod.hideApexVariantFromMake = true
1049 }
1050
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -05001052 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
1053
Ivan Lozanof1868af2022-04-12 13:08:36 -04001054 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -07001055
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 if !toolchain.Supported() {
1057 // This toolchain's unsupported, there's nothing to do for this mod.
1058 return
1059 }
1060
1061 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001062 // Export linkDirs for CC rust generatedlibs
1063 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
1064 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
1065
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066 flags := Flags{
1067 Toolchain: toolchain,
1068 }
1069
Ivan Lozano67eada32021-09-23 11:50:33 -04001070 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +08001071 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -05001072 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +08001073 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074 if mod.compiler != nil {
1075 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -04001076 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +00001077 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001078 }
1079 if mod.coverage != nil {
1080 flags, deps = mod.coverage.flags(ctx, flags, deps)
1081 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001082 if mod.clippy != nil {
1083 flags, deps = mod.clippy.flags(ctx, flags, deps)
1084 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001085 if mod.sanitize != nil {
1086 flags, deps = mod.sanitize.flags(ctx, flags, deps)
1087 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001088
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001089 // SourceProvider needs to call GenerateSource() before compiler calls
1090 // compile() so it can provide the source. A SourceProvider has
1091 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1092 // variant is responsible for effectively generating the source. The
1093 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001094 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001095 if mod.compiler.(libraryInterface).source() {
1096 mod.sourceProvider.GenerateSource(ctx, deps)
1097 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1098 } else {
Yu Liu727204c2025-01-23 20:58:32 +00001099 sourceMod := actx.GetDirectDepProxyWithTag(mod.Name(), sourceDepTag)
1100 sourceLib := android.OtherModuleProviderOrDefault(ctx, sourceMod, RustInfoProvider).SourceProviderInfo
1101 mod.sourceProvider.setOutputFiles(sourceLib.Srcs)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001102 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001103 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001104 }
1105
1106 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001107 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001108 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001109 if ctx.Failed() {
1110 return
1111 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001112 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001113 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001114 if buildOutput.kytheFile != nil {
1115 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1116 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001117 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001118
Dan Albert06feee92021-03-19 15:06:02 -07001119 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1120
Colin Crossff694a82023-12-13 15:54:49 -08001121 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001122 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001123 // If the module has been specifically configure to not be installed then
1124 // hide from make as otherwise it will break when running inside make as the
1125 // output path to install will not be specified. Not all uninstallable
1126 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001127 // side dependencies. In particular, proc-macros need to be captured in the
1128 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001129 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001130 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001131 } else if !mod.installable(apexInfo) {
1132 mod.SkipInstall()
1133 }
1134
1135 // Still call install though, the installs will be stored as PackageSpecs to allow
1136 // using the outputs in a genrule.
1137 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001138 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001139 if ctx.Failed() {
1140 return
1141 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001142 // Export your own directory as a linkDir
1143 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1144
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001145 }
Chris Wailes74be7642021-07-22 16:20:28 -07001146
Colin Crossb614cd42024-10-11 12:52:21 -07001147 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1148 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1149 })
1150
Chris Wailes74be7642021-07-22 16:20:28 -07001151 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001152 }
Wei Lia1aa2972024-06-21 13:08:51 -07001153
Yu Liuf6f85492025-01-13 21:02:36 +00001154 linkableInfo := cc.CreateCommonLinkableInfo(ctx, mod)
Yu Liu8024b922024-12-20 23:31:32 +00001155 linkableInfo.Static = mod.Static()
1156 linkableInfo.Shared = mod.Shared()
1157 linkableInfo.CrateName = mod.CrateName()
1158 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001159 if lib, ok := mod.compiler.(cc.VersionedInterface); ok {
1160 linkableInfo.StubsVersion = lib.StubsVersion()
1161 }
1162
Yu Liu8024b922024-12-20 23:31:32 +00001163 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1164
1165 rustInfo := &RustInfo{
1166 AndroidMkSuffix: mod.AndroidMkSuffix(),
1167 RustSubName: mod.Properties.RustSubName,
1168 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1169 }
1170 if mod.compiler != nil {
1171 rustInfo.CompilerInfo = &CompilerInfo{
1172 NoStdlibs: mod.compiler.noStdlibs(),
1173 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1174 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1175 }
1176 if lib, ok := mod.compiler.(libraryInterface); ok {
1177 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1178 Dylib: lib.dylib(),
1179 Rlib: lib.rlib(),
1180 }
1181 }
1182 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1183 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1184 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1185 }
1186 }
1187 }
1188 if mod.sourceProvider != nil {
Yu Liu727204c2025-01-23 20:58:32 +00001189 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1190 Srcs: mod.sourceProvider.Srcs(),
1191 }
Yu Liu8024b922024-12-20 23:31:32 +00001192 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu727204c2025-01-23 20:58:32 +00001193 rustInfo.SourceProviderInfo.ProtobufDecoratorInfo = &ProtobufDecoratorInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001194 }
1195 }
1196 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001197
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001198 ccInfo := &cc.CcInfo{
1199 IsPrebuilt: mod.IsPrebuilt(),
1200 }
1201
Ivan Lozano9587f452025-01-08 03:17:19 +00001202 // Define the linker info if compiler != nil because Rust currently
1203 // does compilation and linking in one step. If this changes in the future,
1204 // move this as appropriate.
Cole Faustc9b88c92025-02-06 17:58:26 -08001205 baseCompilerProps := mod.compiler.baseCompilerProps()
Ivan Lozano9587f452025-01-08 03:17:19 +00001206 ccInfo.LinkerInfo = &cc.LinkerInfo{
Cole Faustc9b88c92025-02-06 17:58:26 -08001207 WholeStaticLibs: baseCompilerProps.Whole_static_libs.GetOrDefault(ctx, nil),
1208 StaticLibs: baseCompilerProps.Static_libs.GetOrDefault(ctx, nil),
1209 SharedLibs: baseCompilerProps.Shared_libs.GetOrDefault(ctx, nil),
Ivan Lozano9587f452025-01-08 03:17:19 +00001210 }
1211
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001212 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1213
mrziwang0cbd3b02024-06-20 16:39:25 -07001214 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001215
1216 buildComplianceMetadataInfo(ctx, mod, deps)
Cole Faust58eef4f2025-01-29 15:14:17 -08001217
1218 moduleInfoJSON := ctx.ModuleInfoJSON()
1219 if mod.compiler != nil {
1220 mod.compiler.moduleInfoJSON(ctx, moduleInfoJSON)
1221 }
mrziwang0cbd3b02024-06-20 16:39:25 -07001222}
1223
1224func (mod *Module) setOutputFiles(ctx ModuleContext) {
1225 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1226 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1227 } else if mod.OutputFile().Valid() {
1228 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1229 } else {
1230 ctx.SetOutputFiles(android.Paths{}, "")
1231 }
1232 if mod.compiler != nil {
1233 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1234 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001235}
1236
Wei Lia1aa2972024-06-21 13:08:51 -07001237func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1238 // Dump metadata that can not be done in android/compliance-metadata.go
1239 metadataInfo := ctx.ComplianceMetadataInfo()
1240 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1241 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1242
1243 // Static libs
Yu Liu727204c2025-01-23 20:58:32 +00001244 staticDeps := ctx.GetDirectDepsProxyWithTag(rlibDepTag)
Wei Lia1aa2972024-06-21 13:08:51 -07001245 staticDepNames := make([]string, 0, len(staticDeps))
1246 for _, dep := range staticDeps {
1247 staticDepNames = append(staticDepNames, dep.Name())
1248 }
Yu Liu727204c2025-01-23 20:58:32 +00001249 ccStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(false))
Wei Lia1aa2972024-06-21 13:08:51 -07001250 for _, dep := range ccStaticDeps {
1251 staticDepNames = append(staticDepNames, dep.Name())
1252 }
1253
1254 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1255 // C static libraries
1256 for _, dep := range deps.StaticLibs {
1257 staticDepPaths = append(staticDepPaths, dep.String())
1258 }
1259 // Rust static libraries
1260 for _, dep := range deps.RLibs {
1261 staticDepPaths = append(staticDepPaths, dep.Path.String())
1262 }
1263 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1264 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1265
1266 // C Whole static libs
Yu Liu727204c2025-01-23 20:58:32 +00001267 ccWholeStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(true))
Wei Lia1aa2972024-06-21 13:08:51 -07001268 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1269 for _, dep := range ccStaticDeps {
1270 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1271 }
1272 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1273}
1274
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275func (mod *Module) deps(ctx DepsContext) Deps {
1276 deps := Deps{}
1277
1278 if mod.compiler != nil {
1279 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001280 }
1281 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001282 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 }
1284
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001285 if mod.coverage != nil {
1286 deps = mod.coverage.deps(ctx, deps)
1287 }
1288
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001289 if mod.sanitize != nil {
1290 deps = mod.sanitize.deps(ctx, deps)
1291 }
1292
Ivan Lozanoffee3342019-08-27 12:03:00 -07001293 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1294 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001295 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001296 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1297 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1298 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001299 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001300 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001301 return deps
1302
1303}
1304
Ivan Lozanoffee3342019-08-27 12:03:00 -07001305type dependencyTag struct {
1306 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001307 name string
1308 library bool
1309 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001310 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001311}
1312
Jiyong Park65b62242020-11-25 12:44:59 +09001313// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1314// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1315func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001316 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001317}
1318
1319var _ android.InstallNeededDependencyTag = dependencyTag{}
1320
Colin Cross65cb3142021-12-10 23:05:02 +00001321func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1322 if d.library && d.dynamic {
1323 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1324 }
1325 return nil
1326}
1327
Yu Liuc8884602024-03-15 18:48:38 +00001328func (d dependencyTag) PropagateAconfigValidation() bool {
1329 return d == rlibDepTag || d == sourceDepTag
1330}
1331
1332var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1333
Colin Cross65cb3142021-12-10 23:05:02 +00001334var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1335
Ivan Lozanoffee3342019-08-27 12:03:00 -07001336var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001337 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1338 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001339 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001340 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001341 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001342 dataLibDepTag = dependencyTag{name: "data lib"}
1343 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001344)
1345
Jiyong Park99644e92020-11-17 22:21:02 +09001346func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1347 tag, ok := depTag.(dependencyTag)
1348 return ok && tag == dylibDepTag
1349}
1350
Jiyong Park94e22fd2021-04-08 18:19:15 +09001351func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1352 tag, ok := depTag.(dependencyTag)
1353 return ok && tag == rlibDepTag
1354}
1355
Matthew Maurer0f003b12020-06-29 14:34:06 -07001356type autoDep struct {
1357 variation string
1358 depTag dependencyTag
1359}
1360
1361var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001362 sourceVariation = "source"
1363 rlibVariation = "rlib"
1364 dylibVariation = "dylib"
1365 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1366 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001367)
1368
1369type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001370 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001371}
1372
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001373func (mod *Module) begin(ctx BaseModuleContext) {
1374 if mod.coverage != nil {
1375 mod.coverage.begin(ctx)
1376 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001377 if mod.sanitize != nil {
1378 mod.sanitize.begin(ctx)
1379 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001380
1381 if mod.UseSdk() && mod.IsSdkVariant() {
1382 sdkVersion := ""
1383 if ctx.Device() {
1384 sdkVersion = mod.SdkVersion()
1385 }
1386 version, err := cc.NativeApiLevelFromUser(ctx, sdkVersion)
1387 if err != nil {
1388 ctx.PropertyErrorf("sdk_version", err.Error())
1389 mod.Properties.Sdk_version = nil
1390 } else {
1391 mod.Properties.Sdk_version = StringPtr(version.String())
1392 }
1393 }
1394
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001395}
1396
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001397func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001398 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001399 return p.prebuilt()
1400 }
1401 return nil
1402}
1403
Kiyoung Kim37693d02024-04-04 09:56:15 +09001404func (mod *Module) Symlinks() []string {
1405 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1406 return nil
1407}
1408
Yu Liu8024b922024-12-20 23:31:32 +00001409func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1410 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001411 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001412 if rustInfo.SnapshotInfo != nil {
1413 baseName := linkableInfo.BaseModuleName
1414 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001415 }
1416 }
Yu Liu8024b922024-12-20 23:31:32 +00001417 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001418}
1419
Yu Liu8024b922024-12-20 23:31:32 +00001420func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001421 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001422 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1423 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001424 }
1425 }
1426}
Andrew Walbran52533232024-03-19 11:36:04 +00001427
Ivan Lozanoffee3342019-08-27 12:03:00 -07001428func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1429 var depPaths PathDeps
1430
Yu Liu8024b922024-12-20 23:31:32 +00001431 directRlibDeps := []*cc.LinkableInfo{}
1432 directDylibDeps := []*cc.LinkableInfo{}
1433 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001434 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001435 directStaticLibDeps := [](*cc.LinkableInfo){}
1436 directSrcProvidersDeps := []*android.ModuleProxy{}
1437 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001438
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001439 // For the dependency from platform to apex, use the latest stubs
1440 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001441 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001442 if !apexInfo.IsForPlatform() {
1443 mod.apexSdkVersion = apexInfo.MinSdkVersion
1444 }
1445
1446 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1447 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1448 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1449 // (b/144430859)
1450 mod.apexSdkVersion = android.FutureApiLevel
1451 }
1452
Spandan Das604f3762023-03-16 22:51:40 +00001453 skipModuleList := map[string]bool{}
1454
Colin Crossa14fb6a2024-10-23 16:57:06 -07001455 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001456 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001457
Yu Liu8024b922024-12-20 23:31:32 +00001458 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001459 depName := ctx.OtherModuleName(dep)
1460 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001461 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1462
Spandan Das604f3762023-03-16 22:51:40 +00001463 if _, exists := skipModuleList[depName]; exists {
1464 return
1465 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001466
1467 if depTag == android.DarwinUniversalVariantTag {
1468 return
1469 }
1470
Yu Liu8024b922024-12-20 23:31:32 +00001471 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1472 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1473 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1474 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1475 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001477 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001478
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001479 switch {
1480 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001481 dylib := rustInfo.CompilerInfo.LibraryInfo
1482 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1484 return
1485 }
Yu Liu8024b922024-12-20 23:31:32 +00001486 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001487 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001488 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1489
Colin Crossb614cd42024-10-11 12:52:21 -07001490 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1491 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1492 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1493 }
1494
Yu Liu8024b922024-12-20 23:31:32 +00001495 if !rustInfo.CompilerInfo.NoStdlibs {
1496 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1497 if ctx.Device() {
1498 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1499 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001500 if rustDepStdLinkage != modStdLinkage {
1501 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1502 return
1503 }
1504 }
1505
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001506 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001507 rlib := rustInfo.CompilerInfo.LibraryInfo
1508 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001509 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001510 return
1511 }
Yu Liu8024b922024-12-20 23:31:32 +00001512 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001513 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001514 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1515
Ivan Lozano0a468a42024-05-13 21:03:34 -04001516 // rust_ffi rlibs may export include dirs, so collect those here.
1517 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1518 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001519 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001520
Colin Crossb614cd42024-10-11 12:52:21 -07001521 // rlibs are not installed, so don't add the output file to directImplementationDeps
1522 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1523 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1524 }
1525
Yu Liu8024b922024-12-20 23:31:32 +00001526 if !rustInfo.CompilerInfo.NoStdlibs {
1527 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1528 if ctx.Device() {
1529 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1530 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001531 if rustDepStdLinkage != modStdLinkage {
1532 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1533 return
1534 }
1535 }
1536
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001537 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001538 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001539 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001540 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001541 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001542
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001543 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001544 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001545 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001546 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001547 case cc.IsStaticDepTag(depTag):
1548 // Rust FFI rlibs should not be declared in a Rust modules
1549 // "static_libs" list as we can't handle them properly at the
1550 // moment (for example, they only produce an rlib-std variant).
1551 // Instead, a normal rust_library variant should be used.
1552 ctx.PropertyErrorf("static_libs",
1553 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1554 depName)
1555
Paul Duffind5cf92e2021-07-09 17:38:55 +01001556 }
1557
Yu Liu8024b922024-12-20 23:31:32 +00001558 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001559
Paul Duffind5cf92e2021-07-09 17:38:55 +01001560 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001561 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1562 // OS/Arch variant is used.
1563 var helper string
1564 if ctx.Host() {
1565 helper = "missing 'host_supported'?"
1566 } else {
1567 helper = "device module defined?"
1568 }
1569
Yu Liu8024b922024-12-20 23:31:32 +00001570 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001571 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1572 return
Yu Liu8024b922024-12-20 23:31:32 +00001573 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001574 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1575 return
1576 }
Yu Liu8024b922024-12-20 23:31:32 +00001577 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001578 }
1579
Ivan Lozano0a468a42024-05-13 21:03:34 -04001580 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001581
1582 //Append the dependencies exported objects, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001583 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001584 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001585 depPaths.rustLibObjects = append(depPaths.rustLibObjects, exportedInfo.RustLibObjects...)
1586 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, exportedInfo.SharedLibPaths...)
1587 depPaths.staticLibObjects = append(depPaths.staticLibObjects, exportedInfo.StaticLibObjects...)
1588 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, exportedInfo.WholeStaticLibObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001589 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001590 }
1591
Ivan Lozanoffee3342019-08-27 12:03:00 -07001592 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001593 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001594 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001595 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001596 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001597 }
1598 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001599
Ivan Lozanod106efe2023-09-21 23:30:26 -04001600 if depTag == sourceDepTag {
1601 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001602 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001603 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001604 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1605 }
1606 }
1607 }
Yu Liu8024b922024-12-20 23:31:32 +00001608 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001609 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001610 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1611 if !hasRustInfo {
1612 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001613 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1614 return
1615 }
Yu Liu8024b922024-12-20 23:31:32 +00001616 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001617 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1618 return
1619 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001620 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001621 ccLibPath := linkableInfo.OutputFile
1622 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001623 if !ctx.Config().AllowMissingDependencies() {
1624 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1625 } else {
1626 ctx.AddMissingDependencies([]string{depName})
1627 }
1628 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001629 }
1630
Ivan Lozano1f10f682024-11-08 16:16:50 +00001631 linkPath := linkPathFromFilePath(ccLibPath.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001632
Ivan Lozanoffee3342019-08-27 12:03:00 -07001633 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001634 switch {
1635 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001636 if cc.IsWholeStaticLib(depTag) {
1637 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1638 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001639 if mod.Binary() {
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001640 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1641 // final linkage, pass the args directly to the linker to handle these cases.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001642 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", ccLibPath.Path().String(), "-Wl,--no-whole-archive"}...)
1643 } else if libName, ok := libNameFromFilePath(ccLibPath.Path()); ok {
1644 depPaths.depFlags = append(depPaths.depFlags, "-lstatic:+whole-archive="+libName)
1645 depPaths.depLinkFlags = append(depPaths.depLinkFlags, ccLibPath.Path().String())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001646 } else {
1647 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 -05001648 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001649 }
1650
Ivan Lozano1f10f682024-11-08 16:16:50 +00001651 if cc.IsWholeStaticLib(depTag) {
1652 // Add whole staticlibs to wholeStaticLibObjects to propagate to Rust all dependents.
1653 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, ccLibPath.String())
1654 } else {
1655 // Otherwise add to staticLibObjects, which only propagate through rlibs to their dependents.
1656 depPaths.staticLibObjects = append(depPaths.staticLibObjects, ccLibPath.String())
1657
1658 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001659 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1660
Colin Cross313aa542023-12-13 13:47:44 -08001661 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001662 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1663 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1664 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1665 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Yu Liu8024b922024-12-20 23:31:32 +00001666 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001667
1668 // Record baseLibName for snapshots.
1669 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1670
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001671 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001672 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001673 // For the shared lib dependencies, we may link to the stub variant
1674 // of the dependency depending on the context (e.g. if this
1675 // dependency crosses the APEX boundaries).
1676 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1677
Colin Crossb614cd42024-10-11 12:52:21 -07001678 if !sharedLibraryInfo.IsStubs {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001679 // TODO(b/362509506): remove this additional check once all apex_exclude uses are switched to stubs.
1680 if !linkableInfo.RustApexExclude {
1681 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1682 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1683 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1684 }
Colin Crossb614cd42024-10-11 12:52:21 -07001685 }
1686 }
1687
Jiyong Park7d55b612021-06-11 17:22:09 +09001688 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1689 // object (either from stub or non-stub) to use.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001690 ccLibPath = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1691 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001692 if !ctx.Config().AllowMissingDependencies() {
1693 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1694 } else {
1695 ctx.AddMissingDependencies([]string{depName})
1696 }
1697 return
1698 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001699 linkPath = linkPathFromFilePath(ccLibPath.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001700
Ivan Lozanoffee3342019-08-27 12:03:00 -07001701 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001702 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, ccLibPath.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001703 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1704 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1705 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1706 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001707 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001708
1709 // Record baseLibName for snapshots.
1710 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1711
Cole Faustb6e6f992023-08-17 17:42:26 -07001712 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001713 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001714 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001715 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001716 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1717 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1718 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001719 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001720 case depTag == cc.CrtBeginDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001721 depPaths.CrtBegin = append(depPaths.CrtBegin, ccLibPath.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001722 case depTag == cc.CrtEndDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001723 depPaths.CrtEnd = append(depPaths.CrtEnd, ccLibPath.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001724 }
1725
Ivan Lozano1f10f682024-11-08 16:16:50 +00001726 // Make sure shared dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001727 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1728 lib.exportLinkDirs(linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001729 lib.exportSharedLibs(ccLibPath.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001730 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001731 } else {
1732 switch {
1733 case depTag == cc.CrtBeginDepTag:
1734 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1735 case depTag == cc.CrtEndDepTag:
1736 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1737 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001738 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001739
Yu Liuc41eae52025-01-14 01:03:08 +00001740 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001741 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001742 // These are usually genrules which don't have per-target variants.
1743 directSrcDeps = append(directSrcDeps, srcDep)
1744 }
1745 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001746 })
1747
Colin Crossa14fb6a2024-10-23 16:57:06 -07001748 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001749
1750 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001751 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001752 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001753 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001754 if alias, aliased := aliases[crateName]; aliased {
1755 crateName = alias
1756 }
Yu Liu8024b922024-12-20 23:31:32 +00001757 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001758 }
1759 var dylibDepFiles RustLibraries
1760 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001761 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001762 if alias, aliased := aliases[crateName]; aliased {
1763 crateName = alias
1764 }
Yu Liu8024b922024-12-20 23:31:32 +00001765 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001766 }
1767 var procMacroDepFiles RustLibraries
1768 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001769 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001770 if alias, aliased := aliases[crateName]; aliased {
1771 crateName = alias
1772 }
Yu Liu8024b922024-12-20 23:31:32 +00001773 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001774 }
1775
Colin Cross004bd3f2023-10-02 11:39:17 -07001776 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001777 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001778 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001779 }
1780
Colin Cross004bd3f2023-10-02 11:39:17 -07001781 var sharedLibFiles android.Paths
1782 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001783 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001784 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001785 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001786 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001787 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001788 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001789 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001790 }
1791
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001792 var srcProviderDepFiles android.Paths
1793 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001794 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001795 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1796 }
1797 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001798 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001799 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1800 }
1801
Wen-yi Chu41326c12023-09-22 03:58:59 +00001802 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1803 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001804 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1805 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1806 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001807 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001808 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001809
1810 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001811 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001812 depPaths.rustLibObjects = android.FirstUniqueStrings(depPaths.rustLibObjects)
1813 depPaths.staticLibObjects = android.FirstUniqueStrings(depPaths.staticLibObjects)
1814 depPaths.wholeStaticLibObjects = android.FirstUniqueStrings(depPaths.wholeStaticLibObjects)
1815 depPaths.sharedLibObjects = android.FirstUniqueStrings(depPaths.sharedLibObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001816 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001817 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1818 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1819 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoad7ba592025-01-23 01:23:43 +00001820 depPaths.depLinkFlags = android.FirstUniqueStrings(depPaths.depLinkFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001821
1822 return depPaths
1823}
1824
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001825func (mod *Module) InstallInData() bool {
1826 if mod.compiler == nil {
1827 return false
1828 }
1829 return mod.compiler.inData()
1830}
1831
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001832func (mod *Module) InstallInRamdisk() bool {
1833 return mod.InRamdisk()
1834}
1835
1836func (mod *Module) InstallInVendorRamdisk() bool {
1837 return mod.InVendorRamdisk()
1838}
1839
1840func (mod *Module) InstallInRecovery() bool {
1841 return mod.InRecovery()
1842}
1843
Wen-yi Chu41326c12023-09-22 03:58:59 +00001844func linkPathFromFilePath(filepath android.Path) string {
1845 return strings.Split(filepath.String(), filepath.Base())[0]
1846}
1847
Spandan Das604f3762023-03-16 22:51:40 +00001848// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1849func (r *Module) usePublicApi() bool {
1850 return r.Device() && r.UseSdk()
1851}
1852
1853// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1854func (r *Module) useVendorApi() bool {
1855 return r.Device() && (r.InVendor() || r.InProduct())
1856}
1857
Ivan Lozanoffee3342019-08-27 12:03:00 -07001858func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1859 ctx := &depsContext{
1860 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001861 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001862
1863 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001864 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001865
1866 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001867 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001868 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001869
Ivan Lozano2b081132020-09-08 12:46:52 -04001870 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001871 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001872 stdLinkage = "rlib-std"
1873 }
1874
1875 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001876
Ivan Lozano2b081132020-09-08 12:46:52 -04001877 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1878 rlibDepVariations = append(rlibDepVariations,
1879 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1880 }
1881
Ivan Lozano1921e802021-05-20 13:39:16 -04001882 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001883 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001884 for _, lib := range deps.Rlibs {
1885 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001886 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001887 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001888
1889 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001890 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001891
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001892 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001893 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001894 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001895
Ivan Lozano1921e802021-05-20 13:39:16 -04001896 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001897 if deps.Rustlibs != nil {
1898 if !mod.compiler.Disabled() {
1899 for _, lib := range deps.Rustlibs {
1900 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1901 if autoDep.depTag == rlibDepTag {
1902 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001903 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1904
Ivan Lozanod106efe2023-09-21 23:30:26 -04001905 } else {
1906 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1907 // Check for the existence of the dylib deptag variant. Select it if available,
1908 // otherwise select the rlib variant.
1909 autoDepVariations := append(commonDepVariations,
1910 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001911 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1912 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001913
Ivan Lozanod106efe2023-09-21 23:30:26 -04001914 } else {
1915 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001916 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1917
Ivan Lozanod106efe2023-09-21 23:30:26 -04001918 }
1919 }
1920 }
1921 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1922 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001923 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001924 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001925
Ivan Lozano0a468a42024-05-13 21:03:34 -04001926 // Only add rustlib dependencies if they're source providers themselves.
1927 // This is used to track which crate names need to be added to the source generated
1928 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001929 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001930 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001931 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001932 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001933 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001934 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001935
Ivan Lozano1921e802021-05-20 13:39:16 -04001936 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001937 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001938 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001939 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001940 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001941 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001942 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001943 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001944 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001945 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1946
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001947 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001948 }
1949 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001950
1951 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001952 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001953 name, version := cc.StubsLibNameAndVersion(lib)
1954
1955 variations := []blueprint.Variation{
1956 {Mutator: "link", Variation: "shared"},
1957 }
Spandan Dasff665182024-09-11 18:48:44 +00001958 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001959 }
1960
1961 for _, lib := range deps.WholeStaticLibs {
1962 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001963
1964 actx.AddVariationDependencies([]blueprint.Variation{
1965 {Mutator: "link", Variation: "static"},
1966 }, depTag, lib)
1967 }
1968
1969 for _, lib := range deps.StaticLibs {
1970 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001971
1972 actx.AddVariationDependencies([]blueprint.Variation{
1973 {Mutator: "link", Variation: "static"},
1974 }, depTag, lib)
1975 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001976
Zach Johnson3df4e632020-11-06 11:56:27 -08001977 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1978
Colin Cross565cafd2020-09-25 18:47:38 -07001979 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001980 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001981 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001982 }
Colin Crossfe605e12022-01-23 20:46:16 -08001983 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001984 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001985 }
1986
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001987 if mod.sourceProvider != nil {
1988 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1989 bindgen.Properties.Custom_bindgen != "" {
1990 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1991 bindgen.Properties.Custom_bindgen)
1992 }
1993 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001994
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001995 actx.AddVariationDependencies([]blueprint.Variation{
1996 {Mutator: "link", Variation: "shared"},
1997 }, dataLibDepTag, deps.DataLibs...)
1998
1999 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
2000
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07002001 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07002002 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05002003
2004 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07002005}
2006
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002007func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07002008 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002009 mod.beginMutator(ctx)
2010 }
2011}
2012
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002013func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
2014 ctx := &baseModuleContext{
2015 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002016 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002017
2018 mod.begin(ctx)
2019}
2020
Ivan Lozanoffee3342019-08-27 12:03:00 -07002021func (mod *Module) Name() string {
2022 name := mod.ModuleBase.Name()
2023 if p, ok := mod.compiler.(interface {
2024 Name(string) string
2025 }); ok {
2026 name = p.Name(name)
2027 }
2028 return name
2029}
2030
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002031func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04002032 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002033 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04002034 }
2035}
2036
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002037var _ android.HostToolProvider = (*Module)(nil)
2038
2039func (mod *Module) HostToolPath() android.OptionalPath {
2040 if !mod.Host() {
2041 return android.OptionalPath{}
2042 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07002043 if binary, ok := mod.compiler.(*binaryDecorator); ok {
2044 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04002045 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
2046 // Even though proc-macros aren't strictly "tools", since they target the compiler
2047 // and act as compiler plugins, we treat them similarly.
2048 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002049 }
2050 return android.OptionalPath{}
2051}
2052
Jiyong Park99644e92020-11-17 22:21:02 +09002053var _ android.ApexModule = (*Module)(nil)
2054
Ivan Lozano24cf0362024-10-04 16:02:38 +00002055// If a module is marked for exclusion from apexes, don't provide apex variants.
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002056// TODO(b/362509506): remove this once all apex_exclude usages are removed.
Ivan Lozano24cf0362024-10-04 16:02:38 +00002057func (m *Module) CanHaveApexVariants() bool {
2058 if m.ApexExclude() {
2059 return false
2060 } else {
2061 return m.ApexModuleBase.CanHaveApexVariants()
2062 }
2063}
2064
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002065func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002066 return String(mod.Properties.Min_sdk_version)
2067}
2068
Jiyong Park45bf82e2020-12-15 22:29:02 +09002069// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09002070func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002071 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002072 if minSdkVersion == "apex_inherit" {
2073 return nil
2074 }
2075 if minSdkVersion == "" {
2076 return fmt.Errorf("min_sdk_version is not specificed")
2077 }
2078
2079 // Not using nativeApiLevelFromUser because the context here is not
2080 // necessarily a native context.
2081 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
2082 if err != nil {
2083 return err
2084 }
2085
2086 if ver.GreaterThan(sdkVersion) {
2087 return fmt.Errorf("newer SDK(%v)", ver)
2088 }
Jiyong Park99644e92020-11-17 22:21:02 +09002089 return nil
2090}
2091
Jiyong Park45bf82e2020-12-15 22:29:02 +09002092// Implements android.ApexModule
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002093func (mod *Module) AlwaysRequiresPlatformApexVariant() bool {
2094 // stub libraries and native bridge libraries are always available to platform
2095 // TODO(b/362509506): remove the ApexExclude() check once all apex_exclude uses are switched to stubs.
2096 return mod.IsStubs() || mod.Target().NativeBridge == android.NativeBridgeEnabled || mod.ApexExclude()
2097}
2098
2099// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002100func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07002101 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09002102 return false
2103 }
2104
Colin Cross8acea3e2024-12-12 14:53:30 -08002105 if mod.Static() && cc.IsSharedDepTag(depTag) {
2106 // shared_lib dependency from a static lib is considered as crossing
2107 // the APEX boundary because the dependency doesn't actually is
2108 // linked; the dependency is used only during the compilation phase.
2109 return false
2110 }
2111
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002112 if depTag == cc.StubImplDepTag {
2113 // We don't track from an implementation library to its stubs.
2114 return false
2115 }
2116
2117 if cc.ExcludeInApexDepTag(depTag) {
2118 return false
2119 }
2120
2121 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2122 if mod.ApexExclude() {
2123 return false
2124 }
2125
Jiyong Park99644e92020-11-17 22:21:02 +09002126 return true
2127}
2128
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002129func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002130 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2131 if mod.ApexExclude() {
2132 return false
2133 }
2134
2135 if mod.HasStubsVariants() {
Colin Crossbd930bc2025-02-03 12:17:42 -08002136 if cc.IsSharedDepTag(depTag) && !cc.IsExplicitImplSharedDepTag(depTag) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002137 // dynamic dep to a stubs lib crosses APEX boundary
2138 return false
2139 }
2140 if cc.IsRuntimeDepTag(depTag) {
2141 // runtime dep to a stubs lib also crosses APEX boundary
2142 return false
2143 }
2144 if cc.IsHeaderDepTag(depTag) {
2145 return false
2146 }
2147 }
2148 return true
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002149}
2150
Jiyong Park99644e92020-11-17 22:21:02 +09002151// Overrides ApexModule.IsInstallabeToApex()
2152func (mod *Module) IsInstallableToApex() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002153 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2154 if mod.ApexExclude() {
2155 return false
2156 }
2157
Jiyong Park99644e92020-11-17 22:21:02 +09002158 if mod.compiler != nil {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002159 if lib, ok := mod.compiler.(libraryInterface); ok {
2160 return (lib.shared() || lib.dylib()) && !lib.BuildStubs()
Jiyong Park99644e92020-11-17 22:21:02 +09002161 }
2162 if _, ok := mod.compiler.(*binaryDecorator); ok {
2163 return true
2164 }
2165 }
2166 return false
2167}
2168
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05002169// If a library file has a "lib" prefix, extract the library name without the prefix.
2170func libNameFromFilePath(filepath android.Path) (string, bool) {
2171 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
2172 if strings.HasPrefix(libName, "lib") {
2173 libName = libName[3:]
2174 return libName, true
2175 }
2176 return "", false
2177}
2178
Sasha Smundaka76acba2022-04-18 20:12:56 -07002179func kytheExtractRustFactory() android.Singleton {
2180 return &kytheExtractRustSingleton{}
2181}
2182
2183type kytheExtractRustSingleton struct {
2184}
2185
2186func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2187 var xrefTargets android.Paths
2188 ctx.VisitAllModules(func(module android.Module) {
2189 if rustModule, ok := module.(xref); ok {
2190 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
2191 }
2192 })
2193 if len(xrefTargets) > 0 {
2194 ctx.Phony("xref_rust", xrefTargets...)
2195 }
2196}
2197
Jihoon Kangf78a8902022-09-01 22:47:07 +00002198func (c *Module) Partition() string {
2199 return ""
2200}
2201
Ivan Lozanoffee3342019-08-27 12:03:00 -07002202var Bool = proptools.Bool
2203var BoolDefault = proptools.BoolDefault
2204var String = proptools.String
2205var StringPtr = proptools.StringPtr