blob: 246670ff34d8964fb57cb5c3e21f5068a341db63 [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
37func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070038 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070039 android.PreDepsMutators(registerPreDepsMutators)
40 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090041 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070042 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020043 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000044 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070045}
46
Colin Cross8a49a3d2024-05-20 12:22:27 -070047func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
48 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
49 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070050 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070051}
52
53func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070054 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070055}
56
Ivan Lozanoffee3342019-08-27 12:03:00 -070057type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040058 GlobalRustFlags []string // Flags that apply globally to rust
59 GlobalLinkFlags []string // Flags that apply globally to linker
60 RustFlags []string // Flags that apply to rust
61 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070063 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070064 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040065 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020066 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070067 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070068}
69
70type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050071 AndroidMkRlibs []string `blueprint:"mutated"`
72 AndroidMkDylibs []string `blueprint:"mutated"`
73 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050074 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +000075 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040076
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090077 ImageVariation string `blueprint:"mutated"`
78 VndkVersion string `blueprint:"mutated"`
79 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -050080
Ivan Lozanoc08897c2021-04-02 12:41:32 -040081 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
82 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
83 // appended before SubName.
84 RustSubName string `blueprint:"mutated"`
85
Ivan Lozano6a884432020-12-02 09:15:16 -050086 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +000087 ProductVariantNeeded bool `blueprint:"mutated"`
88 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050089 CoreVariantNeeded bool `blueprint:"mutated"`
90 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070091 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080092 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050093 ExtraVariants []string `blueprint:"mutated"`
94
Ivan Lozanoa2268632021-07-22 10:52:06 -040095 // Allows this module to use non-APEX version of libraries. Useful
96 // for building binaries that are started before APEXes are activated.
97 Bootstrap *bool
98
Ivan Lozano1921e802021-05-20 13:39:16 -040099 // Used by vendor snapshot to record dependencies from snapshot modules.
100 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900101 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400102 SnapshotRlibs []string `blueprint:"mutated"`
103 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400104
Matthew Maurerc6868382021-07-13 14:12:37 -0700105 // Make this module available when building for ramdisk.
106 // On device without a dedicated recovery partition, the module is only
107 // available after switching root into
108 // /first_stage_ramdisk. To expose the module before switching root, install
109 // the recovery variant instead.
110 Ramdisk_available *bool
111
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500112 // Make this module available when building for vendor ramdisk.
113 // On device without a dedicated recovery partition, the module is only
114 // available after switching root into
115 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800116 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500117 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400118
Ivan Lozano1921e802021-05-20 13:39:16 -0400119 // Normally Soong uses the directory structure to decide which modules
120 // should be included (framework) or excluded (non-framework) from the
121 // different snapshots (vendor, recovery, etc.), but this property
122 // allows a partner to exclude a module normally thought of as a
123 // framework module from the vendor snapshot.
124 Exclude_from_vendor_snapshot *bool
125
126 // Normally Soong uses the directory structure to decide which modules
127 // should be included (framework) or excluded (non-framework) from the
128 // different snapshots (vendor, recovery, etc.), but this property
129 // allows a partner to exclude a module normally thought of as a
130 // framework module from the recovery snapshot.
131 Exclude_from_recovery_snapshot *bool
132
Matthew Maurer460ee942021-02-11 12:31:46 -0800133 // Make this module available when building for recovery
134 Recovery_available *bool
135
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500136 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
137 Min_sdk_version *string
138
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900139 HideFromMake bool `blueprint:"mutated"`
140 PreventInstall bool `blueprint:"mutated"`
141
142 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143}
144
145type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700146 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147
Ivan Lozano6a884432020-12-02 09:15:16 -0500148 VendorProperties cc.VendorProperties
149
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 Properties BaseProperties
151
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000152 hod android.HostOrDeviceSupported
153 multilib android.Multilib
154 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155
Ivan Lozano6a884432020-12-02 09:15:16 -0500156 makeLinkType string
157
Yi Kong46c6e592022-01-20 22:55:00 +0800158 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400160 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200161 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500162 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700163 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400164 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700165 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400166
Ivan Lozano0a468a42024-05-13 21:03:34 -0400167 exportedLinkDirs []string
168
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400169 // Output file to be installed, may be stripped or unstripped.
170 outputFile android.OptionalPath
171
Sasha Smundaka76acba2022-04-18 20:12:56 -0700172 // Cross-reference input file
173 kytheFiles android.Paths
174
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400175 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900176
177 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500178
179 // For apex variants, this is set as apex.min_sdk_version
180 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700181
Colin Crossa14fb6a2024-10-23 16:57:06 -0700182 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700183}
184
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500185func (mod *Module) Header() bool {
186 //TODO: If Rust libraries provide header variants, this needs to be updated.
187 return false
188}
189
190func (mod *Module) SetPreventInstall() {
191 mod.Properties.PreventInstall = true
192}
193
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194func (mod *Module) SetHideFromMake() {
195 mod.Properties.HideFromMake = true
196}
197
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900198func (mod *Module) HiddenFromMake() bool {
199 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400200}
201
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500203 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
204 // nil since we need compiler to actually sanitize.
205 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500206}
207
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500208func (mod *Module) IsPrebuilt() bool {
209 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
210 return true
211 }
212 return false
213}
214
Ivan Lozano52767be2019-10-18 14:49:46 -0700215func (mod *Module) SelectedStl() string {
216 return ""
217}
218
Ivan Lozano2b262972019-11-21 12:30:50 -0800219func (mod *Module) NonCcVariants() bool {
220 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400221 if library, ok := mod.compiler.(libraryInterface); ok {
222 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800223 }
224 }
225 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
226}
227
Ivan Lozano52767be2019-10-18 14:49:46 -0700228func (mod *Module) Static() bool {
229 if mod.compiler != nil {
230 if library, ok := mod.compiler.(libraryInterface); ok {
231 return library.static()
232 }
233 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400234 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700235}
236
237func (mod *Module) Shared() bool {
238 if mod.compiler != nil {
239 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400240 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700241 }
242 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400243 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700244}
245
Ivan Lozanod7586b62021-04-01 09:49:36 -0400246func (mod *Module) Dylib() bool {
247 if mod.compiler != nil {
248 if library, ok := mod.compiler.(libraryInterface); ok {
249 return library.dylib()
250 }
251 }
252 return false
253}
254
Ivan Lozanod106efe2023-09-21 23:30:26 -0400255func (mod *Module) Source() bool {
256 if mod.compiler != nil {
257 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
258 return library.source()
259 }
260 }
261 return false
262}
263
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400264func (mod *Module) RlibStd() bool {
265 if mod.compiler != nil {
266 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
267 return library.rlibStd()
268 }
269 }
270 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
271}
272
Ivan Lozanod7586b62021-04-01 09:49:36 -0400273func (mod *Module) Rlib() bool {
274 if mod.compiler != nil {
275 if library, ok := mod.compiler.(libraryInterface); ok {
276 return library.rlib()
277 }
278 }
279 return false
280}
281
282func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400283 if binary, ok := mod.compiler.(binaryInterface); ok {
284 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400285 }
286 return false
287}
288
Justin Yun5e035862021-06-29 20:50:37 +0900289func (mod *Module) StaticExecutable() bool {
290 if !mod.Binary() {
291 return false
292 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400293 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900294}
295
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000296func (mod *Module) ApexExclude() bool {
297 if mod.compiler != nil {
298 if library, ok := mod.compiler.(libraryInterface); ok {
299 return library.apexExclude()
300 }
301 }
302 return false
303}
304
Ivan Lozanod7586b62021-04-01 09:49:36 -0400305func (mod *Module) Object() bool {
306 // Rust has no modules which produce only object files.
307 return false
308}
309
Ivan Lozano52767be2019-10-18 14:49:46 -0700310func (mod *Module) Toc() android.OptionalPath {
311 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400312 if lib, ok := mod.compiler.(libraryInterface); ok {
313 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700314 }
315 }
316 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
317}
318
Colin Crossc511bc52020-04-07 16:50:32 +0000319func (mod *Module) UseSdk() bool {
320 return false
321}
322
Ivan Lozanod7586b62021-04-01 09:49:36 -0400323func (mod *Module) RelativeInstallPath() string {
324 if mod.compiler != nil {
325 return mod.compiler.relativeInstallPath()
326 }
327 return ""
328}
329
Ivan Lozano52767be2019-10-18 14:49:46 -0700330func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500331 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700332}
333
Jiyong Park7d55b612021-06-11 17:22:09 +0900334func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400335 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900336}
337
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400338func (mod *Module) SubName() string {
339 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700340}
341
Ivan Lozanof1868af2022-04-12 13:08:36 -0400342func (mod *Module) IsVndkPrebuiltLibrary() bool {
343 // Rust modules do not provide VNDK prebuilts
344 return false
345}
346
347func (mod *Module) IsVendorPublicLibrary() bool {
348 return mod.VendorProperties.IsVendorPublicLibrary
349}
350
351func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
352 // Rust modules to not provide Sdk variants
353 return false
354}
355
Colin Cross127bb8b2020-12-16 16:46:01 -0800356func (c *Module) IsVndkPrivate() bool {
357 return false
358}
359
360func (c *Module) IsLlndk() bool {
361 return false
362}
363
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400364func (mod *Module) KernelHeadersDecorator() bool {
365 return false
366}
367
Colin Cross1f3f1302021-04-26 18:37:44 -0700368func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400369 return false
370}
371
Colin Cross5271fea2021-04-27 13:06:04 -0700372func (m *Module) NeedsVendorPublicLibraryVariants() bool {
373 return false
374}
375
Ivan Lozanod7586b62021-04-01 09:49:36 -0400376func (mod *Module) HasLlndkStubs() bool {
377 return false
378}
379
380func (mod *Module) StubsVersion() string {
381 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
382}
383
Ivan Lozano52767be2019-10-18 14:49:46 -0700384func (mod *Module) SdkVersion() string {
385 return ""
386}
387
Colin Crossc511bc52020-04-07 16:50:32 +0000388func (mod *Module) AlwaysSdk() bool {
389 return false
390}
391
Jiyong Park2286afd2020-06-16 21:58:53 +0900392func (mod *Module) IsSdkVariant() bool {
393 return false
394}
395
Colin Cross1348ce32020-10-01 13:37:16 -0700396func (mod *Module) SplitPerApiLevel() bool {
397 return false
398}
399
Sasha Smundaka76acba2022-04-18 20:12:56 -0700400func (mod *Module) XrefRustFiles() android.Paths {
401 return mod.kytheFiles
402}
403
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400405 Dylibs []string
406 Rlibs []string
407 Rustlibs []string
408 Stdlibs []string
409 ProcMacros []string
410 SharedLibs []string
411 StaticLibs []string
412 WholeStaticLibs []string
413 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400415 // Used for data dependencies adjacent to tests
416 DataLibs []string
417 DataBins []string
418
Colin Crossfe605e12022-01-23 20:46:16 -0800419 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420}
421
422type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700423 DyLibs RustLibraries
424 RLibs RustLibraries
425 SharedLibs android.Paths
426 SharedLibDeps android.Paths
427 StaticLibs android.Paths
428 ProcMacros RustLibraries
429 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000430 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500431
432 // depFlags and depLinkFlags are rustc and linker (clang) flags.
433 depFlags []string
434 depLinkFlags []string
435
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400436 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500437 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000438 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700439 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700440
Ivan Lozano0a468a42024-05-13 21:03:34 -0400441 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
442 // cc_library_static dependants of rlibs.
443 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
444 exportedLinkDirs []string
445
Ivan Lozano45901ed2020-07-24 16:05:01 -0400446 // Used by bindgen modules which call clang
447 depClangFlags []string
448 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400449 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400450 depSystemIncludePaths android.Paths
451
Colin Crossfe605e12022-01-23 20:46:16 -0800452 CrtBegin android.Paths
453 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700454
455 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500456 SrcDeps android.Paths
457 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700458
459 directImplementationDeps android.Paths
460 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700461}
462
463type RustLibraries []RustLibrary
464
465type RustLibrary struct {
466 Path android.Path
467 CrateName string
468}
469
Matthew Maurerbb3add12020-06-25 09:34:12 -0700470type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000471 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700472 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700473}
474
Sasha Smundaka76acba2022-04-18 20:12:56 -0700475type xref interface {
476 XrefRustFiles() android.Paths
477}
478
Matthew Maurerbb3add12020-06-25 09:34:12 -0700479type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000480 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400481 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700482 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700483}
484
Wen-yi Chu41326c12023-09-22 03:58:59 +0000485func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
486 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700487}
488
Colin Cross004bd3f2023-10-02 11:39:17 -0700489func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
490 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400491}
492
Colin Cross0de8a1e2020-09-18 14:15:30 -0700493func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800494 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700495 LinkDirs: flagExporter.linkDirs,
496 LinkObjects: flagExporter.linkObjects,
497 })
498}
499
Matthew Maurerbb3add12020-06-25 09:34:12 -0700500var _ exportedFlagsProducer = (*flagExporter)(nil)
501
502func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700503 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700504}
505
Colin Cross0de8a1e2020-09-18 14:15:30 -0700506type FlagExporterInfo struct {
507 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000508 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700509 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700510}
511
Colin Crossbc7d76c2023-12-12 16:39:03 -0800512var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700513
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400514func (mod *Module) isCoverageVariant() bool {
515 return mod.coverage.Properties.IsCoverageVariant
516}
517
518var _ cc.Coverage = (*Module)(nil)
519
Colin Crosse1a85552024-06-14 12:17:37 -0700520func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400521 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
522}
523
Ivan Lozanod7586b62021-04-01 09:49:36 -0400524func (mod *Module) VndkVersion() string {
525 return mod.Properties.VndkVersion
526}
527
Ivan Lozano0a468a42024-05-13 21:03:34 -0400528func (mod *Module) ExportedCrateLinkDirs() []string {
529 return mod.exportedLinkDirs
530}
531
Ivan Lozanod7586b62021-04-01 09:49:36 -0400532func (mod *Module) PreventInstall() bool {
533 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400534}
535
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400536func (mod *Module) MarkAsCoverageVariant(coverage bool) {
537 mod.coverage.Properties.IsCoverageVariant = coverage
538}
539
540func (mod *Module) EnableCoverageIfNeeded() {
541 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542}
543
544func defaultsFactory() android.Module {
545 return DefaultsFactory()
546}
547
548type Defaults struct {
549 android.ModuleBase
550 android.DefaultsModuleBase
551}
552
553func DefaultsFactory(props ...interface{}) android.Module {
554 module := &Defaults{}
555
556 module.AddProperties(props...)
557 module.AddProperties(
558 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800559 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500560 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100561 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400562 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563 &BaseCompilerProperties{},
564 &BinaryCompilerProperties{},
565 &LibraryCompilerProperties{},
566 &ProcMacroCompilerProperties{},
567 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400568 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700569 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400570 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400571 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200572 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500573 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000574 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700575 )
576
577 android.InitDefaultsModule(module)
578 return module
579}
580
581func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700582 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700583}
584
Ivan Lozano183a3212019-10-18 14:18:45 -0700585func (mod *Module) CcLibrary() bool {
586 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500587 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700588 return true
589 }
590 }
591 return false
592}
593
594func (mod *Module) CcLibraryInterface() bool {
595 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400596 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
597 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000598 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700599 return true
600 }
601 }
602 return false
603}
604
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400605func (mod *Module) RustLibraryInterface() bool {
606 if mod.compiler != nil {
607 if _, ok := mod.compiler.(libraryInterface); ok {
608 return true
609 }
610 }
611 return false
612}
613
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500614func (mod *Module) IsFuzzModule() bool {
615 if _, ok := mod.compiler.(*fuzzDecorator); ok {
616 return true
617 }
618 return false
619}
620
621func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
622 return mod.FuzzModule
623}
624
625func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
626 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
627 return fuzzer.fuzzPackagedModule
628 }
629 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
630}
631
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000632func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500633 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
634 return fuzzer.sharedLibraries
635 }
636 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
637}
638
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400639func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400640 if mod.compiler != nil {
641 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400642 }
643 return nil
644}
645
Ivan Lozano183a3212019-10-18 14:18:45 -0700646func (mod *Module) SetStatic() {
647 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700648 if library, ok := mod.compiler.(libraryInterface); ok {
649 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700650 return
651 }
652 }
653 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
654}
655
656func (mod *Module) SetShared() {
657 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700658 if library, ok := mod.compiler.(libraryInterface); ok {
659 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700660 return
661 }
662 }
663 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
664}
665
Ivan Lozano183a3212019-10-18 14:18:45 -0700666func (mod *Module) BuildStaticVariant() bool {
667 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700668 if library, ok := mod.compiler.(libraryInterface); ok {
669 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700670 }
671 }
672 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
673}
674
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400675func (mod *Module) BuildRlibVariant() bool {
676 if mod.compiler != nil {
677 if library, ok := mod.compiler.(libraryInterface); ok {
678 return library.buildRlib()
679 }
680 }
681 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
682}
683
Ivan Lozano183a3212019-10-18 14:18:45 -0700684func (mod *Module) BuildSharedVariant() bool {
685 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700686 if library, ok := mod.compiler.(libraryInterface); ok {
687 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700688 }
689 }
690 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
691}
692
Ivan Lozano183a3212019-10-18 14:18:45 -0700693func (mod *Module) Module() android.Module {
694 return mod
695}
696
Ivan Lozano183a3212019-10-18 14:18:45 -0700697func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400698 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700699}
700
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400701func (mod *Module) CoverageFiles() android.Paths {
702 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800703 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400704 }
705 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
706}
707
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400708// Rust does not produce gcno files, and therefore does not produce a coverage archive.
709func (mod *Module) CoverageOutputFile() android.OptionalPath {
710 return android.OptionalPath{}
711}
712
713func (mod *Module) IsNdk(config android.Config) bool {
714 return false
715}
716
Spandan Das10c41362024-12-03 01:33:09 +0000717func (mod *Module) HasStubsVariants() bool {
718 return false
719}
720
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400721func (mod *Module) IsStubs() bool {
722 return false
723}
724
Jiyong Park459feca2020-12-15 11:02:21 +0900725func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900726 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900727 return false
728 }
729
Jiyong Park459feca2020-12-15 11:02:21 +0900730 // The apex variant is not installable because it is included in the APEX and won't appear
731 // in the system partition as a standalone file.
732 if !apexInfo.IsForPlatform() {
733 return false
734 }
735
Jiyong Parke54f07e2021-04-07 15:08:04 +0900736 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900737}
738
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500739func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800740 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
741 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500742}
743
Ivan Lozano183a3212019-10-18 14:18:45 -0700744var _ cc.LinkableInterface = (*Module)(nil)
745
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746func (mod *Module) Init() android.Module {
747 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500748 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749
Yi Kong46c6e592022-01-20 22:55:00 +0800750 if mod.afdo != nil {
751 mod.AddProperties(mod.afdo.props()...)
752 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753 if mod.compiler != nil {
754 mod.AddProperties(mod.compiler.compilerProps()...)
755 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400756 if mod.coverage != nil {
757 mod.AddProperties(mod.coverage.props()...)
758 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200759 if mod.clippy != nil {
760 mod.AddProperties(mod.clippy.props()...)
761 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400762 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700763 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400764 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500765 if mod.sanitize != nil {
766 mod.AddProperties(mod.sanitize.props()...)
767 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400768
Ivan Lozanoffee3342019-08-27 12:03:00 -0700769 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900770 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700771
772 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773 return mod
774}
775
776func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
777 return &Module{
778 hod: hod,
779 multilib: multilib,
780 }
781}
782func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
783 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800784 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400785 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200786 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500787 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 return module
789}
790
791type ModuleContext interface {
792 android.ModuleContext
793 ModuleContextIntf
794}
795
796type BaseModuleContext interface {
797 android.BaseModuleContext
798 ModuleContextIntf
799}
800
801type DepsContext interface {
802 android.BottomUpMutatorContext
803 ModuleContextIntf
804}
805
806type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200807 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809}
810
811type depsContext struct {
812 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813}
814
815type moduleContext struct {
816 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817}
818
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200819type baseModuleContext struct {
820 android.BaseModuleContext
821}
822
823func (ctx *moduleContext) RustModule() *Module {
824 return ctx.Module().(*Module)
825}
826
827func (ctx *moduleContext) toolchain() config.Toolchain {
828 return ctx.RustModule().toolchain(ctx)
829}
830
831func (ctx *depsContext) RustModule() *Module {
832 return ctx.Module().(*Module)
833}
834
835func (ctx *depsContext) toolchain() config.Toolchain {
836 return ctx.RustModule().toolchain(ctx)
837}
838
839func (ctx *baseModuleContext) RustModule() *Module {
840 return ctx.Module().(*Module)
841}
842
843func (ctx *baseModuleContext) toolchain() config.Toolchain {
844 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400845}
846
847func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700848 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
849 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
850 return false
851 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400852 return mod.compiler != nil && mod.compiler.nativeCoverage()
853}
854
Ivan Lozanod7586b62021-04-01 09:49:36 -0400855func (mod *Module) EverInstallable() bool {
856 return mod.compiler != nil &&
857 // Check to see whether the module is actually ever installable.
858 mod.compiler.everInstallable()
859}
860
861func (mod *Module) Installable() *bool {
862 return mod.Properties.Installable
863}
864
Ivan Lozano872d5792022-03-23 17:31:39 -0400865func (mod *Module) ProcMacro() bool {
866 if pm, ok := mod.compiler.(procMacroInterface); ok {
867 return pm.ProcMacro()
868 }
869 return false
870}
871
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
873 if mod.cachedToolchain == nil {
874 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
875 }
876 return mod.cachedToolchain
877}
878
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200879func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
880 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
881}
882
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
884}
885
886func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
887 ctx := &moduleContext{
888 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890
Colin Crossff694a82023-12-13 15:54:49 -0800891 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900892 if !apexInfo.IsForPlatform() {
893 mod.hideApexVariantFromMake = true
894 }
895
Ivan Lozanoffee3342019-08-27 12:03:00 -0700896 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500897 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
898
Ivan Lozanof1868af2022-04-12 13:08:36 -0400899 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700900
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901 if !toolchain.Supported() {
902 // This toolchain's unsupported, there's nothing to do for this mod.
903 return
904 }
905
906 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400907 // Export linkDirs for CC rust generatedlibs
908 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
909 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 flags := Flags{
912 Toolchain: toolchain,
913 }
914
Ivan Lozano67eada32021-09-23 11:50:33 -0400915 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800916 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500917 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800918 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919 if mod.compiler != nil {
920 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400921 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +0000922 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400923 }
924 if mod.coverage != nil {
925 flags, deps = mod.coverage.flags(ctx, flags, deps)
926 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200927 if mod.clippy != nil {
928 flags, deps = mod.clippy.flags(ctx, flags, deps)
929 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500930 if mod.sanitize != nil {
931 flags, deps = mod.sanitize.flags(ctx, flags, deps)
932 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400933
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200934 // SourceProvider needs to call GenerateSource() before compiler calls
935 // compile() so it can provide the source. A SourceProvider has
936 // multiple variants (e.g. source, rlib, dylib). Only the "source"
937 // variant is responsible for effectively generating the source. The
938 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400939 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200940 if mod.compiler.(libraryInterface).source() {
941 mod.sourceProvider.GenerateSource(ctx, deps)
942 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
943 } else {
944 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
945 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700946 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200947 }
Colin Crossa6182ab2024-08-21 10:47:44 -0700948 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400949 }
950
951 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100952 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700953 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400954 if ctx.Failed() {
955 return
956 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700957 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -0700958 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700959 if buildOutput.kytheFile != nil {
960 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
961 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400962 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900963
Dan Albert06feee92021-03-19 15:06:02 -0700964 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
965
Colin Crossff694a82023-12-13 15:54:49 -0800966 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400967 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900968 // If the module has been specifically configure to not be installed then
969 // hide from make as otherwise it will break when running inside make as the
970 // output path to install will not be specified. Not all uninstallable
971 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400972 // side dependencies. In particular, proc-macros need to be captured in the
973 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900974 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +0000975 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900976 } else if !mod.installable(apexInfo) {
977 mod.SkipInstall()
978 }
979
980 // Still call install though, the installs will be stored as PackageSpecs to allow
981 // using the outputs in a genrule.
982 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200983 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900984 if ctx.Failed() {
985 return
986 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400987 // Export your own directory as a linkDir
988 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
989
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400990 }
Chris Wailes74be7642021-07-22 16:20:28 -0700991
Colin Crossb614cd42024-10-11 12:52:21 -0700992 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
993 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
994 })
995
Chris Wailes74be7642021-07-22 16:20:28 -0700996 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700997 }
Wei Lia1aa2972024-06-21 13:08:51 -0700998
Yu Liu986d98c2024-11-12 00:28:11 +0000999 android.SetProvider(ctx, cc.LinkableInfoKey, cc.LinkableInfo{
1000 StaticExecutable: mod.StaticExecutable(),
1001 })
1002
mrziwang0cbd3b02024-06-20 16:39:25 -07001003 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001004
1005 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001006}
1007
1008func (mod *Module) setOutputFiles(ctx ModuleContext) {
1009 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1010 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1011 } else if mod.OutputFile().Valid() {
1012 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1013 } else {
1014 ctx.SetOutputFiles(android.Paths{}, "")
1015 }
1016 if mod.compiler != nil {
1017 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1018 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019}
1020
Wei Lia1aa2972024-06-21 13:08:51 -07001021func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1022 // Dump metadata that can not be done in android/compliance-metadata.go
1023 metadataInfo := ctx.ComplianceMetadataInfo()
1024 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1025 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1026
1027 // Static libs
1028 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1029 staticDepNames := make([]string, 0, len(staticDeps))
1030 for _, dep := range staticDeps {
1031 staticDepNames = append(staticDepNames, dep.Name())
1032 }
1033 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1034 for _, dep := range ccStaticDeps {
1035 staticDepNames = append(staticDepNames, dep.Name())
1036 }
1037
1038 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1039 // C static libraries
1040 for _, dep := range deps.StaticLibs {
1041 staticDepPaths = append(staticDepPaths, dep.String())
1042 }
1043 // Rust static libraries
1044 for _, dep := range deps.RLibs {
1045 staticDepPaths = append(staticDepPaths, dep.Path.String())
1046 }
1047 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1048 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1049
1050 // C Whole static libs
1051 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1052 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1053 for _, dep := range ccStaticDeps {
1054 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1055 }
1056 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1057}
1058
Ivan Lozanoffee3342019-08-27 12:03:00 -07001059func (mod *Module) deps(ctx DepsContext) Deps {
1060 deps := Deps{}
1061
1062 if mod.compiler != nil {
1063 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001064 }
1065 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001066 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001067 }
1068
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001069 if mod.coverage != nil {
1070 deps = mod.coverage.deps(ctx, deps)
1071 }
1072
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001073 if mod.sanitize != nil {
1074 deps = mod.sanitize.deps(ctx, deps)
1075 }
1076
Ivan Lozanoffee3342019-08-27 12:03:00 -07001077 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1078 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001079 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001080 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1081 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1082 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001083 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001084 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001085 return deps
1086
1087}
1088
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089type dependencyTag struct {
1090 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001091 name string
1092 library bool
1093 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001094 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001095}
1096
Jiyong Park65b62242020-11-25 12:44:59 +09001097// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1098// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1099func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001100 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001101}
1102
1103var _ android.InstallNeededDependencyTag = dependencyTag{}
1104
Colin Cross65cb3142021-12-10 23:05:02 +00001105func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1106 if d.library && d.dynamic {
1107 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1108 }
1109 return nil
1110}
1111
Yu Liuc8884602024-03-15 18:48:38 +00001112func (d dependencyTag) PropagateAconfigValidation() bool {
1113 return d == rlibDepTag || d == sourceDepTag
1114}
1115
1116var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1117
Colin Cross65cb3142021-12-10 23:05:02 +00001118var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1119
Ivan Lozanoffee3342019-08-27 12:03:00 -07001120var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001121 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1122 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001123 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001124 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001125 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001126 dataLibDepTag = dependencyTag{name: "data lib"}
1127 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001128)
1129
Jiyong Park99644e92020-11-17 22:21:02 +09001130func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1131 tag, ok := depTag.(dependencyTag)
1132 return ok && tag == dylibDepTag
1133}
1134
Jiyong Park94e22fd2021-04-08 18:19:15 +09001135func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1136 tag, ok := depTag.(dependencyTag)
1137 return ok && tag == rlibDepTag
1138}
1139
Matthew Maurer0f003b12020-06-29 14:34:06 -07001140type autoDep struct {
1141 variation string
1142 depTag dependencyTag
1143}
1144
1145var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001146 sourceVariation = "source"
1147 rlibVariation = "rlib"
1148 dylibVariation = "dylib"
1149 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1150 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001151)
1152
1153type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001154 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001155}
1156
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001157func (mod *Module) begin(ctx BaseModuleContext) {
1158 if mod.coverage != nil {
1159 mod.coverage.begin(ctx)
1160 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001161 if mod.sanitize != nil {
1162 mod.sanitize.begin(ctx)
1163 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001164}
1165
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001166func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001167 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001168 return p.prebuilt()
1169 }
1170 return nil
1171}
1172
Kiyoung Kim37693d02024-04-04 09:56:15 +09001173func (mod *Module) Symlinks() []string {
1174 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1175 return nil
1176}
1177
Justin Yun24b246a2023-03-16 10:36:16 +09001178func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1179 if rustDep, ok := dep.(*Module); ok {
1180 // Use base module name for snapshots when exporting to Makefile.
1181 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1182 baseName := rustDep.BaseModuleName()
1183 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1184 }
1185 }
1186 return cc.MakeLibName(ctx, c, dep, depName)
1187}
1188
Ivan Lozanod106efe2023-09-21 23:30:26 -04001189func collectIncludedProtos(mod *Module, dep *Module) {
1190 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1191 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1192 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1193 }
1194 }
1195}
Andrew Walbran52533232024-03-19 11:36:04 +00001196
Ivan Lozanoffee3342019-08-27 12:03:00 -07001197func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1198 var depPaths PathDeps
1199
1200 directRlibDeps := []*Module{}
1201 directDylibDeps := []*Module{}
1202 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001203 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001204 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001205 directSrcProvidersDeps := []*Module{}
1206 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001208 // For the dependency from platform to apex, use the latest stubs
1209 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001210 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001211 if !apexInfo.IsForPlatform() {
1212 mod.apexSdkVersion = apexInfo.MinSdkVersion
1213 }
1214
1215 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1216 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1217 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1218 // (b/144430859)
1219 mod.apexSdkVersion = android.FutureApiLevel
1220 }
1221
Spandan Das604f3762023-03-16 22:51:40 +00001222 skipModuleList := map[string]bool{}
1223
Colin Crossa14fb6a2024-10-23 16:57:06 -07001224 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001225 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001226
Ivan Lozanoffee3342019-08-27 12:03:00 -07001227 ctx.VisitDirectDeps(func(dep android.Module) {
1228 depName := ctx.OtherModuleName(dep)
1229 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001230 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1231
Spandan Das604f3762023-03-16 22:51:40 +00001232 if _, exists := skipModuleList[depName]; exists {
1233 return
1234 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001235
1236 if depTag == android.DarwinUniversalVariantTag {
1237 return
1238 }
1239
Ivan Lozano0a468a42024-05-13 21:03:34 -04001240 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001241 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001242 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001243
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001244 switch {
1245 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 dylib, ok := rustDep.compiler.(libraryInterface)
1247 if !ok || !dylib.dylib() {
1248 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1249 return
1250 }
1251 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001252 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001253 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1254
Colin Crossb614cd42024-10-11 12:52:21 -07001255 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1256 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1257 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1258 }
1259
Ivan Lozano806efd32024-12-11 21:38:53 +00001260 if !rustDep.compiler.noStdlibs() {
1261 rustDepStdLinkage := rustDep.compiler.stdLinkage(ctx.Device())
1262 if rustDepStdLinkage != modStdLinkage {
1263 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1264 return
1265 }
1266 }
1267
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001268 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269 rlib, ok := rustDep.compiler.(libraryInterface)
1270 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001271 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001272 return
1273 }
1274 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001275 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001276 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1277
Ivan Lozano0a468a42024-05-13 21:03:34 -04001278 // rust_ffi rlibs may export include dirs, so collect those here.
1279 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1280 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1281 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1282
Colin Crossb614cd42024-10-11 12:52:21 -07001283 // rlibs are not installed, so don't add the output file to directImplementationDeps
1284 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1285 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1286 }
1287
Ivan Lozano806efd32024-12-11 21:38:53 +00001288 if !rustDep.compiler.noStdlibs() {
1289 rustDepStdLinkage := rustDep.compiler.stdLinkage(ctx.Device())
1290 if rustDepStdLinkage != modStdLinkage {
1291 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1292 return
1293 }
1294 }
1295
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001296 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001297 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001298 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001299 // proc_macro link dirs need to be exported, so collect those here.
1300 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001301
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001302 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001303 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1304 collectIncludedProtos(mod, rustDep)
1305 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001306 case cc.IsStaticDepTag(depTag):
1307 // Rust FFI rlibs should not be declared in a Rust modules
1308 // "static_libs" list as we can't handle them properly at the
1309 // moment (for example, they only produce an rlib-std variant).
1310 // Instead, a normal rust_library variant should be used.
1311 ctx.PropertyErrorf("static_libs",
1312 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1313 depName)
1314
Paul Duffind5cf92e2021-07-09 17:38:55 +01001315 }
1316
Cole Faustb6e6f992023-08-17 17:42:26 -07001317 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1318
Paul Duffind5cf92e2021-07-09 17:38:55 +01001319 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001320 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1321 // OS/Arch variant is used.
1322 var helper string
1323 if ctx.Host() {
1324 helper = "missing 'host_supported'?"
1325 } else {
1326 helper = "device module defined?"
1327 }
1328
1329 if dep.Target().Os != ctx.Os() {
1330 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1331 return
1332 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1333 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1334 return
1335 }
1336 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001337 }
1338
Ivan Lozano0a468a42024-05-13 21:03:34 -04001339 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001340 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001341 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001342 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1343 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001344 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001345 }
1346
Ivan Lozanoffee3342019-08-27 12:03:00 -07001347 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001348 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001349 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001350 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001351 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001352 }
1353 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001354
Ivan Lozanod106efe2023-09-21 23:30:26 -04001355 if depTag == sourceDepTag {
1356 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1357 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001358 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001359 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1360 }
1361 }
1362 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001363 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001364 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001365 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001366 if _, ok := ccDep.(*Module); !ok {
1367 if ccDep.Module().Target().Os != ctx.Os() {
1368 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1369 return
1370 }
1371 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1372 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1373 return
1374 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001375 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001376 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001377 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001378 if !ctx.Config().AllowMissingDependencies() {
1379 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1380 } else {
1381 ctx.AddMissingDependencies([]string{depName})
1382 }
1383 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001384 }
1385
Wen-yi Chu41326c12023-09-22 03:58:59 +00001386 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001387
Ivan Lozanoffee3342019-08-27 12:03:00 -07001388 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001389 switch {
1390 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001391 if cc.IsWholeStaticLib(depTag) {
1392 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1393 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001394 if mod.Binary() {
1395 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1396 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1397 // final linkage, pass the args directly to the linker to handle these cases.
1398 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1399 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001400 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001401 } else {
1402 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 -05001403 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001404 }
1405
1406 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1407 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001408 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001409 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1410
Colin Cross313aa542023-12-13 13:47:44 -08001411 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001412 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1413 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1414 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1415 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001416 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001417
1418 // Record baseLibName for snapshots.
1419 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1420
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001421 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001422 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001423 // For the shared lib dependencies, we may link to the stub variant
1424 // of the dependency depending on the context (e.g. if this
1425 // dependency crosses the APEX boundaries).
1426 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1427
Colin Crossb614cd42024-10-11 12:52:21 -07001428 if !sharedLibraryInfo.IsStubs {
1429 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1430 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1431 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1432 }
1433 }
1434
Jiyong Park7d55b612021-06-11 17:22:09 +09001435 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1436 // object (either from stub or non-stub) to use.
1437 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001438 if !linkObject.Valid() {
1439 if !ctx.Config().AllowMissingDependencies() {
1440 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1441 } else {
1442 ctx.AddMissingDependencies([]string{depName})
1443 }
1444 return
1445 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001446 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001447
Ivan Lozanoffee3342019-08-27 12:03:00 -07001448 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001449 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001450 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1451 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1452 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1453 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001454 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001455
1456 // Record baseLibName for snapshots.
1457 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1458
Cole Faustb6e6f992023-08-17 17:42:26 -07001459 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001460 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001461 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001462 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001463 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1464 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1465 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001466 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001467 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001468 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001469 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001470 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001471 }
1472
1473 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001474 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1475 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001476 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001477 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001478 } else {
1479 switch {
1480 case depTag == cc.CrtBeginDepTag:
1481 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1482 case depTag == cc.CrtEndDepTag:
1483 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1484 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001485 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001486
1487 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001488 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001489 // These are usually genrules which don't have per-target variants.
1490 directSrcDeps = append(directSrcDeps, srcDep)
1491 }
1492 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001493 })
1494
Colin Crossa14fb6a2024-10-23 16:57:06 -07001495 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001496
1497 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001498 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001499 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001500 crateName := dep.CrateName()
1501 if alias, aliased := aliases[crateName]; aliased {
1502 crateName = alias
1503 }
1504 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001505 }
1506 var dylibDepFiles RustLibraries
1507 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001508 crateName := dep.CrateName()
1509 if alias, aliased := aliases[crateName]; aliased {
1510 crateName = alias
1511 }
1512 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001513 }
1514 var procMacroDepFiles RustLibraries
1515 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001516 crateName := dep.CrateName()
1517 if alias, aliased := aliases[crateName]; aliased {
1518 crateName = alias
1519 }
1520 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001521 }
1522
Colin Cross004bd3f2023-10-02 11:39:17 -07001523 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001524 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001525 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001526 }
1527
Colin Cross004bd3f2023-10-02 11:39:17 -07001528 var sharedLibFiles android.Paths
1529 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001530 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001531 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001532 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001533 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001534 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001535 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001536 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001537 }
1538
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001539 var srcProviderDepFiles android.Paths
1540 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001541 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001542 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1543 }
1544 for _, dep := range directSrcDeps {
1545 srcs := dep.Srcs()
1546 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1547 }
1548
Wen-yi Chu41326c12023-09-22 03:58:59 +00001549 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1550 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001551 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1552 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1553 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001554 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001555 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001556
1557 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001558 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001559 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001560 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001561 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1562 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1563 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001564
1565 return depPaths
1566}
1567
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001568func (mod *Module) InstallInData() bool {
1569 if mod.compiler == nil {
1570 return false
1571 }
1572 return mod.compiler.inData()
1573}
1574
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001575func (mod *Module) InstallInRamdisk() bool {
1576 return mod.InRamdisk()
1577}
1578
1579func (mod *Module) InstallInVendorRamdisk() bool {
1580 return mod.InVendorRamdisk()
1581}
1582
1583func (mod *Module) InstallInRecovery() bool {
1584 return mod.InRecovery()
1585}
1586
Wen-yi Chu41326c12023-09-22 03:58:59 +00001587func linkPathFromFilePath(filepath android.Path) string {
1588 return strings.Split(filepath.String(), filepath.Base())[0]
1589}
1590
Spandan Das604f3762023-03-16 22:51:40 +00001591// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1592func (r *Module) usePublicApi() bool {
1593 return r.Device() && r.UseSdk()
1594}
1595
1596// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1597func (r *Module) useVendorApi() bool {
1598 return r.Device() && (r.InVendor() || r.InProduct())
1599}
1600
Ivan Lozanoffee3342019-08-27 12:03:00 -07001601func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1602 ctx := &depsContext{
1603 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001604 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001605
1606 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001607 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001608
1609 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001610 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001611 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001612
Ivan Lozano2b081132020-09-08 12:46:52 -04001613 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001614 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001615 stdLinkage = "rlib-std"
1616 }
1617
1618 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001619
Ivan Lozano2b081132020-09-08 12:46:52 -04001620 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1621 rlibDepVariations = append(rlibDepVariations,
1622 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1623 }
1624
Ivan Lozano1921e802021-05-20 13:39:16 -04001625 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001626 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001627 for _, lib := range deps.Rlibs {
1628 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001629 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001630 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001631
1632 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001633 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001634
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001635 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001636 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001637 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001638
Ivan Lozano1921e802021-05-20 13:39:16 -04001639 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001640 if deps.Rustlibs != nil {
1641 if !mod.compiler.Disabled() {
1642 for _, lib := range deps.Rustlibs {
1643 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1644 if autoDep.depTag == rlibDepTag {
1645 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001646 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1647
Ivan Lozanod106efe2023-09-21 23:30:26 -04001648 } else {
1649 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1650 // Check for the existence of the dylib deptag variant. Select it if available,
1651 // otherwise select the rlib variant.
1652 autoDepVariations := append(commonDepVariations,
1653 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001654 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1655 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001656
Ivan Lozanod106efe2023-09-21 23:30:26 -04001657 } else {
1658 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001659 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1660
Ivan Lozanod106efe2023-09-21 23:30:26 -04001661 }
1662 }
1663 }
1664 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1665 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001666 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001667 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001668
Ivan Lozano0a468a42024-05-13 21:03:34 -04001669 // Only add rustlib dependencies if they're source providers themselves.
1670 // This is used to track which crate names need to be added to the source generated
1671 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001672 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001673 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001674 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001675 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001676 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001677 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001678
Ivan Lozano1921e802021-05-20 13:39:16 -04001679 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001680 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001681 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001682 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001683 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001684 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001685 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001686 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001687 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001688 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1689
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001690 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001691 }
1692 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001693
1694 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001695 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001696 name, version := cc.StubsLibNameAndVersion(lib)
1697
1698 variations := []blueprint.Variation{
1699 {Mutator: "link", Variation: "shared"},
1700 }
Spandan Dasff665182024-09-11 18:48:44 +00001701 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001702 }
1703
1704 for _, lib := range deps.WholeStaticLibs {
1705 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001706
1707 actx.AddVariationDependencies([]blueprint.Variation{
1708 {Mutator: "link", Variation: "static"},
1709 }, depTag, lib)
1710 }
1711
1712 for _, lib := range deps.StaticLibs {
1713 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001714
1715 actx.AddVariationDependencies([]blueprint.Variation{
1716 {Mutator: "link", Variation: "static"},
1717 }, depTag, lib)
1718 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001719
Zach Johnson3df4e632020-11-06 11:56:27 -08001720 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1721
Colin Cross565cafd2020-09-25 18:47:38 -07001722 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001723 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001724 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001725 }
Colin Crossfe605e12022-01-23 20:46:16 -08001726 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001727 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001728 }
1729
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001730 if mod.sourceProvider != nil {
1731 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1732 bindgen.Properties.Custom_bindgen != "" {
1733 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1734 bindgen.Properties.Custom_bindgen)
1735 }
1736 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001737
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001738 actx.AddVariationDependencies([]blueprint.Variation{
1739 {Mutator: "link", Variation: "shared"},
1740 }, dataLibDepTag, deps.DataLibs...)
1741
1742 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1743
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001744 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001745 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001746
1747 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001748}
1749
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001750func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001751 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001752 mod.beginMutator(ctx)
1753 }
1754}
1755
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001756func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1757 ctx := &baseModuleContext{
1758 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001759 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001760
1761 mod.begin(ctx)
1762}
1763
Ivan Lozanoffee3342019-08-27 12:03:00 -07001764func (mod *Module) Name() string {
1765 name := mod.ModuleBase.Name()
1766 if p, ok := mod.compiler.(interface {
1767 Name(string) string
1768 }); ok {
1769 name = p.Name(name)
1770 }
1771 return name
1772}
1773
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001774func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001775 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001776 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001777 }
1778}
1779
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001780var _ android.HostToolProvider = (*Module)(nil)
1781
1782func (mod *Module) HostToolPath() android.OptionalPath {
1783 if !mod.Host() {
1784 return android.OptionalPath{}
1785 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001786 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1787 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001788 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1789 // Even though proc-macros aren't strictly "tools", since they target the compiler
1790 // and act as compiler plugins, we treat them similarly.
1791 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001792 }
1793 return android.OptionalPath{}
1794}
1795
Jiyong Park99644e92020-11-17 22:21:02 +09001796var _ android.ApexModule = (*Module)(nil)
1797
Ivan Lozano24cf0362024-10-04 16:02:38 +00001798// If a module is marked for exclusion from apexes, don't provide apex variants.
1799// TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
1800func (m *Module) CanHaveApexVariants() bool {
1801 if m.ApexExclude() {
1802 return false
1803 } else {
1804 return m.ApexModuleBase.CanHaveApexVariants()
1805 }
1806}
1807
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001808func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001809 return String(mod.Properties.Min_sdk_version)
1810}
1811
Jiyong Park45bf82e2020-12-15 22:29:02 +09001812// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001813func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001814 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001815 if minSdkVersion == "apex_inherit" {
1816 return nil
1817 }
1818 if minSdkVersion == "" {
1819 return fmt.Errorf("min_sdk_version is not specificed")
1820 }
1821
1822 // Not using nativeApiLevelFromUser because the context here is not
1823 // necessarily a native context.
1824 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1825 if err != nil {
1826 return err
1827 }
1828
1829 if ver.GreaterThan(sdkVersion) {
1830 return fmt.Errorf("newer SDK(%v)", ver)
1831 }
Jiyong Park99644e92020-11-17 22:21:02 +09001832 return nil
1833}
1834
Jiyong Park45bf82e2020-12-15 22:29:02 +09001835// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001836func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07001837 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001838 return false
1839 }
1840
Colin Cross8acea3e2024-12-12 14:53:30 -08001841 if mod.Static() && cc.IsSharedDepTag(depTag) {
1842 // shared_lib dependency from a static lib is considered as crossing
1843 // the APEX boundary because the dependency doesn't actually is
1844 // linked; the dependency is used only during the compilation phase.
1845 return false
1846 }
1847
Jiyong Park99644e92020-11-17 22:21:02 +09001848 return true
1849}
1850
Colin Crossf7bbd2f2024-12-05 13:57:10 -08001851func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
1852 return !mod.ApexExclude()
1853}
1854
Jiyong Park99644e92020-11-17 22:21:02 +09001855// Overrides ApexModule.IsInstallabeToApex()
1856func (mod *Module) IsInstallableToApex() bool {
1857 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001858 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001859 return true
1860 }
1861 if _, ok := mod.compiler.(*binaryDecorator); ok {
1862 return true
1863 }
1864 }
1865 return false
1866}
1867
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001868// If a library file has a "lib" prefix, extract the library name without the prefix.
1869func libNameFromFilePath(filepath android.Path) (string, bool) {
1870 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1871 if strings.HasPrefix(libName, "lib") {
1872 libName = libName[3:]
1873 return libName, true
1874 }
1875 return "", false
1876}
1877
Sasha Smundaka76acba2022-04-18 20:12:56 -07001878func kytheExtractRustFactory() android.Singleton {
1879 return &kytheExtractRustSingleton{}
1880}
1881
1882type kytheExtractRustSingleton struct {
1883}
1884
1885func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1886 var xrefTargets android.Paths
1887 ctx.VisitAllModules(func(module android.Module) {
1888 if rustModule, ok := module.(xref); ok {
1889 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1890 }
1891 })
1892 if len(xrefTargets) > 0 {
1893 ctx.Phony("xref_rust", xrefTargets...)
1894 }
1895}
1896
Jihoon Kangf78a8902022-09-01 22:47:07 +00001897func (c *Module) Partition() string {
1898 return ""
1899}
1900
Ivan Lozanoffee3342019-08-27 12:03:00 -07001901var Bool = proptools.Bool
1902var BoolDefault = proptools.BoolDefault
1903var String = proptools.String
1904var StringPtr = proptools.StringPtr