blob: f987d114671964a90f93a1369161a4bc83d0a4e8 [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"
Aditya Choudhary87b2ab22023-11-17 15:27:06 +000023 "android/soong/testing"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090024
Ivan Lozanoffee3342019-08-27 12:03:00 -070025 "github.com/google/blueprint"
Colin Crossa14fb6a2024-10-23 16:57:06 -070026 "github.com/google/blueprint/depset"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "github.com/google/blueprint/proptools"
28
29 "android/soong/android"
30 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020031 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070032 "android/soong/fuzz"
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 "android/soong/rust/config"
34)
35
36var pctx = android.NewPackageContext("android/soong/rust")
37
38func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070039 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070040 android.PreDepsMutators(registerPreDepsMutators)
41 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090042 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070043 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020044 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000045 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070046}
47
Colin Cross8a49a3d2024-05-20 12:22:27 -070048func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
49 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
50 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070051 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070052}
53
54func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070055 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070056}
57
Ivan Lozanoffee3342019-08-27 12:03:00 -070058type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040059 GlobalRustFlags []string // Flags that apply globally to rust
60 GlobalLinkFlags []string // Flags that apply globally to linker
61 RustFlags []string // Flags that apply to rust
62 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020063 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070064 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070065 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040066 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020067 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070068 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070069}
70
71type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050072 AndroidMkRlibs []string `blueprint:"mutated"`
73 AndroidMkDylibs []string `blueprint:"mutated"`
74 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050075 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +000076 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040077
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090078 ImageVariation string `blueprint:"mutated"`
79 VndkVersion string `blueprint:"mutated"`
80 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -050081
Ivan Lozanoc08897c2021-04-02 12:41:32 -040082 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
83 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
84 // appended before SubName.
85 RustSubName string `blueprint:"mutated"`
86
Ivan Lozano6a884432020-12-02 09:15:16 -050087 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +000088 ProductVariantNeeded bool `blueprint:"mutated"`
89 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050090 CoreVariantNeeded bool `blueprint:"mutated"`
91 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070092 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080093 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050094 ExtraVariants []string `blueprint:"mutated"`
95
Ivan Lozanoa2268632021-07-22 10:52:06 -040096 // Allows this module to use non-APEX version of libraries. Useful
97 // for building binaries that are started before APEXes are activated.
98 Bootstrap *bool
99
Ivan Lozano1921e802021-05-20 13:39:16 -0400100 // Used by vendor snapshot to record dependencies from snapshot modules.
101 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900102 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400103 SnapshotRlibs []string `blueprint:"mutated"`
104 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400105
Matthew Maurerc6868382021-07-13 14:12:37 -0700106 // Make this module available when building for ramdisk.
107 // On device without a dedicated recovery partition, the module is only
108 // available after switching root into
109 // /first_stage_ramdisk. To expose the module before switching root, install
110 // the recovery variant instead.
111 Ramdisk_available *bool
112
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500113 // Make this module available when building for vendor ramdisk.
114 // On device without a dedicated recovery partition, the module is only
115 // available after switching root into
116 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800117 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500118 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400119
Ivan Lozano1921e802021-05-20 13:39:16 -0400120 // Normally Soong uses the directory structure to decide which modules
121 // should be included (framework) or excluded (non-framework) from the
122 // different snapshots (vendor, recovery, etc.), but this property
123 // allows a partner to exclude a module normally thought of as a
124 // framework module from the vendor snapshot.
125 Exclude_from_vendor_snapshot *bool
126
127 // Normally Soong uses the directory structure to decide which modules
128 // should be included (framework) or excluded (non-framework) from the
129 // different snapshots (vendor, recovery, etc.), but this property
130 // allows a partner to exclude a module normally thought of as a
131 // framework module from the recovery snapshot.
132 Exclude_from_recovery_snapshot *bool
133
Matthew Maurer460ee942021-02-11 12:31:46 -0800134 // Make this module available when building for recovery
135 Recovery_available *bool
136
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500137 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
138 Min_sdk_version *string
139
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900140 HideFromMake bool `blueprint:"mutated"`
141 PreventInstall bool `blueprint:"mutated"`
142
143 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144}
145
146type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700147 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148
Ivan Lozano6a884432020-12-02 09:15:16 -0500149 VendorProperties cc.VendorProperties
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 Properties BaseProperties
152
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000153 hod android.HostOrDeviceSupported
154 multilib android.Multilib
155 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156
Ivan Lozano6a884432020-12-02 09:15:16 -0500157 makeLinkType string
158
Yi Kong46c6e592022-01-20 22:55:00 +0800159 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700160 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400161 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200162 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500163 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700164 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400165 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700166 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400167
Ivan Lozano0a468a42024-05-13 21:03:34 -0400168 exportedLinkDirs []string
169
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400170 // Output file to be installed, may be stripped or unstripped.
171 outputFile android.OptionalPath
172
Sasha Smundaka76acba2022-04-18 20:12:56 -0700173 // Cross-reference input file
174 kytheFiles android.Paths
175
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400176 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900177
178 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500179
180 // For apex variants, this is set as apex.min_sdk_version
181 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700182
Colin Crossa14fb6a2024-10-23 16:57:06 -0700183 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700184}
185
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500186func (mod *Module) Header() bool {
187 //TODO: If Rust libraries provide header variants, this needs to be updated.
188 return false
189}
190
191func (mod *Module) SetPreventInstall() {
192 mod.Properties.PreventInstall = true
193}
194
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500195func (mod *Module) SetHideFromMake() {
196 mod.Properties.HideFromMake = true
197}
198
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900199func (mod *Module) HiddenFromMake() bool {
200 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400201}
202
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500203func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500204 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
205 // nil since we need compiler to actually sanitize.
206 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500207}
208
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500209func (mod *Module) IsPrebuilt() bool {
210 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
211 return true
212 }
213 return false
214}
215
Ivan Lozano52767be2019-10-18 14:49:46 -0700216func (mod *Module) SelectedStl() string {
217 return ""
218}
219
Ivan Lozano2b262972019-11-21 12:30:50 -0800220func (mod *Module) NonCcVariants() bool {
221 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400222 if library, ok := mod.compiler.(libraryInterface); ok {
223 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800224 }
225 }
226 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
227}
228
Ivan Lozano52767be2019-10-18 14:49:46 -0700229func (mod *Module) Static() bool {
230 if mod.compiler != nil {
231 if library, ok := mod.compiler.(libraryInterface); ok {
232 return library.static()
233 }
234 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400235 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700236}
237
238func (mod *Module) Shared() bool {
239 if mod.compiler != nil {
240 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400241 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700242 }
243 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400244 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700245}
246
Ivan Lozanod7586b62021-04-01 09:49:36 -0400247func (mod *Module) Dylib() bool {
248 if mod.compiler != nil {
249 if library, ok := mod.compiler.(libraryInterface); ok {
250 return library.dylib()
251 }
252 }
253 return false
254}
255
Ivan Lozanod106efe2023-09-21 23:30:26 -0400256func (mod *Module) Source() bool {
257 if mod.compiler != nil {
258 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
259 return library.source()
260 }
261 }
262 return false
263}
264
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400265func (mod *Module) RlibStd() bool {
266 if mod.compiler != nil {
267 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
268 return library.rlibStd()
269 }
270 }
271 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
272}
273
Ivan Lozanod7586b62021-04-01 09:49:36 -0400274func (mod *Module) Rlib() bool {
275 if mod.compiler != nil {
276 if library, ok := mod.compiler.(libraryInterface); ok {
277 return library.rlib()
278 }
279 }
280 return false
281}
282
283func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400284 if binary, ok := mod.compiler.(binaryInterface); ok {
285 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400286 }
287 return false
288}
289
Justin Yun5e035862021-06-29 20:50:37 +0900290func (mod *Module) StaticExecutable() bool {
291 if !mod.Binary() {
292 return false
293 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400294 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900295}
296
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000297func (mod *Module) ApexExclude() bool {
298 if mod.compiler != nil {
299 if library, ok := mod.compiler.(libraryInterface); ok {
300 return library.apexExclude()
301 }
302 }
303 return false
304}
305
Ivan Lozanod7586b62021-04-01 09:49:36 -0400306func (mod *Module) Object() bool {
307 // Rust has no modules which produce only object files.
308 return false
309}
310
Ivan Lozano52767be2019-10-18 14:49:46 -0700311func (mod *Module) Toc() android.OptionalPath {
312 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400313 if lib, ok := mod.compiler.(libraryInterface); ok {
314 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700315 }
316 }
317 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
318}
319
Colin Crossc511bc52020-04-07 16:50:32 +0000320func (mod *Module) UseSdk() bool {
321 return false
322}
323
Ivan Lozanod7586b62021-04-01 09:49:36 -0400324func (mod *Module) RelativeInstallPath() string {
325 if mod.compiler != nil {
326 return mod.compiler.relativeInstallPath()
327 }
328 return ""
329}
330
Ivan Lozano52767be2019-10-18 14:49:46 -0700331func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500332 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700333}
334
Jiyong Park7d55b612021-06-11 17:22:09 +0900335func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400336 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900337}
338
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400339func (mod *Module) SubName() string {
340 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700341}
342
Ivan Lozanof1868af2022-04-12 13:08:36 -0400343func (mod *Module) IsVndkPrebuiltLibrary() bool {
344 // Rust modules do not provide VNDK prebuilts
345 return false
346}
347
348func (mod *Module) IsVendorPublicLibrary() bool {
349 return mod.VendorProperties.IsVendorPublicLibrary
350}
351
352func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
353 // Rust modules to not provide Sdk variants
354 return false
355}
356
Colin Cross127bb8b2020-12-16 16:46:01 -0800357func (c *Module) IsVndkPrivate() bool {
358 return false
359}
360
361func (c *Module) IsLlndk() bool {
362 return false
363}
364
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400365func (mod *Module) KernelHeadersDecorator() bool {
366 return false
367}
368
Colin Cross1f3f1302021-04-26 18:37:44 -0700369func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400370 return false
371}
372
Colin Cross5271fea2021-04-27 13:06:04 -0700373func (m *Module) NeedsVendorPublicLibraryVariants() bool {
374 return false
375}
376
Ivan Lozanod7586b62021-04-01 09:49:36 -0400377func (mod *Module) HasLlndkStubs() bool {
378 return false
379}
380
381func (mod *Module) StubsVersion() string {
382 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
383}
384
Ivan Lozano52767be2019-10-18 14:49:46 -0700385func (mod *Module) SdkVersion() string {
386 return ""
387}
388
Colin Crossc511bc52020-04-07 16:50:32 +0000389func (mod *Module) AlwaysSdk() bool {
390 return false
391}
392
Jiyong Park2286afd2020-06-16 21:58:53 +0900393func (mod *Module) IsSdkVariant() bool {
394 return false
395}
396
Colin Cross1348ce32020-10-01 13:37:16 -0700397func (mod *Module) SplitPerApiLevel() bool {
398 return false
399}
400
Sasha Smundaka76acba2022-04-18 20:12:56 -0700401func (mod *Module) XrefRustFiles() android.Paths {
402 return mod.kytheFiles
403}
404
Ivan Lozanoffee3342019-08-27 12:03:00 -0700405type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400406 Dylibs []string
407 Rlibs []string
408 Rustlibs []string
409 Stdlibs []string
410 ProcMacros []string
411 SharedLibs []string
412 StaticLibs []string
413 WholeStaticLibs []string
414 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400416 // Used for data dependencies adjacent to tests
417 DataLibs []string
418 DataBins []string
419
Colin Crossfe605e12022-01-23 20:46:16 -0800420 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421}
422
423type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700424 DyLibs RustLibraries
425 RLibs RustLibraries
426 SharedLibs android.Paths
427 SharedLibDeps android.Paths
428 StaticLibs android.Paths
429 ProcMacros RustLibraries
430 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500431
432 // depFlags and depLinkFlags are rustc and linker (clang) flags.
433 depFlags []string
434 depLinkFlags []string
435
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400436 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500437 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000438 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700439 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700440
Ivan Lozano0a468a42024-05-13 21:03:34 -0400441 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
442 // cc_library_static dependants of rlibs.
443 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
444 exportedLinkDirs []string
445
Ivan Lozano45901ed2020-07-24 16:05:01 -0400446 // Used by bindgen modules which call clang
447 depClangFlags []string
448 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400449 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400450 depSystemIncludePaths android.Paths
451
Colin Crossfe605e12022-01-23 20:46:16 -0800452 CrtBegin android.Paths
453 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700454
455 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500456 SrcDeps android.Paths
457 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700458}
459
460type RustLibraries []RustLibrary
461
462type RustLibrary struct {
463 Path android.Path
464 CrateName string
465}
466
Matthew Maurerbb3add12020-06-25 09:34:12 -0700467type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000468 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700469 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700470}
471
Sasha Smundaka76acba2022-04-18 20:12:56 -0700472type xref interface {
473 XrefRustFiles() android.Paths
474}
475
Matthew Maurerbb3add12020-06-25 09:34:12 -0700476type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000477 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400478 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700479 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700480}
481
Wen-yi Chu41326c12023-09-22 03:58:59 +0000482func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
483 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700484}
485
Colin Cross004bd3f2023-10-02 11:39:17 -0700486func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
487 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400488}
489
Colin Cross0de8a1e2020-09-18 14:15:30 -0700490func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800491 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700492 LinkDirs: flagExporter.linkDirs,
493 LinkObjects: flagExporter.linkObjects,
494 })
495}
496
Matthew Maurerbb3add12020-06-25 09:34:12 -0700497var _ exportedFlagsProducer = (*flagExporter)(nil)
498
499func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700500 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700501}
502
Colin Cross0de8a1e2020-09-18 14:15:30 -0700503type FlagExporterInfo struct {
504 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000505 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700506 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700507}
508
Colin Crossbc7d76c2023-12-12 16:39:03 -0800509var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700510
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400511func (mod *Module) isCoverageVariant() bool {
512 return mod.coverage.Properties.IsCoverageVariant
513}
514
515var _ cc.Coverage = (*Module)(nil)
516
Colin Crosse1a85552024-06-14 12:17:37 -0700517func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400518 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
519}
520
Ivan Lozanod7586b62021-04-01 09:49:36 -0400521func (mod *Module) VndkVersion() string {
522 return mod.Properties.VndkVersion
523}
524
Ivan Lozano0a468a42024-05-13 21:03:34 -0400525func (mod *Module) ExportedCrateLinkDirs() []string {
526 return mod.exportedLinkDirs
527}
528
Ivan Lozanod7586b62021-04-01 09:49:36 -0400529func (mod *Module) PreventInstall() bool {
530 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531}
532
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533func (mod *Module) MarkAsCoverageVariant(coverage bool) {
534 mod.coverage.Properties.IsCoverageVariant = coverage
535}
536
537func (mod *Module) EnableCoverageIfNeeded() {
538 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539}
540
541func defaultsFactory() android.Module {
542 return DefaultsFactory()
543}
544
545type Defaults struct {
546 android.ModuleBase
547 android.DefaultsModuleBase
548}
549
550func DefaultsFactory(props ...interface{}) android.Module {
551 module := &Defaults{}
552
553 module.AddProperties(props...)
554 module.AddProperties(
555 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800556 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500557 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100558 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400559 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560 &BaseCompilerProperties{},
561 &BinaryCompilerProperties{},
562 &LibraryCompilerProperties{},
563 &ProcMacroCompilerProperties{},
564 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400565 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700566 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400567 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400568 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200569 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500570 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000571 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572 )
573
574 android.InitDefaultsModule(module)
575 return module
576}
577
578func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700579 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700580}
581
Ivan Lozano183a3212019-10-18 14:18:45 -0700582func (mod *Module) CcLibrary() bool {
583 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500584 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700585 return true
586 }
587 }
588 return false
589}
590
591func (mod *Module) CcLibraryInterface() bool {
592 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400593 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
594 // VariantIs{Static,Shared} is set.
595 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700596 return true
597 }
598 }
599 return false
600}
601
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400602func (mod *Module) RustLibraryInterface() bool {
603 if mod.compiler != nil {
604 if _, ok := mod.compiler.(libraryInterface); ok {
605 return true
606 }
607 }
608 return false
609}
610
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500611func (mod *Module) IsFuzzModule() bool {
612 if _, ok := mod.compiler.(*fuzzDecorator); ok {
613 return true
614 }
615 return false
616}
617
618func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
619 return mod.FuzzModule
620}
621
622func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
623 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
624 return fuzzer.fuzzPackagedModule
625 }
626 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
627}
628
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000629func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500630 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
631 return fuzzer.sharedLibraries
632 }
633 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
634}
635
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400636func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400637 if mod.compiler != nil {
638 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400639 }
640 return nil
641}
642
Ivan Lozano183a3212019-10-18 14:18:45 -0700643func (mod *Module) SetStatic() {
644 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700645 if library, ok := mod.compiler.(libraryInterface); ok {
646 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700647 return
648 }
649 }
650 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
651}
652
653func (mod *Module) SetShared() {
654 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700655 if library, ok := mod.compiler.(libraryInterface); ok {
656 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700657 return
658 }
659 }
660 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
661}
662
Ivan Lozano183a3212019-10-18 14:18:45 -0700663func (mod *Module) BuildStaticVariant() bool {
664 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700665 if library, ok := mod.compiler.(libraryInterface); ok {
666 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700667 }
668 }
669 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
670}
671
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400672func (mod *Module) BuildRlibVariant() bool {
673 if mod.compiler != nil {
674 if library, ok := mod.compiler.(libraryInterface); ok {
675 return library.buildRlib()
676 }
677 }
678 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
679}
680
681func (mod *Module) IsRustFFI() bool {
682 if mod.compiler != nil {
683 if library, ok := mod.compiler.(libraryInterface); ok {
684 return library.isFFILibrary()
685 }
686 }
687 return false
688}
689
Ivan Lozano183a3212019-10-18 14:18:45 -0700690func (mod *Module) BuildSharedVariant() bool {
691 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700692 if library, ok := mod.compiler.(libraryInterface); ok {
693 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700694 }
695 }
696 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
697}
698
Ivan Lozano183a3212019-10-18 14:18:45 -0700699func (mod *Module) Module() android.Module {
700 return mod
701}
702
Ivan Lozano183a3212019-10-18 14:18:45 -0700703func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400704 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700705}
706
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400707func (mod *Module) CoverageFiles() android.Paths {
708 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800709 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400710 }
711 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
712}
713
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400714// Rust does not produce gcno files, and therefore does not produce a coverage archive.
715func (mod *Module) CoverageOutputFile() android.OptionalPath {
716 return android.OptionalPath{}
717}
718
719func (mod *Module) IsNdk(config android.Config) bool {
720 return false
721}
722
723func (mod *Module) IsStubs() bool {
724 return false
725}
726
Jiyong Park459feca2020-12-15 11:02:21 +0900727func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900728 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900729 return false
730 }
731
Jiyong Park459feca2020-12-15 11:02:21 +0900732 // The apex variant is not installable because it is included in the APEX and won't appear
733 // in the system partition as a standalone file.
734 if !apexInfo.IsForPlatform() {
735 return false
736 }
737
Jiyong Parke54f07e2021-04-07 15:08:04 +0900738 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900739}
740
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500741func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800742 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
743 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500744}
745
Ivan Lozano183a3212019-10-18 14:18:45 -0700746var _ cc.LinkableInterface = (*Module)(nil)
747
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748func (mod *Module) Init() android.Module {
749 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500750 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751
Yi Kong46c6e592022-01-20 22:55:00 +0800752 if mod.afdo != nil {
753 mod.AddProperties(mod.afdo.props()...)
754 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 if mod.compiler != nil {
756 mod.AddProperties(mod.compiler.compilerProps()...)
757 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400758 if mod.coverage != nil {
759 mod.AddProperties(mod.coverage.props()...)
760 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200761 if mod.clippy != nil {
762 mod.AddProperties(mod.clippy.props()...)
763 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400764 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700765 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400766 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500767 if mod.sanitize != nil {
768 mod.AddProperties(mod.sanitize.props()...)
769 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400770
Ivan Lozanoffee3342019-08-27 12:03:00 -0700771 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900772 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773
774 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700775 return mod
776}
777
778func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
779 return &Module{
780 hod: hod,
781 multilib: multilib,
782 }
783}
784func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
785 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800786 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400787 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200788 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500789 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790 return module
791}
792
793type ModuleContext interface {
794 android.ModuleContext
795 ModuleContextIntf
796}
797
798type BaseModuleContext interface {
799 android.BaseModuleContext
800 ModuleContextIntf
801}
802
803type DepsContext interface {
804 android.BottomUpMutatorContext
805 ModuleContextIntf
806}
807
808type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200809 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811}
812
813type depsContext struct {
814 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815}
816
817type moduleContext struct {
818 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819}
820
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200821type baseModuleContext struct {
822 android.BaseModuleContext
823}
824
825func (ctx *moduleContext) RustModule() *Module {
826 return ctx.Module().(*Module)
827}
828
829func (ctx *moduleContext) toolchain() config.Toolchain {
830 return ctx.RustModule().toolchain(ctx)
831}
832
833func (ctx *depsContext) RustModule() *Module {
834 return ctx.Module().(*Module)
835}
836
837func (ctx *depsContext) toolchain() config.Toolchain {
838 return ctx.RustModule().toolchain(ctx)
839}
840
841func (ctx *baseModuleContext) RustModule() *Module {
842 return ctx.Module().(*Module)
843}
844
845func (ctx *baseModuleContext) toolchain() config.Toolchain {
846 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400847}
848
849func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700850 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
851 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
852 return false
853 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400854 return mod.compiler != nil && mod.compiler.nativeCoverage()
855}
856
Ivan Lozanod7586b62021-04-01 09:49:36 -0400857func (mod *Module) EverInstallable() bool {
858 return mod.compiler != nil &&
859 // Check to see whether the module is actually ever installable.
860 mod.compiler.everInstallable()
861}
862
863func (mod *Module) Installable() *bool {
864 return mod.Properties.Installable
865}
866
Ivan Lozano872d5792022-03-23 17:31:39 -0400867func (mod *Module) ProcMacro() bool {
868 if pm, ok := mod.compiler.(procMacroInterface); ok {
869 return pm.ProcMacro()
870 }
871 return false
872}
873
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
875 if mod.cachedToolchain == nil {
876 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
877 }
878 return mod.cachedToolchain
879}
880
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200881func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
882 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
883}
884
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
886}
887
888func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
889 ctx := &moduleContext{
890 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892
Colin Crossff694a82023-12-13 15:54:49 -0800893 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900894 if !apexInfo.IsForPlatform() {
895 mod.hideApexVariantFromMake = true
896 }
897
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500899 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
900
Ivan Lozanof1868af2022-04-12 13:08:36 -0400901 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700902
Ivan Lozanoffee3342019-08-27 12:03:00 -0700903 if !toolchain.Supported() {
904 // This toolchain's unsupported, there's nothing to do for this mod.
905 return
906 }
907
908 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400909 // Export linkDirs for CC rust generatedlibs
910 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
911 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
912
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 flags := Flags{
914 Toolchain: toolchain,
915 }
916
Ivan Lozano67eada32021-09-23 11:50:33 -0400917 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800918 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500919 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800920 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921 if mod.compiler != nil {
922 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400923 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +0000924 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400925 }
926 if mod.coverage != nil {
927 flags, deps = mod.coverage.flags(ctx, flags, deps)
928 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200929 if mod.clippy != nil {
930 flags, deps = mod.clippy.flags(ctx, flags, deps)
931 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500932 if mod.sanitize != nil {
933 flags, deps = mod.sanitize.flags(ctx, flags, deps)
934 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400935
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200936 // SourceProvider needs to call GenerateSource() before compiler calls
937 // compile() so it can provide the source. A SourceProvider has
938 // multiple variants (e.g. source, rlib, dylib). Only the "source"
939 // variant is responsible for effectively generating the source. The
940 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400941 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200942 if mod.compiler.(libraryInterface).source() {
943 mod.sourceProvider.GenerateSource(ctx, deps)
944 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
945 } else {
946 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
947 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700948 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200949 }
Colin Crossa6182ab2024-08-21 10:47:44 -0700950 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Colin Cross40213022023-12-13 15:19:49 -0800951 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400952 }
953
954 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100955 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700956 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400957 if ctx.Failed() {
958 return
959 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700960 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -0700961 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700962 if buildOutput.kytheFile != nil {
963 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
964 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400965 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900966
Dan Albert06feee92021-03-19 15:06:02 -0700967 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
968
Colin Crossff694a82023-12-13 15:54:49 -0800969 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400970 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900971 // If the module has been specifically configure to not be installed then
972 // hide from make as otherwise it will break when running inside make as the
973 // output path to install will not be specified. Not all uninstallable
974 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400975 // side dependencies. In particular, proc-macros need to be captured in the
976 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900977 mod.HideFromMake()
978 } else if !mod.installable(apexInfo) {
979 mod.SkipInstall()
980 }
981
982 // Still call install though, the installs will be stored as PackageSpecs to allow
983 // using the outputs in a genrule.
984 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200985 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900986 if ctx.Failed() {
987 return
988 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400989 // Export your own directory as a linkDir
990 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
991
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400992 }
Chris Wailes74be7642021-07-22 16:20:28 -0700993
994 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700995 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000996 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800997 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000998 }
Wei Lia1aa2972024-06-21 13:08:51 -0700999
mrziwang0cbd3b02024-06-20 16:39:25 -07001000 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001001
1002 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001003}
1004
1005func (mod *Module) setOutputFiles(ctx ModuleContext) {
1006 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1007 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1008 } else if mod.OutputFile().Valid() {
1009 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1010 } else {
1011 ctx.SetOutputFiles(android.Paths{}, "")
1012 }
1013 if mod.compiler != nil {
1014 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1015 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001016}
1017
Wei Lia1aa2972024-06-21 13:08:51 -07001018func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1019 // Dump metadata that can not be done in android/compliance-metadata.go
1020 metadataInfo := ctx.ComplianceMetadataInfo()
1021 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1022 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1023
1024 // Static libs
1025 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1026 staticDepNames := make([]string, 0, len(staticDeps))
1027 for _, dep := range staticDeps {
1028 staticDepNames = append(staticDepNames, dep.Name())
1029 }
1030 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1031 for _, dep := range ccStaticDeps {
1032 staticDepNames = append(staticDepNames, dep.Name())
1033 }
1034
1035 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1036 // C static libraries
1037 for _, dep := range deps.StaticLibs {
1038 staticDepPaths = append(staticDepPaths, dep.String())
1039 }
1040 // Rust static libraries
1041 for _, dep := range deps.RLibs {
1042 staticDepPaths = append(staticDepPaths, dep.Path.String())
1043 }
1044 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1045 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1046
1047 // C Whole static libs
1048 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1049 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1050 for _, dep := range ccStaticDeps {
1051 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1052 }
1053 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1054}
1055
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056func (mod *Module) deps(ctx DepsContext) Deps {
1057 deps := Deps{}
1058
1059 if mod.compiler != nil {
1060 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001061 }
1062 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001063 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001064 }
1065
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001066 if mod.coverage != nil {
1067 deps = mod.coverage.deps(ctx, deps)
1068 }
1069
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001070 if mod.sanitize != nil {
1071 deps = mod.sanitize.deps(ctx, deps)
1072 }
1073
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1075 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001076 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001077 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1078 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1079 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001080 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001081 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001082 return deps
1083
1084}
1085
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086type dependencyTag struct {
1087 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001088 name string
1089 library bool
1090 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001091 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001092}
1093
Jiyong Park65b62242020-11-25 12:44:59 +09001094// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1095// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1096func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001097 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001098}
1099
1100var _ android.InstallNeededDependencyTag = dependencyTag{}
1101
Colin Cross65cb3142021-12-10 23:05:02 +00001102func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1103 if d.library && d.dynamic {
1104 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1105 }
1106 return nil
1107}
1108
Yu Liuc8884602024-03-15 18:48:38 +00001109func (d dependencyTag) PropagateAconfigValidation() bool {
1110 return d == rlibDepTag || d == sourceDepTag
1111}
1112
1113var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1114
Colin Cross65cb3142021-12-10 23:05:02 +00001115var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1116
Ivan Lozanoffee3342019-08-27 12:03:00 -07001117var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001118 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1119 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001120 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001121 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001122 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001123 dataLibDepTag = dependencyTag{name: "data lib"}
1124 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001125)
1126
Jiyong Park99644e92020-11-17 22:21:02 +09001127func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1128 tag, ok := depTag.(dependencyTag)
1129 return ok && tag == dylibDepTag
1130}
1131
Jiyong Park94e22fd2021-04-08 18:19:15 +09001132func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1133 tag, ok := depTag.(dependencyTag)
1134 return ok && tag == rlibDepTag
1135}
1136
Matthew Maurer0f003b12020-06-29 14:34:06 -07001137type autoDep struct {
1138 variation string
1139 depTag dependencyTag
1140}
1141
1142var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001143 sourceVariation = "source"
1144 rlibVariation = "rlib"
1145 dylibVariation = "dylib"
1146 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1147 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001148)
1149
1150type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001151 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001152}
1153
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001154func (mod *Module) begin(ctx BaseModuleContext) {
1155 if mod.coverage != nil {
1156 mod.coverage.begin(ctx)
1157 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001158 if mod.sanitize != nil {
1159 mod.sanitize.begin(ctx)
1160 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001161}
1162
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001163func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001164 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001165 return p.prebuilt()
1166 }
1167 return nil
1168}
1169
Kiyoung Kim37693d02024-04-04 09:56:15 +09001170func (mod *Module) Symlinks() []string {
1171 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1172 return nil
1173}
1174
Justin Yun24b246a2023-03-16 10:36:16 +09001175func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1176 if rustDep, ok := dep.(*Module); ok {
1177 // Use base module name for snapshots when exporting to Makefile.
1178 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1179 baseName := rustDep.BaseModuleName()
1180 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1181 }
1182 }
1183 return cc.MakeLibName(ctx, c, dep, depName)
1184}
1185
Ivan Lozanod106efe2023-09-21 23:30:26 -04001186func collectIncludedProtos(mod *Module, dep *Module) {
1187 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1188 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1189 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1190 }
1191 }
1192}
Andrew Walbran52533232024-03-19 11:36:04 +00001193
Ivan Lozanoffee3342019-08-27 12:03:00 -07001194func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1195 var depPaths PathDeps
1196
1197 directRlibDeps := []*Module{}
1198 directDylibDeps := []*Module{}
1199 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001200 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001201 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001202 directSrcProvidersDeps := []*Module{}
1203 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001204
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001205 // For the dependency from platform to apex, use the latest stubs
1206 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001207 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001208 if !apexInfo.IsForPlatform() {
1209 mod.apexSdkVersion = apexInfo.MinSdkVersion
1210 }
1211
1212 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1213 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1214 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1215 // (b/144430859)
1216 mod.apexSdkVersion = android.FutureApiLevel
1217 }
1218
Spandan Das604f3762023-03-16 22:51:40 +00001219 skipModuleList := map[string]bool{}
1220
Colin Crossa14fb6a2024-10-23 16:57:06 -07001221 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001222 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001223
Ivan Lozanoffee3342019-08-27 12:03:00 -07001224 ctx.VisitDirectDeps(func(dep android.Module) {
1225 depName := ctx.OtherModuleName(dep)
1226 depTag := ctx.OtherModuleDependencyTag(dep)
Spandan Das604f3762023-03-16 22:51:40 +00001227 if _, exists := skipModuleList[depName]; exists {
1228 return
1229 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001230
1231 if depTag == android.DarwinUniversalVariantTag {
1232 return
1233 }
1234
Ivan Lozano0a468a42024-05-13 21:03:34 -04001235 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001236 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001237 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001238
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001239 switch {
1240 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001241 dylib, ok := rustDep.compiler.(libraryInterface)
1242 if !ok || !dylib.dylib() {
1243 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1244 return
1245 }
1246 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001247 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001248 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1249
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001250 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001251 rlib, ok := rustDep.compiler.(libraryInterface)
1252 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001253 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001254 return
1255 }
1256 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001257 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001258 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1259
Ivan Lozano0a468a42024-05-13 21:03:34 -04001260 // rust_ffi rlibs may export include dirs, so collect those here.
1261 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1262 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1263 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1264
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001265 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001266 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001267 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001268 // proc_macro link dirs need to be exported, so collect those here.
1269 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001270
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001271 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001272 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1273 collectIncludedProtos(mod, rustDep)
1274 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001275 case cc.IsStaticDepTag(depTag):
1276 // Rust FFI rlibs should not be declared in a Rust modules
1277 // "static_libs" list as we can't handle them properly at the
1278 // moment (for example, they only produce an rlib-std variant).
1279 // Instead, a normal rust_library variant should be used.
1280 ctx.PropertyErrorf("static_libs",
1281 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1282 depName)
1283
Paul Duffind5cf92e2021-07-09 17:38:55 +01001284 }
1285
Cole Faustb6e6f992023-08-17 17:42:26 -07001286 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1287
Paul Duffind5cf92e2021-07-09 17:38:55 +01001288 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001289 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1290 // OS/Arch variant is used.
1291 var helper string
1292 if ctx.Host() {
1293 helper = "missing 'host_supported'?"
1294 } else {
1295 helper = "device module defined?"
1296 }
1297
1298 if dep.Target().Os != ctx.Os() {
1299 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1300 return
1301 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1302 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1303 return
1304 }
1305 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001306 }
1307
Ivan Lozano0a468a42024-05-13 21:03:34 -04001308 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001309 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001310 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001311 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1312 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001313 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001314 }
1315
Ivan Lozanoffee3342019-08-27 12:03:00 -07001316 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001317 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001318 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001319 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001320 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001321 }
1322 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001323
Ivan Lozanod106efe2023-09-21 23:30:26 -04001324 if depTag == sourceDepTag {
1325 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1326 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001327 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001328 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1329 }
1330 }
1331 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001332 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001333 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001334 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001335 if _, ok := ccDep.(*Module); !ok {
1336 if ccDep.Module().Target().Os != ctx.Os() {
1337 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1338 return
1339 }
1340 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1341 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1342 return
1343 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001344 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001345 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001346 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001347 if !ctx.Config().AllowMissingDependencies() {
1348 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1349 } else {
1350 ctx.AddMissingDependencies([]string{depName})
1351 }
1352 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001353 }
1354
Wen-yi Chu41326c12023-09-22 03:58:59 +00001355 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001356
Ivan Lozanoffee3342019-08-27 12:03:00 -07001357 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001358 switch {
1359 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001360 if cc.IsWholeStaticLib(depTag) {
1361 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1362 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001363 if mod.Binary() {
1364 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1365 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1366 // final linkage, pass the args directly to the linker to handle these cases.
1367 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1368 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001369 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001370 } else {
1371 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 -05001372 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001373 }
1374
1375 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1376 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001377 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001378 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1379
Colin Cross313aa542023-12-13 13:47:44 -08001380 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001381 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1382 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1383 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1384 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001385 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001386
1387 // Record baseLibName for snapshots.
1388 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1389
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001390 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001391 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001392 // For the shared lib dependencies, we may link to the stub variant
1393 // of the dependency depending on the context (e.g. if this
1394 // dependency crosses the APEX boundaries).
1395 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1396
1397 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1398 // object (either from stub or non-stub) to use.
1399 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001400 if !linkObject.Valid() {
1401 if !ctx.Config().AllowMissingDependencies() {
1402 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1403 } else {
1404 ctx.AddMissingDependencies([]string{depName})
1405 }
1406 return
1407 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001408 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001409
Ivan Lozanoffee3342019-08-27 12:03:00 -07001410 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001411 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001412 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1413 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1414 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1415 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001416 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001417
1418 // Record baseLibName for snapshots.
1419 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1420
Cole Faustb6e6f992023-08-17 17:42:26 -07001421 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001422 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001423 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001424 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001425 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1426 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1427 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001428 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001429 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001430 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001431 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001432 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001433 }
1434
1435 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001436 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1437 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001438 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001439 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001440 } else {
1441 switch {
1442 case depTag == cc.CrtBeginDepTag:
1443 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1444 case depTag == cc.CrtEndDepTag:
1445 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1446 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001448
1449 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001450 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001451 // These are usually genrules which don't have per-target variants.
1452 directSrcDeps = append(directSrcDeps, srcDep)
1453 }
1454 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 })
1456
Colin Crossa14fb6a2024-10-23 16:57:06 -07001457 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001458
1459 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001460 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001461 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001462 crateName := dep.CrateName()
1463 if alias, aliased := aliases[crateName]; aliased {
1464 crateName = alias
1465 }
1466 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001467 }
1468 var dylibDepFiles RustLibraries
1469 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001470 crateName := dep.CrateName()
1471 if alias, aliased := aliases[crateName]; aliased {
1472 crateName = alias
1473 }
1474 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001475 }
1476 var procMacroDepFiles RustLibraries
1477 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001478 crateName := dep.CrateName()
1479 if alias, aliased := aliases[crateName]; aliased {
1480 crateName = alias
1481 }
1482 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001483 }
1484
Colin Cross004bd3f2023-10-02 11:39:17 -07001485 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001486 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001487 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001488 }
1489
Colin Cross004bd3f2023-10-02 11:39:17 -07001490 var sharedLibFiles android.Paths
1491 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001492 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001493 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001494 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001495 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001496 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001497 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001498 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001499 }
1500
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001501 var srcProviderDepFiles android.Paths
1502 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001503 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001504 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1505 }
1506 for _, dep := range directSrcDeps {
1507 srcs := dep.Srcs()
1508 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1509 }
1510
Wen-yi Chu41326c12023-09-22 03:58:59 +00001511 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1512 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001513 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1514 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1515 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001516 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001517 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001518
1519 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001520 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001521 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001522 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001523 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1524 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1525 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001526
1527 return depPaths
1528}
1529
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001530func (mod *Module) InstallInData() bool {
1531 if mod.compiler == nil {
1532 return false
1533 }
1534 return mod.compiler.inData()
1535}
1536
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001537func (mod *Module) InstallInRamdisk() bool {
1538 return mod.InRamdisk()
1539}
1540
1541func (mod *Module) InstallInVendorRamdisk() bool {
1542 return mod.InVendorRamdisk()
1543}
1544
1545func (mod *Module) InstallInRecovery() bool {
1546 return mod.InRecovery()
1547}
1548
Wen-yi Chu41326c12023-09-22 03:58:59 +00001549func linkPathFromFilePath(filepath android.Path) string {
1550 return strings.Split(filepath.String(), filepath.Base())[0]
1551}
1552
Spandan Das604f3762023-03-16 22:51:40 +00001553// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1554func (r *Module) usePublicApi() bool {
1555 return r.Device() && r.UseSdk()
1556}
1557
1558// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1559func (r *Module) useVendorApi() bool {
1560 return r.Device() && (r.InVendor() || r.InProduct())
1561}
1562
Ivan Lozanoffee3342019-08-27 12:03:00 -07001563func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1564 ctx := &depsContext{
1565 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001566 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001567
1568 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001569 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001570
1571 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001572 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001573 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001574
Ivan Lozano2b081132020-09-08 12:46:52 -04001575 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001576 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001577 stdLinkage = "rlib-std"
1578 }
1579
1580 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001581
Ivan Lozano2b081132020-09-08 12:46:52 -04001582 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1583 rlibDepVariations = append(rlibDepVariations,
1584 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1585 }
1586
Ivan Lozano1921e802021-05-20 13:39:16 -04001587 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001588 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001589 for _, lib := range deps.Rlibs {
1590 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001591 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001592 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001593
1594 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001595 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001596
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001597 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001598 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001599 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001600
Ivan Lozano1921e802021-05-20 13:39:16 -04001601 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001602 if deps.Rustlibs != nil {
1603 if !mod.compiler.Disabled() {
1604 for _, lib := range deps.Rustlibs {
1605 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1606 if autoDep.depTag == rlibDepTag {
1607 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001608 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1609
Ivan Lozanod106efe2023-09-21 23:30:26 -04001610 } else {
1611 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1612 // Check for the existence of the dylib deptag variant. Select it if available,
1613 // otherwise select the rlib variant.
1614 autoDepVariations := append(commonDepVariations,
1615 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001616 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1617 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001618
Ivan Lozanod106efe2023-09-21 23:30:26 -04001619 } else {
1620 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001621 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1622
Ivan Lozanod106efe2023-09-21 23:30:26 -04001623 }
1624 }
1625 }
1626 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1627 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001628 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001629 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001630
Ivan Lozano0a468a42024-05-13 21:03:34 -04001631 // Only add rustlib dependencies if they're source providers themselves.
1632 // This is used to track which crate names need to be added to the source generated
1633 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001634 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001635 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001636 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001637 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001638 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001639 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001640
Ivan Lozano1921e802021-05-20 13:39:16 -04001641 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001642 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001643 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001644 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001645 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001646 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001647 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001648 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001649 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001650 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1651
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001652 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001653 }
1654 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001655
1656 for _, lib := range deps.SharedLibs {
1657 depTag := cc.SharedDepTag()
1658 name, version := cc.StubsLibNameAndVersion(lib)
1659
1660 variations := []blueprint.Variation{
1661 {Mutator: "link", Variation: "shared"},
1662 }
Spandan Dasff665182024-09-11 18:48:44 +00001663 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001664 }
1665
1666 for _, lib := range deps.WholeStaticLibs {
1667 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001668
1669 actx.AddVariationDependencies([]blueprint.Variation{
1670 {Mutator: "link", Variation: "static"},
1671 }, depTag, lib)
1672 }
1673
1674 for _, lib := range deps.StaticLibs {
1675 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001676
1677 actx.AddVariationDependencies([]blueprint.Variation{
1678 {Mutator: "link", Variation: "static"},
1679 }, depTag, lib)
1680 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001681
Zach Johnson3df4e632020-11-06 11:56:27 -08001682 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1683
Colin Cross565cafd2020-09-25 18:47:38 -07001684 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001685 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001686 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001687 }
Colin Crossfe605e12022-01-23 20:46:16 -08001688 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001689 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001690 }
1691
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001692 if mod.sourceProvider != nil {
1693 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1694 bindgen.Properties.Custom_bindgen != "" {
1695 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1696 bindgen.Properties.Custom_bindgen)
1697 }
1698 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001699
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001700 actx.AddVariationDependencies([]blueprint.Variation{
1701 {Mutator: "link", Variation: "shared"},
1702 }, dataLibDepTag, deps.DataLibs...)
1703
1704 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1705
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001706 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001707 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001708
1709 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001710}
1711
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001712func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001713 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001714 mod.beginMutator(ctx)
1715 }
1716}
1717
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001718func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1719 ctx := &baseModuleContext{
1720 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001721 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001722
1723 mod.begin(ctx)
1724}
1725
Ivan Lozanoffee3342019-08-27 12:03:00 -07001726func (mod *Module) Name() string {
1727 name := mod.ModuleBase.Name()
1728 if p, ok := mod.compiler.(interface {
1729 Name(string) string
1730 }); ok {
1731 name = p.Name(name)
1732 }
1733 return name
1734}
1735
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001736func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001737 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001738 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001739 }
1740}
1741
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001742var _ android.HostToolProvider = (*Module)(nil)
1743
1744func (mod *Module) HostToolPath() android.OptionalPath {
1745 if !mod.Host() {
1746 return android.OptionalPath{}
1747 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001748 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1749 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001750 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1751 // Even though proc-macros aren't strictly "tools", since they target the compiler
1752 // and act as compiler plugins, we treat them similarly.
1753 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001754 }
1755 return android.OptionalPath{}
1756}
1757
Jiyong Park99644e92020-11-17 22:21:02 +09001758var _ android.ApexModule = (*Module)(nil)
1759
Ivan Lozano24cf0362024-10-04 16:02:38 +00001760// If a module is marked for exclusion from apexes, don't provide apex variants.
1761// TODO(b/362509506): remove this once stubs are properly supported by rust_ffi targets.
1762func (m *Module) CanHaveApexVariants() bool {
1763 if m.ApexExclude() {
1764 return false
1765 } else {
1766 return m.ApexModuleBase.CanHaveApexVariants()
1767 }
1768}
1769
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001770func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001771 return String(mod.Properties.Min_sdk_version)
1772}
1773
Jiyong Park45bf82e2020-12-15 22:29:02 +09001774// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001775func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001776 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001777 if minSdkVersion == "apex_inherit" {
1778 return nil
1779 }
1780 if minSdkVersion == "" {
1781 return fmt.Errorf("min_sdk_version is not specificed")
1782 }
1783
1784 // Not using nativeApiLevelFromUser because the context here is not
1785 // necessarily a native context.
1786 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1787 if err != nil {
1788 return err
1789 }
1790
1791 if ver.GreaterThan(sdkVersion) {
1792 return fmt.Errorf("newer SDK(%v)", ver)
1793 }
Jiyong Park99644e92020-11-17 22:21:02 +09001794 return nil
1795}
1796
Jiyong Park45bf82e2020-12-15 22:29:02 +09001797// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001798func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1799 depTag := ctx.OtherModuleDependencyTag(dep)
1800
1801 if ccm, ok := dep.(*cc.Module); ok {
1802 if ccm.HasStubsVariants() {
1803 if cc.IsSharedDepTag(depTag) {
1804 // dynamic dep to a stubs lib crosses APEX boundary
1805 return false
1806 }
1807 if cc.IsRuntimeDepTag(depTag) {
1808 // runtime dep to a stubs lib also crosses APEX boundary
1809 return false
1810 }
1811
1812 if cc.IsHeaderDepTag(depTag) {
1813 return false
1814 }
1815 }
1816 if mod.Static() && cc.IsSharedDepTag(depTag) {
1817 // shared_lib dependency from a static lib is considered as crossing
1818 // the APEX boundary because the dependency doesn't actually is
1819 // linked; the dependency is used only during the compilation phase.
1820 return false
1821 }
1822 }
1823
Matthew Maurer581b6d82022-09-29 16:46:25 -07001824 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001825 return false
1826 }
1827
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +00001828 if rustDep, ok := dep.(*Module); ok && rustDep.ApexExclude() {
1829 return false
1830 }
1831
Jiyong Park99644e92020-11-17 22:21:02 +09001832 return true
1833}
1834
1835// Overrides ApexModule.IsInstallabeToApex()
1836func (mod *Module) IsInstallableToApex() bool {
1837 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001838 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001839 return true
1840 }
1841 if _, ok := mod.compiler.(*binaryDecorator); ok {
1842 return true
1843 }
1844 }
1845 return false
1846}
1847
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001848// If a library file has a "lib" prefix, extract the library name without the prefix.
1849func libNameFromFilePath(filepath android.Path) (string, bool) {
1850 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1851 if strings.HasPrefix(libName, "lib") {
1852 libName = libName[3:]
1853 return libName, true
1854 }
1855 return "", false
1856}
1857
Sasha Smundaka76acba2022-04-18 20:12:56 -07001858func kytheExtractRustFactory() android.Singleton {
1859 return &kytheExtractRustSingleton{}
1860}
1861
1862type kytheExtractRustSingleton struct {
1863}
1864
1865func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1866 var xrefTargets android.Paths
1867 ctx.VisitAllModules(func(module android.Module) {
1868 if rustModule, ok := module.(xref); ok {
1869 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1870 }
1871 })
1872 if len(xrefTargets) > 0 {
1873 ctx.Phony("xref_rust", xrefTargets...)
1874 }
1875}
1876
Jihoon Kangf78a8902022-09-01 22:47:07 +00001877func (c *Module) Partition() string {
1878 return ""
1879}
1880
Ivan Lozanoffee3342019-08-27 12:03:00 -07001881var Bool = proptools.Bool
1882var BoolDefault = proptools.BoolDefault
1883var String = proptools.String
1884var StringPtr = proptools.StringPtr