blob: ed38ad7a7628bc840b4c9e1367526a82f0775ec4 [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 Lozano3dfa12d2021-02-04 11:29:41 -0500430
431 // depFlags and depLinkFlags are rustc and linker (clang) flags.
432 depFlags []string
433 depLinkFlags []string
434
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400435 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500436 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000437 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700438 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700439
Ivan Lozano0a468a42024-05-13 21:03:34 -0400440 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
441 // cc_library_static dependants of rlibs.
442 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
443 exportedLinkDirs []string
444
Ivan Lozano45901ed2020-07-24 16:05:01 -0400445 // Used by bindgen modules which call clang
446 depClangFlags []string
447 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400448 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400449 depSystemIncludePaths android.Paths
450
Colin Crossfe605e12022-01-23 20:46:16 -0800451 CrtBegin android.Paths
452 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700453
454 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500455 SrcDeps android.Paths
456 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700457
458 directImplementationDeps android.Paths
459 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700460}
461
462type RustLibraries []RustLibrary
463
464type RustLibrary struct {
465 Path android.Path
466 CrateName string
467}
468
Matthew Maurerbb3add12020-06-25 09:34:12 -0700469type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000470 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700471 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700472}
473
Sasha Smundaka76acba2022-04-18 20:12:56 -0700474type xref interface {
475 XrefRustFiles() android.Paths
476}
477
Matthew Maurerbb3add12020-06-25 09:34:12 -0700478type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000479 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400480 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700481 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700482}
483
Wen-yi Chu41326c12023-09-22 03:58:59 +0000484func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
485 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700486}
487
Colin Cross004bd3f2023-10-02 11:39:17 -0700488func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
489 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400490}
491
Colin Cross0de8a1e2020-09-18 14:15:30 -0700492func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800493 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700494 LinkDirs: flagExporter.linkDirs,
495 LinkObjects: flagExporter.linkObjects,
496 })
497}
498
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499var _ exportedFlagsProducer = (*flagExporter)(nil)
500
501func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700502 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700503}
504
Colin Cross0de8a1e2020-09-18 14:15:30 -0700505type FlagExporterInfo struct {
506 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000507 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700508 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700509}
510
Colin Crossbc7d76c2023-12-12 16:39:03 -0800511var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700512
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513func (mod *Module) isCoverageVariant() bool {
514 return mod.coverage.Properties.IsCoverageVariant
515}
516
517var _ cc.Coverage = (*Module)(nil)
518
Colin Crosse1a85552024-06-14 12:17:37 -0700519func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400520 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
521}
522
Ivan Lozanod7586b62021-04-01 09:49:36 -0400523func (mod *Module) VndkVersion() string {
524 return mod.Properties.VndkVersion
525}
526
Ivan Lozano0a468a42024-05-13 21:03:34 -0400527func (mod *Module) ExportedCrateLinkDirs() []string {
528 return mod.exportedLinkDirs
529}
530
Ivan Lozanod7586b62021-04-01 09:49:36 -0400531func (mod *Module) PreventInstall() bool {
532 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533}
534
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400535func (mod *Module) MarkAsCoverageVariant(coverage bool) {
536 mod.coverage.Properties.IsCoverageVariant = coverage
537}
538
539func (mod *Module) EnableCoverageIfNeeded() {
540 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541}
542
543func defaultsFactory() android.Module {
544 return DefaultsFactory()
545}
546
547type Defaults struct {
548 android.ModuleBase
549 android.DefaultsModuleBase
550}
551
552func DefaultsFactory(props ...interface{}) android.Module {
553 module := &Defaults{}
554
555 module.AddProperties(props...)
556 module.AddProperties(
557 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800558 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500559 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100560 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400561 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700562 &BaseCompilerProperties{},
563 &BinaryCompilerProperties{},
564 &LibraryCompilerProperties{},
565 &ProcMacroCompilerProperties{},
566 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400567 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700568 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400569 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400570 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200571 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500572 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000573 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700574 )
575
576 android.InitDefaultsModule(module)
577 return module
578}
579
580func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700581 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700582}
583
Ivan Lozano183a3212019-10-18 14:18:45 -0700584func (mod *Module) CcLibrary() bool {
585 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500586 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700587 return true
588 }
589 }
590 return false
591}
592
593func (mod *Module) CcLibraryInterface() bool {
594 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400595 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
596 // VariantIs{Static,Shared} is set.
597 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700598 return true
599 }
600 }
601 return false
602}
603
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400604func (mod *Module) RustLibraryInterface() bool {
605 if mod.compiler != nil {
606 if _, ok := mod.compiler.(libraryInterface); ok {
607 return true
608 }
609 }
610 return false
611}
612
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500613func (mod *Module) IsFuzzModule() bool {
614 if _, ok := mod.compiler.(*fuzzDecorator); ok {
615 return true
616 }
617 return false
618}
619
620func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
621 return mod.FuzzModule
622}
623
624func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
625 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
626 return fuzzer.fuzzPackagedModule
627 }
628 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
629}
630
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000631func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500632 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
633 return fuzzer.sharedLibraries
634 }
635 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
636}
637
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400638func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400639 if mod.compiler != nil {
640 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400641 }
642 return nil
643}
644
Ivan Lozano183a3212019-10-18 14:18:45 -0700645func (mod *Module) SetStatic() {
646 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700647 if library, ok := mod.compiler.(libraryInterface); ok {
648 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700649 return
650 }
651 }
652 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
653}
654
655func (mod *Module) SetShared() {
656 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700657 if library, ok := mod.compiler.(libraryInterface); ok {
658 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700659 return
660 }
661 }
662 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
663}
664
Ivan Lozano183a3212019-10-18 14:18:45 -0700665func (mod *Module) BuildStaticVariant() bool {
666 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700667 if library, ok := mod.compiler.(libraryInterface); ok {
668 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700669 }
670 }
671 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
672}
673
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400674func (mod *Module) BuildRlibVariant() bool {
675 if mod.compiler != nil {
676 if library, ok := mod.compiler.(libraryInterface); ok {
677 return library.buildRlib()
678 }
679 }
680 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
681}
682
683func (mod *Module) IsRustFFI() bool {
684 if mod.compiler != nil {
685 if library, ok := mod.compiler.(libraryInterface); ok {
686 return library.isFFILibrary()
687 }
688 }
689 return false
690}
691
Ivan Lozano183a3212019-10-18 14:18:45 -0700692func (mod *Module) BuildSharedVariant() bool {
693 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700694 if library, ok := mod.compiler.(libraryInterface); ok {
695 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700696 }
697 }
698 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
699}
700
Ivan Lozano183a3212019-10-18 14:18:45 -0700701func (mod *Module) Module() android.Module {
702 return mod
703}
704
Ivan Lozano183a3212019-10-18 14:18:45 -0700705func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400706 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700707}
708
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400709func (mod *Module) CoverageFiles() android.Paths {
710 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800711 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400712 }
713 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
714}
715
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400716// Rust does not produce gcno files, and therefore does not produce a coverage archive.
717func (mod *Module) CoverageOutputFile() android.OptionalPath {
718 return android.OptionalPath{}
719}
720
721func (mod *Module) IsNdk(config android.Config) bool {
722 return false
723}
724
725func (mod *Module) IsStubs() bool {
726 return false
727}
728
Jiyong Park459feca2020-12-15 11:02:21 +0900729func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900730 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900731 return false
732 }
733
Jiyong Park459feca2020-12-15 11:02:21 +0900734 // The apex variant is not installable because it is included in the APEX and won't appear
735 // in the system partition as a standalone file.
736 if !apexInfo.IsForPlatform() {
737 return false
738 }
739
Jiyong Parke54f07e2021-04-07 15:08:04 +0900740 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900741}
742
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500743func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800744 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
745 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500746}
747
Ivan Lozano183a3212019-10-18 14:18:45 -0700748var _ cc.LinkableInterface = (*Module)(nil)
749
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750func (mod *Module) Init() android.Module {
751 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500752 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753
Yi Kong46c6e592022-01-20 22:55:00 +0800754 if mod.afdo != nil {
755 mod.AddProperties(mod.afdo.props()...)
756 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 if mod.compiler != nil {
758 mod.AddProperties(mod.compiler.compilerProps()...)
759 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400760 if mod.coverage != nil {
761 mod.AddProperties(mod.coverage.props()...)
762 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200763 if mod.clippy != nil {
764 mod.AddProperties(mod.clippy.props()...)
765 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400766 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700767 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400768 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500769 if mod.sanitize != nil {
770 mod.AddProperties(mod.sanitize.props()...)
771 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400772
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900774 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775
776 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777 return mod
778}
779
780func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
781 return &Module{
782 hod: hod,
783 multilib: multilib,
784 }
785}
786func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
787 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800788 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400789 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200790 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500791 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792 return module
793}
794
795type ModuleContext interface {
796 android.ModuleContext
797 ModuleContextIntf
798}
799
800type BaseModuleContext interface {
801 android.BaseModuleContext
802 ModuleContextIntf
803}
804
805type DepsContext interface {
806 android.BottomUpMutatorContext
807 ModuleContextIntf
808}
809
810type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200811 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813}
814
815type depsContext struct {
816 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817}
818
819type moduleContext struct {
820 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821}
822
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200823type baseModuleContext struct {
824 android.BaseModuleContext
825}
826
827func (ctx *moduleContext) RustModule() *Module {
828 return ctx.Module().(*Module)
829}
830
831func (ctx *moduleContext) toolchain() config.Toolchain {
832 return ctx.RustModule().toolchain(ctx)
833}
834
835func (ctx *depsContext) RustModule() *Module {
836 return ctx.Module().(*Module)
837}
838
839func (ctx *depsContext) toolchain() config.Toolchain {
840 return ctx.RustModule().toolchain(ctx)
841}
842
843func (ctx *baseModuleContext) RustModule() *Module {
844 return ctx.Module().(*Module)
845}
846
847func (ctx *baseModuleContext) toolchain() config.Toolchain {
848 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400849}
850
851func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700852 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
853 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
854 return false
855 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400856 return mod.compiler != nil && mod.compiler.nativeCoverage()
857}
858
Ivan Lozanod7586b62021-04-01 09:49:36 -0400859func (mod *Module) EverInstallable() bool {
860 return mod.compiler != nil &&
861 // Check to see whether the module is actually ever installable.
862 mod.compiler.everInstallable()
863}
864
865func (mod *Module) Installable() *bool {
866 return mod.Properties.Installable
867}
868
Ivan Lozano872d5792022-03-23 17:31:39 -0400869func (mod *Module) ProcMacro() bool {
870 if pm, ok := mod.compiler.(procMacroInterface); ok {
871 return pm.ProcMacro()
872 }
873 return false
874}
875
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
877 if mod.cachedToolchain == nil {
878 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
879 }
880 return mod.cachedToolchain
881}
882
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200883func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
884 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
885}
886
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
888}
889
890func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
891 ctx := &moduleContext{
892 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894
Colin Crossff694a82023-12-13 15:54:49 -0800895 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900896 if !apexInfo.IsForPlatform() {
897 mod.hideApexVariantFromMake = true
898 }
899
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500901 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
902
Ivan Lozanof1868af2022-04-12 13:08:36 -0400903 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700904
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905 if !toolchain.Supported() {
906 // This toolchain's unsupported, there's nothing to do for this mod.
907 return
908 }
909
910 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400911 // Export linkDirs for CC rust generatedlibs
912 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
913 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
914
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915 flags := Flags{
916 Toolchain: toolchain,
917 }
918
Ivan Lozano67eada32021-09-23 11:50:33 -0400919 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800920 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500921 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800922 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700923 if mod.compiler != nil {
924 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400925 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +0000926 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400927 }
928 if mod.coverage != nil {
929 flags, deps = mod.coverage.flags(ctx, flags, deps)
930 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200931 if mod.clippy != nil {
932 flags, deps = mod.clippy.flags(ctx, flags, deps)
933 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500934 if mod.sanitize != nil {
935 flags, deps = mod.sanitize.flags(ctx, flags, deps)
936 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400937
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200938 // SourceProvider needs to call GenerateSource() before compiler calls
939 // compile() so it can provide the source. A SourceProvider has
940 // multiple variants (e.g. source, rlib, dylib). Only the "source"
941 // variant is responsible for effectively generating the source. The
942 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400943 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200944 if mod.compiler.(libraryInterface).source() {
945 mod.sourceProvider.GenerateSource(ctx, deps)
946 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
947 } else {
948 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
949 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700950 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200951 }
Colin Crossa6182ab2024-08-21 10:47:44 -0700952 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Colin Cross40213022023-12-13 15:19:49 -0800953 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400954 }
955
956 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100957 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700958 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400959 if ctx.Failed() {
960 return
961 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700962 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -0700963 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700964 if buildOutput.kytheFile != nil {
965 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
966 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400967 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900968
Dan Albert06feee92021-03-19 15:06:02 -0700969 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
970
Colin Crossff694a82023-12-13 15:54:49 -0800971 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400972 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900973 // If the module has been specifically configure to not be installed then
974 // hide from make as otherwise it will break when running inside make as the
975 // output path to install will not be specified. Not all uninstallable
976 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400977 // side dependencies. In particular, proc-macros need to be captured in the
978 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900979 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +0000980 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900981 } else if !mod.installable(apexInfo) {
982 mod.SkipInstall()
983 }
984
985 // Still call install though, the installs will be stored as PackageSpecs to allow
986 // using the outputs in a genrule.
987 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200988 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900989 if ctx.Failed() {
990 return
991 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400992 // Export your own directory as a linkDir
993 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
994
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400995 }
Chris Wailes74be7642021-07-22 16:20:28 -0700996
Colin Crossb614cd42024-10-11 12:52:21 -0700997 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
998 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
999 })
1000
Chris Wailes74be7642021-07-22 16:20:28 -07001001 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002 }
Wei Lia1aa2972024-06-21 13:08:51 -07001003
mrziwang0cbd3b02024-06-20 16:39:25 -07001004 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001005
1006 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001007}
1008
1009func (mod *Module) setOutputFiles(ctx ModuleContext) {
1010 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1011 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1012 } else if mod.OutputFile().Valid() {
1013 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1014 } else {
1015 ctx.SetOutputFiles(android.Paths{}, "")
1016 }
1017 if mod.compiler != nil {
1018 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1019 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020}
1021
Wei Lia1aa2972024-06-21 13:08:51 -07001022func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1023 // Dump metadata that can not be done in android/compliance-metadata.go
1024 metadataInfo := ctx.ComplianceMetadataInfo()
1025 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1026 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1027
1028 // Static libs
1029 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1030 staticDepNames := make([]string, 0, len(staticDeps))
1031 for _, dep := range staticDeps {
1032 staticDepNames = append(staticDepNames, dep.Name())
1033 }
1034 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1035 for _, dep := range ccStaticDeps {
1036 staticDepNames = append(staticDepNames, dep.Name())
1037 }
1038
1039 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1040 // C static libraries
1041 for _, dep := range deps.StaticLibs {
1042 staticDepPaths = append(staticDepPaths, dep.String())
1043 }
1044 // Rust static libraries
1045 for _, dep := range deps.RLibs {
1046 staticDepPaths = append(staticDepPaths, dep.Path.String())
1047 }
1048 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1049 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1050
1051 // C Whole static libs
1052 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1053 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1054 for _, dep := range ccStaticDeps {
1055 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1056 }
1057 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1058}
1059
Ivan Lozanoffee3342019-08-27 12:03:00 -07001060func (mod *Module) deps(ctx DepsContext) Deps {
1061 deps := Deps{}
1062
1063 if mod.compiler != nil {
1064 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001065 }
1066 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001067 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001068 }
1069
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001070 if mod.coverage != nil {
1071 deps = mod.coverage.deps(ctx, deps)
1072 }
1073
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001074 if mod.sanitize != nil {
1075 deps = mod.sanitize.deps(ctx, deps)
1076 }
1077
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1079 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001080 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001081 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1082 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1083 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001084 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001085 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086 return deps
1087
1088}
1089
Ivan Lozanoffee3342019-08-27 12:03:00 -07001090type dependencyTag struct {
1091 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001092 name string
1093 library bool
1094 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001095 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001096}
1097
Jiyong Park65b62242020-11-25 12:44:59 +09001098// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1099// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1100func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001101 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001102}
1103
1104var _ android.InstallNeededDependencyTag = dependencyTag{}
1105
Colin Cross65cb3142021-12-10 23:05:02 +00001106func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1107 if d.library && d.dynamic {
1108 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1109 }
1110 return nil
1111}
1112
Yu Liuc8884602024-03-15 18:48:38 +00001113func (d dependencyTag) PropagateAconfigValidation() bool {
1114 return d == rlibDepTag || d == sourceDepTag
1115}
1116
1117var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1118
Colin Cross65cb3142021-12-10 23:05:02 +00001119var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1120
Ivan Lozanoffee3342019-08-27 12:03:00 -07001121var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001122 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1123 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001124 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001125 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001126 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001127 dataLibDepTag = dependencyTag{name: "data lib"}
1128 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001129)
1130
Jiyong Park99644e92020-11-17 22:21:02 +09001131func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1132 tag, ok := depTag.(dependencyTag)
1133 return ok && tag == dylibDepTag
1134}
1135
Jiyong Park94e22fd2021-04-08 18:19:15 +09001136func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1137 tag, ok := depTag.(dependencyTag)
1138 return ok && tag == rlibDepTag
1139}
1140
Matthew Maurer0f003b12020-06-29 14:34:06 -07001141type autoDep struct {
1142 variation string
1143 depTag dependencyTag
1144}
1145
1146var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001147 sourceVariation = "source"
1148 rlibVariation = "rlib"
1149 dylibVariation = "dylib"
1150 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1151 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001152)
1153
1154type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001155 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001156}
1157
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001158func (mod *Module) begin(ctx BaseModuleContext) {
1159 if mod.coverage != nil {
1160 mod.coverage.begin(ctx)
1161 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001162 if mod.sanitize != nil {
1163 mod.sanitize.begin(ctx)
1164 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001165}
1166
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001167func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001168 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001169 return p.prebuilt()
1170 }
1171 return nil
1172}
1173
Kiyoung Kim37693d02024-04-04 09:56:15 +09001174func (mod *Module) Symlinks() []string {
1175 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1176 return nil
1177}
1178
Justin Yun24b246a2023-03-16 10:36:16 +09001179func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1180 if rustDep, ok := dep.(*Module); ok {
1181 // Use base module name for snapshots when exporting to Makefile.
1182 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1183 baseName := rustDep.BaseModuleName()
1184 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1185 }
1186 }
1187 return cc.MakeLibName(ctx, c, dep, depName)
1188}
1189
Ivan Lozanod106efe2023-09-21 23:30:26 -04001190func collectIncludedProtos(mod *Module, dep *Module) {
1191 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1192 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1193 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1194 }
1195 }
1196}
Andrew Walbran52533232024-03-19 11:36:04 +00001197
Ivan Lozanoffee3342019-08-27 12:03:00 -07001198func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1199 var depPaths PathDeps
1200
1201 directRlibDeps := []*Module{}
1202 directDylibDeps := []*Module{}
1203 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001204 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001205 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001206 directSrcProvidersDeps := []*Module{}
1207 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001208
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001209 // For the dependency from platform to apex, use the latest stubs
1210 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001211 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001212 if !apexInfo.IsForPlatform() {
1213 mod.apexSdkVersion = apexInfo.MinSdkVersion
1214 }
1215
1216 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1217 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1218 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1219 // (b/144430859)
1220 mod.apexSdkVersion = android.FutureApiLevel
1221 }
1222
Spandan Das604f3762023-03-16 22:51:40 +00001223 skipModuleList := map[string]bool{}
1224
Colin Crossa14fb6a2024-10-23 16:57:06 -07001225 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001226 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001227
Ivan Lozanoffee3342019-08-27 12:03:00 -07001228 ctx.VisitDirectDeps(func(dep android.Module) {
1229 depName := ctx.OtherModuleName(dep)
1230 depTag := ctx.OtherModuleDependencyTag(dep)
Spandan Das604f3762023-03-16 22:51:40 +00001231 if _, exists := skipModuleList[depName]; exists {
1232 return
1233 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001234
1235 if depTag == android.DarwinUniversalVariantTag {
1236 return
1237 }
1238
Ivan Lozano0a468a42024-05-13 21:03:34 -04001239 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001240 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001241 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001242
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001243 switch {
1244 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 dylib, ok := rustDep.compiler.(libraryInterface)
1246 if !ok || !dylib.dylib() {
1247 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1248 return
1249 }
1250 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001251 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001252 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1253
Colin Crossb614cd42024-10-11 12:52:21 -07001254 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1255 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1256 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1257 }
1258
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001259 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001260 rlib, ok := rustDep.compiler.(libraryInterface)
1261 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001262 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001263 return
1264 }
1265 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001266 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001267 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1268
Ivan Lozano0a468a42024-05-13 21:03:34 -04001269 // rust_ffi rlibs may export include dirs, so collect those here.
1270 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1271 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1272 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1273
Colin Crossb614cd42024-10-11 12:52:21 -07001274 // rlibs are not installed, so don't add the output file to directImplementationDeps
1275 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1276 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1277 }
1278
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001279 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001280 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001281 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001282 // proc_macro link dirs need to be exported, so collect those here.
1283 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001284
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001285 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001286 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1287 collectIncludedProtos(mod, rustDep)
1288 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001289 case cc.IsStaticDepTag(depTag):
1290 // Rust FFI rlibs should not be declared in a Rust modules
1291 // "static_libs" list as we can't handle them properly at the
1292 // moment (for example, they only produce an rlib-std variant).
1293 // Instead, a normal rust_library variant should be used.
1294 ctx.PropertyErrorf("static_libs",
1295 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1296 depName)
1297
Paul Duffind5cf92e2021-07-09 17:38:55 +01001298 }
1299
Cole Faustb6e6f992023-08-17 17:42:26 -07001300 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1301
Paul Duffind5cf92e2021-07-09 17:38:55 +01001302 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001303 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1304 // OS/Arch variant is used.
1305 var helper string
1306 if ctx.Host() {
1307 helper = "missing 'host_supported'?"
1308 } else {
1309 helper = "device module defined?"
1310 }
1311
1312 if dep.Target().Os != ctx.Os() {
1313 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1314 return
1315 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1316 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1317 return
1318 }
1319 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001320 }
1321
Ivan Lozano0a468a42024-05-13 21:03:34 -04001322 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001323 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001324 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001325 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1326 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001327 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001328 }
1329
Ivan Lozanoffee3342019-08-27 12:03:00 -07001330 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001331 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001332 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001333 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001334 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001335 }
1336 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001337
Ivan Lozanod106efe2023-09-21 23:30:26 -04001338 if depTag == sourceDepTag {
1339 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1340 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001341 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001342 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1343 }
1344 }
1345 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001346 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001347 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001348 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001349 if _, ok := ccDep.(*Module); !ok {
1350 if ccDep.Module().Target().Os != ctx.Os() {
1351 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1352 return
1353 }
1354 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1355 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1356 return
1357 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001358 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001359 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001360 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001361 if !ctx.Config().AllowMissingDependencies() {
1362 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1363 } else {
1364 ctx.AddMissingDependencies([]string{depName})
1365 }
1366 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001367 }
1368
Wen-yi Chu41326c12023-09-22 03:58:59 +00001369 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001370
Ivan Lozanoffee3342019-08-27 12:03:00 -07001371 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001372 switch {
1373 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001374 if cc.IsWholeStaticLib(depTag) {
1375 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1376 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001377 if mod.Binary() {
1378 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1379 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1380 // final linkage, pass the args directly to the linker to handle these cases.
1381 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1382 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001383 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001384 } else {
1385 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 -05001386 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001387 }
1388
1389 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1390 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001391 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001392 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1393
Colin Cross313aa542023-12-13 13:47:44 -08001394 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001395 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1396 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1397 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1398 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001399 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001400
1401 // Record baseLibName for snapshots.
1402 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1403
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001404 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001405 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001406 // For the shared lib dependencies, we may link to the stub variant
1407 // of the dependency depending on the context (e.g. if this
1408 // dependency crosses the APEX boundaries).
1409 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1410
Colin Crossb614cd42024-10-11 12:52:21 -07001411 if !sharedLibraryInfo.IsStubs {
1412 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1413 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1414 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1415 }
1416 }
1417
Jiyong Park7d55b612021-06-11 17:22:09 +09001418 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1419 // object (either from stub or non-stub) to use.
1420 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001421 if !linkObject.Valid() {
1422 if !ctx.Config().AllowMissingDependencies() {
1423 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1424 } else {
1425 ctx.AddMissingDependencies([]string{depName})
1426 }
1427 return
1428 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001429 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001430
Ivan Lozanoffee3342019-08-27 12:03:00 -07001431 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001432 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001433 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1434 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1435 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1436 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001437 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001438
1439 // Record baseLibName for snapshots.
1440 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1441
Cole Faustb6e6f992023-08-17 17:42:26 -07001442 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001444 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001445 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001446 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1447 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1448 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001449 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001450 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001451 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001452 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001453 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001454 }
1455
1456 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001457 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1458 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001459 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001460 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001461 } else {
1462 switch {
1463 case depTag == cc.CrtBeginDepTag:
1464 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1465 case depTag == cc.CrtEndDepTag:
1466 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1467 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001468 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001469
1470 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001471 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001472 // These are usually genrules which don't have per-target variants.
1473 directSrcDeps = append(directSrcDeps, srcDep)
1474 }
1475 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476 })
1477
Colin Crossa14fb6a2024-10-23 16:57:06 -07001478 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001479
1480 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001481 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001482 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001483 crateName := dep.CrateName()
1484 if alias, aliased := aliases[crateName]; aliased {
1485 crateName = alias
1486 }
1487 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001488 }
1489 var dylibDepFiles RustLibraries
1490 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001491 crateName := dep.CrateName()
1492 if alias, aliased := aliases[crateName]; aliased {
1493 crateName = alias
1494 }
1495 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001496 }
1497 var procMacroDepFiles RustLibraries
1498 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001499 crateName := dep.CrateName()
1500 if alias, aliased := aliases[crateName]; aliased {
1501 crateName = alias
1502 }
1503 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001504 }
1505
Colin Cross004bd3f2023-10-02 11:39:17 -07001506 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001507 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001508 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001509 }
1510
Colin Cross004bd3f2023-10-02 11:39:17 -07001511 var sharedLibFiles android.Paths
1512 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001513 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001514 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001515 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001516 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001517 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001518 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001519 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001520 }
1521
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001522 var srcProviderDepFiles android.Paths
1523 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001524 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001525 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1526 }
1527 for _, dep := range directSrcDeps {
1528 srcs := dep.Srcs()
1529 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1530 }
1531
Wen-yi Chu41326c12023-09-22 03:58:59 +00001532 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1533 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001534 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1535 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1536 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001537 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001538 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001539
1540 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001541 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001542 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001543 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001544 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1545 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1546 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001547
1548 return depPaths
1549}
1550
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001551func (mod *Module) InstallInData() bool {
1552 if mod.compiler == nil {
1553 return false
1554 }
1555 return mod.compiler.inData()
1556}
1557
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001558func (mod *Module) InstallInRamdisk() bool {
1559 return mod.InRamdisk()
1560}
1561
1562func (mod *Module) InstallInVendorRamdisk() bool {
1563 return mod.InVendorRamdisk()
1564}
1565
1566func (mod *Module) InstallInRecovery() bool {
1567 return mod.InRecovery()
1568}
1569
Wen-yi Chu41326c12023-09-22 03:58:59 +00001570func linkPathFromFilePath(filepath android.Path) string {
1571 return strings.Split(filepath.String(), filepath.Base())[0]
1572}
1573
Spandan Das604f3762023-03-16 22:51:40 +00001574// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1575func (r *Module) usePublicApi() bool {
1576 return r.Device() && r.UseSdk()
1577}
1578
1579// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1580func (r *Module) useVendorApi() bool {
1581 return r.Device() && (r.InVendor() || r.InProduct())
1582}
1583
Ivan Lozanoffee3342019-08-27 12:03:00 -07001584func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1585 ctx := &depsContext{
1586 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001587 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001588
1589 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001590 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001591
1592 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001593 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001594 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001595
Ivan Lozano2b081132020-09-08 12:46:52 -04001596 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001597 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001598 stdLinkage = "rlib-std"
1599 }
1600
1601 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001602
Ivan Lozano2b081132020-09-08 12:46:52 -04001603 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1604 rlibDepVariations = append(rlibDepVariations,
1605 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1606 }
1607
Ivan Lozano1921e802021-05-20 13:39:16 -04001608 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001609 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001610 for _, lib := range deps.Rlibs {
1611 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001612 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001613 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001614
1615 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001616 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001617
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001618 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001619 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001620 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001621
Ivan Lozano1921e802021-05-20 13:39:16 -04001622 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001623 if deps.Rustlibs != nil {
1624 if !mod.compiler.Disabled() {
1625 for _, lib := range deps.Rustlibs {
1626 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1627 if autoDep.depTag == rlibDepTag {
1628 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001629 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1630
Ivan Lozanod106efe2023-09-21 23:30:26 -04001631 } else {
1632 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1633 // Check for the existence of the dylib deptag variant. Select it if available,
1634 // otherwise select the rlib variant.
1635 autoDepVariations := append(commonDepVariations,
1636 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001637 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1638 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001639
Ivan Lozanod106efe2023-09-21 23:30:26 -04001640 } else {
1641 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001642 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1643
Ivan Lozanod106efe2023-09-21 23:30:26 -04001644 }
1645 }
1646 }
1647 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1648 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001649 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001650 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001651
Ivan Lozano0a468a42024-05-13 21:03:34 -04001652 // Only add rustlib dependencies if they're source providers themselves.
1653 // This is used to track which crate names need to be added to the source generated
1654 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001655 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001656 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001657 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001658 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001659 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001660 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001661
Ivan Lozano1921e802021-05-20 13:39:16 -04001662 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001663 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001664 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001665 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001666 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001667 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001668 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001669 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001670 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001671 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1672
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001673 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001674 }
1675 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001676
1677 for _, lib := range deps.SharedLibs {
1678 depTag := cc.SharedDepTag()
1679 name, version := cc.StubsLibNameAndVersion(lib)
1680
1681 variations := []blueprint.Variation{
1682 {Mutator: "link", Variation: "shared"},
1683 }
Spandan Dasff665182024-09-11 18:48:44 +00001684 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001685 }
1686
1687 for _, lib := range deps.WholeStaticLibs {
1688 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001689
1690 actx.AddVariationDependencies([]blueprint.Variation{
1691 {Mutator: "link", Variation: "static"},
1692 }, depTag, lib)
1693 }
1694
1695 for _, lib := range deps.StaticLibs {
1696 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001697
1698 actx.AddVariationDependencies([]blueprint.Variation{
1699 {Mutator: "link", Variation: "static"},
1700 }, depTag, lib)
1701 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001702
Zach Johnson3df4e632020-11-06 11:56:27 -08001703 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1704
Colin Cross565cafd2020-09-25 18:47:38 -07001705 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001706 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001707 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001708 }
Colin Crossfe605e12022-01-23 20:46:16 -08001709 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001710 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001711 }
1712
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001713 if mod.sourceProvider != nil {
1714 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1715 bindgen.Properties.Custom_bindgen != "" {
1716 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1717 bindgen.Properties.Custom_bindgen)
1718 }
1719 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001720
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001721 actx.AddVariationDependencies([]blueprint.Variation{
1722 {Mutator: "link", Variation: "shared"},
1723 }, dataLibDepTag, deps.DataLibs...)
1724
1725 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1726
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001727 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001728 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001729
1730 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001731}
1732
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001733func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001734 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001735 mod.beginMutator(ctx)
1736 }
1737}
1738
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001739func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1740 ctx := &baseModuleContext{
1741 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001742 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001743
1744 mod.begin(ctx)
1745}
1746
Ivan Lozanoffee3342019-08-27 12:03:00 -07001747func (mod *Module) Name() string {
1748 name := mod.ModuleBase.Name()
1749 if p, ok := mod.compiler.(interface {
1750 Name(string) string
1751 }); ok {
1752 name = p.Name(name)
1753 }
1754 return name
1755}
1756
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001757func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001758 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001759 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001760 }
1761}
1762
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001763var _ android.HostToolProvider = (*Module)(nil)
1764
1765func (mod *Module) HostToolPath() android.OptionalPath {
1766 if !mod.Host() {
1767 return android.OptionalPath{}
1768 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001769 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1770 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001771 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1772 // Even though proc-macros aren't strictly "tools", since they target the compiler
1773 // and act as compiler plugins, we treat them similarly.
1774 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001775 }
1776 return android.OptionalPath{}
1777}
1778
Jiyong Park99644e92020-11-17 22:21:02 +09001779var _ android.ApexModule = (*Module)(nil)
1780
Ivan Lozano24cf0362024-10-04 16:02:38 +00001781// If a module is marked for exclusion from apexes, don't provide apex variants.
1782// TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
1783func (m *Module) CanHaveApexVariants() bool {
1784 if m.ApexExclude() {
1785 return false
1786 } else {
1787 return m.ApexModuleBase.CanHaveApexVariants()
1788 }
1789}
1790
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001791func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001792 return String(mod.Properties.Min_sdk_version)
1793}
1794
Jiyong Park45bf82e2020-12-15 22:29:02 +09001795// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001796func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001797 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001798 if minSdkVersion == "apex_inherit" {
1799 return nil
1800 }
1801 if minSdkVersion == "" {
1802 return fmt.Errorf("min_sdk_version is not specificed")
1803 }
1804
1805 // Not using nativeApiLevelFromUser because the context here is not
1806 // necessarily a native context.
1807 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1808 if err != nil {
1809 return err
1810 }
1811
1812 if ver.GreaterThan(sdkVersion) {
1813 return fmt.Errorf("newer SDK(%v)", ver)
1814 }
Jiyong Park99644e92020-11-17 22:21:02 +09001815 return nil
1816}
1817
Jiyong Park45bf82e2020-12-15 22:29:02 +09001818// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001819func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1820 depTag := ctx.OtherModuleDependencyTag(dep)
1821
1822 if ccm, ok := dep.(*cc.Module); ok {
1823 if ccm.HasStubsVariants() {
1824 if cc.IsSharedDepTag(depTag) {
1825 // dynamic dep to a stubs lib crosses APEX boundary
1826 return false
1827 }
1828 if cc.IsRuntimeDepTag(depTag) {
1829 // runtime dep to a stubs lib also crosses APEX boundary
1830 return false
1831 }
1832
1833 if cc.IsHeaderDepTag(depTag) {
1834 return false
1835 }
1836 }
1837 if mod.Static() && cc.IsSharedDepTag(depTag) {
1838 // shared_lib dependency from a static lib is considered as crossing
1839 // the APEX boundary because the dependency doesn't actually is
1840 // linked; the dependency is used only during the compilation phase.
1841 return false
1842 }
1843 }
1844
Matthew Maurer581b6d82022-09-29 16:46:25 -07001845 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001846 return false
1847 }
1848
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +00001849 if rustDep, ok := dep.(*Module); ok && rustDep.ApexExclude() {
1850 return false
1851 }
1852
Jiyong Park99644e92020-11-17 22:21:02 +09001853 return true
1854}
1855
1856// Overrides ApexModule.IsInstallabeToApex()
1857func (mod *Module) IsInstallableToApex() bool {
1858 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001859 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001860 return true
1861 }
1862 if _, ok := mod.compiler.(*binaryDecorator); ok {
1863 return true
1864 }
1865 }
1866 return false
1867}
1868
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001869// If a library file has a "lib" prefix, extract the library name without the prefix.
1870func libNameFromFilePath(filepath android.Path) (string, bool) {
1871 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1872 if strings.HasPrefix(libName, "lib") {
1873 libName = libName[3:]
1874 return libName, true
1875 }
1876 return "", false
1877}
1878
Sasha Smundaka76acba2022-04-18 20:12:56 -07001879func kytheExtractRustFactory() android.Singleton {
1880 return &kytheExtractRustSingleton{}
1881}
1882
1883type kytheExtractRustSingleton struct {
1884}
1885
1886func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1887 var xrefTargets android.Paths
1888 ctx.VisitAllModules(func(module android.Module) {
1889 if rustModule, ok := module.(xref); ok {
1890 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1891 }
1892 })
1893 if len(xrefTargets) > 0 {
1894 ctx.Phony("xref_rust", xrefTargets...)
1895 }
1896}
1897
Jihoon Kangf78a8902022-09-01 22:47:07 +00001898func (c *Module) Partition() string {
1899 return ""
1900}
1901
Ivan Lozanoffee3342019-08-27 12:03:00 -07001902var Bool = proptools.Bool
1903var BoolDefault = proptools.BoolDefault
1904var String = proptools.String
1905var StringPtr = proptools.StringPtr