blob: f4fda2224763f5a90b5559c87965ce807a74fc33 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
Wei Lia1aa2972024-06-21 13:08:51 -070019 "strconv"
Chris Parsons637458d2023-09-19 20:09:00 +000020 "strings"
21
Sasha Smundaka76acba2022-04-18 20:12:56 -070022 "android/soong/bloaty"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090023
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "github.com/google/blueprint"
Colin Crossa14fb6a2024-10-23 16:57:06 -070025 "github.com/google/blueprint/depset"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020030 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070031 "android/soong/fuzz"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
33)
34
35var pctx = android.NewPackageContext("android/soong/rust")
36
Yu Liu8024b922024-12-20 23:31:32 +000037type LibraryInfo struct {
38 Rlib bool
39 Dylib bool
40}
41
42type CompilerInfo struct {
43 StdLinkageForDevice RustLinkage
44 StdLinkageForNonDevice RustLinkage
45 NoStdlibs bool
46 LibraryInfo *LibraryInfo
47}
48
49type ProtobufDecoratorInfo struct{}
50
51type SourceProviderInfo struct {
Yu Liu727204c2025-01-23 20:58:32 +000052 Srcs android.Paths
Yu Liu8024b922024-12-20 23:31:32 +000053 ProtobufDecoratorInfo *ProtobufDecoratorInfo
54}
55
56type RustInfo struct {
57 AndroidMkSuffix string
58 RustSubName string
59 TransitiveAndroidMkSharedLibs depset.DepSet[string]
60 CompilerInfo *CompilerInfo
61 SnapshotInfo *cc.SnapshotInfo
62 SourceProviderInfo *SourceProviderInfo
63}
64
65var RustInfoProvider = blueprint.NewProvider[*RustInfo]()
66
Ivan Lozanoffee3342019-08-27 12:03:00 -070067func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070068 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070069 android.PreDepsMutators(registerPreDepsMutators)
70 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090071 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070072 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020073 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000074 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070075}
76
Colin Cross8a49a3d2024-05-20 12:22:27 -070077func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
78 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
79 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070080 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070081}
82
83func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070084 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070085}
86
Ivan Lozanoffee3342019-08-27 12:03:00 -070087type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040088 GlobalRustFlags []string // Flags that apply globally to rust
89 GlobalLinkFlags []string // Flags that apply globally to linker
90 RustFlags []string // Flags that apply to rust
91 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020092 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070093 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070094 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040095 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020096 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070097 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070098}
99
100type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -0500101 AndroidMkRlibs []string `blueprint:"mutated"`
102 AndroidMkDylibs []string `blueprint:"mutated"`
103 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -0500104 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +0000105 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -0400106
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900107 ImageVariation string `blueprint:"mutated"`
108 VndkVersion string `blueprint:"mutated"`
109 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -0500110
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400111 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
112 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
113 // appended before SubName.
114 RustSubName string `blueprint:"mutated"`
115
Ivan Lozano6a884432020-12-02 09:15:16 -0500116 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +0000117 ProductVariantNeeded bool `blueprint:"mutated"`
118 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500119 CoreVariantNeeded bool `blueprint:"mutated"`
120 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -0700121 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -0800122 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500123 ExtraVariants []string `blueprint:"mutated"`
124
Ivan Lozanoa2268632021-07-22 10:52:06 -0400125 // Allows this module to use non-APEX version of libraries. Useful
126 // for building binaries that are started before APEXes are activated.
127 Bootstrap *bool
128
Ivan Lozano1921e802021-05-20 13:39:16 -0400129 // Used by vendor snapshot to record dependencies from snapshot modules.
130 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900131 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400132 SnapshotRlibs []string `blueprint:"mutated"`
133 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400134
Matthew Maurerc6868382021-07-13 14:12:37 -0700135 // Make this module available when building for ramdisk.
136 // On device without a dedicated recovery partition, the module is only
137 // available after switching root into
138 // /first_stage_ramdisk. To expose the module before switching root, install
139 // the recovery variant instead.
140 Ramdisk_available *bool
141
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500142 // Make this module available when building for vendor ramdisk.
143 // On device without a dedicated recovery partition, the module is only
144 // available after switching root into
145 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800146 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500147 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400148
Ivan Lozano1921e802021-05-20 13:39:16 -0400149 // Normally Soong uses the directory structure to decide which modules
150 // should be included (framework) or excluded (non-framework) from the
151 // different snapshots (vendor, recovery, etc.), but this property
152 // allows a partner to exclude a module normally thought of as a
153 // framework module from the vendor snapshot.
154 Exclude_from_vendor_snapshot *bool
155
156 // Normally Soong uses the directory structure to decide which modules
157 // should be included (framework) or excluded (non-framework) from the
158 // different snapshots (vendor, recovery, etc.), but this property
159 // allows a partner to exclude a module normally thought of as a
160 // framework module from the recovery snapshot.
161 Exclude_from_recovery_snapshot *bool
162
Matthew Maurer460ee942021-02-11 12:31:46 -0800163 // Make this module available when building for recovery
164 Recovery_available *bool
165
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000166 // The API level that this module is built against. The APIs of this API level will be
167 // visible at build time, but use of any APIs newer than min_sdk_version will render the
168 // module unloadable on older devices. In the future it will be possible to weakly-link new
169 // APIs, making the behavior match Java: such modules will load on older devices, but
170 // calling new APIs on devices that do not support them will result in a crash.
171 //
172 // This property has the same behavior as sdk_version does for Java modules. For those
173 // familiar with Android Gradle, the property behaves similarly to how compileSdkVersion
174 // does for Java code.
175 //
176 // In addition, setting this property causes two variants to be built, one for the platform
177 // and one for apps.
178 Sdk_version *string
179
180 // Minimum OS API level supported by this C or C++ module. This property becomes the value
181 // of the __ANDROID_API__ macro. When the C or C++ module is included in an APEX or an APK,
182 // this property is also used to ensure that the min_sdk_version of the containing module is
183 // not older (i.e. less) than this module's min_sdk_version. When not set, this property
184 // defaults to the value of sdk_version. When this is set to "apex_inherit", this tracks
185 // min_sdk_version of the containing APEX. When the module
186 // is not built for an APEX, "apex_inherit" defaults to sdk_version.
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500187 Min_sdk_version *string
188
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000189 // Variant is an SDK variant created by sdkMutator
190 IsSdkVariant bool `blueprint:"mutated"`
191
192 // Set by factories of module types that can only be referenced from variants compiled against
193 // the SDK.
194 AlwaysSdk bool `blueprint:"mutated"`
195
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900196 HideFromMake bool `blueprint:"mutated"`
197 PreventInstall bool `blueprint:"mutated"`
198
199 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700200}
201
202type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700203 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204
Ivan Lozano6a884432020-12-02 09:15:16 -0500205 VendorProperties cc.VendorProperties
206
Ivan Lozanoffee3342019-08-27 12:03:00 -0700207 Properties BaseProperties
208
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000209 hod android.HostOrDeviceSupported
210 multilib android.Multilib
211 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212
Ivan Lozano6a884432020-12-02 09:15:16 -0500213 makeLinkType string
214
Yi Kong46c6e592022-01-20 22:55:00 +0800215 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700216 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400217 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200218 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500219 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700220 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400221 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700222 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400223
Ivan Lozano0a468a42024-05-13 21:03:34 -0400224 exportedLinkDirs []string
225
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400226 // Output file to be installed, may be stripped or unstripped.
227 outputFile android.OptionalPath
228
Sasha Smundaka76acba2022-04-18 20:12:56 -0700229 // Cross-reference input file
230 kytheFiles android.Paths
231
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400232 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900233
234 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500235
236 // For apex variants, this is set as apex.min_sdk_version
237 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700238
Colin Crossa14fb6a2024-10-23 16:57:06 -0700239 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000240
241 // Shared flags among stubs build rules of this module
242 sharedFlags cc.SharedFlags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243}
244
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500245func (mod *Module) Header() bool {
246 //TODO: If Rust libraries provide header variants, this needs to be updated.
247 return false
248}
249
250func (mod *Module) SetPreventInstall() {
251 mod.Properties.PreventInstall = true
252}
253
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500254func (mod *Module) SetHideFromMake() {
255 mod.Properties.HideFromMake = true
256}
257
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900258func (mod *Module) HiddenFromMake() bool {
259 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400260}
261
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500262func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500263 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
264 // nil since we need compiler to actually sanitize.
265 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500266}
267
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500268func (mod *Module) IsPrebuilt() bool {
269 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
270 return true
271 }
272 return false
273}
274
Ivan Lozano52767be2019-10-18 14:49:46 -0700275func (mod *Module) SelectedStl() string {
276 return ""
277}
278
Ivan Lozano2b262972019-11-21 12:30:50 -0800279func (mod *Module) NonCcVariants() bool {
280 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400281 if library, ok := mod.compiler.(libraryInterface); ok {
282 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800283 }
284 }
285 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
286}
287
Ivan Lozano52767be2019-10-18 14:49:46 -0700288func (mod *Module) Static() bool {
289 if mod.compiler != nil {
290 if library, ok := mod.compiler.(libraryInterface); ok {
291 return library.static()
292 }
293 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400294 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700295}
296
297func (mod *Module) Shared() bool {
298 if mod.compiler != nil {
299 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400300 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 }
302 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400303 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700304}
305
Ivan Lozanod7586b62021-04-01 09:49:36 -0400306func (mod *Module) Dylib() bool {
307 if mod.compiler != nil {
308 if library, ok := mod.compiler.(libraryInterface); ok {
309 return library.dylib()
310 }
311 }
312 return false
313}
314
Ivan Lozanod106efe2023-09-21 23:30:26 -0400315func (mod *Module) Source() bool {
316 if mod.compiler != nil {
317 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
318 return library.source()
319 }
320 }
321 return false
322}
323
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400324func (mod *Module) RlibStd() bool {
325 if mod.compiler != nil {
326 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
327 return library.rlibStd()
328 }
329 }
330 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
331}
332
Ivan Lozanod7586b62021-04-01 09:49:36 -0400333func (mod *Module) Rlib() bool {
334 if mod.compiler != nil {
335 if library, ok := mod.compiler.(libraryInterface); ok {
336 return library.rlib()
337 }
338 }
339 return false
340}
341
342func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400343 if binary, ok := mod.compiler.(binaryInterface); ok {
344 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400345 }
346 return false
347}
348
Justin Yun5e035862021-06-29 20:50:37 +0900349func (mod *Module) StaticExecutable() bool {
350 if !mod.Binary() {
351 return false
352 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400353 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900354}
355
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000356func (mod *Module) ApexExclude() bool {
357 if mod.compiler != nil {
358 if library, ok := mod.compiler.(libraryInterface); ok {
359 return library.apexExclude()
360 }
361 }
362 return false
363}
364
Ivan Lozanod7586b62021-04-01 09:49:36 -0400365func (mod *Module) Object() bool {
366 // Rust has no modules which produce only object files.
367 return false
368}
369
Ivan Lozano52767be2019-10-18 14:49:46 -0700370func (mod *Module) Toc() android.OptionalPath {
371 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400372 if lib, ok := mod.compiler.(libraryInterface); ok {
373 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700374 }
375 }
376 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
377}
378
Colin Crossc511bc52020-04-07 16:50:32 +0000379func (mod *Module) UseSdk() bool {
380 return false
381}
382
Ivan Lozanod7586b62021-04-01 09:49:36 -0400383func (mod *Module) RelativeInstallPath() string {
384 if mod.compiler != nil {
385 return mod.compiler.relativeInstallPath()
386 }
387 return ""
388}
389
Ivan Lozano52767be2019-10-18 14:49:46 -0700390func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500391 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700392}
393
Jiyong Park7d55b612021-06-11 17:22:09 +0900394func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400395 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900396}
397
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400398func (mod *Module) SubName() string {
399 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700400}
401
Ivan Lozanof1868af2022-04-12 13:08:36 -0400402func (mod *Module) IsVndkPrebuiltLibrary() bool {
403 // Rust modules do not provide VNDK prebuilts
404 return false
405}
406
407func (mod *Module) IsVendorPublicLibrary() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000408 // Rust modules do not currently support vendor_public_library
409 return false
Ivan Lozanof1868af2022-04-12 13:08:36 -0400410}
411
412func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
413 // Rust modules to not provide Sdk variants
414 return false
415}
416
Colin Cross127bb8b2020-12-16 16:46:01 -0800417func (c *Module) IsVndkPrivate() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000418 // Rust modules do not currently support VNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800419 return false
420}
421
422func (c *Module) IsLlndk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000423 // Rust modules do not currently support LLNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800424 return false
425}
426
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400427func (mod *Module) KernelHeadersDecorator() bool {
428 return false
429}
430
Colin Cross1f3f1302021-04-26 18:37:44 -0700431func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000432 // Rust modules do not currently support LLNDK variants
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400433 return false
434}
435
Colin Cross5271fea2021-04-27 13:06:04 -0700436func (m *Module) NeedsVendorPublicLibraryVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000437 // Rust modules do not currently support vendor_public_library
Colin Cross5271fea2021-04-27 13:06:04 -0700438 return false
439}
440
Ivan Lozanod7586b62021-04-01 09:49:36 -0400441func (mod *Module) HasLlndkStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000442 // Rust modules do not currently support LLNDK stubs
Ivan Lozanod7586b62021-04-01 09:49:36 -0400443 return false
444}
445
Ivan Lozano52767be2019-10-18 14:49:46 -0700446func (mod *Module) SdkVersion() string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000447 return String(mod.Properties.Sdk_version)
Ivan Lozano52767be2019-10-18 14:49:46 -0700448}
449
Colin Crossc511bc52020-04-07 16:50:32 +0000450func (mod *Module) AlwaysSdk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000451 return mod.Properties.AlwaysSdk
Colin Crossc511bc52020-04-07 16:50:32 +0000452}
453
Jiyong Park2286afd2020-06-16 21:58:53 +0900454func (mod *Module) IsSdkVariant() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000455 return mod.Properties.IsSdkVariant
Jiyong Park2286afd2020-06-16 21:58:53 +0900456}
457
Colin Cross1348ce32020-10-01 13:37:16 -0700458func (mod *Module) SplitPerApiLevel() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000459 return cc.CanUseSdk(mod) && mod.IsCrt()
Colin Cross1348ce32020-10-01 13:37:16 -0700460}
461
Sasha Smundaka76acba2022-04-18 20:12:56 -0700462func (mod *Module) XrefRustFiles() android.Paths {
463 return mod.kytheFiles
464}
465
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400467 Dylibs []string
468 Rlibs []string
469 Rustlibs []string
470 Stdlibs []string
471 ProcMacros []string
472 SharedLibs []string
473 StaticLibs []string
474 WholeStaticLibs []string
475 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400477 // Used for data dependencies adjacent to tests
478 DataLibs []string
479 DataBins []string
480
Colin Crossfe605e12022-01-23 20:46:16 -0800481 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700482}
483
484type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700485 DyLibs RustLibraries
486 RLibs RustLibraries
487 SharedLibs android.Paths
488 SharedLibDeps android.Paths
489 StaticLibs android.Paths
490 ProcMacros RustLibraries
491 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000492 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500493
494 // depFlags and depLinkFlags are rustc and linker (clang) flags.
495 depFlags []string
496 depLinkFlags []string
497
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400498 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500499 // Both of these are exported and propagate to dependencies.
Ivan Lozano1f10f682024-11-08 16:16:50 +0000500 linkDirs []string
501 rustLibObjects []string
502 staticLibObjects []string
503 wholeStaticLibObjects []string
504 sharedLibObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700505
Ivan Lozano0a468a42024-05-13 21:03:34 -0400506 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
507 // cc_library_static dependants of rlibs.
508 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
509 exportedLinkDirs []string
510
Ivan Lozano45901ed2020-07-24 16:05:01 -0400511 // Used by bindgen modules which call clang
512 depClangFlags []string
513 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400514 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400515 depSystemIncludePaths android.Paths
516
Colin Crossfe605e12022-01-23 20:46:16 -0800517 CrtBegin android.Paths
518 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700519
520 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500521 SrcDeps android.Paths
522 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700523
524 directImplementationDeps android.Paths
525 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700526}
527
528type RustLibraries []RustLibrary
529
530type RustLibrary struct {
531 Path android.Path
532 CrateName string
533}
534
Matthew Maurerbb3add12020-06-25 09:34:12 -0700535type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000536 exportLinkDirs(...string)
Ivan Lozano1f10f682024-11-08 16:16:50 +0000537 exportRustLibs(...string)
538 exportStaticLibs(...string)
539 exportWholeStaticLibs(...string)
540 exportSharedLibs(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700541}
542
Sasha Smundaka76acba2022-04-18 20:12:56 -0700543type xref interface {
544 XrefRustFiles() android.Paths
545}
546
Matthew Maurerbb3add12020-06-25 09:34:12 -0700547type flagExporter struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000548 linkDirs []string
549 ccLinkDirs []string
550 rustLibPaths []string
551 staticLibObjects []string
552 sharedLibObjects []string
553 wholeStaticLibObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700554}
555
Wen-yi Chu41326c12023-09-22 03:58:59 +0000556func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
557 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700558}
559
Ivan Lozano1f10f682024-11-08 16:16:50 +0000560func (flagExporter *flagExporter) exportRustLibs(flags ...string) {
561 flagExporter.rustLibPaths = android.FirstUniqueStrings(append(flagExporter.rustLibPaths, flags...))
562}
563
564func (flagExporter *flagExporter) exportStaticLibs(flags ...string) {
565 flagExporter.staticLibObjects = android.FirstUniqueStrings(append(flagExporter.staticLibObjects, flags...))
566}
567
568func (flagExporter *flagExporter) exportSharedLibs(flags ...string) {
569 flagExporter.sharedLibObjects = android.FirstUniqueStrings(append(flagExporter.sharedLibObjects, flags...))
570}
571
572func (flagExporter *flagExporter) exportWholeStaticLibs(flags ...string) {
573 flagExporter.wholeStaticLibObjects = android.FirstUniqueStrings(append(flagExporter.wholeStaticLibObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400574}
575
Colin Cross0de8a1e2020-09-18 14:15:30 -0700576func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800577 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Ivan Lozano1f10f682024-11-08 16:16:50 +0000578 LinkDirs: flagExporter.linkDirs,
579 RustLibObjects: flagExporter.rustLibPaths,
580 StaticLibObjects: flagExporter.staticLibObjects,
581 WholeStaticLibObjects: flagExporter.wholeStaticLibObjects,
582 SharedLibPaths: flagExporter.sharedLibObjects,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700583 })
584}
585
Matthew Maurerbb3add12020-06-25 09:34:12 -0700586var _ exportedFlagsProducer = (*flagExporter)(nil)
587
588func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700589 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700590}
591
Colin Cross0de8a1e2020-09-18 14:15:30 -0700592type FlagExporterInfo struct {
Ivan Lozano1f10f682024-11-08 16:16:50 +0000593 Flags []string
594 LinkDirs []string
595 RustLibObjects []string
596 StaticLibObjects []string
597 WholeStaticLibObjects []string
598 SharedLibPaths []string
Colin Cross0de8a1e2020-09-18 14:15:30 -0700599}
600
Colin Crossbc7d76c2023-12-12 16:39:03 -0800601var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700602
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400603func (mod *Module) isCoverageVariant() bool {
604 return mod.coverage.Properties.IsCoverageVariant
605}
606
607var _ cc.Coverage = (*Module)(nil)
608
Colin Crosse1a85552024-06-14 12:17:37 -0700609func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400610 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
611}
612
Ivan Lozanod7586b62021-04-01 09:49:36 -0400613func (mod *Module) VndkVersion() string {
614 return mod.Properties.VndkVersion
615}
616
Ivan Lozano0a468a42024-05-13 21:03:34 -0400617func (mod *Module) ExportedCrateLinkDirs() []string {
618 return mod.exportedLinkDirs
619}
620
Ivan Lozanod7586b62021-04-01 09:49:36 -0400621func (mod *Module) PreventInstall() bool {
622 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400623}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000624func (c *Module) ForceDisableSanitizers() {
625 c.sanitize.Properties.ForceDisable = true
626}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400627
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400628func (mod *Module) MarkAsCoverageVariant(coverage bool) {
629 mod.coverage.Properties.IsCoverageVariant = coverage
630}
631
632func (mod *Module) EnableCoverageIfNeeded() {
633 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700634}
635
636func defaultsFactory() android.Module {
637 return DefaultsFactory()
638}
639
640type Defaults struct {
641 android.ModuleBase
642 android.DefaultsModuleBase
643}
644
645func DefaultsFactory(props ...interface{}) android.Module {
646 module := &Defaults{}
647
648 module.AddProperties(props...)
649 module.AddProperties(
650 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800651 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500652 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100653 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400654 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655 &BaseCompilerProperties{},
656 &BinaryCompilerProperties{},
657 &LibraryCompilerProperties{},
658 &ProcMacroCompilerProperties{},
659 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400660 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700661 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400662 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400663 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200664 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500665 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000666 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667 )
668
669 android.InitDefaultsModule(module)
670 return module
671}
672
673func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700674 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675}
676
Ivan Lozano183a3212019-10-18 14:18:45 -0700677func (mod *Module) CcLibrary() bool {
678 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500679 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700680 return true
681 }
682 }
683 return false
684}
685
686func (mod *Module) CcLibraryInterface() bool {
687 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400688 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
689 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000690 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700691 return true
692 }
693 }
694 return false
695}
696
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400697func (mod *Module) RustLibraryInterface() bool {
698 if mod.compiler != nil {
699 if _, ok := mod.compiler.(libraryInterface); ok {
700 return true
701 }
702 }
703 return false
704}
705
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500706func (mod *Module) IsFuzzModule() bool {
707 if _, ok := mod.compiler.(*fuzzDecorator); ok {
708 return true
709 }
710 return false
711}
712
713func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
714 return mod.FuzzModule
715}
716
717func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
718 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
719 return fuzzer.fuzzPackagedModule
720 }
721 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
722}
723
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000724func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500725 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
726 return fuzzer.sharedLibraries
727 }
728 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
729}
730
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400731func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400732 if mod.compiler != nil {
733 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400734 }
735 return nil
736}
737
Ivan Lozano183a3212019-10-18 14:18:45 -0700738func (mod *Module) SetStatic() {
739 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700740 if library, ok := mod.compiler.(libraryInterface); ok {
741 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700742 return
743 }
744 }
745 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
746}
747
748func (mod *Module) SetShared() {
749 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700750 if library, ok := mod.compiler.(libraryInterface); ok {
751 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700752 return
753 }
754 }
755 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
756}
757
Ivan Lozano183a3212019-10-18 14:18:45 -0700758func (mod *Module) BuildStaticVariant() bool {
759 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700760 if library, ok := mod.compiler.(libraryInterface); ok {
761 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700762 }
763 }
764 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
765}
766
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400767func (mod *Module) BuildRlibVariant() bool {
768 if mod.compiler != nil {
769 if library, ok := mod.compiler.(libraryInterface); ok {
770 return library.buildRlib()
771 }
772 }
773 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
774}
775
Ivan Lozano183a3212019-10-18 14:18:45 -0700776func (mod *Module) BuildSharedVariant() bool {
777 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700778 if library, ok := mod.compiler.(libraryInterface); ok {
779 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700780 }
781 }
782 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
783}
784
Ivan Lozano183a3212019-10-18 14:18:45 -0700785func (mod *Module) Module() android.Module {
786 return mod
787}
788
Ivan Lozano183a3212019-10-18 14:18:45 -0700789func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400790 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700791}
792
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400793func (mod *Module) CoverageFiles() android.Paths {
794 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800795 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400796 }
797 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
798}
799
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400800// Rust does not produce gcno files, and therefore does not produce a coverage archive.
801func (mod *Module) CoverageOutputFile() android.OptionalPath {
802 return android.OptionalPath{}
803}
804
805func (mod *Module) IsNdk(config android.Config) bool {
806 return false
807}
808
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000809func (mod *Module) IsStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000810 if lib, ok := mod.compiler.(libraryInterface); ok {
811 return lib.BuildStubs()
812 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000813 return false
814}
815
Spandan Das10c41362024-12-03 01:33:09 +0000816func (mod *Module) HasStubsVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000817 if lib, ok := mod.compiler.(libraryInterface); ok {
818 return lib.HasStubsVariants()
819 }
Spandan Das10c41362024-12-03 01:33:09 +0000820 return false
821}
822
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000823func (mod *Module) ApexSdkVersion() android.ApiLevel {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000824 return mod.apexSdkVersion
825}
826
827func (mod *Module) RustApexExclude() bool {
828 return mod.ApexExclude()
829}
830
831func (mod *Module) getSharedFlags() *cc.SharedFlags {
832 shared := &mod.sharedFlags
833 if shared.FlagsMap == nil {
834 shared.NumSharedFlags = 0
835 shared.FlagsMap = make(map[string]string)
836 }
837 return shared
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000838}
839
840func (mod *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000841 name := mod.BaseModuleName()
842 if versioned, ok := mod.compiler.(cc.VersionedInterface); ok {
843 name = versioned.ImplementationModuleName(name)
844 }
845 return name
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000846}
847
848func (mod *Module) Multilib() string {
849 return mod.Arch().ArchType.Multilib
850}
851
852func (mod *Module) IsCrt() bool {
853 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400854 return false
855}
856
Jiyong Park459feca2020-12-15 11:02:21 +0900857func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900858 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900859 return false
860 }
861
Jiyong Park459feca2020-12-15 11:02:21 +0900862 // The apex variant is not installable because it is included in the APEX and won't appear
863 // in the system partition as a standalone file.
864 if !apexInfo.IsForPlatform() {
865 return false
866 }
867
Jiyong Parke54f07e2021-04-07 15:08:04 +0900868 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900869}
870
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500871func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800872 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
873 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500874}
875
Ivan Lozano183a3212019-10-18 14:18:45 -0700876var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000877var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700878
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879func (mod *Module) Init() android.Module {
880 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500881 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882
Yi Kong46c6e592022-01-20 22:55:00 +0800883 if mod.afdo != nil {
884 mod.AddProperties(mod.afdo.props()...)
885 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886 if mod.compiler != nil {
887 mod.AddProperties(mod.compiler.compilerProps()...)
888 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400889 if mod.coverage != nil {
890 mod.AddProperties(mod.coverage.props()...)
891 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200892 if mod.clippy != nil {
893 mod.AddProperties(mod.clippy.props()...)
894 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400895 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700896 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400897 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500898 if mod.sanitize != nil {
899 mod.AddProperties(mod.sanitize.props()...)
900 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400901
Ivan Lozanoffee3342019-08-27 12:03:00 -0700902 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900903 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904
905 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 return mod
907}
908
909func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
910 return &Module{
911 hod: hod,
912 multilib: multilib,
913 }
914}
915func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
916 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800917 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400918 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200919 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500920 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921 return module
922}
923
924type ModuleContext interface {
925 android.ModuleContext
926 ModuleContextIntf
927}
928
929type BaseModuleContext interface {
930 android.BaseModuleContext
931 ModuleContextIntf
932}
933
934type DepsContext interface {
935 android.BottomUpMutatorContext
936 ModuleContextIntf
937}
938
939type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200940 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942}
943
944type depsContext struct {
945 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700946}
947
948type moduleContext struct {
949 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950}
951
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200952type baseModuleContext struct {
953 android.BaseModuleContext
954}
955
956func (ctx *moduleContext) RustModule() *Module {
957 return ctx.Module().(*Module)
958}
959
960func (ctx *moduleContext) toolchain() config.Toolchain {
961 return ctx.RustModule().toolchain(ctx)
962}
963
964func (ctx *depsContext) RustModule() *Module {
965 return ctx.Module().(*Module)
966}
967
968func (ctx *depsContext) toolchain() config.Toolchain {
969 return ctx.RustModule().toolchain(ctx)
970}
971
972func (ctx *baseModuleContext) RustModule() *Module {
973 return ctx.Module().(*Module)
974}
975
976func (ctx *baseModuleContext) toolchain() config.Toolchain {
977 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400978}
979
980func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700981 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
982 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
983 return false
984 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400985 return mod.compiler != nil && mod.compiler.nativeCoverage()
986}
987
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000988func (mod *Module) SetStl(s string) {
989 // STL is a CC concept; do nothing for Rust
990}
991
992func (mod *Module) SetSdkVersion(s string) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000993 mod.Properties.Sdk_version = StringPtr(s)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000994}
995
996func (mod *Module) SetMinSdkVersion(s string) {
997 mod.Properties.Min_sdk_version = StringPtr(s)
998}
999
1000func (mod *Module) VersionedInterface() cc.VersionedInterface {
1001 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
1002 return mod.compiler.(cc.VersionedInterface)
1003 }
1004 return nil
1005}
1006
Ivan Lozanod7586b62021-04-01 09:49:36 -04001007func (mod *Module) EverInstallable() bool {
1008 return mod.compiler != nil &&
1009 // Check to see whether the module is actually ever installable.
1010 mod.compiler.everInstallable()
1011}
1012
1013func (mod *Module) Installable() *bool {
1014 return mod.Properties.Installable
1015}
1016
Ivan Lozano872d5792022-03-23 17:31:39 -04001017func (mod *Module) ProcMacro() bool {
1018 if pm, ok := mod.compiler.(procMacroInterface); ok {
1019 return pm.ProcMacro()
1020 }
1021 return false
1022}
1023
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
1025 if mod.cachedToolchain == nil {
1026 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
1027 }
1028 return mod.cachedToolchain
1029}
1030
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +02001031func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
1032 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
1033}
1034
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1036}
1037
1038func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1039 ctx := &moduleContext{
1040 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001041 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042
Colin Crossff694a82023-12-13 15:54:49 -08001043 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +09001044 if !apexInfo.IsForPlatform() {
1045 mod.hideApexVariantFromMake = true
1046 }
1047
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -05001049 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
1050
Ivan Lozanof1868af2022-04-12 13:08:36 -04001051 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -07001052
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053 if !toolchain.Supported() {
1054 // This toolchain's unsupported, there's nothing to do for this mod.
1055 return
1056 }
1057
1058 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001059 // Export linkDirs for CC rust generatedlibs
1060 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
1061 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
1062
Ivan Lozanoffee3342019-08-27 12:03:00 -07001063 flags := Flags{
1064 Toolchain: toolchain,
1065 }
1066
Ivan Lozano67eada32021-09-23 11:50:33 -04001067 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +08001068 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -05001069 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +08001070 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001071 if mod.compiler != nil {
1072 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -04001073 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +00001074 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001075 }
1076 if mod.coverage != nil {
1077 flags, deps = mod.coverage.flags(ctx, flags, deps)
1078 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001079 if mod.clippy != nil {
1080 flags, deps = mod.clippy.flags(ctx, flags, deps)
1081 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001082 if mod.sanitize != nil {
1083 flags, deps = mod.sanitize.flags(ctx, flags, deps)
1084 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001085
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001086 // SourceProvider needs to call GenerateSource() before compiler calls
1087 // compile() so it can provide the source. A SourceProvider has
1088 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1089 // variant is responsible for effectively generating the source. The
1090 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001091 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001092 if mod.compiler.(libraryInterface).source() {
1093 mod.sourceProvider.GenerateSource(ctx, deps)
1094 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1095 } else {
Yu Liu727204c2025-01-23 20:58:32 +00001096 sourceMod := actx.GetDirectDepProxyWithTag(mod.Name(), sourceDepTag)
1097 sourceLib := android.OtherModuleProviderOrDefault(ctx, sourceMod, RustInfoProvider).SourceProviderInfo
1098 mod.sourceProvider.setOutputFiles(sourceLib.Srcs)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001099 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001100 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001101 }
1102
1103 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001104 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001105 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001106 if ctx.Failed() {
1107 return
1108 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001109 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001110 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001111 if buildOutput.kytheFile != nil {
1112 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1113 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001114 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001115
Dan Albert06feee92021-03-19 15:06:02 -07001116 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1117
Colin Crossff694a82023-12-13 15:54:49 -08001118 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001119 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001120 // If the module has been specifically configure to not be installed then
1121 // hide from make as otherwise it will break when running inside make as the
1122 // output path to install will not be specified. Not all uninstallable
1123 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001124 // side dependencies. In particular, proc-macros need to be captured in the
1125 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001126 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001127 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001128 } else if !mod.installable(apexInfo) {
1129 mod.SkipInstall()
1130 }
1131
1132 // Still call install though, the installs will be stored as PackageSpecs to allow
1133 // using the outputs in a genrule.
1134 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001135 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001136 if ctx.Failed() {
1137 return
1138 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001139 // Export your own directory as a linkDir
1140 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1141
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001142 }
Chris Wailes74be7642021-07-22 16:20:28 -07001143
Colin Crossb614cd42024-10-11 12:52:21 -07001144 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1145 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1146 })
1147
Chris Wailes74be7642021-07-22 16:20:28 -07001148 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001149 }
Wei Lia1aa2972024-06-21 13:08:51 -07001150
Yu Liuf6f85492025-01-13 21:02:36 +00001151 linkableInfo := cc.CreateCommonLinkableInfo(ctx, mod)
Yu Liu8024b922024-12-20 23:31:32 +00001152 linkableInfo.Static = mod.Static()
1153 linkableInfo.Shared = mod.Shared()
1154 linkableInfo.CrateName = mod.CrateName()
1155 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001156 if lib, ok := mod.compiler.(cc.VersionedInterface); ok {
1157 linkableInfo.StubsVersion = lib.StubsVersion()
1158 }
1159
Yu Liu8024b922024-12-20 23:31:32 +00001160 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1161
1162 rustInfo := &RustInfo{
1163 AndroidMkSuffix: mod.AndroidMkSuffix(),
1164 RustSubName: mod.Properties.RustSubName,
1165 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1166 }
1167 if mod.compiler != nil {
1168 rustInfo.CompilerInfo = &CompilerInfo{
1169 NoStdlibs: mod.compiler.noStdlibs(),
1170 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1171 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1172 }
1173 if lib, ok := mod.compiler.(libraryInterface); ok {
1174 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1175 Dylib: lib.dylib(),
1176 Rlib: lib.rlib(),
1177 }
1178 }
1179 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1180 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1181 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1182 }
1183 }
1184 }
1185 if mod.sourceProvider != nil {
Yu Liu727204c2025-01-23 20:58:32 +00001186 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1187 Srcs: mod.sourceProvider.Srcs(),
1188 }
Yu Liu8024b922024-12-20 23:31:32 +00001189 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu727204c2025-01-23 20:58:32 +00001190 rustInfo.SourceProviderInfo.ProtobufDecoratorInfo = &ProtobufDecoratorInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001191 }
1192 }
1193 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001194
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001195 ccInfo := &cc.CcInfo{
1196 IsPrebuilt: mod.IsPrebuilt(),
1197 }
1198
Ivan Lozano9587f452025-01-08 03:17:19 +00001199 // Define the linker info if compiler != nil because Rust currently
1200 // does compilation and linking in one step. If this changes in the future,
1201 // move this as appropriate.
1202 ccInfo.LinkerInfo = &cc.LinkerInfo{
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001203 WholeStaticLibs: mod.compiler.baseCompilerProps().Whole_static_libs,
1204 StaticLibs: mod.compiler.baseCompilerProps().Static_libs,
1205 SharedLibs: mod.compiler.baseCompilerProps().Shared_libs,
Ivan Lozano9587f452025-01-08 03:17:19 +00001206 }
1207
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001208 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1209
mrziwang0cbd3b02024-06-20 16:39:25 -07001210 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001211
1212 buildComplianceMetadataInfo(ctx, mod, deps)
Cole Faust58eef4f2025-01-29 15:14:17 -08001213
1214 moduleInfoJSON := ctx.ModuleInfoJSON()
1215 if mod.compiler != nil {
1216 mod.compiler.moduleInfoJSON(ctx, moduleInfoJSON)
1217 }
mrziwang0cbd3b02024-06-20 16:39:25 -07001218}
1219
1220func (mod *Module) setOutputFiles(ctx ModuleContext) {
1221 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1222 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1223 } else if mod.OutputFile().Valid() {
1224 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1225 } else {
1226 ctx.SetOutputFiles(android.Paths{}, "")
1227 }
1228 if mod.compiler != nil {
1229 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1230 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001231}
1232
Wei Lia1aa2972024-06-21 13:08:51 -07001233func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1234 // Dump metadata that can not be done in android/compliance-metadata.go
1235 metadataInfo := ctx.ComplianceMetadataInfo()
1236 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1237 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1238
1239 // Static libs
Yu Liu727204c2025-01-23 20:58:32 +00001240 staticDeps := ctx.GetDirectDepsProxyWithTag(rlibDepTag)
Wei Lia1aa2972024-06-21 13:08:51 -07001241 staticDepNames := make([]string, 0, len(staticDeps))
1242 for _, dep := range staticDeps {
1243 staticDepNames = append(staticDepNames, dep.Name())
1244 }
Yu Liu727204c2025-01-23 20:58:32 +00001245 ccStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(false))
Wei Lia1aa2972024-06-21 13:08:51 -07001246 for _, dep := range ccStaticDeps {
1247 staticDepNames = append(staticDepNames, dep.Name())
1248 }
1249
1250 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1251 // C static libraries
1252 for _, dep := range deps.StaticLibs {
1253 staticDepPaths = append(staticDepPaths, dep.String())
1254 }
1255 // Rust static libraries
1256 for _, dep := range deps.RLibs {
1257 staticDepPaths = append(staticDepPaths, dep.Path.String())
1258 }
1259 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1260 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1261
1262 // C Whole static libs
Yu Liu727204c2025-01-23 20:58:32 +00001263 ccWholeStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(true))
Wei Lia1aa2972024-06-21 13:08:51 -07001264 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1265 for _, dep := range ccStaticDeps {
1266 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1267 }
1268 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1269}
1270
Ivan Lozanoffee3342019-08-27 12:03:00 -07001271func (mod *Module) deps(ctx DepsContext) Deps {
1272 deps := Deps{}
1273
1274 if mod.compiler != nil {
1275 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001276 }
1277 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001278 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001279 }
1280
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001281 if mod.coverage != nil {
1282 deps = mod.coverage.deps(ctx, deps)
1283 }
1284
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001285 if mod.sanitize != nil {
1286 deps = mod.sanitize.deps(ctx, deps)
1287 }
1288
Ivan Lozanoffee3342019-08-27 12:03:00 -07001289 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1290 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001291 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1293 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1294 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001295 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001296 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001297 return deps
1298
1299}
1300
Ivan Lozanoffee3342019-08-27 12:03:00 -07001301type dependencyTag struct {
1302 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001303 name string
1304 library bool
1305 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001306 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001307}
1308
Jiyong Park65b62242020-11-25 12:44:59 +09001309// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1310// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1311func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001312 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001313}
1314
1315var _ android.InstallNeededDependencyTag = dependencyTag{}
1316
Colin Cross65cb3142021-12-10 23:05:02 +00001317func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1318 if d.library && d.dynamic {
1319 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1320 }
1321 return nil
1322}
1323
Yu Liuc8884602024-03-15 18:48:38 +00001324func (d dependencyTag) PropagateAconfigValidation() bool {
1325 return d == rlibDepTag || d == sourceDepTag
1326}
1327
1328var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1329
Colin Cross65cb3142021-12-10 23:05:02 +00001330var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1331
Ivan Lozanoffee3342019-08-27 12:03:00 -07001332var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001333 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1334 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001335 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001336 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001337 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001338 dataLibDepTag = dependencyTag{name: "data lib"}
1339 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001340)
1341
Jiyong Park99644e92020-11-17 22:21:02 +09001342func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1343 tag, ok := depTag.(dependencyTag)
1344 return ok && tag == dylibDepTag
1345}
1346
Jiyong Park94e22fd2021-04-08 18:19:15 +09001347func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1348 tag, ok := depTag.(dependencyTag)
1349 return ok && tag == rlibDepTag
1350}
1351
Matthew Maurer0f003b12020-06-29 14:34:06 -07001352type autoDep struct {
1353 variation string
1354 depTag dependencyTag
1355}
1356
1357var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001358 sourceVariation = "source"
1359 rlibVariation = "rlib"
1360 dylibVariation = "dylib"
1361 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1362 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001363)
1364
1365type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001366 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001367}
1368
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001369func (mod *Module) begin(ctx BaseModuleContext) {
1370 if mod.coverage != nil {
1371 mod.coverage.begin(ctx)
1372 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001373 if mod.sanitize != nil {
1374 mod.sanitize.begin(ctx)
1375 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001376
1377 if mod.UseSdk() && mod.IsSdkVariant() {
1378 sdkVersion := ""
1379 if ctx.Device() {
1380 sdkVersion = mod.SdkVersion()
1381 }
1382 version, err := cc.NativeApiLevelFromUser(ctx, sdkVersion)
1383 if err != nil {
1384 ctx.PropertyErrorf("sdk_version", err.Error())
1385 mod.Properties.Sdk_version = nil
1386 } else {
1387 mod.Properties.Sdk_version = StringPtr(version.String())
1388 }
1389 }
1390
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001391}
1392
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001393func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001394 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001395 return p.prebuilt()
1396 }
1397 return nil
1398}
1399
Kiyoung Kim37693d02024-04-04 09:56:15 +09001400func (mod *Module) Symlinks() []string {
1401 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1402 return nil
1403}
1404
Yu Liu8024b922024-12-20 23:31:32 +00001405func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1406 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001407 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001408 if rustInfo.SnapshotInfo != nil {
1409 baseName := linkableInfo.BaseModuleName
1410 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001411 }
1412 }
Yu Liu8024b922024-12-20 23:31:32 +00001413 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001414}
1415
Yu Liu8024b922024-12-20 23:31:32 +00001416func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001417 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001418 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1419 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001420 }
1421 }
1422}
Andrew Walbran52533232024-03-19 11:36:04 +00001423
Ivan Lozanoffee3342019-08-27 12:03:00 -07001424func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1425 var depPaths PathDeps
1426
Yu Liu8024b922024-12-20 23:31:32 +00001427 directRlibDeps := []*cc.LinkableInfo{}
1428 directDylibDeps := []*cc.LinkableInfo{}
1429 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001430 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001431 directStaticLibDeps := [](*cc.LinkableInfo){}
1432 directSrcProvidersDeps := []*android.ModuleProxy{}
1433 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001434
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001435 // For the dependency from platform to apex, use the latest stubs
1436 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001437 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001438 if !apexInfo.IsForPlatform() {
1439 mod.apexSdkVersion = apexInfo.MinSdkVersion
1440 }
1441
1442 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1443 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1444 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1445 // (b/144430859)
1446 mod.apexSdkVersion = android.FutureApiLevel
1447 }
1448
Spandan Das604f3762023-03-16 22:51:40 +00001449 skipModuleList := map[string]bool{}
1450
Colin Crossa14fb6a2024-10-23 16:57:06 -07001451 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001452 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001453
Yu Liu8024b922024-12-20 23:31:32 +00001454 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 depName := ctx.OtherModuleName(dep)
1456 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001457 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1458
Spandan Das604f3762023-03-16 22:51:40 +00001459 if _, exists := skipModuleList[depName]; exists {
1460 return
1461 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001462
1463 if depTag == android.DarwinUniversalVariantTag {
1464 return
1465 }
1466
Yu Liu8024b922024-12-20 23:31:32 +00001467 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1468 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1469 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1470 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1471 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001473 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001474
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001475 switch {
1476 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001477 dylib := rustInfo.CompilerInfo.LibraryInfo
1478 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001479 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1480 return
1481 }
Yu Liu8024b922024-12-20 23:31:32 +00001482 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001483 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001484 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1485
Colin Crossb614cd42024-10-11 12:52:21 -07001486 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1487 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1488 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1489 }
1490
Yu Liu8024b922024-12-20 23:31:32 +00001491 if !rustInfo.CompilerInfo.NoStdlibs {
1492 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1493 if ctx.Device() {
1494 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1495 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001496 if rustDepStdLinkage != modStdLinkage {
1497 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1498 return
1499 }
1500 }
1501
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001502 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001503 rlib := rustInfo.CompilerInfo.LibraryInfo
1504 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001505 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001506 return
1507 }
Yu Liu8024b922024-12-20 23:31:32 +00001508 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001509 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001510 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1511
Ivan Lozano0a468a42024-05-13 21:03:34 -04001512 // rust_ffi rlibs may export include dirs, so collect those here.
1513 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1514 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001515 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001516
Colin Crossb614cd42024-10-11 12:52:21 -07001517 // rlibs are not installed, so don't add the output file to directImplementationDeps
1518 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1519 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1520 }
1521
Yu Liu8024b922024-12-20 23:31:32 +00001522 if !rustInfo.CompilerInfo.NoStdlibs {
1523 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1524 if ctx.Device() {
1525 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1526 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001527 if rustDepStdLinkage != modStdLinkage {
1528 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1529 return
1530 }
1531 }
1532
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001533 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001534 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001535 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001536 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001537 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001538
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001539 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001540 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001541 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001542 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001543 case cc.IsStaticDepTag(depTag):
1544 // Rust FFI rlibs should not be declared in a Rust modules
1545 // "static_libs" list as we can't handle them properly at the
1546 // moment (for example, they only produce an rlib-std variant).
1547 // Instead, a normal rust_library variant should be used.
1548 ctx.PropertyErrorf("static_libs",
1549 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1550 depName)
1551
Paul Duffind5cf92e2021-07-09 17:38:55 +01001552 }
1553
Yu Liu8024b922024-12-20 23:31:32 +00001554 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001555
Paul Duffind5cf92e2021-07-09 17:38:55 +01001556 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001557 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1558 // OS/Arch variant is used.
1559 var helper string
1560 if ctx.Host() {
1561 helper = "missing 'host_supported'?"
1562 } else {
1563 helper = "device module defined?"
1564 }
1565
Yu Liu8024b922024-12-20 23:31:32 +00001566 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001567 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1568 return
Yu Liu8024b922024-12-20 23:31:32 +00001569 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001570 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1571 return
1572 }
Yu Liu8024b922024-12-20 23:31:32 +00001573 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001574 }
1575
Ivan Lozano0a468a42024-05-13 21:03:34 -04001576 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001577
1578 //Append the dependencies exported objects, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001579 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001580 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001581 depPaths.rustLibObjects = append(depPaths.rustLibObjects, exportedInfo.RustLibObjects...)
1582 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, exportedInfo.SharedLibPaths...)
1583 depPaths.staticLibObjects = append(depPaths.staticLibObjects, exportedInfo.StaticLibObjects...)
1584 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, exportedInfo.WholeStaticLibObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001585 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001586 }
1587
Ivan Lozanoffee3342019-08-27 12:03:00 -07001588 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001589 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001590 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001591 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001592 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001593 }
1594 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001595
Ivan Lozanod106efe2023-09-21 23:30:26 -04001596 if depTag == sourceDepTag {
1597 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001598 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001599 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001600 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1601 }
1602 }
1603 }
Yu Liu8024b922024-12-20 23:31:32 +00001604 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001605 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001606 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1607 if !hasRustInfo {
1608 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001609 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1610 return
1611 }
Yu Liu8024b922024-12-20 23:31:32 +00001612 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001613 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1614 return
1615 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001616 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001617 ccLibPath := linkableInfo.OutputFile
1618 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001619 if !ctx.Config().AllowMissingDependencies() {
1620 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1621 } else {
1622 ctx.AddMissingDependencies([]string{depName})
1623 }
1624 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001625 }
1626
Ivan Lozano1f10f682024-11-08 16:16:50 +00001627 linkPath := linkPathFromFilePath(ccLibPath.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001628
Ivan Lozanoffee3342019-08-27 12:03:00 -07001629 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001630 switch {
1631 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001632 if cc.IsWholeStaticLib(depTag) {
1633 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1634 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001635 if mod.Binary() {
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001636 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1637 // final linkage, pass the args directly to the linker to handle these cases.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001638 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", ccLibPath.Path().String(), "-Wl,--no-whole-archive"}...)
1639 } else if libName, ok := libNameFromFilePath(ccLibPath.Path()); ok {
1640 depPaths.depFlags = append(depPaths.depFlags, "-lstatic:+whole-archive="+libName)
1641 depPaths.depLinkFlags = append(depPaths.depLinkFlags, ccLibPath.Path().String())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001642 } else {
1643 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 -05001644 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001645 }
1646
Ivan Lozano1f10f682024-11-08 16:16:50 +00001647 if cc.IsWholeStaticLib(depTag) {
1648 // Add whole staticlibs to wholeStaticLibObjects to propagate to Rust all dependents.
1649 depPaths.wholeStaticLibObjects = append(depPaths.wholeStaticLibObjects, ccLibPath.String())
1650 } else {
1651 // Otherwise add to staticLibObjects, which only propagate through rlibs to their dependents.
1652 depPaths.staticLibObjects = append(depPaths.staticLibObjects, ccLibPath.String())
1653
1654 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001655 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1656
Colin Cross313aa542023-12-13 13:47:44 -08001657 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001658 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1659 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1660 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1661 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Yu Liu8024b922024-12-20 23:31:32 +00001662 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001663
1664 // Record baseLibName for snapshots.
1665 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1666
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001667 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001668 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001669 // For the shared lib dependencies, we may link to the stub variant
1670 // of the dependency depending on the context (e.g. if this
1671 // dependency crosses the APEX boundaries).
1672 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1673
Colin Crossb614cd42024-10-11 12:52:21 -07001674 if !sharedLibraryInfo.IsStubs {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001675 // TODO(b/362509506): remove this additional check once all apex_exclude uses are switched to stubs.
1676 if !linkableInfo.RustApexExclude {
1677 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1678 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1679 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1680 }
Colin Crossb614cd42024-10-11 12:52:21 -07001681 }
1682 }
1683
Jiyong Park7d55b612021-06-11 17:22:09 +09001684 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1685 // object (either from stub or non-stub) to use.
Ivan Lozano1f10f682024-11-08 16:16:50 +00001686 ccLibPath = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1687 if !ccLibPath.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001688 if !ctx.Config().AllowMissingDependencies() {
1689 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1690 } else {
1691 ctx.AddMissingDependencies([]string{depName})
1692 }
1693 return
1694 }
Ivan Lozano1f10f682024-11-08 16:16:50 +00001695 linkPath = linkPathFromFilePath(ccLibPath.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001696
Ivan Lozanoffee3342019-08-27 12:03:00 -07001697 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001698 depPaths.sharedLibObjects = append(depPaths.sharedLibObjects, ccLibPath.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001699 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1700 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1701 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1702 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001703 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001704
1705 // Record baseLibName for snapshots.
1706 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1707
Cole Faustb6e6f992023-08-17 17:42:26 -07001708 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001709 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001710 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001711 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001712 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1713 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1714 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001715 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001716 case depTag == cc.CrtBeginDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001717 depPaths.CrtBegin = append(depPaths.CrtBegin, ccLibPath.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001718 case depTag == cc.CrtEndDepTag:
Ivan Lozano1f10f682024-11-08 16:16:50 +00001719 depPaths.CrtEnd = append(depPaths.CrtEnd, ccLibPath.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001720 }
1721
Ivan Lozano1f10f682024-11-08 16:16:50 +00001722 // Make sure shared dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001723 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1724 lib.exportLinkDirs(linkPath)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001725 lib.exportSharedLibs(ccLibPath.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001726 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001727 } else {
1728 switch {
1729 case depTag == cc.CrtBeginDepTag:
1730 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1731 case depTag == cc.CrtEndDepTag:
1732 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1733 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001734 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001735
Yu Liuc41eae52025-01-14 01:03:08 +00001736 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001737 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001738 // These are usually genrules which don't have per-target variants.
1739 directSrcDeps = append(directSrcDeps, srcDep)
1740 }
1741 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001742 })
1743
Colin Crossa14fb6a2024-10-23 16:57:06 -07001744 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001745
1746 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001747 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001748 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001749 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001750 if alias, aliased := aliases[crateName]; aliased {
1751 crateName = alias
1752 }
Yu Liu8024b922024-12-20 23:31:32 +00001753 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001754 }
1755 var dylibDepFiles RustLibraries
1756 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001757 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001758 if alias, aliased := aliases[crateName]; aliased {
1759 crateName = alias
1760 }
Yu Liu8024b922024-12-20 23:31:32 +00001761 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001762 }
1763 var procMacroDepFiles RustLibraries
1764 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001765 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001766 if alias, aliased := aliases[crateName]; aliased {
1767 crateName = alias
1768 }
Yu Liu8024b922024-12-20 23:31:32 +00001769 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001770 }
1771
Colin Cross004bd3f2023-10-02 11:39:17 -07001772 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001773 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001774 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001775 }
1776
Colin Cross004bd3f2023-10-02 11:39:17 -07001777 var sharedLibFiles android.Paths
1778 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001779 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001780 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001781 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001782 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001783 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001784 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001785 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001786 }
1787
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001788 var srcProviderDepFiles android.Paths
1789 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001790 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001791 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1792 }
1793 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001794 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001795 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1796 }
1797
Wen-yi Chu41326c12023-09-22 03:58:59 +00001798 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1799 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001800 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1801 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1802 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001803 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001804 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001805
1806 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001807 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozano1f10f682024-11-08 16:16:50 +00001808 depPaths.rustLibObjects = android.FirstUniqueStrings(depPaths.rustLibObjects)
1809 depPaths.staticLibObjects = android.FirstUniqueStrings(depPaths.staticLibObjects)
1810 depPaths.wholeStaticLibObjects = android.FirstUniqueStrings(depPaths.wholeStaticLibObjects)
1811 depPaths.sharedLibObjects = android.FirstUniqueStrings(depPaths.sharedLibObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001812 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001813 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1814 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1815 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoad7ba592025-01-23 01:23:43 +00001816 depPaths.depLinkFlags = android.FirstUniqueStrings(depPaths.depLinkFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001817
1818 return depPaths
1819}
1820
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001821func (mod *Module) InstallInData() bool {
1822 if mod.compiler == nil {
1823 return false
1824 }
1825 return mod.compiler.inData()
1826}
1827
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001828func (mod *Module) InstallInRamdisk() bool {
1829 return mod.InRamdisk()
1830}
1831
1832func (mod *Module) InstallInVendorRamdisk() bool {
1833 return mod.InVendorRamdisk()
1834}
1835
1836func (mod *Module) InstallInRecovery() bool {
1837 return mod.InRecovery()
1838}
1839
Wen-yi Chu41326c12023-09-22 03:58:59 +00001840func linkPathFromFilePath(filepath android.Path) string {
1841 return strings.Split(filepath.String(), filepath.Base())[0]
1842}
1843
Spandan Das604f3762023-03-16 22:51:40 +00001844// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1845func (r *Module) usePublicApi() bool {
1846 return r.Device() && r.UseSdk()
1847}
1848
1849// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1850func (r *Module) useVendorApi() bool {
1851 return r.Device() && (r.InVendor() || r.InProduct())
1852}
1853
Ivan Lozanoffee3342019-08-27 12:03:00 -07001854func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1855 ctx := &depsContext{
1856 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001857 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001858
1859 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001860 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001861
1862 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001863 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001864 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001865
Ivan Lozano2b081132020-09-08 12:46:52 -04001866 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001867 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001868 stdLinkage = "rlib-std"
1869 }
1870
1871 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001872
Ivan Lozano2b081132020-09-08 12:46:52 -04001873 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1874 rlibDepVariations = append(rlibDepVariations,
1875 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1876 }
1877
Ivan Lozano1921e802021-05-20 13:39:16 -04001878 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001879 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001880 for _, lib := range deps.Rlibs {
1881 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001882 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001883 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001884
1885 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001886 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001887
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001888 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001889 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001890 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001891
Ivan Lozano1921e802021-05-20 13:39:16 -04001892 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001893 if deps.Rustlibs != nil {
1894 if !mod.compiler.Disabled() {
1895 for _, lib := range deps.Rustlibs {
1896 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1897 if autoDep.depTag == rlibDepTag {
1898 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001899 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1900
Ivan Lozanod106efe2023-09-21 23:30:26 -04001901 } else {
1902 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1903 // Check for the existence of the dylib deptag variant. Select it if available,
1904 // otherwise select the rlib variant.
1905 autoDepVariations := append(commonDepVariations,
1906 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001907 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1908 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001909
Ivan Lozanod106efe2023-09-21 23:30:26 -04001910 } else {
1911 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001912 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1913
Ivan Lozanod106efe2023-09-21 23:30:26 -04001914 }
1915 }
1916 }
1917 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1918 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001919 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001920 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001921
Ivan Lozano0a468a42024-05-13 21:03:34 -04001922 // Only add rustlib dependencies if they're source providers themselves.
1923 // This is used to track which crate names need to be added to the source generated
1924 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001925 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001926 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001927 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001928 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001929 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001930 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001931
Ivan Lozano1921e802021-05-20 13:39:16 -04001932 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001933 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001934 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001935 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001936 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001937 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001938 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001939 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001940 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001941 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1942
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001943 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001944 }
1945 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001946
1947 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001948 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001949 name, version := cc.StubsLibNameAndVersion(lib)
1950
1951 variations := []blueprint.Variation{
1952 {Mutator: "link", Variation: "shared"},
1953 }
Spandan Dasff665182024-09-11 18:48:44 +00001954 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001955 }
1956
1957 for _, lib := range deps.WholeStaticLibs {
1958 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001959
1960 actx.AddVariationDependencies([]blueprint.Variation{
1961 {Mutator: "link", Variation: "static"},
1962 }, depTag, lib)
1963 }
1964
1965 for _, lib := range deps.StaticLibs {
1966 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001967
1968 actx.AddVariationDependencies([]blueprint.Variation{
1969 {Mutator: "link", Variation: "static"},
1970 }, depTag, lib)
1971 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001972
Zach Johnson3df4e632020-11-06 11:56:27 -08001973 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1974
Colin Cross565cafd2020-09-25 18:47:38 -07001975 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001976 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001977 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001978 }
Colin Crossfe605e12022-01-23 20:46:16 -08001979 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001980 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001981 }
1982
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001983 if mod.sourceProvider != nil {
1984 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1985 bindgen.Properties.Custom_bindgen != "" {
1986 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1987 bindgen.Properties.Custom_bindgen)
1988 }
1989 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001990
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001991 actx.AddVariationDependencies([]blueprint.Variation{
1992 {Mutator: "link", Variation: "shared"},
1993 }, dataLibDepTag, deps.DataLibs...)
1994
1995 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1996
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001997 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001998 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001999
2000 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07002001}
2002
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002003func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07002004 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002005 mod.beginMutator(ctx)
2006 }
2007}
2008
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002009func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
2010 ctx := &baseModuleContext{
2011 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002012 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04002013
2014 mod.begin(ctx)
2015}
2016
Ivan Lozanoffee3342019-08-27 12:03:00 -07002017func (mod *Module) Name() string {
2018 name := mod.ModuleBase.Name()
2019 if p, ok := mod.compiler.(interface {
2020 Name(string) string
2021 }); ok {
2022 name = p.Name(name)
2023 }
2024 return name
2025}
2026
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002027func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04002028 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02002029 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04002030 }
2031}
2032
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002033var _ android.HostToolProvider = (*Module)(nil)
2034
2035func (mod *Module) HostToolPath() android.OptionalPath {
2036 if !mod.Host() {
2037 return android.OptionalPath{}
2038 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07002039 if binary, ok := mod.compiler.(*binaryDecorator); ok {
2040 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04002041 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
2042 // Even though proc-macros aren't strictly "tools", since they target the compiler
2043 // and act as compiler plugins, we treat them similarly.
2044 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002045 }
2046 return android.OptionalPath{}
2047}
2048
Jiyong Park99644e92020-11-17 22:21:02 +09002049var _ android.ApexModule = (*Module)(nil)
2050
Ivan Lozano24cf0362024-10-04 16:02:38 +00002051// If a module is marked for exclusion from apexes, don't provide apex variants.
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002052// TODO(b/362509506): remove this once all apex_exclude usages are removed.
Ivan Lozano24cf0362024-10-04 16:02:38 +00002053func (m *Module) CanHaveApexVariants() bool {
2054 if m.ApexExclude() {
2055 return false
2056 } else {
2057 return m.ApexModuleBase.CanHaveApexVariants()
2058 }
2059}
2060
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002061func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002062 return String(mod.Properties.Min_sdk_version)
2063}
2064
Jiyong Park45bf82e2020-12-15 22:29:02 +09002065// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09002066func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002067 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002068 if minSdkVersion == "apex_inherit" {
2069 return nil
2070 }
2071 if minSdkVersion == "" {
2072 return fmt.Errorf("min_sdk_version is not specificed")
2073 }
2074
2075 // Not using nativeApiLevelFromUser because the context here is not
2076 // necessarily a native context.
2077 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
2078 if err != nil {
2079 return err
2080 }
2081
2082 if ver.GreaterThan(sdkVersion) {
2083 return fmt.Errorf("newer SDK(%v)", ver)
2084 }
Jiyong Park99644e92020-11-17 22:21:02 +09002085 return nil
2086}
2087
Jiyong Park45bf82e2020-12-15 22:29:02 +09002088// Implements android.ApexModule
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002089func (mod *Module) AlwaysRequiresPlatformApexVariant() bool {
2090 // stub libraries and native bridge libraries are always available to platform
2091 // TODO(b/362509506): remove the ApexExclude() check once all apex_exclude uses are switched to stubs.
2092 return mod.IsStubs() || mod.Target().NativeBridge == android.NativeBridgeEnabled || mod.ApexExclude()
2093}
2094
2095// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002096func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07002097 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09002098 return false
2099 }
2100
Colin Cross8acea3e2024-12-12 14:53:30 -08002101 if mod.Static() && cc.IsSharedDepTag(depTag) {
2102 // shared_lib dependency from a static lib is considered as crossing
2103 // the APEX boundary because the dependency doesn't actually is
2104 // linked; the dependency is used only during the compilation phase.
2105 return false
2106 }
2107
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002108 if depTag == cc.StubImplDepTag {
2109 // We don't track from an implementation library to its stubs.
2110 return false
2111 }
2112
2113 if cc.ExcludeInApexDepTag(depTag) {
2114 return false
2115 }
2116
2117 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2118 if mod.ApexExclude() {
2119 return false
2120 }
2121
Jiyong Park99644e92020-11-17 22:21:02 +09002122 return true
2123}
2124
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002125func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002126 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2127 if mod.ApexExclude() {
2128 return false
2129 }
2130
2131 if mod.HasStubsVariants() {
2132 if cc.IsSharedDepTag(depTag) {
2133 // dynamic dep to a stubs lib crosses APEX boundary
2134 return false
2135 }
2136 if cc.IsRuntimeDepTag(depTag) {
2137 // runtime dep to a stubs lib also crosses APEX boundary
2138 return false
2139 }
2140 if cc.IsHeaderDepTag(depTag) {
2141 return false
2142 }
2143 }
2144 return true
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002145}
2146
Jiyong Park99644e92020-11-17 22:21:02 +09002147// Overrides ApexModule.IsInstallabeToApex()
2148func (mod *Module) IsInstallableToApex() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002149 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2150 if mod.ApexExclude() {
2151 return false
2152 }
2153
Jiyong Park99644e92020-11-17 22:21:02 +09002154 if mod.compiler != nil {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002155 if lib, ok := mod.compiler.(libraryInterface); ok {
2156 return (lib.shared() || lib.dylib()) && !lib.BuildStubs()
Jiyong Park99644e92020-11-17 22:21:02 +09002157 }
2158 if _, ok := mod.compiler.(*binaryDecorator); ok {
2159 return true
2160 }
2161 }
2162 return false
2163}
2164
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05002165// If a library file has a "lib" prefix, extract the library name without the prefix.
2166func libNameFromFilePath(filepath android.Path) (string, bool) {
2167 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
2168 if strings.HasPrefix(libName, "lib") {
2169 libName = libName[3:]
2170 return libName, true
2171 }
2172 return "", false
2173}
2174
Sasha Smundaka76acba2022-04-18 20:12:56 -07002175func kytheExtractRustFactory() android.Singleton {
2176 return &kytheExtractRustSingleton{}
2177}
2178
2179type kytheExtractRustSingleton struct {
2180}
2181
2182func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2183 var xrefTargets android.Paths
2184 ctx.VisitAllModules(func(module android.Module) {
2185 if rustModule, ok := module.(xref); ok {
2186 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
2187 }
2188 })
2189 if len(xrefTargets) > 0 {
2190 ctx.Phony("xref_rust", xrefTargets...)
2191 }
2192}
2193
Jihoon Kangf78a8902022-09-01 22:47:07 +00002194func (c *Module) Partition() string {
2195 return ""
2196}
2197
Ivan Lozanoffee3342019-08-27 12:03:00 -07002198var Bool = proptools.Bool
2199var BoolDefault = proptools.BoolDefault
2200var String = proptools.String
2201var StringPtr = proptools.StringPtr