blob: 9082678bc47e545f9436ee5f10633d78a6048eb0 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
Wei Lia1aa2972024-06-21 13:08:51 -070019 "strconv"
Chris Parsons637458d2023-09-19 20:09:00 +000020 "strings"
21
Sasha Smundaka76acba2022-04-18 20:12:56 -070022 "android/soong/bloaty"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090023
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "github.com/google/blueprint"
Colin Crossa14fb6a2024-10-23 16:57:06 -070025 "github.com/google/blueprint/depset"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020030 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070031 "android/soong/fuzz"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
33)
34
35var pctx = android.NewPackageContext("android/soong/rust")
36
Yu Liu8024b922024-12-20 23:31:32 +000037type LibraryInfo struct {
38 Rlib bool
39 Dylib bool
40}
41
42type CompilerInfo struct {
43 StdLinkageForDevice RustLinkage
44 StdLinkageForNonDevice RustLinkage
45 NoStdlibs bool
46 LibraryInfo *LibraryInfo
47}
48
49type ProtobufDecoratorInfo struct{}
50
51type SourceProviderInfo struct {
Yu Liu727204c2025-01-23 20:58:32 +000052 Srcs android.Paths
Yu Liu8024b922024-12-20 23:31:32 +000053 ProtobufDecoratorInfo *ProtobufDecoratorInfo
54}
55
56type RustInfo struct {
57 AndroidMkSuffix string
58 RustSubName string
59 TransitiveAndroidMkSharedLibs depset.DepSet[string]
60 CompilerInfo *CompilerInfo
61 SnapshotInfo *cc.SnapshotInfo
62 SourceProviderInfo *SourceProviderInfo
63}
64
65var RustInfoProvider = blueprint.NewProvider[*RustInfo]()
66
Ivan Lozanoffee3342019-08-27 12:03:00 -070067func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070068 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070069 android.PreDepsMutators(registerPreDepsMutators)
70 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090071 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070072 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020073 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000074 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070075}
76
Colin Cross8a49a3d2024-05-20 12:22:27 -070077func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
78 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
79 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070080 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070081}
82
83func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070084 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070085}
86
Ivan Lozanoffee3342019-08-27 12:03:00 -070087type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040088 GlobalRustFlags []string // Flags that apply globally to rust
89 GlobalLinkFlags []string // Flags that apply globally to linker
90 RustFlags []string // Flags that apply to rust
91 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020092 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070093 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070094 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040095 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020096 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070097 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070098}
99
100type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -0500101 AndroidMkRlibs []string `blueprint:"mutated"`
102 AndroidMkDylibs []string `blueprint:"mutated"`
103 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -0500104 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +0000105 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -0400106
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900107 ImageVariation string `blueprint:"mutated"`
108 VndkVersion string `blueprint:"mutated"`
109 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -0500110
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400111 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
112 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
113 // appended before SubName.
114 RustSubName string `blueprint:"mutated"`
115
Ivan Lozano6a884432020-12-02 09:15:16 -0500116 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +0000117 ProductVariantNeeded bool `blueprint:"mutated"`
118 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500119 CoreVariantNeeded bool `blueprint:"mutated"`
120 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -0700121 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -0800122 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500123 ExtraVariants []string `blueprint:"mutated"`
124
Ivan Lozanoa2268632021-07-22 10:52:06 -0400125 // Allows this module to use non-APEX version of libraries. Useful
126 // for building binaries that are started before APEXes are activated.
127 Bootstrap *bool
128
Ivan Lozano1921e802021-05-20 13:39:16 -0400129 // Used by vendor snapshot to record dependencies from snapshot modules.
130 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900131 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400132 SnapshotRlibs []string `blueprint:"mutated"`
133 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400134
Matthew Maurerc6868382021-07-13 14:12:37 -0700135 // Make this module available when building for ramdisk.
136 // On device without a dedicated recovery partition, the module is only
137 // available after switching root into
138 // /first_stage_ramdisk. To expose the module before switching root, install
139 // the recovery variant instead.
140 Ramdisk_available *bool
141
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500142 // Make this module available when building for vendor ramdisk.
143 // On device without a dedicated recovery partition, the module is only
144 // available after switching root into
145 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800146 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500147 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400148
Ivan Lozano1921e802021-05-20 13:39:16 -0400149 // Normally Soong uses the directory structure to decide which modules
150 // should be included (framework) or excluded (non-framework) from the
151 // different snapshots (vendor, recovery, etc.), but this property
152 // allows a partner to exclude a module normally thought of as a
153 // framework module from the vendor snapshot.
154 Exclude_from_vendor_snapshot *bool
155
156 // Normally Soong uses the directory structure to decide which modules
157 // should be included (framework) or excluded (non-framework) from the
158 // different snapshots (vendor, recovery, etc.), but this property
159 // allows a partner to exclude a module normally thought of as a
160 // framework module from the recovery snapshot.
161 Exclude_from_recovery_snapshot *bool
162
Matthew Maurer460ee942021-02-11 12:31:46 -0800163 // Make this module available when building for recovery
164 Recovery_available *bool
165
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000166 // The API level that this module is built against. The APIs of this API level will be
167 // visible at build time, but use of any APIs newer than min_sdk_version will render the
168 // module unloadable on older devices. In the future it will be possible to weakly-link new
169 // APIs, making the behavior match Java: such modules will load on older devices, but
170 // calling new APIs on devices that do not support them will result in a crash.
171 //
172 // This property has the same behavior as sdk_version does for Java modules. For those
173 // familiar with Android Gradle, the property behaves similarly to how compileSdkVersion
174 // does for Java code.
175 //
176 // In addition, setting this property causes two variants to be built, one for the platform
177 // and one for apps.
178 Sdk_version *string
179
180 // Minimum OS API level supported by this C or C++ module. This property becomes the value
181 // of the __ANDROID_API__ macro. When the C or C++ module is included in an APEX or an APK,
182 // this property is also used to ensure that the min_sdk_version of the containing module is
183 // not older (i.e. less) than this module's min_sdk_version. When not set, this property
184 // defaults to the value of sdk_version. When this is set to "apex_inherit", this tracks
185 // min_sdk_version of the containing APEX. When the module
186 // is not built for an APEX, "apex_inherit" defaults to sdk_version.
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500187 Min_sdk_version *string
188
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000189 // Variant is an SDK variant created by sdkMutator
190 IsSdkVariant bool `blueprint:"mutated"`
191
192 // Set by factories of module types that can only be referenced from variants compiled against
193 // the SDK.
194 AlwaysSdk bool `blueprint:"mutated"`
195
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900196 HideFromMake bool `blueprint:"mutated"`
197 PreventInstall bool `blueprint:"mutated"`
198
199 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700200}
201
202type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700203 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700204
Ivan Lozano6a884432020-12-02 09:15:16 -0500205 VendorProperties cc.VendorProperties
206
Ivan Lozanoffee3342019-08-27 12:03:00 -0700207 Properties BaseProperties
208
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000209 hod android.HostOrDeviceSupported
210 multilib android.Multilib
211 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212
Ivan Lozano6a884432020-12-02 09:15:16 -0500213 makeLinkType string
214
Yi Kong46c6e592022-01-20 22:55:00 +0800215 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700216 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400217 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200218 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500219 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700220 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400221 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700222 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400223
Ivan Lozano0a468a42024-05-13 21:03:34 -0400224 exportedLinkDirs []string
225
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400226 // Output file to be installed, may be stripped or unstripped.
227 outputFile android.OptionalPath
228
Sasha Smundaka76acba2022-04-18 20:12:56 -0700229 // Cross-reference input file
230 kytheFiles android.Paths
231
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400232 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900233
234 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500235
236 // For apex variants, this is set as apex.min_sdk_version
237 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700238
Colin Crossa14fb6a2024-10-23 16:57:06 -0700239 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000240
241 // Shared flags among stubs build rules of this module
242 sharedFlags cc.SharedFlags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243}
244
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500245func (mod *Module) Header() bool {
246 //TODO: If Rust libraries provide header variants, this needs to be updated.
247 return false
248}
249
250func (mod *Module) SetPreventInstall() {
251 mod.Properties.PreventInstall = true
252}
253
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500254func (mod *Module) SetHideFromMake() {
255 mod.Properties.HideFromMake = true
256}
257
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900258func (mod *Module) HiddenFromMake() bool {
259 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400260}
261
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500262func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500263 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
264 // nil since we need compiler to actually sanitize.
265 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500266}
267
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500268func (mod *Module) IsPrebuilt() bool {
269 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
270 return true
271 }
272 return false
273}
274
Ivan Lozano52767be2019-10-18 14:49:46 -0700275func (mod *Module) SelectedStl() string {
276 return ""
277}
278
Ivan Lozano2b262972019-11-21 12:30:50 -0800279func (mod *Module) NonCcVariants() bool {
280 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400281 if library, ok := mod.compiler.(libraryInterface); ok {
282 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800283 }
284 }
285 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
286}
287
Ivan Lozano52767be2019-10-18 14:49:46 -0700288func (mod *Module) Static() bool {
289 if mod.compiler != nil {
290 if library, ok := mod.compiler.(libraryInterface); ok {
291 return library.static()
292 }
293 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400294 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700295}
296
297func (mod *Module) Shared() bool {
298 if mod.compiler != nil {
299 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400300 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 }
302 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400303 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700304}
305
Ivan Lozanod7586b62021-04-01 09:49:36 -0400306func (mod *Module) Dylib() bool {
307 if mod.compiler != nil {
308 if library, ok := mod.compiler.(libraryInterface); ok {
309 return library.dylib()
310 }
311 }
312 return false
313}
314
Ivan Lozanod106efe2023-09-21 23:30:26 -0400315func (mod *Module) Source() bool {
316 if mod.compiler != nil {
317 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
318 return library.source()
319 }
320 }
321 return false
322}
323
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400324func (mod *Module) RlibStd() bool {
325 if mod.compiler != nil {
326 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
327 return library.rlibStd()
328 }
329 }
330 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
331}
332
Ivan Lozanod7586b62021-04-01 09:49:36 -0400333func (mod *Module) Rlib() bool {
334 if mod.compiler != nil {
335 if library, ok := mod.compiler.(libraryInterface); ok {
336 return library.rlib()
337 }
338 }
339 return false
340}
341
342func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400343 if binary, ok := mod.compiler.(binaryInterface); ok {
344 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400345 }
346 return false
347}
348
Justin Yun5e035862021-06-29 20:50:37 +0900349func (mod *Module) StaticExecutable() bool {
350 if !mod.Binary() {
351 return false
352 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400353 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900354}
355
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000356func (mod *Module) ApexExclude() bool {
357 if mod.compiler != nil {
358 if library, ok := mod.compiler.(libraryInterface); ok {
359 return library.apexExclude()
360 }
361 }
362 return false
363}
364
Ivan Lozanod7586b62021-04-01 09:49:36 -0400365func (mod *Module) Object() bool {
366 // Rust has no modules which produce only object files.
367 return false
368}
369
Ivan Lozano52767be2019-10-18 14:49:46 -0700370func (mod *Module) Toc() android.OptionalPath {
371 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400372 if lib, ok := mod.compiler.(libraryInterface); ok {
373 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700374 }
375 }
376 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
377}
378
Colin Crossc511bc52020-04-07 16:50:32 +0000379func (mod *Module) UseSdk() bool {
Ivan Lozano8ef256d2025-02-05 21:15:03 +0000380 if cc.CanUseSdk(mod) {
381 return String(mod.Properties.Sdk_version) != ""
382 }
Colin Crossc511bc52020-04-07 16:50:32 +0000383 return false
384}
385
Ivan Lozanod7586b62021-04-01 09:49:36 -0400386func (mod *Module) RelativeInstallPath() string {
387 if mod.compiler != nil {
388 return mod.compiler.relativeInstallPath()
389 }
390 return ""
391}
392
Ivan Lozano52767be2019-10-18 14:49:46 -0700393func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500394 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700395}
396
Jiyong Park7d55b612021-06-11 17:22:09 +0900397func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400398 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900399}
400
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400401func (mod *Module) SubName() string {
402 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700403}
404
Ivan Lozanof1868af2022-04-12 13:08:36 -0400405func (mod *Module) IsVndkPrebuiltLibrary() bool {
406 // Rust modules do not provide VNDK prebuilts
407 return false
408}
409
410func (mod *Module) IsVendorPublicLibrary() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000411 // Rust modules do not currently support vendor_public_library
412 return false
Ivan Lozanof1868af2022-04-12 13:08:36 -0400413}
414
415func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
416 // Rust modules to not provide Sdk variants
417 return false
418}
419
Colin Cross127bb8b2020-12-16 16:46:01 -0800420func (c *Module) IsVndkPrivate() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000421 // Rust modules do not currently support VNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800422 return false
423}
424
425func (c *Module) IsLlndk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000426 // Rust modules do not currently support LLNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800427 return false
428}
429
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400430func (mod *Module) KernelHeadersDecorator() bool {
431 return false
432}
433
Colin Cross1f3f1302021-04-26 18:37:44 -0700434func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000435 // Rust modules do not currently support LLNDK variants
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400436 return false
437}
438
Colin Cross5271fea2021-04-27 13:06:04 -0700439func (m *Module) NeedsVendorPublicLibraryVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000440 // Rust modules do not currently support vendor_public_library
Colin Cross5271fea2021-04-27 13:06:04 -0700441 return false
442}
443
Ivan Lozanod7586b62021-04-01 09:49:36 -0400444func (mod *Module) HasLlndkStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000445 // Rust modules do not currently support LLNDK stubs
Ivan Lozanod7586b62021-04-01 09:49:36 -0400446 return false
447}
448
Ivan Lozano52767be2019-10-18 14:49:46 -0700449func (mod *Module) SdkVersion() string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000450 return String(mod.Properties.Sdk_version)
Ivan Lozano52767be2019-10-18 14:49:46 -0700451}
452
Colin Crossc511bc52020-04-07 16:50:32 +0000453func (mod *Module) AlwaysSdk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000454 return mod.Properties.AlwaysSdk
Colin Crossc511bc52020-04-07 16:50:32 +0000455}
456
Jiyong Park2286afd2020-06-16 21:58:53 +0900457func (mod *Module) IsSdkVariant() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000458 return mod.Properties.IsSdkVariant
Jiyong Park2286afd2020-06-16 21:58:53 +0900459}
460
Colin Cross1348ce32020-10-01 13:37:16 -0700461func (mod *Module) SplitPerApiLevel() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000462 return cc.CanUseSdk(mod) && mod.IsCrt()
Colin Cross1348ce32020-10-01 13:37:16 -0700463}
464
Sasha Smundaka76acba2022-04-18 20:12:56 -0700465func (mod *Module) XrefRustFiles() android.Paths {
466 return mod.kytheFiles
467}
468
Ivan Lozanoffee3342019-08-27 12:03:00 -0700469type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400470 Dylibs []string
471 Rlibs []string
472 Rustlibs []string
473 Stdlibs []string
474 ProcMacros []string
475 SharedLibs []string
476 StaticLibs []string
477 WholeStaticLibs []string
478 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400480 // Used for data dependencies adjacent to tests
481 DataLibs []string
482 DataBins []string
483
Colin Crossfe605e12022-01-23 20:46:16 -0800484 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485}
486
487type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700488 DyLibs RustLibraries
489 RLibs RustLibraries
490 SharedLibs android.Paths
491 SharedLibDeps android.Paths
492 StaticLibs android.Paths
493 ProcMacros RustLibraries
494 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000495 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500496
497 // depFlags and depLinkFlags are rustc and linker (clang) flags.
498 depFlags []string
499 depLinkFlags []string
500
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400501 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500502 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000503 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700504 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700505
Ivan Lozano0a468a42024-05-13 21:03:34 -0400506 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
507 // cc_library_static dependants of rlibs.
508 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
509 exportedLinkDirs []string
510
Ivan Lozano45901ed2020-07-24 16:05:01 -0400511 // Used by bindgen modules which call clang
512 depClangFlags []string
513 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400514 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400515 depSystemIncludePaths android.Paths
516
Colin Crossfe605e12022-01-23 20:46:16 -0800517 CrtBegin android.Paths
518 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700519
520 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500521 SrcDeps android.Paths
522 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700523
524 directImplementationDeps android.Paths
525 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700526}
527
528type RustLibraries []RustLibrary
529
530type RustLibrary struct {
531 Path android.Path
532 CrateName string
533}
534
Matthew Maurerbb3add12020-06-25 09:34:12 -0700535type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000536 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700537 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700538}
539
Sasha Smundaka76acba2022-04-18 20:12:56 -0700540type xref interface {
541 XrefRustFiles() android.Paths
542}
543
Matthew Maurerbb3add12020-06-25 09:34:12 -0700544type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000545 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400546 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700547 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700548}
549
Wen-yi Chu41326c12023-09-22 03:58:59 +0000550func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
551 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700552}
553
Colin Cross004bd3f2023-10-02 11:39:17 -0700554func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
555 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400556}
557
Colin Cross0de8a1e2020-09-18 14:15:30 -0700558func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800559 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700560 LinkDirs: flagExporter.linkDirs,
561 LinkObjects: flagExporter.linkObjects,
562 })
563}
564
Matthew Maurerbb3add12020-06-25 09:34:12 -0700565var _ exportedFlagsProducer = (*flagExporter)(nil)
566
567func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700568 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700569}
570
Colin Cross0de8a1e2020-09-18 14:15:30 -0700571type FlagExporterInfo struct {
572 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000573 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700574 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700575}
576
Colin Crossbc7d76c2023-12-12 16:39:03 -0800577var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700578
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400579func (mod *Module) isCoverageVariant() bool {
580 return mod.coverage.Properties.IsCoverageVariant
581}
582
583var _ cc.Coverage = (*Module)(nil)
584
Colin Crosse1a85552024-06-14 12:17:37 -0700585func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400586 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
587}
588
Ivan Lozanod7586b62021-04-01 09:49:36 -0400589func (mod *Module) VndkVersion() string {
590 return mod.Properties.VndkVersion
591}
592
Ivan Lozano0a468a42024-05-13 21:03:34 -0400593func (mod *Module) ExportedCrateLinkDirs() []string {
594 return mod.exportedLinkDirs
595}
596
Ivan Lozanod7586b62021-04-01 09:49:36 -0400597func (mod *Module) PreventInstall() bool {
598 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400599}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000600func (c *Module) ForceDisableSanitizers() {
601 c.sanitize.Properties.ForceDisable = true
602}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400603
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400604func (mod *Module) MarkAsCoverageVariant(coverage bool) {
605 mod.coverage.Properties.IsCoverageVariant = coverage
606}
607
608func (mod *Module) EnableCoverageIfNeeded() {
609 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
612func defaultsFactory() android.Module {
613 return DefaultsFactory()
614}
615
616type Defaults struct {
617 android.ModuleBase
618 android.DefaultsModuleBase
619}
620
621func DefaultsFactory(props ...interface{}) android.Module {
622 module := &Defaults{}
623
624 module.AddProperties(props...)
625 module.AddProperties(
626 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800627 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500628 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100629 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400630 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700631 &BaseCompilerProperties{},
632 &BinaryCompilerProperties{},
633 &LibraryCompilerProperties{},
634 &ProcMacroCompilerProperties{},
635 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400636 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700637 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400638 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400639 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200640 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500641 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000642 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643 )
644
645 android.InitDefaultsModule(module)
646 return module
647}
648
649func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700650 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651}
652
Ivan Lozano183a3212019-10-18 14:18:45 -0700653func (mod *Module) CcLibrary() bool {
654 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500655 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700656 return true
657 }
658 }
659 return false
660}
661
662func (mod *Module) CcLibraryInterface() bool {
663 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400664 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
665 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000666 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700667 return true
668 }
669 }
670 return false
671}
672
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400673func (mod *Module) RustLibraryInterface() bool {
674 if mod.compiler != nil {
675 if _, ok := mod.compiler.(libraryInterface); ok {
676 return true
677 }
678 }
679 return false
680}
681
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500682func (mod *Module) IsFuzzModule() bool {
683 if _, ok := mod.compiler.(*fuzzDecorator); ok {
684 return true
685 }
686 return false
687}
688
689func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
690 return mod.FuzzModule
691}
692
693func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
694 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
695 return fuzzer.fuzzPackagedModule
696 }
697 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
698}
699
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000700func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500701 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
702 return fuzzer.sharedLibraries
703 }
704 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
705}
706
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400707func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400708 if mod.compiler != nil {
709 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400710 }
711 return nil
712}
713
Ivan Lozano183a3212019-10-18 14:18:45 -0700714func (mod *Module) SetStatic() {
715 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700716 if library, ok := mod.compiler.(libraryInterface); ok {
717 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700718 return
719 }
720 }
721 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
722}
723
724func (mod *Module) SetShared() {
725 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700726 if library, ok := mod.compiler.(libraryInterface); ok {
727 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700728 return
729 }
730 }
731 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
732}
733
Ivan Lozano183a3212019-10-18 14:18:45 -0700734func (mod *Module) BuildStaticVariant() bool {
735 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700736 if library, ok := mod.compiler.(libraryInterface); ok {
737 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700738 }
739 }
740 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
741}
742
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400743func (mod *Module) BuildRlibVariant() bool {
744 if mod.compiler != nil {
745 if library, ok := mod.compiler.(libraryInterface); ok {
746 return library.buildRlib()
747 }
748 }
749 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
750}
751
Ivan Lozano183a3212019-10-18 14:18:45 -0700752func (mod *Module) BuildSharedVariant() bool {
753 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700754 if library, ok := mod.compiler.(libraryInterface); ok {
755 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700756 }
757 }
758 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
759}
760
Ivan Lozano183a3212019-10-18 14:18:45 -0700761func (mod *Module) Module() android.Module {
762 return mod
763}
764
Ivan Lozano183a3212019-10-18 14:18:45 -0700765func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400766 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700767}
768
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400769func (mod *Module) CoverageFiles() android.Paths {
770 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800771 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400772 }
773 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
774}
775
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400776// Rust does not produce gcno files, and therefore does not produce a coverage archive.
777func (mod *Module) CoverageOutputFile() android.OptionalPath {
778 return android.OptionalPath{}
779}
780
781func (mod *Module) IsNdk(config android.Config) bool {
782 return false
783}
784
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000785func (mod *Module) IsStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000786 if lib, ok := mod.compiler.(libraryInterface); ok {
787 return lib.BuildStubs()
788 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000789 return false
790}
791
Spandan Das10c41362024-12-03 01:33:09 +0000792func (mod *Module) HasStubsVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000793 if lib, ok := mod.compiler.(libraryInterface); ok {
794 return lib.HasStubsVariants()
795 }
Spandan Das10c41362024-12-03 01:33:09 +0000796 return false
797}
798
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000799func (mod *Module) ApexSdkVersion() android.ApiLevel {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000800 return mod.apexSdkVersion
801}
802
803func (mod *Module) RustApexExclude() bool {
804 return mod.ApexExclude()
805}
806
807func (mod *Module) getSharedFlags() *cc.SharedFlags {
808 shared := &mod.sharedFlags
809 if shared.FlagsMap == nil {
810 shared.NumSharedFlags = 0
811 shared.FlagsMap = make(map[string]string)
812 }
813 return shared
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000814}
815
816func (mod *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000817 name := mod.BaseModuleName()
818 if versioned, ok := mod.compiler.(cc.VersionedInterface); ok {
819 name = versioned.ImplementationModuleName(name)
820 }
821 return name
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000822}
823
824func (mod *Module) Multilib() string {
825 return mod.Arch().ArchType.Multilib
826}
827
828func (mod *Module) IsCrt() bool {
829 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400830 return false
831}
832
Jiyong Park459feca2020-12-15 11:02:21 +0900833func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900834 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900835 return false
836 }
837
Jiyong Park459feca2020-12-15 11:02:21 +0900838 // The apex variant is not installable because it is included in the APEX and won't appear
839 // in the system partition as a standalone file.
840 if !apexInfo.IsForPlatform() {
841 return false
842 }
843
Jiyong Parke54f07e2021-04-07 15:08:04 +0900844 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900845}
846
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500847func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800848 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
849 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500850}
851
Ivan Lozano183a3212019-10-18 14:18:45 -0700852var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000853var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700854
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855func (mod *Module) Init() android.Module {
856 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500857 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858
Yi Kong46c6e592022-01-20 22:55:00 +0800859 if mod.afdo != nil {
860 mod.AddProperties(mod.afdo.props()...)
861 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 if mod.compiler != nil {
863 mod.AddProperties(mod.compiler.compilerProps()...)
864 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400865 if mod.coverage != nil {
866 mod.AddProperties(mod.coverage.props()...)
867 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200868 if mod.clippy != nil {
869 mod.AddProperties(mod.clippy.props()...)
870 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400871 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700872 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400873 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500874 if mod.sanitize != nil {
875 mod.AddProperties(mod.sanitize.props()...)
876 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400877
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900879 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880
881 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 return mod
883}
884
885func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
886 return &Module{
887 hod: hod,
888 multilib: multilib,
889 }
890}
891func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
892 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800893 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400894 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200895 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500896 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897 return module
898}
899
900type ModuleContext interface {
901 android.ModuleContext
902 ModuleContextIntf
903}
904
905type BaseModuleContext interface {
906 android.BaseModuleContext
907 ModuleContextIntf
908}
909
910type DepsContext interface {
911 android.BottomUpMutatorContext
912 ModuleContextIntf
913}
914
915type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200916 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700917 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918}
919
920type depsContext struct {
921 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922}
923
924type moduleContext struct {
925 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926}
927
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200928type baseModuleContext struct {
929 android.BaseModuleContext
930}
931
932func (ctx *moduleContext) RustModule() *Module {
933 return ctx.Module().(*Module)
934}
935
936func (ctx *moduleContext) toolchain() config.Toolchain {
937 return ctx.RustModule().toolchain(ctx)
938}
939
940func (ctx *depsContext) RustModule() *Module {
941 return ctx.Module().(*Module)
942}
943
944func (ctx *depsContext) toolchain() config.Toolchain {
945 return ctx.RustModule().toolchain(ctx)
946}
947
948func (ctx *baseModuleContext) RustModule() *Module {
949 return ctx.Module().(*Module)
950}
951
952func (ctx *baseModuleContext) toolchain() config.Toolchain {
953 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400954}
955
956func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700957 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
958 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
959 return false
960 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400961 return mod.compiler != nil && mod.compiler.nativeCoverage()
962}
963
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000964func (mod *Module) SetStl(s string) {
965 // STL is a CC concept; do nothing for Rust
966}
967
968func (mod *Module) SetSdkVersion(s string) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000969 mod.Properties.Sdk_version = StringPtr(s)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000970}
971
972func (mod *Module) SetMinSdkVersion(s string) {
973 mod.Properties.Min_sdk_version = StringPtr(s)
974}
975
976func (mod *Module) VersionedInterface() cc.VersionedInterface {
977 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
978 return mod.compiler.(cc.VersionedInterface)
979 }
980 return nil
981}
982
Ivan Lozanod7586b62021-04-01 09:49:36 -0400983func (mod *Module) EverInstallable() bool {
984 return mod.compiler != nil &&
985 // Check to see whether the module is actually ever installable.
986 mod.compiler.everInstallable()
987}
988
989func (mod *Module) Installable() *bool {
990 return mod.Properties.Installable
991}
992
Ivan Lozano872d5792022-03-23 17:31:39 -0400993func (mod *Module) ProcMacro() bool {
994 if pm, ok := mod.compiler.(procMacroInterface); ok {
995 return pm.ProcMacro()
996 }
997 return false
998}
999
Ivan Lozanoffee3342019-08-27 12:03:00 -07001000func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
1001 if mod.cachedToolchain == nil {
1002 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
1003 }
1004 return mod.cachedToolchain
1005}
1006
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +02001007func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
1008 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
1009}
1010
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1012}
1013
1014func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1015 ctx := &moduleContext{
1016 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018
Colin Crossff694a82023-12-13 15:54:49 -08001019 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +09001020 if !apexInfo.IsForPlatform() {
1021 mod.hideApexVariantFromMake = true
1022 }
1023
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -05001025 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
1026
Ivan Lozanof1868af2022-04-12 13:08:36 -04001027 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -07001028
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 if !toolchain.Supported() {
1030 // This toolchain's unsupported, there's nothing to do for this mod.
1031 return
1032 }
1033
1034 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001035 // Export linkDirs for CC rust generatedlibs
1036 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
1037 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
1038
Ivan Lozanoffee3342019-08-27 12:03:00 -07001039 flags := Flags{
1040 Toolchain: toolchain,
1041 }
1042
Ivan Lozano67eada32021-09-23 11:50:33 -04001043 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +08001044 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -05001045 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +08001046 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 if mod.compiler != nil {
1048 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -04001049 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +00001050 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001051 }
1052 if mod.coverage != nil {
1053 flags, deps = mod.coverage.flags(ctx, flags, deps)
1054 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001055 if mod.clippy != nil {
1056 flags, deps = mod.clippy.flags(ctx, flags, deps)
1057 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001058 if mod.sanitize != nil {
1059 flags, deps = mod.sanitize.flags(ctx, flags, deps)
1060 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001061
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001062 // SourceProvider needs to call GenerateSource() before compiler calls
1063 // compile() so it can provide the source. A SourceProvider has
1064 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1065 // variant is responsible for effectively generating the source. The
1066 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001067 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001068 if mod.compiler.(libraryInterface).source() {
1069 mod.sourceProvider.GenerateSource(ctx, deps)
1070 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1071 } else {
Yu Liu727204c2025-01-23 20:58:32 +00001072 sourceMod := actx.GetDirectDepProxyWithTag(mod.Name(), sourceDepTag)
1073 sourceLib := android.OtherModuleProviderOrDefault(ctx, sourceMod, RustInfoProvider).SourceProviderInfo
1074 mod.sourceProvider.setOutputFiles(sourceLib.Srcs)
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001075 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001076 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001077 }
1078
1079 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001080 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001081 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001082 if ctx.Failed() {
1083 return
1084 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001085 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001086 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001087 if buildOutput.kytheFile != nil {
1088 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1089 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001090 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001091
Dan Albert06feee92021-03-19 15:06:02 -07001092 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1093
Colin Crossff694a82023-12-13 15:54:49 -08001094 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001095 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001096 // If the module has been specifically configure to not be installed then
1097 // hide from make as otherwise it will break when running inside make as the
1098 // output path to install will not be specified. Not all uninstallable
1099 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001100 // side dependencies. In particular, proc-macros need to be captured in the
1101 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001102 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001103 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001104 } else if !mod.installable(apexInfo) {
1105 mod.SkipInstall()
1106 }
1107
1108 // Still call install though, the installs will be stored as PackageSpecs to allow
1109 // using the outputs in a genrule.
1110 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001111 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001112 if ctx.Failed() {
1113 return
1114 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001115 // Export your own directory as a linkDir
1116 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1117
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001118 }
Chris Wailes74be7642021-07-22 16:20:28 -07001119
Colin Crossb614cd42024-10-11 12:52:21 -07001120 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1121 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1122 })
1123
Chris Wailes74be7642021-07-22 16:20:28 -07001124 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001125 }
Wei Lia1aa2972024-06-21 13:08:51 -07001126
Yu Liuf6f85492025-01-13 21:02:36 +00001127 linkableInfo := cc.CreateCommonLinkableInfo(ctx, mod)
Yu Liu8024b922024-12-20 23:31:32 +00001128 linkableInfo.Static = mod.Static()
1129 linkableInfo.Shared = mod.Shared()
1130 linkableInfo.CrateName = mod.CrateName()
1131 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001132 if lib, ok := mod.compiler.(cc.VersionedInterface); ok {
1133 linkableInfo.StubsVersion = lib.StubsVersion()
1134 }
1135
Yu Liu8024b922024-12-20 23:31:32 +00001136 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1137
1138 rustInfo := &RustInfo{
1139 AndroidMkSuffix: mod.AndroidMkSuffix(),
1140 RustSubName: mod.Properties.RustSubName,
1141 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1142 }
1143 if mod.compiler != nil {
1144 rustInfo.CompilerInfo = &CompilerInfo{
1145 NoStdlibs: mod.compiler.noStdlibs(),
1146 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1147 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1148 }
1149 if lib, ok := mod.compiler.(libraryInterface); ok {
1150 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1151 Dylib: lib.dylib(),
1152 Rlib: lib.rlib(),
1153 }
1154 }
1155 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1156 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1157 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1158 }
1159 }
1160 }
1161 if mod.sourceProvider != nil {
Yu Liu727204c2025-01-23 20:58:32 +00001162 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1163 Srcs: mod.sourceProvider.Srcs(),
1164 }
Yu Liu8024b922024-12-20 23:31:32 +00001165 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu727204c2025-01-23 20:58:32 +00001166 rustInfo.SourceProviderInfo.ProtobufDecoratorInfo = &ProtobufDecoratorInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001167 }
1168 }
1169 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001170
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001171 ccInfo := &cc.CcInfo{
1172 IsPrebuilt: mod.IsPrebuilt(),
1173 }
1174
Ivan Lozano9587f452025-01-08 03:17:19 +00001175 // Define the linker info if compiler != nil because Rust currently
1176 // does compilation and linking in one step. If this changes in the future,
1177 // move this as appropriate.
1178 ccInfo.LinkerInfo = &cc.LinkerInfo{
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001179 WholeStaticLibs: mod.compiler.baseCompilerProps().Whole_static_libs,
1180 StaticLibs: mod.compiler.baseCompilerProps().Static_libs,
1181 SharedLibs: mod.compiler.baseCompilerProps().Shared_libs,
Ivan Lozano9587f452025-01-08 03:17:19 +00001182 }
1183
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001184 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1185
mrziwang0cbd3b02024-06-20 16:39:25 -07001186 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001187
1188 buildComplianceMetadataInfo(ctx, mod, deps)
Cole Faust58eef4f2025-01-29 15:14:17 -08001189
1190 moduleInfoJSON := ctx.ModuleInfoJSON()
1191 if mod.compiler != nil {
1192 mod.compiler.moduleInfoJSON(ctx, moduleInfoJSON)
1193 }
mrziwang0cbd3b02024-06-20 16:39:25 -07001194}
1195
1196func (mod *Module) setOutputFiles(ctx ModuleContext) {
1197 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1198 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1199 } else if mod.OutputFile().Valid() {
1200 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1201 } else {
1202 ctx.SetOutputFiles(android.Paths{}, "")
1203 }
1204 if mod.compiler != nil {
1205 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1206 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207}
1208
Wei Lia1aa2972024-06-21 13:08:51 -07001209func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1210 // Dump metadata that can not be done in android/compliance-metadata.go
1211 metadataInfo := ctx.ComplianceMetadataInfo()
1212 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1213 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1214
1215 // Static libs
Yu Liu727204c2025-01-23 20:58:32 +00001216 staticDeps := ctx.GetDirectDepsProxyWithTag(rlibDepTag)
Wei Lia1aa2972024-06-21 13:08:51 -07001217 staticDepNames := make([]string, 0, len(staticDeps))
1218 for _, dep := range staticDeps {
1219 staticDepNames = append(staticDepNames, dep.Name())
1220 }
Yu Liu727204c2025-01-23 20:58:32 +00001221 ccStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(false))
Wei Lia1aa2972024-06-21 13:08:51 -07001222 for _, dep := range ccStaticDeps {
1223 staticDepNames = append(staticDepNames, dep.Name())
1224 }
1225
1226 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1227 // C static libraries
1228 for _, dep := range deps.StaticLibs {
1229 staticDepPaths = append(staticDepPaths, dep.String())
1230 }
1231 // Rust static libraries
1232 for _, dep := range deps.RLibs {
1233 staticDepPaths = append(staticDepPaths, dep.Path.String())
1234 }
1235 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1236 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1237
1238 // C Whole static libs
Yu Liu727204c2025-01-23 20:58:32 +00001239 ccWholeStaticDeps := ctx.GetDirectDepsProxyWithTag(cc.StaticDepTag(true))
Wei Lia1aa2972024-06-21 13:08:51 -07001240 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1241 for _, dep := range ccStaticDeps {
1242 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1243 }
1244 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1245}
1246
Ivan Lozanoffee3342019-08-27 12:03:00 -07001247func (mod *Module) deps(ctx DepsContext) Deps {
1248 deps := Deps{}
1249
1250 if mod.compiler != nil {
1251 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001252 }
1253 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001254 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001255 }
1256
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001257 if mod.coverage != nil {
1258 deps = mod.coverage.deps(ctx, deps)
1259 }
1260
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001261 if mod.sanitize != nil {
1262 deps = mod.sanitize.deps(ctx, deps)
1263 }
1264
Ivan Lozanoffee3342019-08-27 12:03:00 -07001265 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1266 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001267 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001268 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1269 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1270 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001271 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001272 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001273 return deps
1274
1275}
1276
Ivan Lozanoffee3342019-08-27 12:03:00 -07001277type dependencyTag struct {
1278 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001279 name string
1280 library bool
1281 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001282 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283}
1284
Jiyong Park65b62242020-11-25 12:44:59 +09001285// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1286// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1287func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001288 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001289}
1290
1291var _ android.InstallNeededDependencyTag = dependencyTag{}
1292
Colin Cross65cb3142021-12-10 23:05:02 +00001293func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1294 if d.library && d.dynamic {
1295 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1296 }
1297 return nil
1298}
1299
Yu Liuc8884602024-03-15 18:48:38 +00001300func (d dependencyTag) PropagateAconfigValidation() bool {
1301 return d == rlibDepTag || d == sourceDepTag
1302}
1303
1304var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1305
Colin Cross65cb3142021-12-10 23:05:02 +00001306var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1307
Ivan Lozanoffee3342019-08-27 12:03:00 -07001308var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001309 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1310 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001311 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001312 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001313 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001314 dataLibDepTag = dependencyTag{name: "data lib"}
1315 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001316)
1317
Jiyong Park99644e92020-11-17 22:21:02 +09001318func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1319 tag, ok := depTag.(dependencyTag)
1320 return ok && tag == dylibDepTag
1321}
1322
Jiyong Park94e22fd2021-04-08 18:19:15 +09001323func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1324 tag, ok := depTag.(dependencyTag)
1325 return ok && tag == rlibDepTag
1326}
1327
Matthew Maurer0f003b12020-06-29 14:34:06 -07001328type autoDep struct {
1329 variation string
1330 depTag dependencyTag
1331}
1332
1333var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001334 sourceVariation = "source"
1335 rlibVariation = "rlib"
1336 dylibVariation = "dylib"
1337 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1338 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001339)
1340
1341type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001342 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001343}
1344
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001345func (mod *Module) begin(ctx BaseModuleContext) {
1346 if mod.coverage != nil {
1347 mod.coverage.begin(ctx)
1348 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001349 if mod.sanitize != nil {
1350 mod.sanitize.begin(ctx)
1351 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001352
1353 if mod.UseSdk() && mod.IsSdkVariant() {
1354 sdkVersion := ""
1355 if ctx.Device() {
1356 sdkVersion = mod.SdkVersion()
1357 }
1358 version, err := cc.NativeApiLevelFromUser(ctx, sdkVersion)
1359 if err != nil {
1360 ctx.PropertyErrorf("sdk_version", err.Error())
1361 mod.Properties.Sdk_version = nil
1362 } else {
1363 mod.Properties.Sdk_version = StringPtr(version.String())
1364 }
1365 }
1366
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001367}
1368
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001369func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001370 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001371 return p.prebuilt()
1372 }
1373 return nil
1374}
1375
Kiyoung Kim37693d02024-04-04 09:56:15 +09001376func (mod *Module) Symlinks() []string {
1377 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1378 return nil
1379}
1380
Yu Liu8024b922024-12-20 23:31:32 +00001381func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1382 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001383 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001384 if rustInfo.SnapshotInfo != nil {
1385 baseName := linkableInfo.BaseModuleName
1386 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001387 }
1388 }
Yu Liu8024b922024-12-20 23:31:32 +00001389 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001390}
1391
Yu Liu8024b922024-12-20 23:31:32 +00001392func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001393 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001394 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1395 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001396 }
1397 }
1398}
Andrew Walbran52533232024-03-19 11:36:04 +00001399
Ivan Lozanoffee3342019-08-27 12:03:00 -07001400func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1401 var depPaths PathDeps
1402
Yu Liu8024b922024-12-20 23:31:32 +00001403 directRlibDeps := []*cc.LinkableInfo{}
1404 directDylibDeps := []*cc.LinkableInfo{}
1405 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001406 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001407 directStaticLibDeps := [](*cc.LinkableInfo){}
1408 directSrcProvidersDeps := []*android.ModuleProxy{}
1409 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001410
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001411 // For the dependency from platform to apex, use the latest stubs
1412 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001413 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001414 if !apexInfo.IsForPlatform() {
1415 mod.apexSdkVersion = apexInfo.MinSdkVersion
1416 }
1417
1418 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1419 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1420 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1421 // (b/144430859)
1422 mod.apexSdkVersion = android.FutureApiLevel
1423 }
1424
Spandan Das604f3762023-03-16 22:51:40 +00001425 skipModuleList := map[string]bool{}
1426
Colin Crossa14fb6a2024-10-23 16:57:06 -07001427 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001428 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001429
Yu Liu8024b922024-12-20 23:31:32 +00001430 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001431 depName := ctx.OtherModuleName(dep)
1432 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001433 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1434
Spandan Das604f3762023-03-16 22:51:40 +00001435 if _, exists := skipModuleList[depName]; exists {
1436 return
1437 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001438
1439 if depTag == android.DarwinUniversalVariantTag {
1440 return
1441 }
1442
Yu Liu8024b922024-12-20 23:31:32 +00001443 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1444 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1445 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1446 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1447 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001448 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001449 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001450
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001451 switch {
1452 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001453 dylib := rustInfo.CompilerInfo.LibraryInfo
1454 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1456 return
1457 }
Yu Liu8024b922024-12-20 23:31:32 +00001458 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001459 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001460 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1461
Colin Crossb614cd42024-10-11 12:52:21 -07001462 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1463 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1464 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1465 }
1466
Yu Liu8024b922024-12-20 23:31:32 +00001467 if !rustInfo.CompilerInfo.NoStdlibs {
1468 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1469 if ctx.Device() {
1470 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1471 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001472 if rustDepStdLinkage != modStdLinkage {
1473 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1474 return
1475 }
1476 }
1477
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001478 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001479 rlib := rustInfo.CompilerInfo.LibraryInfo
1480 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001481 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001482 return
1483 }
Yu Liu8024b922024-12-20 23:31:32 +00001484 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001485 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001486 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1487
Ivan Lozano0a468a42024-05-13 21:03:34 -04001488 // rust_ffi rlibs may export include dirs, so collect those here.
1489 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1490 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001491 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001492
Colin Crossb614cd42024-10-11 12:52:21 -07001493 // rlibs are not installed, so don't add the output file to directImplementationDeps
1494 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1495 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1496 }
1497
Yu Liu8024b922024-12-20 23:31:32 +00001498 if !rustInfo.CompilerInfo.NoStdlibs {
1499 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1500 if ctx.Device() {
1501 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1502 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001503 if rustDepStdLinkage != modStdLinkage {
1504 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1505 return
1506 }
1507 }
1508
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001509 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001510 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001511 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001512 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001513 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001514
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001515 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001516 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001517 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001518 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001519 case cc.IsStaticDepTag(depTag):
1520 // Rust FFI rlibs should not be declared in a Rust modules
1521 // "static_libs" list as we can't handle them properly at the
1522 // moment (for example, they only produce an rlib-std variant).
1523 // Instead, a normal rust_library variant should be used.
1524 ctx.PropertyErrorf("static_libs",
1525 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1526 depName)
1527
Paul Duffind5cf92e2021-07-09 17:38:55 +01001528 }
1529
Yu Liu8024b922024-12-20 23:31:32 +00001530 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001531
Paul Duffind5cf92e2021-07-09 17:38:55 +01001532 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001533 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1534 // OS/Arch variant is used.
1535 var helper string
1536 if ctx.Host() {
1537 helper = "missing 'host_supported'?"
1538 } else {
1539 helper = "device module defined?"
1540 }
1541
Yu Liu8024b922024-12-20 23:31:32 +00001542 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001543 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1544 return
Yu Liu8024b922024-12-20 23:31:32 +00001545 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001546 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1547 return
1548 }
Yu Liu8024b922024-12-20 23:31:32 +00001549 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001550 }
1551
Ivan Lozano0a468a42024-05-13 21:03:34 -04001552 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001553 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001554 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001555 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1556 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001557 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001558 }
1559
Ivan Lozanoffee3342019-08-27 12:03:00 -07001560 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001561 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001562 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001563 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001564 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001565 }
1566 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001567
Ivan Lozanod106efe2023-09-21 23:30:26 -04001568 if depTag == sourceDepTag {
1569 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001570 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001571 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001572 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1573 }
1574 }
1575 }
Yu Liu8024b922024-12-20 23:31:32 +00001576 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001577 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001578 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1579 if !hasRustInfo {
1580 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001581 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1582 return
1583 }
Yu Liu8024b922024-12-20 23:31:32 +00001584 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001585 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1586 return
1587 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001588 }
Yu Liu8024b922024-12-20 23:31:32 +00001589 linkObject := linkableInfo.OutputFile
Ivan Lozano2093af22020-08-25 12:48:19 -04001590 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001591 if !ctx.Config().AllowMissingDependencies() {
1592 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1593 } else {
1594 ctx.AddMissingDependencies([]string{depName})
1595 }
1596 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001597 }
1598
Wen-yi Chu41326c12023-09-22 03:58:59 +00001599 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001600
Ivan Lozanoffee3342019-08-27 12:03:00 -07001601 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001602 switch {
1603 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001604 if cc.IsWholeStaticLib(depTag) {
1605 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1606 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001607 if mod.Binary() {
1608 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1609 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1610 // final linkage, pass the args directly to the linker to handle these cases.
1611 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1612 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001613 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001614 } else {
1615 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 -05001616 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001617 }
1618
1619 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1620 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001621 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001622 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1623
Colin Cross313aa542023-12-13 13:47:44 -08001624 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001625 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1626 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1627 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1628 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Yu Liu8024b922024-12-20 23:31:32 +00001629 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001630
1631 // Record baseLibName for snapshots.
1632 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1633
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001634 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001635 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001636 // For the shared lib dependencies, we may link to the stub variant
1637 // of the dependency depending on the context (e.g. if this
1638 // dependency crosses the APEX boundaries).
1639 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1640
Colin Crossb614cd42024-10-11 12:52:21 -07001641 if !sharedLibraryInfo.IsStubs {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001642 // TODO(b/362509506): remove this additional check once all apex_exclude uses are switched to stubs.
1643 if !linkableInfo.RustApexExclude {
1644 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1645 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1646 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1647 }
Colin Crossb614cd42024-10-11 12:52:21 -07001648 }
1649 }
1650
Jiyong Park7d55b612021-06-11 17:22:09 +09001651 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1652 // object (either from stub or non-stub) to use.
1653 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001654 if !linkObject.Valid() {
1655 if !ctx.Config().AllowMissingDependencies() {
1656 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1657 } else {
1658 ctx.AddMissingDependencies([]string{depName})
1659 }
1660 return
1661 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001662 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001663
Ivan Lozanoffee3342019-08-27 12:03:00 -07001664 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001665 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001666 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1667 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1668 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1669 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001670 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001671
1672 // Record baseLibName for snapshots.
1673 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1674
Cole Faustb6e6f992023-08-17 17:42:26 -07001675 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001676 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001677 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001678 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001679 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1680 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1681 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001682 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001683 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001684 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001685 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001686 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001687 }
1688
1689 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001690 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1691 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001692 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001693 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001694 } else {
1695 switch {
1696 case depTag == cc.CrtBeginDepTag:
1697 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1698 case depTag == cc.CrtEndDepTag:
1699 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1700 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001701 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001702
Yu Liuc41eae52025-01-14 01:03:08 +00001703 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001704 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001705 // These are usually genrules which don't have per-target variants.
1706 directSrcDeps = append(directSrcDeps, srcDep)
1707 }
1708 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001709 })
1710
Colin Crossa14fb6a2024-10-23 16:57:06 -07001711 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001712
1713 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001714 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001715 for _, dep := range directRlibDeps {
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 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001721 }
1722 var dylibDepFiles RustLibraries
1723 for _, dep := range directDylibDeps {
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 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001729 }
1730 var procMacroDepFiles RustLibraries
1731 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001732 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001733 if alias, aliased := aliases[crateName]; aliased {
1734 crateName = alias
1735 }
Yu Liu8024b922024-12-20 23:31:32 +00001736 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001737 }
1738
Colin Cross004bd3f2023-10-02 11:39:17 -07001739 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001740 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001741 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001742 }
1743
Colin Cross004bd3f2023-10-02 11:39:17 -07001744 var sharedLibFiles android.Paths
1745 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001746 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001747 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001748 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001749 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001750 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001751 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001752 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001753 }
1754
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001755 var srcProviderDepFiles android.Paths
1756 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001757 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001758 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1759 }
1760 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001761 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001762 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1763 }
1764
Wen-yi Chu41326c12023-09-22 03:58:59 +00001765 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1766 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001767 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1768 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1769 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001770 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001771 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001772
1773 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001774 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001775 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001776 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001777 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1778 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1779 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoad7ba592025-01-23 01:23:43 +00001780 depPaths.depLinkFlags = android.FirstUniqueStrings(depPaths.depLinkFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001781
1782 return depPaths
1783}
1784
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001785func (mod *Module) InstallInData() bool {
1786 if mod.compiler == nil {
1787 return false
1788 }
1789 return mod.compiler.inData()
1790}
1791
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001792func (mod *Module) InstallInRamdisk() bool {
1793 return mod.InRamdisk()
1794}
1795
1796func (mod *Module) InstallInVendorRamdisk() bool {
1797 return mod.InVendorRamdisk()
1798}
1799
1800func (mod *Module) InstallInRecovery() bool {
1801 return mod.InRecovery()
1802}
1803
Wen-yi Chu41326c12023-09-22 03:58:59 +00001804func linkPathFromFilePath(filepath android.Path) string {
1805 return strings.Split(filepath.String(), filepath.Base())[0]
1806}
1807
Spandan Das604f3762023-03-16 22:51:40 +00001808// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1809func (r *Module) usePublicApi() bool {
1810 return r.Device() && r.UseSdk()
1811}
1812
1813// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1814func (r *Module) useVendorApi() bool {
1815 return r.Device() && (r.InVendor() || r.InProduct())
1816}
1817
Ivan Lozanoffee3342019-08-27 12:03:00 -07001818func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1819 ctx := &depsContext{
1820 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001821 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001822
1823 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001824 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001825
1826 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001827 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001828 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001829
Ivan Lozano2b081132020-09-08 12:46:52 -04001830 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001831 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001832 stdLinkage = "rlib-std"
1833 }
1834
1835 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001836
Ivan Lozano2b081132020-09-08 12:46:52 -04001837 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1838 rlibDepVariations = append(rlibDepVariations,
1839 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1840 }
1841
Ivan Lozano1921e802021-05-20 13:39:16 -04001842 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001843 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001844 for _, lib := range deps.Rlibs {
1845 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001846 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001847 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001848
1849 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001850 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001851
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001852 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001853 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001854 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001855
Ivan Lozano1921e802021-05-20 13:39:16 -04001856 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001857 if deps.Rustlibs != nil {
1858 if !mod.compiler.Disabled() {
1859 for _, lib := range deps.Rustlibs {
1860 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1861 if autoDep.depTag == rlibDepTag {
1862 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001863 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1864
Ivan Lozanod106efe2023-09-21 23:30:26 -04001865 } else {
1866 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1867 // Check for the existence of the dylib deptag variant. Select it if available,
1868 // otherwise select the rlib variant.
1869 autoDepVariations := append(commonDepVariations,
1870 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001871 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1872 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001873
Ivan Lozanod106efe2023-09-21 23:30:26 -04001874 } else {
1875 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001876 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1877
Ivan Lozanod106efe2023-09-21 23:30:26 -04001878 }
1879 }
1880 }
1881 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1882 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001883 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001884 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001885
Ivan Lozano0a468a42024-05-13 21:03:34 -04001886 // Only add rustlib dependencies if they're source providers themselves.
1887 // This is used to track which crate names need to be added to the source generated
1888 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001889 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001890 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001891 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001892 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001893 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001894 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001895
Ivan Lozano1921e802021-05-20 13:39:16 -04001896 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001897 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001898 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001899 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001900 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001901 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001902 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001903 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001904 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001905 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1906
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001907 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001908 }
1909 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001910
1911 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001912 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001913 name, version := cc.StubsLibNameAndVersion(lib)
1914
1915 variations := []blueprint.Variation{
1916 {Mutator: "link", Variation: "shared"},
1917 }
Spandan Dasff665182024-09-11 18:48:44 +00001918 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001919 }
1920
1921 for _, lib := range deps.WholeStaticLibs {
1922 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001923
1924 actx.AddVariationDependencies([]blueprint.Variation{
1925 {Mutator: "link", Variation: "static"},
1926 }, depTag, lib)
1927 }
1928
1929 for _, lib := range deps.StaticLibs {
1930 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001931
1932 actx.AddVariationDependencies([]blueprint.Variation{
1933 {Mutator: "link", Variation: "static"},
1934 }, depTag, lib)
1935 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001936
Zach Johnson3df4e632020-11-06 11:56:27 -08001937 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1938
Colin Cross565cafd2020-09-25 18:47:38 -07001939 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001940 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001941 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001942 }
Colin Crossfe605e12022-01-23 20:46:16 -08001943 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001944 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001945 }
1946
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001947 if mod.sourceProvider != nil {
1948 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1949 bindgen.Properties.Custom_bindgen != "" {
1950 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1951 bindgen.Properties.Custom_bindgen)
1952 }
1953 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001954
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001955 actx.AddVariationDependencies([]blueprint.Variation{
1956 {Mutator: "link", Variation: "shared"},
1957 }, dataLibDepTag, deps.DataLibs...)
1958
1959 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1960
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001961 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001962 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001963
1964 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001965}
1966
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001967func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001968 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001969 mod.beginMutator(ctx)
1970 }
1971}
1972
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001973func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1974 ctx := &baseModuleContext{
1975 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001976 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001977
1978 mod.begin(ctx)
1979}
1980
Ivan Lozanoffee3342019-08-27 12:03:00 -07001981func (mod *Module) Name() string {
1982 name := mod.ModuleBase.Name()
1983 if p, ok := mod.compiler.(interface {
1984 Name(string) string
1985 }); ok {
1986 name = p.Name(name)
1987 }
1988 return name
1989}
1990
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001991func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001992 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001993 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001994 }
1995}
1996
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001997var _ android.HostToolProvider = (*Module)(nil)
1998
1999func (mod *Module) HostToolPath() android.OptionalPath {
2000 if !mod.Host() {
2001 return android.OptionalPath{}
2002 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07002003 if binary, ok := mod.compiler.(*binaryDecorator); ok {
2004 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04002005 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
2006 // Even though proc-macros aren't strictly "tools", since they target the compiler
2007 // and act as compiler plugins, we treat them similarly.
2008 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07002009 }
2010 return android.OptionalPath{}
2011}
2012
Jiyong Park99644e92020-11-17 22:21:02 +09002013var _ android.ApexModule = (*Module)(nil)
2014
Ivan Lozano24cf0362024-10-04 16:02:38 +00002015// If a module is marked for exclusion from apexes, don't provide apex variants.
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002016// TODO(b/362509506): remove this once all apex_exclude usages are removed.
Ivan Lozano24cf0362024-10-04 16:02:38 +00002017func (m *Module) CanHaveApexVariants() bool {
2018 if m.ApexExclude() {
2019 return false
2020 } else {
2021 return m.ApexModuleBase.CanHaveApexVariants()
2022 }
2023}
2024
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002025func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002026 return String(mod.Properties.Min_sdk_version)
2027}
2028
Jiyong Park45bf82e2020-12-15 22:29:02 +09002029// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09002030func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002031 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002032 if minSdkVersion == "apex_inherit" {
2033 return nil
2034 }
2035 if minSdkVersion == "" {
2036 return fmt.Errorf("min_sdk_version is not specificed")
2037 }
2038
2039 // Not using nativeApiLevelFromUser because the context here is not
2040 // necessarily a native context.
2041 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
2042 if err != nil {
2043 return err
2044 }
2045
2046 if ver.GreaterThan(sdkVersion) {
2047 return fmt.Errorf("newer SDK(%v)", ver)
2048 }
Jiyong Park99644e92020-11-17 22:21:02 +09002049 return nil
2050}
2051
Jiyong Park45bf82e2020-12-15 22:29:02 +09002052// Implements android.ApexModule
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002053func (mod *Module) AlwaysRequiresPlatformApexVariant() bool {
2054 // stub libraries and native bridge libraries are always available to platform
2055 // TODO(b/362509506): remove the ApexExclude() check once all apex_exclude uses are switched to stubs.
2056 return mod.IsStubs() || mod.Target().NativeBridge == android.NativeBridgeEnabled || mod.ApexExclude()
2057}
2058
2059// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002060func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07002061 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09002062 return false
2063 }
2064
Colin Cross8acea3e2024-12-12 14:53:30 -08002065 if mod.Static() && cc.IsSharedDepTag(depTag) {
2066 // shared_lib dependency from a static lib is considered as crossing
2067 // the APEX boundary because the dependency doesn't actually is
2068 // linked; the dependency is used only during the compilation phase.
2069 return false
2070 }
2071
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002072 if depTag == cc.StubImplDepTag {
2073 // We don't track from an implementation library to its stubs.
2074 return false
2075 }
2076
2077 if cc.ExcludeInApexDepTag(depTag) {
2078 return false
2079 }
2080
2081 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2082 if mod.ApexExclude() {
2083 return false
2084 }
2085
Jiyong Park99644e92020-11-17 22:21:02 +09002086 return true
2087}
2088
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002089func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002090 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2091 if mod.ApexExclude() {
2092 return false
2093 }
2094
2095 if mod.HasStubsVariants() {
2096 if cc.IsSharedDepTag(depTag) {
2097 // dynamic dep to a stubs lib crosses APEX boundary
2098 return false
2099 }
2100 if cc.IsRuntimeDepTag(depTag) {
2101 // runtime dep to a stubs lib also crosses APEX boundary
2102 return false
2103 }
2104 if cc.IsHeaderDepTag(depTag) {
2105 return false
2106 }
2107 }
2108 return true
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002109}
2110
Jiyong Park99644e92020-11-17 22:21:02 +09002111// Overrides ApexModule.IsInstallabeToApex()
2112func (mod *Module) IsInstallableToApex() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002113 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2114 if mod.ApexExclude() {
2115 return false
2116 }
2117
Jiyong Park99644e92020-11-17 22:21:02 +09002118 if mod.compiler != nil {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002119 if lib, ok := mod.compiler.(libraryInterface); ok {
2120 return (lib.shared() || lib.dylib()) && !lib.BuildStubs()
Jiyong Park99644e92020-11-17 22:21:02 +09002121 }
2122 if _, ok := mod.compiler.(*binaryDecorator); ok {
2123 return true
2124 }
2125 }
2126 return false
2127}
2128
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05002129// If a library file has a "lib" prefix, extract the library name without the prefix.
2130func libNameFromFilePath(filepath android.Path) (string, bool) {
2131 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
2132 if strings.HasPrefix(libName, "lib") {
2133 libName = libName[3:]
2134 return libName, true
2135 }
2136 return "", false
2137}
2138
Sasha Smundaka76acba2022-04-18 20:12:56 -07002139func kytheExtractRustFactory() android.Singleton {
2140 return &kytheExtractRustSingleton{}
2141}
2142
2143type kytheExtractRustSingleton struct {
2144}
2145
2146func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2147 var xrefTargets android.Paths
2148 ctx.VisitAllModules(func(module android.Module) {
2149 if rustModule, ok := module.(xref); ok {
2150 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
2151 }
2152 })
2153 if len(xrefTargets) > 0 {
2154 ctx.Phony("xref_rust", xrefTargets...)
2155 }
2156}
2157
Jihoon Kangf78a8902022-09-01 22:47:07 +00002158func (c *Module) Partition() string {
2159 return ""
2160}
2161
Ivan Lozanoffee3342019-08-27 12:03:00 -07002162var Bool = proptools.Bool
2163var BoolDefault = proptools.BoolDefault
2164var String = proptools.String
2165var StringPtr = proptools.StringPtr