blob: 88918cf82537364dfe54d6872e13207d97599650 [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.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000500 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700501 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700502
Ivan Lozano0a468a42024-05-13 21:03:34 -0400503 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
504 // cc_library_static dependants of rlibs.
505 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
506 exportedLinkDirs []string
507
Ivan Lozano45901ed2020-07-24 16:05:01 -0400508 // Used by bindgen modules which call clang
509 depClangFlags []string
510 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400511 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400512 depSystemIncludePaths android.Paths
513
Colin Crossfe605e12022-01-23 20:46:16 -0800514 CrtBegin android.Paths
515 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700516
517 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500518 SrcDeps android.Paths
519 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700520
521 directImplementationDeps android.Paths
522 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523}
524
525type RustLibraries []RustLibrary
526
527type RustLibrary struct {
528 Path android.Path
529 CrateName string
530}
531
Matthew Maurerbb3add12020-06-25 09:34:12 -0700532type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000533 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700534 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700535}
536
Sasha Smundaka76acba2022-04-18 20:12:56 -0700537type xref interface {
538 XrefRustFiles() android.Paths
539}
540
Matthew Maurerbb3add12020-06-25 09:34:12 -0700541type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000542 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400543 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700544 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700545}
546
Wen-yi Chu41326c12023-09-22 03:58:59 +0000547func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
548 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700549}
550
Colin Cross004bd3f2023-10-02 11:39:17 -0700551func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
552 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400553}
554
Colin Cross0de8a1e2020-09-18 14:15:30 -0700555func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800556 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700557 LinkDirs: flagExporter.linkDirs,
558 LinkObjects: flagExporter.linkObjects,
559 })
560}
561
Matthew Maurerbb3add12020-06-25 09:34:12 -0700562var _ exportedFlagsProducer = (*flagExporter)(nil)
563
564func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700565 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700566}
567
Colin Cross0de8a1e2020-09-18 14:15:30 -0700568type FlagExporterInfo struct {
569 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000570 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700571 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700572}
573
Colin Crossbc7d76c2023-12-12 16:39:03 -0800574var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700575
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400576func (mod *Module) isCoverageVariant() bool {
577 return mod.coverage.Properties.IsCoverageVariant
578}
579
580var _ cc.Coverage = (*Module)(nil)
581
Colin Crosse1a85552024-06-14 12:17:37 -0700582func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
584}
585
Ivan Lozanod7586b62021-04-01 09:49:36 -0400586func (mod *Module) VndkVersion() string {
587 return mod.Properties.VndkVersion
588}
589
Ivan Lozano0a468a42024-05-13 21:03:34 -0400590func (mod *Module) ExportedCrateLinkDirs() []string {
591 return mod.exportedLinkDirs
592}
593
Ivan Lozanod7586b62021-04-01 09:49:36 -0400594func (mod *Module) PreventInstall() bool {
595 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400596}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000597func (c *Module) ForceDisableSanitizers() {
598 c.sanitize.Properties.ForceDisable = true
599}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400600
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400601func (mod *Module) MarkAsCoverageVariant(coverage bool) {
602 mod.coverage.Properties.IsCoverageVariant = coverage
603}
604
605func (mod *Module) EnableCoverageIfNeeded() {
606 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607}
608
609func defaultsFactory() android.Module {
610 return DefaultsFactory()
611}
612
613type Defaults struct {
614 android.ModuleBase
615 android.DefaultsModuleBase
616}
617
618func DefaultsFactory(props ...interface{}) android.Module {
619 module := &Defaults{}
620
621 module.AddProperties(props...)
622 module.AddProperties(
623 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800624 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500625 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100626 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400627 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700628 &BaseCompilerProperties{},
629 &BinaryCompilerProperties{},
630 &LibraryCompilerProperties{},
631 &ProcMacroCompilerProperties{},
632 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400633 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700634 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400635 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400636 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200637 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500638 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000639 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640 )
641
642 android.InitDefaultsModule(module)
643 return module
644}
645
646func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700647 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648}
649
Ivan Lozano183a3212019-10-18 14:18:45 -0700650func (mod *Module) CcLibrary() bool {
651 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500652 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700653 return true
654 }
655 }
656 return false
657}
658
659func (mod *Module) CcLibraryInterface() bool {
660 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400661 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
662 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000663 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700664 return true
665 }
666 }
667 return false
668}
669
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400670func (mod *Module) RustLibraryInterface() bool {
671 if mod.compiler != nil {
672 if _, ok := mod.compiler.(libraryInterface); ok {
673 return true
674 }
675 }
676 return false
677}
678
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500679func (mod *Module) IsFuzzModule() bool {
680 if _, ok := mod.compiler.(*fuzzDecorator); ok {
681 return true
682 }
683 return false
684}
685
686func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
687 return mod.FuzzModule
688}
689
690func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
691 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
692 return fuzzer.fuzzPackagedModule
693 }
694 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
695}
696
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000697func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500698 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
699 return fuzzer.sharedLibraries
700 }
701 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
702}
703
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400704func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400705 if mod.compiler != nil {
706 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400707 }
708 return nil
709}
710
Ivan Lozano183a3212019-10-18 14:18:45 -0700711func (mod *Module) SetStatic() {
712 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700713 if library, ok := mod.compiler.(libraryInterface); ok {
714 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700715 return
716 }
717 }
718 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
719}
720
721func (mod *Module) SetShared() {
722 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700723 if library, ok := mod.compiler.(libraryInterface); ok {
724 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700725 return
726 }
727 }
728 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
729}
730
Ivan Lozano183a3212019-10-18 14:18:45 -0700731func (mod *Module) BuildStaticVariant() bool {
732 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700733 if library, ok := mod.compiler.(libraryInterface); ok {
734 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700735 }
736 }
737 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
738}
739
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400740func (mod *Module) BuildRlibVariant() bool {
741 if mod.compiler != nil {
742 if library, ok := mod.compiler.(libraryInterface); ok {
743 return library.buildRlib()
744 }
745 }
746 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
747}
748
Ivan Lozano183a3212019-10-18 14:18:45 -0700749func (mod *Module) BuildSharedVariant() bool {
750 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700751 if library, ok := mod.compiler.(libraryInterface); ok {
752 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700753 }
754 }
755 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
756}
757
Ivan Lozano183a3212019-10-18 14:18:45 -0700758func (mod *Module) Module() android.Module {
759 return mod
760}
761
Ivan Lozano183a3212019-10-18 14:18:45 -0700762func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400763 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700764}
765
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766func (mod *Module) CoverageFiles() android.Paths {
767 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800768 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400769 }
770 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
771}
772
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400773// Rust does not produce gcno files, and therefore does not produce a coverage archive.
774func (mod *Module) CoverageOutputFile() android.OptionalPath {
775 return android.OptionalPath{}
776}
777
778func (mod *Module) IsNdk(config android.Config) bool {
779 return false
780}
781
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000782func (mod *Module) IsStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000783 if lib, ok := mod.compiler.(libraryInterface); ok {
784 return lib.BuildStubs()
785 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000786 return false
787}
788
Spandan Das10c41362024-12-03 01:33:09 +0000789func (mod *Module) HasStubsVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000790 if lib, ok := mod.compiler.(libraryInterface); ok {
791 return lib.HasStubsVariants()
792 }
Spandan Das10c41362024-12-03 01:33:09 +0000793 return false
794}
795
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000796func (mod *Module) ApexSdkVersion() android.ApiLevel {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000797 return mod.apexSdkVersion
798}
799
800func (mod *Module) RustApexExclude() bool {
801 return mod.ApexExclude()
802}
803
804func (mod *Module) getSharedFlags() *cc.SharedFlags {
805 shared := &mod.sharedFlags
806 if shared.FlagsMap == nil {
807 shared.NumSharedFlags = 0
808 shared.FlagsMap = make(map[string]string)
809 }
810 return shared
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000811}
812
813func (mod *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000814 name := mod.BaseModuleName()
815 if versioned, ok := mod.compiler.(cc.VersionedInterface); ok {
816 name = versioned.ImplementationModuleName(name)
817 }
818 return name
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000819}
820
821func (mod *Module) Multilib() string {
822 return mod.Arch().ArchType.Multilib
823}
824
825func (mod *Module) IsCrt() bool {
826 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400827 return false
828}
829
Jiyong Park459feca2020-12-15 11:02:21 +0900830func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900831 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900832 return false
833 }
834
Jiyong Park459feca2020-12-15 11:02:21 +0900835 // The apex variant is not installable because it is included in the APEX and won't appear
836 // in the system partition as a standalone file.
837 if !apexInfo.IsForPlatform() {
838 return false
839 }
840
Jiyong Parke54f07e2021-04-07 15:08:04 +0900841 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900842}
843
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500844func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800845 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
846 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500847}
848
Ivan Lozano183a3212019-10-18 14:18:45 -0700849var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000850var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700851
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852func (mod *Module) Init() android.Module {
853 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500854 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855
Yi Kong46c6e592022-01-20 22:55:00 +0800856 if mod.afdo != nil {
857 mod.AddProperties(mod.afdo.props()...)
858 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859 if mod.compiler != nil {
860 mod.AddProperties(mod.compiler.compilerProps()...)
861 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400862 if mod.coverage != nil {
863 mod.AddProperties(mod.coverage.props()...)
864 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200865 if mod.clippy != nil {
866 mod.AddProperties(mod.clippy.props()...)
867 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400868 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700869 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400870 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500871 if mod.sanitize != nil {
872 mod.AddProperties(mod.sanitize.props()...)
873 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400874
Ivan Lozanoffee3342019-08-27 12:03:00 -0700875 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900876 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877
878 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879 return mod
880}
881
882func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
883 return &Module{
884 hod: hod,
885 multilib: multilib,
886 }
887}
888func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
889 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800890 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400891 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200892 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500893 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 return module
895}
896
897type ModuleContext interface {
898 android.ModuleContext
899 ModuleContextIntf
900}
901
902type BaseModuleContext interface {
903 android.BaseModuleContext
904 ModuleContextIntf
905}
906
907type DepsContext interface {
908 android.BottomUpMutatorContext
909 ModuleContextIntf
910}
911
912type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200913 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915}
916
917type depsContext struct {
918 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919}
920
921type moduleContext struct {
922 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700923}
924
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200925type baseModuleContext struct {
926 android.BaseModuleContext
927}
928
929func (ctx *moduleContext) RustModule() *Module {
930 return ctx.Module().(*Module)
931}
932
933func (ctx *moduleContext) toolchain() config.Toolchain {
934 return ctx.RustModule().toolchain(ctx)
935}
936
937func (ctx *depsContext) RustModule() *Module {
938 return ctx.Module().(*Module)
939}
940
941func (ctx *depsContext) toolchain() config.Toolchain {
942 return ctx.RustModule().toolchain(ctx)
943}
944
945func (ctx *baseModuleContext) RustModule() *Module {
946 return ctx.Module().(*Module)
947}
948
949func (ctx *baseModuleContext) toolchain() config.Toolchain {
950 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400951}
952
953func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700954 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
955 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
956 return false
957 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400958 return mod.compiler != nil && mod.compiler.nativeCoverage()
959}
960
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000961func (mod *Module) SetStl(s string) {
962 // STL is a CC concept; do nothing for Rust
963}
964
965func (mod *Module) SetSdkVersion(s string) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000966 mod.Properties.Sdk_version = StringPtr(s)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000967}
968
969func (mod *Module) SetMinSdkVersion(s string) {
970 mod.Properties.Min_sdk_version = StringPtr(s)
971}
972
973func (mod *Module) VersionedInterface() cc.VersionedInterface {
974 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
975 return mod.compiler.(cc.VersionedInterface)
976 }
977 return nil
978}
979
Ivan Lozanod7586b62021-04-01 09:49:36 -0400980func (mod *Module) EverInstallable() bool {
981 return mod.compiler != nil &&
982 // Check to see whether the module is actually ever installable.
983 mod.compiler.everInstallable()
984}
985
986func (mod *Module) Installable() *bool {
987 return mod.Properties.Installable
988}
989
Ivan Lozano872d5792022-03-23 17:31:39 -0400990func (mod *Module) ProcMacro() bool {
991 if pm, ok := mod.compiler.(procMacroInterface); ok {
992 return pm.ProcMacro()
993 }
994 return false
995}
996
Ivan Lozanoffee3342019-08-27 12:03:00 -0700997func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
998 if mod.cachedToolchain == nil {
999 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
1000 }
1001 return mod.cachedToolchain
1002}
1003
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +02001004func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
1005 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
1006}
1007
Ivan Lozanoffee3342019-08-27 12:03:00 -07001008func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1009}
1010
1011func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1012 ctx := &moduleContext{
1013 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015
Colin Crossff694a82023-12-13 15:54:49 -08001016 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +09001017 if !apexInfo.IsForPlatform() {
1018 mod.hideApexVariantFromMake = true
1019 }
1020
Ivan Lozanoffee3342019-08-27 12:03:00 -07001021 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -05001022 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
1023
Ivan Lozanof1868af2022-04-12 13:08:36 -04001024 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -07001025
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026 if !toolchain.Supported() {
1027 // This toolchain's unsupported, there's nothing to do for this mod.
1028 return
1029 }
1030
1031 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001032 // Export linkDirs for CC rust generatedlibs
1033 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
1034 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
1035
Ivan Lozanoffee3342019-08-27 12:03:00 -07001036 flags := Flags{
1037 Toolchain: toolchain,
1038 }
1039
Ivan Lozano67eada32021-09-23 11:50:33 -04001040 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +08001041 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -05001042 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +08001043 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044 if mod.compiler != nil {
1045 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -04001046 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +00001047 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001048 }
1049 if mod.coverage != nil {
1050 flags, deps = mod.coverage.flags(ctx, flags, deps)
1051 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001052 if mod.clippy != nil {
1053 flags, deps = mod.clippy.flags(ctx, flags, deps)
1054 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001055 if mod.sanitize != nil {
1056 flags, deps = mod.sanitize.flags(ctx, flags, deps)
1057 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001058
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001059 // SourceProvider needs to call GenerateSource() before compiler calls
1060 // compile() so it can provide the source. A SourceProvider has
1061 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1062 // variant is responsible for effectively generating the source. The
1063 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001064 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001065 if mod.compiler.(libraryInterface).source() {
1066 mod.sourceProvider.GenerateSource(ctx, deps)
1067 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1068 } else {
Yu Liu727204c2025-01-23 20:58:32 +00001069 sourceMod := actx.GetDirectDepProxyWithTag(mod.Name(), sourceDepTag)
1070 sourceLib := android.OtherModuleProviderOrDefault(ctx, sourceMod, RustInfoProvider).SourceProviderInfo
1071 mod.sourceProvider.setOutputFiles(sourceLib.Srcs)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001072 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001073 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001074 }
1075
1076 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001077 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001078 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001079 if ctx.Failed() {
1080 return
1081 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001082 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001083 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001084 if buildOutput.kytheFile != nil {
1085 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1086 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001087 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001088
Dan Albert06feee92021-03-19 15:06:02 -07001089 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1090
Colin Crossff694a82023-12-13 15:54:49 -08001091 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001092 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001093 // If the module has been specifically configure to not be installed then
1094 // hide from make as otherwise it will break when running inside make as the
1095 // output path to install will not be specified. Not all uninstallable
1096 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001097 // side dependencies. In particular, proc-macros need to be captured in the
1098 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001099 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001100 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001101 } else if !mod.installable(apexInfo) {
1102 mod.SkipInstall()
1103 }
1104
1105 // Still call install though, the installs will be stored as PackageSpecs to allow
1106 // using the outputs in a genrule.
1107 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001108 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001109 if ctx.Failed() {
1110 return
1111 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001112 // Export your own directory as a linkDir
1113 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1114
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001115 }
Chris Wailes74be7642021-07-22 16:20:28 -07001116
Colin Crossb614cd42024-10-11 12:52:21 -07001117 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1118 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1119 })
1120
Chris Wailes74be7642021-07-22 16:20:28 -07001121 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001122 }
Wei Lia1aa2972024-06-21 13:08:51 -07001123
Yu Liuf6f85492025-01-13 21:02:36 +00001124 linkableInfo := cc.CreateCommonLinkableInfo(ctx, mod)
Yu Liu8024b922024-12-20 23:31:32 +00001125 linkableInfo.Static = mod.Static()
1126 linkableInfo.Shared = mod.Shared()
1127 linkableInfo.CrateName = mod.CrateName()
1128 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001129 if lib, ok := mod.compiler.(cc.VersionedInterface); ok {
1130 linkableInfo.StubsVersion = lib.StubsVersion()
1131 }
1132
Yu Liu8024b922024-12-20 23:31:32 +00001133 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1134
1135 rustInfo := &RustInfo{
1136 AndroidMkSuffix: mod.AndroidMkSuffix(),
1137 RustSubName: mod.Properties.RustSubName,
1138 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1139 }
1140 if mod.compiler != nil {
1141 rustInfo.CompilerInfo = &CompilerInfo{
1142 NoStdlibs: mod.compiler.noStdlibs(),
1143 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1144 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1145 }
1146 if lib, ok := mod.compiler.(libraryInterface); ok {
1147 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1148 Dylib: lib.dylib(),
1149 Rlib: lib.rlib(),
1150 }
1151 }
1152 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1153 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1154 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1155 }
1156 }
1157 }
1158 if mod.sourceProvider != nil {
Yu Liu727204c2025-01-23 20:58:32 +00001159 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1160 Srcs: mod.sourceProvider.Srcs(),
1161 }
Yu Liu8024b922024-12-20 23:31:32 +00001162 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu727204c2025-01-23 20:58:32 +00001163 rustInfo.SourceProviderInfo.ProtobufDecoratorInfo = &ProtobufDecoratorInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001164 }
1165 }
1166 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001167
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001168 ccInfo := &cc.CcInfo{
1169 IsPrebuilt: mod.IsPrebuilt(),
1170 }
1171
Ivan Lozano9587f452025-01-08 03:17:19 +00001172 // Define the linker info if compiler != nil because Rust currently
1173 // does compilation and linking in one step. If this changes in the future,
1174 // move this as appropriate.
1175 ccInfo.LinkerInfo = &cc.LinkerInfo{
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001176 WholeStaticLibs: mod.compiler.baseCompilerProps().Whole_static_libs,
1177 StaticLibs: mod.compiler.baseCompilerProps().Static_libs,
1178 SharedLibs: mod.compiler.baseCompilerProps().Shared_libs,
Ivan Lozano9587f452025-01-08 03:17:19 +00001179 }
1180
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001181 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1182
mrziwang0cbd3b02024-06-20 16:39:25 -07001183 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001184
1185 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001186}
1187
1188func (mod *Module) setOutputFiles(ctx ModuleContext) {
1189 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1190 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1191 } else if mod.OutputFile().Valid() {
1192 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1193 } else {
1194 ctx.SetOutputFiles(android.Paths{}, "")
1195 }
1196 if mod.compiler != nil {
1197 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1198 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001199}
1200
Wei Lia1aa2972024-06-21 13:08:51 -07001201func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1202 // Dump metadata that can not be done in android/compliance-metadata.go
1203 metadataInfo := ctx.ComplianceMetadataInfo()
1204 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1205 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1206
1207 // Static libs
Yu Liu727204c2025-01-23 20:58:32 +00001208 staticDeps := ctx.GetDirectDepsProxyWithTag(rlibDepTag)
Wei Lia1aa2972024-06-21 13:08:51 -07001209 staticDepNames := make([]string, 0, len(staticDeps))
1210 for _, dep := range staticDeps {
1211 staticDepNames = append(staticDepNames, dep.Name())
1212 }
Yu Liu727204c2025-01-23 20:58:32 +00001213 ccStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(false))
Wei Lia1aa2972024-06-21 13:08:51 -07001214 for _, dep := range ccStaticDeps {
1215 staticDepNames = append(staticDepNames, dep.Name())
1216 }
1217
1218 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1219 // C static libraries
1220 for _, dep := range deps.StaticLibs {
1221 staticDepPaths = append(staticDepPaths, dep.String())
1222 }
1223 // Rust static libraries
1224 for _, dep := range deps.RLibs {
1225 staticDepPaths = append(staticDepPaths, dep.Path.String())
1226 }
1227 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1228 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1229
1230 // C Whole static libs
Yu Liu727204c2025-01-23 20:58:32 +00001231 ccWholeStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(true))
Wei Lia1aa2972024-06-21 13:08:51 -07001232 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1233 for _, dep := range ccStaticDeps {
1234 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1235 }
1236 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1237}
1238
Ivan Lozanoffee3342019-08-27 12:03:00 -07001239func (mod *Module) deps(ctx DepsContext) Deps {
1240 deps := Deps{}
1241
1242 if mod.compiler != nil {
1243 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001244 }
1245 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001246 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001247 }
1248
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001249 if mod.coverage != nil {
1250 deps = mod.coverage.deps(ctx, deps)
1251 }
1252
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001253 if mod.sanitize != nil {
1254 deps = mod.sanitize.deps(ctx, deps)
1255 }
1256
Ivan Lozanoffee3342019-08-27 12:03:00 -07001257 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1258 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001259 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001260 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1261 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1262 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001263 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001264 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001265 return deps
1266
1267}
1268
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269type dependencyTag struct {
1270 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001271 name string
1272 library bool
1273 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001274 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275}
1276
Jiyong Park65b62242020-11-25 12:44:59 +09001277// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1278// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1279func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001280 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001281}
1282
1283var _ android.InstallNeededDependencyTag = dependencyTag{}
1284
Colin Cross65cb3142021-12-10 23:05:02 +00001285func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1286 if d.library && d.dynamic {
1287 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1288 }
1289 return nil
1290}
1291
Yu Liuc8884602024-03-15 18:48:38 +00001292func (d dependencyTag) PropagateAconfigValidation() bool {
1293 return d == rlibDepTag || d == sourceDepTag
1294}
1295
1296var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1297
Colin Cross65cb3142021-12-10 23:05:02 +00001298var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1299
Ivan Lozanoffee3342019-08-27 12:03:00 -07001300var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001301 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1302 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001303 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001304 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001305 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001306 dataLibDepTag = dependencyTag{name: "data lib"}
1307 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001308)
1309
Jiyong Park99644e92020-11-17 22:21:02 +09001310func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1311 tag, ok := depTag.(dependencyTag)
1312 return ok && tag == dylibDepTag
1313}
1314
Jiyong Park94e22fd2021-04-08 18:19:15 +09001315func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1316 tag, ok := depTag.(dependencyTag)
1317 return ok && tag == rlibDepTag
1318}
1319
Matthew Maurer0f003b12020-06-29 14:34:06 -07001320type autoDep struct {
1321 variation string
1322 depTag dependencyTag
1323}
1324
1325var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001326 sourceVariation = "source"
1327 rlibVariation = "rlib"
1328 dylibVariation = "dylib"
1329 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1330 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001331)
1332
1333type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001334 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001335}
1336
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001337func (mod *Module) begin(ctx BaseModuleContext) {
1338 if mod.coverage != nil {
1339 mod.coverage.begin(ctx)
1340 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001341 if mod.sanitize != nil {
1342 mod.sanitize.begin(ctx)
1343 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001344
1345 if mod.UseSdk() && mod.IsSdkVariant() {
1346 sdkVersion := ""
1347 if ctx.Device() {
1348 sdkVersion = mod.SdkVersion()
1349 }
1350 version, err := cc.NativeApiLevelFromUser(ctx, sdkVersion)
1351 if err != nil {
1352 ctx.PropertyErrorf("sdk_version", err.Error())
1353 mod.Properties.Sdk_version = nil
1354 } else {
1355 mod.Properties.Sdk_version = StringPtr(version.String())
1356 }
1357 }
1358
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001359}
1360
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001361func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001362 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001363 return p.prebuilt()
1364 }
1365 return nil
1366}
1367
Kiyoung Kim37693d02024-04-04 09:56:15 +09001368func (mod *Module) Symlinks() []string {
1369 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1370 return nil
1371}
1372
Yu Liu8024b922024-12-20 23:31:32 +00001373func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1374 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001375 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001376 if rustInfo.SnapshotInfo != nil {
1377 baseName := linkableInfo.BaseModuleName
1378 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001379 }
1380 }
Yu Liu8024b922024-12-20 23:31:32 +00001381 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001382}
1383
Yu Liu8024b922024-12-20 23:31:32 +00001384func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001385 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001386 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1387 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001388 }
1389 }
1390}
Andrew Walbran52533232024-03-19 11:36:04 +00001391
Ivan Lozanoffee3342019-08-27 12:03:00 -07001392func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1393 var depPaths PathDeps
1394
Yu Liu8024b922024-12-20 23:31:32 +00001395 directRlibDeps := []*cc.LinkableInfo{}
1396 directDylibDeps := []*cc.LinkableInfo{}
1397 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001398 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001399 directStaticLibDeps := [](*cc.LinkableInfo){}
1400 directSrcProvidersDeps := []*android.ModuleProxy{}
1401 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001402
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001403 // For the dependency from platform to apex, use the latest stubs
1404 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001405 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001406 if !apexInfo.IsForPlatform() {
1407 mod.apexSdkVersion = apexInfo.MinSdkVersion
1408 }
1409
1410 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1411 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1412 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1413 // (b/144430859)
1414 mod.apexSdkVersion = android.FutureApiLevel
1415 }
1416
Spandan Das604f3762023-03-16 22:51:40 +00001417 skipModuleList := map[string]bool{}
1418
Colin Crossa14fb6a2024-10-23 16:57:06 -07001419 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001420 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001421
Yu Liu8024b922024-12-20 23:31:32 +00001422 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001423 depName := ctx.OtherModuleName(dep)
1424 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001425 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1426
Spandan Das604f3762023-03-16 22:51:40 +00001427 if _, exists := skipModuleList[depName]; exists {
1428 return
1429 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001430
1431 if depTag == android.DarwinUniversalVariantTag {
1432 return
1433 }
1434
Yu Liu8024b922024-12-20 23:31:32 +00001435 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1436 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1437 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1438 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1439 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001440 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001441 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001442
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001443 switch {
1444 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001445 dylib := rustInfo.CompilerInfo.LibraryInfo
1446 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1448 return
1449 }
Yu Liu8024b922024-12-20 23:31:32 +00001450 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001451 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001452 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1453
Colin Crossb614cd42024-10-11 12:52:21 -07001454 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1455 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1456 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1457 }
1458
Yu Liu8024b922024-12-20 23:31:32 +00001459 if !rustInfo.CompilerInfo.NoStdlibs {
1460 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1461 if ctx.Device() {
1462 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1463 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001464 if rustDepStdLinkage != modStdLinkage {
1465 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1466 return
1467 }
1468 }
1469
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001470 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001471 rlib := rustInfo.CompilerInfo.LibraryInfo
1472 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001473 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001474 return
1475 }
Yu Liu8024b922024-12-20 23:31:32 +00001476 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001477 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001478 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1479
Ivan Lozano0a468a42024-05-13 21:03:34 -04001480 // rust_ffi rlibs may export include dirs, so collect those here.
1481 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1482 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001483 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001484
Colin Crossb614cd42024-10-11 12:52:21 -07001485 // rlibs are not installed, so don't add the output file to directImplementationDeps
1486 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1487 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1488 }
1489
Yu Liu8024b922024-12-20 23:31:32 +00001490 if !rustInfo.CompilerInfo.NoStdlibs {
1491 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1492 if ctx.Device() {
1493 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1494 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001495 if rustDepStdLinkage != modStdLinkage {
1496 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1497 return
1498 }
1499 }
1500
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001501 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001502 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001503 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001504 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001505 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001506
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001507 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001508 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001509 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001510 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001511 case cc.IsStaticDepTag(depTag):
1512 // Rust FFI rlibs should not be declared in a Rust modules
1513 // "static_libs" list as we can't handle them properly at the
1514 // moment (for example, they only produce an rlib-std variant).
1515 // Instead, a normal rust_library variant should be used.
1516 ctx.PropertyErrorf("static_libs",
1517 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1518 depName)
1519
Paul Duffind5cf92e2021-07-09 17:38:55 +01001520 }
1521
Yu Liu8024b922024-12-20 23:31:32 +00001522 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001523
Paul Duffind5cf92e2021-07-09 17:38:55 +01001524 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001525 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1526 // OS/Arch variant is used.
1527 var helper string
1528 if ctx.Host() {
1529 helper = "missing 'host_supported'?"
1530 } else {
1531 helper = "device module defined?"
1532 }
1533
Yu Liu8024b922024-12-20 23:31:32 +00001534 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001535 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1536 return
Yu Liu8024b922024-12-20 23:31:32 +00001537 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001538 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1539 return
1540 }
Yu Liu8024b922024-12-20 23:31:32 +00001541 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001542 }
1543
Ivan Lozano0a468a42024-05-13 21:03:34 -04001544 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001545 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001546 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001547 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1548 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001549 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001550 }
1551
Ivan Lozanoffee3342019-08-27 12:03:00 -07001552 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001553 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001554 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001555 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001556 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001557 }
1558 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001559
Ivan Lozanod106efe2023-09-21 23:30:26 -04001560 if depTag == sourceDepTag {
1561 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001562 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001563 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001564 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1565 }
1566 }
1567 }
Yu Liu8024b922024-12-20 23:31:32 +00001568 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001569 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001570 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1571 if !hasRustInfo {
1572 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001573 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1574 return
1575 }
Yu Liu8024b922024-12-20 23:31:32 +00001576 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001577 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1578 return
1579 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001580 }
Yu Liu8024b922024-12-20 23:31:32 +00001581 linkObject := linkableInfo.OutputFile
Ivan Lozano2093af22020-08-25 12:48:19 -04001582 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001583 if !ctx.Config().AllowMissingDependencies() {
1584 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1585 } else {
1586 ctx.AddMissingDependencies([]string{depName})
1587 }
1588 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001589 }
1590
Wen-yi Chu41326c12023-09-22 03:58:59 +00001591 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001592
Ivan Lozanoffee3342019-08-27 12:03:00 -07001593 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001594 switch {
1595 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001596 if cc.IsWholeStaticLib(depTag) {
1597 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1598 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001599 if mod.Binary() {
1600 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1601 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1602 // final linkage, pass the args directly to the linker to handle these cases.
1603 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1604 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001605 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001606 } else {
1607 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 -05001608 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001609 }
1610
1611 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1612 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001613 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001614 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1615
Colin Cross313aa542023-12-13 13:47:44 -08001616 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001617 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1618 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1619 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1620 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Yu Liu8024b922024-12-20 23:31:32 +00001621 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001622
1623 // Record baseLibName for snapshots.
1624 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1625
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001626 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001627 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001628 // For the shared lib dependencies, we may link to the stub variant
1629 // of the dependency depending on the context (e.g. if this
1630 // dependency crosses the APEX boundaries).
1631 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1632
Colin Crossb614cd42024-10-11 12:52:21 -07001633 if !sharedLibraryInfo.IsStubs {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001634 // TODO(b/362509506): remove this additional check once all apex_exclude uses are switched to stubs.
1635 if !linkableInfo.RustApexExclude {
1636 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1637 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1638 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1639 }
Colin Crossb614cd42024-10-11 12:52:21 -07001640 }
1641 }
1642
Jiyong Park7d55b612021-06-11 17:22:09 +09001643 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1644 // object (either from stub or non-stub) to use.
1645 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001646 if !linkObject.Valid() {
1647 if !ctx.Config().AllowMissingDependencies() {
1648 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1649 } else {
1650 ctx.AddMissingDependencies([]string{depName})
1651 }
1652 return
1653 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001654 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001655
Ivan Lozanoffee3342019-08-27 12:03:00 -07001656 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001657 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
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...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001662 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001663
1664 // Record baseLibName for snapshots.
1665 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1666
Cole Faustb6e6f992023-08-17 17:42:26 -07001667 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001668 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001669 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001670 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001671 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1672 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1673 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001674 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001675 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001676 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001677 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001678 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001679 }
1680
1681 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001682 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1683 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001684 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001685 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001686 } else {
1687 switch {
1688 case depTag == cc.CrtBeginDepTag:
1689 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1690 case depTag == cc.CrtEndDepTag:
1691 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1692 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001693 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001694
Yu Liuc41eae52025-01-14 01:03:08 +00001695 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001696 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001697 // These are usually genrules which don't have per-target variants.
1698 directSrcDeps = append(directSrcDeps, srcDep)
1699 }
1700 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001701 })
1702
Colin Crossa14fb6a2024-10-23 16:57:06 -07001703 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001704
1705 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001706 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001707 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001708 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001709 if alias, aliased := aliases[crateName]; aliased {
1710 crateName = alias
1711 }
Yu Liu8024b922024-12-20 23:31:32 +00001712 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001713 }
1714 var dylibDepFiles RustLibraries
1715 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001716 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001717 if alias, aliased := aliases[crateName]; aliased {
1718 crateName = alias
1719 }
Yu Liu8024b922024-12-20 23:31:32 +00001720 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001721 }
1722 var procMacroDepFiles RustLibraries
1723 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001724 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001725 if alias, aliased := aliases[crateName]; aliased {
1726 crateName = alias
1727 }
Yu Liu8024b922024-12-20 23:31:32 +00001728 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001729 }
1730
Colin Cross004bd3f2023-10-02 11:39:17 -07001731 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001732 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001733 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001734 }
1735
Colin Cross004bd3f2023-10-02 11:39:17 -07001736 var sharedLibFiles android.Paths
1737 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001738 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001739 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001740 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001741 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001742 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001743 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001744 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001745 }
1746
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001747 var srcProviderDepFiles android.Paths
1748 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001749 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001750 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1751 }
1752 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001753 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001754 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1755 }
1756
Wen-yi Chu41326c12023-09-22 03:58:59 +00001757 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1758 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001759 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1760 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1761 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001762 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001763 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001764
1765 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001766 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001767 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001768 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001769 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1770 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1771 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoad7ba592025-01-23 01:23:43 +00001772 depPaths.depLinkFlags = android.FirstUniqueStrings(depPaths.depLinkFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001773
1774 return depPaths
1775}
1776
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001777func (mod *Module) InstallInData() bool {
1778 if mod.compiler == nil {
1779 return false
1780 }
1781 return mod.compiler.inData()
1782}
1783
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001784func (mod *Module) InstallInRamdisk() bool {
1785 return mod.InRamdisk()
1786}
1787
1788func (mod *Module) InstallInVendorRamdisk() bool {
1789 return mod.InVendorRamdisk()
1790}
1791
1792func (mod *Module) InstallInRecovery() bool {
1793 return mod.InRecovery()
1794}
1795
Wen-yi Chu41326c12023-09-22 03:58:59 +00001796func linkPathFromFilePath(filepath android.Path) string {
1797 return strings.Split(filepath.String(), filepath.Base())[0]
1798}
1799
Spandan Das604f3762023-03-16 22:51:40 +00001800// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1801func (r *Module) usePublicApi() bool {
1802 return r.Device() && r.UseSdk()
1803}
1804
1805// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1806func (r *Module) useVendorApi() bool {
1807 return r.Device() && (r.InVendor() || r.InProduct())
1808}
1809
Ivan Lozanoffee3342019-08-27 12:03:00 -07001810func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1811 ctx := &depsContext{
1812 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001813 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001814
1815 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001816 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001817
1818 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001819 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001820 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001821
Ivan Lozano2b081132020-09-08 12:46:52 -04001822 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001823 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001824 stdLinkage = "rlib-std"
1825 }
1826
1827 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001828
Ivan Lozano2b081132020-09-08 12:46:52 -04001829 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1830 rlibDepVariations = append(rlibDepVariations,
1831 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1832 }
1833
Ivan Lozano1921e802021-05-20 13:39:16 -04001834 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001835 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001836 for _, lib := range deps.Rlibs {
1837 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001838 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001839 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001840
1841 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001842 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001843
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001844 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001845 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001846 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001847
Ivan Lozano1921e802021-05-20 13:39:16 -04001848 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001849 if deps.Rustlibs != nil {
1850 if !mod.compiler.Disabled() {
1851 for _, lib := range deps.Rustlibs {
1852 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1853 if autoDep.depTag == rlibDepTag {
1854 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001855 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1856
Ivan Lozanod106efe2023-09-21 23:30:26 -04001857 } else {
1858 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1859 // Check for the existence of the dylib deptag variant. Select it if available,
1860 // otherwise select the rlib variant.
1861 autoDepVariations := append(commonDepVariations,
1862 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001863 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1864 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001865
Ivan Lozanod106efe2023-09-21 23:30:26 -04001866 } else {
1867 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001868 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1869
Ivan Lozanod106efe2023-09-21 23:30:26 -04001870 }
1871 }
1872 }
1873 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1874 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001875 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001876 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001877
Ivan Lozano0a468a42024-05-13 21:03:34 -04001878 // Only add rustlib dependencies if they're source providers themselves.
1879 // This is used to track which crate names need to be added to the source generated
1880 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001881 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001882 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001883 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001884 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001885 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001886 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001887
Ivan Lozano1921e802021-05-20 13:39:16 -04001888 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001889 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001890 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001891 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001892 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001893 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001894 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001895 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001896 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001897 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1898
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001899 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001900 }
1901 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001902
1903 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001904 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001905 name, version := cc.StubsLibNameAndVersion(lib)
1906
1907 variations := []blueprint.Variation{
1908 {Mutator: "link", Variation: "shared"},
1909 }
Spandan Dasff665182024-09-11 18:48:44 +00001910 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001911 }
1912
1913 for _, lib := range deps.WholeStaticLibs {
1914 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001915
1916 actx.AddVariationDependencies([]blueprint.Variation{
1917 {Mutator: "link", Variation: "static"},
1918 }, depTag, lib)
1919 }
1920
1921 for _, lib := range deps.StaticLibs {
1922 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001923
1924 actx.AddVariationDependencies([]blueprint.Variation{
1925 {Mutator: "link", Variation: "static"},
1926 }, depTag, lib)
1927 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001928
Zach Johnson3df4e632020-11-06 11:56:27 -08001929 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1930
Colin Cross565cafd2020-09-25 18:47:38 -07001931 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001932 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001933 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001934 }
Colin Crossfe605e12022-01-23 20:46:16 -08001935 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001936 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001937 }
1938
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001939 if mod.sourceProvider != nil {
1940 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1941 bindgen.Properties.Custom_bindgen != "" {
1942 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1943 bindgen.Properties.Custom_bindgen)
1944 }
1945 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001946
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001947 actx.AddVariationDependencies([]blueprint.Variation{
1948 {Mutator: "link", Variation: "shared"},
1949 }, dataLibDepTag, deps.DataLibs...)
1950
1951 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1952
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001953 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001954 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001955
1956 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001957}
1958
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001959func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001960 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001961 mod.beginMutator(ctx)
1962 }
1963}
1964
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001965func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1966 ctx := &baseModuleContext{
1967 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001968 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001969
1970 mod.begin(ctx)
1971}
1972
Ivan Lozanoffee3342019-08-27 12:03:00 -07001973func (mod *Module) Name() string {
1974 name := mod.ModuleBase.Name()
1975 if p, ok := mod.compiler.(interface {
1976 Name(string) string
1977 }); ok {
1978 name = p.Name(name)
1979 }
1980 return name
1981}
1982
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001983func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001984 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001985 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001986 }
1987}
1988
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001989var _ android.HostToolProvider = (*Module)(nil)
1990
1991func (mod *Module) HostToolPath() android.OptionalPath {
1992 if !mod.Host() {
1993 return android.OptionalPath{}
1994 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001995 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1996 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001997 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1998 // Even though proc-macros aren't strictly "tools", since they target the compiler
1999 // and act as compiler plugins, we treat them similarly.
2000 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002001 }
2002 return android.OptionalPath{}
2003}
2004
Jiyong Park99644e92020-11-17 22:21:02 +09002005var _ android.ApexModule = (*Module)(nil)
2006
Ivan Lozano24cf0362024-10-04 16:02:38 +00002007// If a module is marked for exclusion from apexes, don't provide apex variants.
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002008// TODO(b/362509506): remove this once all apex_exclude usages are removed.
Ivan Lozano24cf0362024-10-04 16:02:38 +00002009func (m *Module) CanHaveApexVariants() bool {
2010 if m.ApexExclude() {
2011 return false
2012 } else {
2013 return m.ApexModuleBase.CanHaveApexVariants()
2014 }
2015}
2016
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002017func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002018 return String(mod.Properties.Min_sdk_version)
2019}
2020
Jiyong Park45bf82e2020-12-15 22:29:02 +09002021// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09002022func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002023 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002024 if minSdkVersion == "apex_inherit" {
2025 return nil
2026 }
2027 if minSdkVersion == "" {
2028 return fmt.Errorf("min_sdk_version is not specificed")
2029 }
2030
2031 // Not using nativeApiLevelFromUser because the context here is not
2032 // necessarily a native context.
2033 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
2034 if err != nil {
2035 return err
2036 }
2037
2038 if ver.GreaterThan(sdkVersion) {
2039 return fmt.Errorf("newer SDK(%v)", ver)
2040 }
Jiyong Park99644e92020-11-17 22:21:02 +09002041 return nil
2042}
2043
Jiyong Park45bf82e2020-12-15 22:29:02 +09002044// Implements android.ApexModule
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002045func (mod *Module) AlwaysRequiresPlatformApexVariant() bool {
2046 // stub libraries and native bridge libraries are always available to platform
2047 // TODO(b/362509506): remove the ApexExclude() check once all apex_exclude uses are switched to stubs.
2048 return mod.IsStubs() || mod.Target().NativeBridge == android.NativeBridgeEnabled || mod.ApexExclude()
2049}
2050
2051// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002052func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07002053 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09002054 return false
2055 }
2056
Colin Cross8acea3e2024-12-12 14:53:30 -08002057 if mod.Static() && cc.IsSharedDepTag(depTag) {
2058 // shared_lib dependency from a static lib is considered as crossing
2059 // the APEX boundary because the dependency doesn't actually is
2060 // linked; the dependency is used only during the compilation phase.
2061 return false
2062 }
2063
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002064 if depTag == cc.StubImplDepTag {
2065 // We don't track from an implementation library to its stubs.
2066 return false
2067 }
2068
2069 if cc.ExcludeInApexDepTag(depTag) {
2070 return false
2071 }
2072
2073 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2074 if mod.ApexExclude() {
2075 return false
2076 }
2077
Jiyong Park99644e92020-11-17 22:21:02 +09002078 return true
2079}
2080
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002081func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002082 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2083 if mod.ApexExclude() {
2084 return false
2085 }
2086
2087 if mod.HasStubsVariants() {
Colin Crossbd930bc2025-02-03 12:17:42 -08002088 if cc.IsSharedDepTag(depTag) && !cc.IsExplicitImplSharedDepTag(depTag) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002089 // dynamic dep to a stubs lib crosses APEX boundary
2090 return false
2091 }
2092 if cc.IsRuntimeDepTag(depTag) {
2093 // runtime dep to a stubs lib also crosses APEX boundary
2094 return false
2095 }
2096 if cc.IsHeaderDepTag(depTag) {
2097 return false
2098 }
2099 }
2100 return true
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002101}
2102
Jiyong Park99644e92020-11-17 22:21:02 +09002103// Overrides ApexModule.IsInstallabeToApex()
2104func (mod *Module) IsInstallableToApex() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002105 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2106 if mod.ApexExclude() {
2107 return false
2108 }
2109
Jiyong Park99644e92020-11-17 22:21:02 +09002110 if mod.compiler != nil {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002111 if lib, ok := mod.compiler.(libraryInterface); ok {
2112 return (lib.shared() || lib.dylib()) && !lib.BuildStubs()
Jiyong Park99644e92020-11-17 22:21:02 +09002113 }
2114 if _, ok := mod.compiler.(*binaryDecorator); ok {
2115 return true
2116 }
2117 }
2118 return false
2119}
2120
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05002121// If a library file has a "lib" prefix, extract the library name without the prefix.
2122func libNameFromFilePath(filepath android.Path) (string, bool) {
2123 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
2124 if strings.HasPrefix(libName, "lib") {
2125 libName = libName[3:]
2126 return libName, true
2127 }
2128 return "", false
2129}
2130
Sasha Smundaka76acba2022-04-18 20:12:56 -07002131func kytheExtractRustFactory() android.Singleton {
2132 return &kytheExtractRustSingleton{}
2133}
2134
2135type kytheExtractRustSingleton struct {
2136}
2137
2138func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2139 var xrefTargets android.Paths
2140 ctx.VisitAllModules(func(module android.Module) {
2141 if rustModule, ok := module.(xref); ok {
2142 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
2143 }
2144 })
2145 if len(xrefTargets) > 0 {
2146 ctx.Phony("xref_rust", xrefTargets...)
2147 }
2148}
2149
Jihoon Kangf78a8902022-09-01 22:47:07 +00002150func (c *Module) Partition() string {
2151 return ""
2152}
2153
Ivan Lozanoffee3342019-08-27 12:03:00 -07002154var Bool = proptools.Bool
2155var BoolDefault = proptools.BoolDefault
2156var String = proptools.String
2157var StringPtr = proptools.StringPtr