blob: 5a973c4e7e3ef79375d2d26828f4b5268976a2ed [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"
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020030 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070031 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000032 "android/soong/multitree"
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{})
51 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
52}
53
54func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
55 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
56}
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
183 transitiveAndroidMkSharedLibs *android.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)
924 flags = mod.compiler.featureFlags(ctx, 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 Cross40213022023-12-13 15:19:49 -0800950 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400951 }
952
953 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100954 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700955 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400956 if ctx.Failed() {
957 return
958 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700959 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
960 if buildOutput.kytheFile != nil {
961 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
962 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400963 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900964
Dan Albert06feee92021-03-19 15:06:02 -0700965 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000966 if mod.docTimestampFile.Valid() {
967 ctx.CheckbuildFile(mod.docTimestampFile.Path())
968 }
Dan Albert06feee92021-03-19 15:06:02 -0700969
Colin Crossff694a82023-12-13 15:54:49 -0800970 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400971 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900972 // If the module has been specifically configure to not be installed then
973 // hide from make as otherwise it will break when running inside make as the
974 // output path to install will not be specified. Not all uninstallable
975 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400976 // side dependencies. In particular, proc-macros need to be captured in the
977 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900978 mod.HideFromMake()
979 } else if !mod.installable(apexInfo) {
980 mod.SkipInstall()
981 }
982
983 // Still call install though, the installs will be stored as PackageSpecs to allow
984 // using the outputs in a genrule.
985 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200986 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900987 if ctx.Failed() {
988 return
989 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400990 // Export your own directory as a linkDir
991 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
992
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400993 }
Chris Wailes74be7642021-07-22 16:20:28 -0700994
995 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700996 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000997 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800998 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000999 }
Wei Lia1aa2972024-06-21 13:08:51 -07001000
mrziwang0cbd3b02024-06-20 16:39:25 -07001001 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001002
1003 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001004}
1005
1006func (mod *Module) setOutputFiles(ctx ModuleContext) {
1007 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1008 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1009 } else if mod.OutputFile().Valid() {
1010 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1011 } else {
1012 ctx.SetOutputFiles(android.Paths{}, "")
1013 }
1014 if mod.compiler != nil {
1015 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1016 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017}
1018
Wei Lia1aa2972024-06-21 13:08:51 -07001019func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1020 // Dump metadata that can not be done in android/compliance-metadata.go
1021 metadataInfo := ctx.ComplianceMetadataInfo()
1022 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1023 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1024
1025 // Static libs
1026 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1027 staticDepNames := make([]string, 0, len(staticDeps))
1028 for _, dep := range staticDeps {
1029 staticDepNames = append(staticDepNames, dep.Name())
1030 }
1031 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1032 for _, dep := range ccStaticDeps {
1033 staticDepNames = append(staticDepNames, dep.Name())
1034 }
1035
1036 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1037 // C static libraries
1038 for _, dep := range deps.StaticLibs {
1039 staticDepPaths = append(staticDepPaths, dep.String())
1040 }
1041 // Rust static libraries
1042 for _, dep := range deps.RLibs {
1043 staticDepPaths = append(staticDepPaths, dep.Path.String())
1044 }
1045 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1046 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1047
1048 // C Whole static libs
1049 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1050 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1051 for _, dep := range ccStaticDeps {
1052 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1053 }
1054 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1055}
1056
Ivan Lozanoffee3342019-08-27 12:03:00 -07001057func (mod *Module) deps(ctx DepsContext) Deps {
1058 deps := Deps{}
1059
1060 if mod.compiler != nil {
1061 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001062 }
1063 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001064 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065 }
1066
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001067 if mod.coverage != nil {
1068 deps = mod.coverage.deps(ctx, deps)
1069 }
1070
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001071 if mod.sanitize != nil {
1072 deps = mod.sanitize.deps(ctx, deps)
1073 }
1074
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1076 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001077 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1079 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1080 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001081 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001082 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001083 return deps
1084
1085}
1086
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087type dependencyTag struct {
1088 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001089 name string
1090 library bool
1091 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001092 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001093}
1094
Jiyong Park65b62242020-11-25 12:44:59 +09001095// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1096// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1097func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001098 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001099}
1100
1101var _ android.InstallNeededDependencyTag = dependencyTag{}
1102
Colin Cross65cb3142021-12-10 23:05:02 +00001103func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1104 if d.library && d.dynamic {
1105 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1106 }
1107 return nil
1108}
1109
Yu Liuc8884602024-03-15 18:48:38 +00001110func (d dependencyTag) PropagateAconfigValidation() bool {
1111 return d == rlibDepTag || d == sourceDepTag
1112}
1113
1114var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1115
Colin Cross65cb3142021-12-10 23:05:02 +00001116var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1117
Ivan Lozanoffee3342019-08-27 12:03:00 -07001118var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001119 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1120 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001121 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001122 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001123 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001124 dataLibDepTag = dependencyTag{name: "data lib"}
1125 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001126)
1127
Jiyong Park99644e92020-11-17 22:21:02 +09001128func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1129 tag, ok := depTag.(dependencyTag)
1130 return ok && tag == dylibDepTag
1131}
1132
Jiyong Park94e22fd2021-04-08 18:19:15 +09001133func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1134 tag, ok := depTag.(dependencyTag)
1135 return ok && tag == rlibDepTag
1136}
1137
Matthew Maurer0f003b12020-06-29 14:34:06 -07001138type autoDep struct {
1139 variation string
1140 depTag dependencyTag
1141}
1142
1143var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001144 sourceVariation = "source"
1145 rlibVariation = "rlib"
1146 dylibVariation = "dylib"
1147 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1148 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001149)
1150
1151type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001152 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001153}
1154
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001155func (mod *Module) begin(ctx BaseModuleContext) {
1156 if mod.coverage != nil {
1157 mod.coverage.begin(ctx)
1158 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001159 if mod.sanitize != nil {
1160 mod.sanitize.begin(ctx)
1161 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001162}
1163
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001164func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001165 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001166 return p.prebuilt()
1167 }
1168 return nil
1169}
1170
Kiyoung Kim37693d02024-04-04 09:56:15 +09001171func (mod *Module) Symlinks() []string {
1172 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1173 return nil
1174}
1175
Justin Yun24b246a2023-03-16 10:36:16 +09001176func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1177 if rustDep, ok := dep.(*Module); ok {
1178 // Use base module name for snapshots when exporting to Makefile.
1179 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1180 baseName := rustDep.BaseModuleName()
1181 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1182 }
1183 }
1184 return cc.MakeLibName(ctx, c, dep, depName)
1185}
1186
Ivan Lozanod106efe2023-09-21 23:30:26 -04001187func collectIncludedProtos(mod *Module, dep *Module) {
1188 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1189 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1190 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1191 }
1192 }
1193}
Andrew Walbran52533232024-03-19 11:36:04 +00001194
Ivan Lozanoffee3342019-08-27 12:03:00 -07001195func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1196 var depPaths PathDeps
1197
1198 directRlibDeps := []*Module{}
1199 directDylibDeps := []*Module{}
1200 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001201 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001202 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001203 directSrcProvidersDeps := []*Module{}
1204 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001205
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001206 // For the dependency from platform to apex, use the latest stubs
1207 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001208 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001209 if !apexInfo.IsForPlatform() {
1210 mod.apexSdkVersion = apexInfo.MinSdkVersion
1211 }
1212
1213 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1214 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1215 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1216 // (b/144430859)
1217 mod.apexSdkVersion = android.FutureApiLevel
1218 }
1219
Spandan Das604f3762023-03-16 22:51:40 +00001220 skipModuleList := map[string]bool{}
1221
1222 var apiImportInfo multitree.ApiImportInfo
1223 hasApiImportInfo := false
1224
1225 ctx.VisitDirectDeps(func(dep android.Module) {
1226 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001227 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001228 hasApiImportInfo = true
1229 }
1230 })
1231
1232 if hasApiImportInfo {
1233 targetStubModuleList := map[string]string{}
1234 targetOrigModuleList := map[string]string{}
1235
1236 // Search for dependency which both original module and API imported library with APEX stub exists
1237 ctx.VisitDirectDeps(func(dep android.Module) {
1238 depName := ctx.OtherModuleName(dep)
1239 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1240 targetStubModuleList[apiLibrary] = depName
1241 }
1242 })
1243 ctx.VisitDirectDeps(func(dep android.Module) {
1244 depName := ctx.OtherModuleName(dep)
1245 if origLibrary, ok := targetStubModuleList[depName]; ok {
1246 targetOrigModuleList[origLibrary] = depName
1247 }
1248 })
1249
1250 // Decide which library should be used between original and API imported library
1251 ctx.VisitDirectDeps(func(dep android.Module) {
1252 depName := ctx.OtherModuleName(dep)
1253 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1254 if cc.ShouldUseStubForApex(ctx, dep) {
1255 skipModuleList[depName] = true
1256 } else {
1257 skipModuleList[apiLibrary] = true
1258 }
1259 }
1260 })
1261 }
1262
Cole Faustb6e6f992023-08-17 17:42:26 -07001263 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1264 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001265
Ivan Lozanoffee3342019-08-27 12:03:00 -07001266 ctx.VisitDirectDeps(func(dep android.Module) {
1267 depName := ctx.OtherModuleName(dep)
1268 depTag := ctx.OtherModuleDependencyTag(dep)
Spandan Das604f3762023-03-16 22:51:40 +00001269 if _, exists := skipModuleList[depName]; exists {
1270 return
1271 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001272
1273 if depTag == android.DarwinUniversalVariantTag {
1274 return
1275 }
1276
Ivan Lozano0a468a42024-05-13 21:03:34 -04001277 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001278 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001279 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001280
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001281 switch {
1282 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 dylib, ok := rustDep.compiler.(libraryInterface)
1284 if !ok || !dylib.dylib() {
1285 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1286 return
1287 }
1288 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001289 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001290 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1291
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001292 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001293 rlib, ok := rustDep.compiler.(libraryInterface)
1294 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001295 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001296 return
1297 }
1298 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001299 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001300 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1301
Ivan Lozano0a468a42024-05-13 21:03:34 -04001302 // rust_ffi rlibs may export include dirs, so collect those here.
1303 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1304 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1305 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1306
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001307 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001308 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001309 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001310 // proc_macro link dirs need to be exported, so collect those here.
1311 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001312
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001313 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001314 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1315 collectIncludedProtos(mod, rustDep)
1316 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001317 case cc.IsStaticDepTag(depTag):
1318 // Rust FFI rlibs should not be declared in a Rust modules
1319 // "static_libs" list as we can't handle them properly at the
1320 // moment (for example, they only produce an rlib-std variant).
1321 // Instead, a normal rust_library variant should be used.
1322 ctx.PropertyErrorf("static_libs",
1323 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1324 depName)
1325
Paul Duffind5cf92e2021-07-09 17:38:55 +01001326 }
1327
Cole Faustb6e6f992023-08-17 17:42:26 -07001328 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1329
Paul Duffind5cf92e2021-07-09 17:38:55 +01001330 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001331 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1332 // OS/Arch variant is used.
1333 var helper string
1334 if ctx.Host() {
1335 helper = "missing 'host_supported'?"
1336 } else {
1337 helper = "device module defined?"
1338 }
1339
1340 if dep.Target().Os != ctx.Os() {
1341 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1342 return
1343 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1344 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1345 return
1346 }
1347 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001348 }
1349
Ivan Lozano0a468a42024-05-13 21:03:34 -04001350 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001351 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001352 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001353 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1354 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001355 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001356 }
1357
Ivan Lozanoffee3342019-08-27 12:03:00 -07001358 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001359 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001360 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001361 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001362 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001363 }
1364 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001365
Ivan Lozanod106efe2023-09-21 23:30:26 -04001366 if depTag == sourceDepTag {
1367 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1368 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001369 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001370 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1371 }
1372 }
1373 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001374 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001375 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001376 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001377 if _, ok := ccDep.(*Module); !ok {
1378 if ccDep.Module().Target().Os != ctx.Os() {
1379 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1380 return
1381 }
1382 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1383 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1384 return
1385 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001386 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001387 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001388 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001389 if !ctx.Config().AllowMissingDependencies() {
1390 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1391 } else {
1392 ctx.AddMissingDependencies([]string{depName})
1393 }
1394 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001395 }
1396
Wen-yi Chu41326c12023-09-22 03:58:59 +00001397 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001398
Ivan Lozanoffee3342019-08-27 12:03:00 -07001399 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001400 switch {
1401 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001402 if cc.IsWholeStaticLib(depTag) {
1403 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1404 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001405 if mod.Binary() {
1406 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1407 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1408 // final linkage, pass the args directly to the linker to handle these cases.
1409 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1410 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001411 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001412 } else {
1413 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 -05001414 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001415 }
1416
1417 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1418 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001419 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001420 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1421
Colin Cross313aa542023-12-13 13:47:44 -08001422 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001423 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1424 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1425 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1426 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001427 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001428
1429 // Record baseLibName for snapshots.
1430 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1431
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001432 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001433 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001434 // For the shared lib dependencies, we may link to the stub variant
1435 // of the dependency depending on the context (e.g. if this
1436 // dependency crosses the APEX boundaries).
1437 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1438
1439 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1440 // object (either from stub or non-stub) to use.
1441 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001442 if !linkObject.Valid() {
1443 if !ctx.Config().AllowMissingDependencies() {
1444 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1445 } else {
1446 ctx.AddMissingDependencies([]string{depName})
1447 }
1448 return
1449 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001450 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001451
Ivan Lozanoffee3342019-08-27 12:03:00 -07001452 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001453 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001454 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1455 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1456 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1457 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001458 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001459
1460 // Record baseLibName for snapshots.
1461 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1462
Cole Faustb6e6f992023-08-17 17:42:26 -07001463 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001464 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001465 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001466 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001467 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1468 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1469 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001470 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001471 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001472 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001473 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001474 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001475 }
1476
1477 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001478 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1479 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001480 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001481 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001482 } else {
1483 switch {
1484 case depTag == cc.CrtBeginDepTag:
1485 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1486 case depTag == cc.CrtEndDepTag:
1487 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1488 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001489 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001490
1491 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001492 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001493 // These are usually genrules which don't have per-target variants.
1494 directSrcDeps = append(directSrcDeps, srcDep)
1495 }
1496 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001497 })
1498
Wen-yi Chu41326c12023-09-22 03:58:59 +00001499 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1500
1501 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001502 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001503 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001504 crateName := dep.CrateName()
1505 if alias, aliased := aliases[crateName]; aliased {
1506 crateName = alias
1507 }
1508 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001509 }
1510 var dylibDepFiles RustLibraries
1511 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001512 crateName := dep.CrateName()
1513 if alias, aliased := aliases[crateName]; aliased {
1514 crateName = alias
1515 }
1516 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001517 }
1518 var procMacroDepFiles RustLibraries
1519 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001520 crateName := dep.CrateName()
1521 if alias, aliased := aliases[crateName]; aliased {
1522 crateName = alias
1523 }
1524 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001525 }
1526
Colin Cross004bd3f2023-10-02 11:39:17 -07001527 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001528 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001529 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001530 }
1531
Colin Cross004bd3f2023-10-02 11:39:17 -07001532 var sharedLibFiles android.Paths
1533 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001534 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001535 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001536 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001537 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001538 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001539 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001540 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001541 }
1542
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001543 var srcProviderDepFiles android.Paths
1544 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001545 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001546 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1547 }
1548 for _, dep := range directSrcDeps {
1549 srcs := dep.Srcs()
1550 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1551 }
1552
Wen-yi Chu41326c12023-09-22 03:58:59 +00001553 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1554 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001555 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1556 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1557 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001558 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001559 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001560
1561 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001562 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001563 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001564 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001565 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1566 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1567 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001568
1569 return depPaths
1570}
1571
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001572func (mod *Module) InstallInData() bool {
1573 if mod.compiler == nil {
1574 return false
1575 }
1576 return mod.compiler.inData()
1577}
1578
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001579func (mod *Module) InstallInRamdisk() bool {
1580 return mod.InRamdisk()
1581}
1582
1583func (mod *Module) InstallInVendorRamdisk() bool {
1584 return mod.InVendorRamdisk()
1585}
1586
1587func (mod *Module) InstallInRecovery() bool {
1588 return mod.InRecovery()
1589}
1590
Wen-yi Chu41326c12023-09-22 03:58:59 +00001591func linkPathFromFilePath(filepath android.Path) string {
1592 return strings.Split(filepath.String(), filepath.Base())[0]
1593}
1594
Spandan Das604f3762023-03-16 22:51:40 +00001595// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1596func (r *Module) usePublicApi() bool {
1597 return r.Device() && r.UseSdk()
1598}
1599
1600// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1601func (r *Module) useVendorApi() bool {
1602 return r.Device() && (r.InVendor() || r.InProduct())
1603}
1604
Ivan Lozanoffee3342019-08-27 12:03:00 -07001605func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1606 ctx := &depsContext{
1607 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001608 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001609
1610 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001611 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001612
Kiyoung Kim487689e2022-07-26 09:48:22 +09001613 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001614 if mod.usePublicApi() || mod.useVendorApi() {
1615 for idx, lib := range deps.SharedLibs {
1616 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1617 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001618 }
1619
Ivan Lozano1921e802021-05-20 13:39:16 -04001620 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001621 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001622 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001623
Ivan Lozano2b081132020-09-08 12:46:52 -04001624 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001625 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001626 stdLinkage = "rlib-std"
1627 }
1628
1629 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001630
Ivan Lozano2b081132020-09-08 12:46:52 -04001631 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1632 rlibDepVariations = append(rlibDepVariations,
1633 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1634 }
1635
Ivan Lozano1921e802021-05-20 13:39:16 -04001636 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001637 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001638 for _, lib := range deps.Rlibs {
1639 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001640 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001641 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001642
1643 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001644 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001645
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001646 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001647 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001648 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001649
Ivan Lozano1921e802021-05-20 13:39:16 -04001650 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001651 if deps.Rustlibs != nil {
1652 if !mod.compiler.Disabled() {
1653 for _, lib := range deps.Rustlibs {
1654 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1655 if autoDep.depTag == rlibDepTag {
1656 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001657 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1658
Ivan Lozanod106efe2023-09-21 23:30:26 -04001659 } else {
1660 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1661 // Check for the existence of the dylib deptag variant. Select it if available,
1662 // otherwise select the rlib variant.
1663 autoDepVariations := append(commonDepVariations,
1664 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001665 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1666 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001667
Ivan Lozanod106efe2023-09-21 23:30:26 -04001668 } else {
1669 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001670 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1671
Ivan Lozanod106efe2023-09-21 23:30:26 -04001672 }
1673 }
1674 }
1675 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1676 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001677 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001678 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001679
Ivan Lozano0a468a42024-05-13 21:03:34 -04001680 // Only add rustlib dependencies if they're source providers themselves.
1681 // This is used to track which crate names need to be added to the source generated
1682 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001683 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001684 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001685 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001686 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001687 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001688 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001689
Ivan Lozano1921e802021-05-20 13:39:16 -04001690 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001691 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001692 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001693 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001694 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001695 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001696 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001697 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001698 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001699 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1700
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001701 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001702 }
1703 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001704
1705 for _, lib := range deps.SharedLibs {
1706 depTag := cc.SharedDepTag()
1707 name, version := cc.StubsLibNameAndVersion(lib)
1708
1709 variations := []blueprint.Variation{
1710 {Mutator: "link", Variation: "shared"},
1711 }
Spandan Das604f3762023-03-16 22:51:40 +00001712 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1713 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1714 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1715 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1716 }
1717
1718 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1719 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1720 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001721 }
1722
1723 for _, lib := range deps.WholeStaticLibs {
1724 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001725
1726 actx.AddVariationDependencies([]blueprint.Variation{
1727 {Mutator: "link", Variation: "static"},
1728 }, depTag, lib)
1729 }
1730
1731 for _, lib := range deps.StaticLibs {
1732 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001733
1734 actx.AddVariationDependencies([]blueprint.Variation{
1735 {Mutator: "link", Variation: "static"},
1736 }, depTag, lib)
1737 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001738
Zach Johnson3df4e632020-11-06 11:56:27 -08001739 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1740
Colin Cross565cafd2020-09-25 18:47:38 -07001741 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001742 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001743 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001744 }
Colin Crossfe605e12022-01-23 20:46:16 -08001745 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001746 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001747 }
1748
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001749 if mod.sourceProvider != nil {
1750 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1751 bindgen.Properties.Custom_bindgen != "" {
1752 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1753 bindgen.Properties.Custom_bindgen)
1754 }
1755 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001756
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001757 actx.AddVariationDependencies([]blueprint.Variation{
1758 {Mutator: "link", Variation: "shared"},
1759 }, dataLibDepTag, deps.DataLibs...)
1760
1761 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1762
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001763 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001764 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001765
1766 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001767}
1768
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001769func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001770 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001771 mod.beginMutator(ctx)
1772 }
1773}
1774
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001775func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1776 ctx := &baseModuleContext{
1777 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001778 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001779
1780 mod.begin(ctx)
1781}
1782
Ivan Lozanoffee3342019-08-27 12:03:00 -07001783func (mod *Module) Name() string {
1784 name := mod.ModuleBase.Name()
1785 if p, ok := mod.compiler.(interface {
1786 Name(string) string
1787 }); ok {
1788 name = p.Name(name)
1789 }
1790 return name
1791}
1792
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001793func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001794 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001795 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001796 }
1797}
1798
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001799var _ android.HostToolProvider = (*Module)(nil)
1800
1801func (mod *Module) HostToolPath() android.OptionalPath {
1802 if !mod.Host() {
1803 return android.OptionalPath{}
1804 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001805 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1806 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001807 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1808 // Even though proc-macros aren't strictly "tools", since they target the compiler
1809 // and act as compiler plugins, we treat them similarly.
1810 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001811 }
1812 return android.OptionalPath{}
1813}
1814
Jiyong Park99644e92020-11-17 22:21:02 +09001815var _ android.ApexModule = (*Module)(nil)
1816
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001817func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001818 return String(mod.Properties.Min_sdk_version)
1819}
1820
Jiyong Park45bf82e2020-12-15 22:29:02 +09001821// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001822func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001823 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001824 if minSdkVersion == "apex_inherit" {
1825 return nil
1826 }
1827 if minSdkVersion == "" {
1828 return fmt.Errorf("min_sdk_version is not specificed")
1829 }
1830
1831 // Not using nativeApiLevelFromUser because the context here is not
1832 // necessarily a native context.
1833 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1834 if err != nil {
1835 return err
1836 }
1837
1838 if ver.GreaterThan(sdkVersion) {
1839 return fmt.Errorf("newer SDK(%v)", ver)
1840 }
Jiyong Park99644e92020-11-17 22:21:02 +09001841 return nil
1842}
1843
Jiyong Park45bf82e2020-12-15 22:29:02 +09001844// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001845func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1846 depTag := ctx.OtherModuleDependencyTag(dep)
1847
1848 if ccm, ok := dep.(*cc.Module); ok {
1849 if ccm.HasStubsVariants() {
1850 if cc.IsSharedDepTag(depTag) {
1851 // dynamic dep to a stubs lib crosses APEX boundary
1852 return false
1853 }
1854 if cc.IsRuntimeDepTag(depTag) {
1855 // runtime dep to a stubs lib also crosses APEX boundary
1856 return false
1857 }
1858
1859 if cc.IsHeaderDepTag(depTag) {
1860 return false
1861 }
1862 }
1863 if mod.Static() && cc.IsSharedDepTag(depTag) {
1864 // shared_lib dependency from a static lib is considered as crossing
1865 // the APEX boundary because the dependency doesn't actually is
1866 // linked; the dependency is used only during the compilation phase.
1867 return false
1868 }
1869 }
1870
Matthew Maurer581b6d82022-09-29 16:46:25 -07001871 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001872 return false
1873 }
1874
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +00001875 if rustDep, ok := dep.(*Module); ok && rustDep.ApexExclude() {
1876 return false
1877 }
1878
Jiyong Park99644e92020-11-17 22:21:02 +09001879 return true
1880}
1881
1882// Overrides ApexModule.IsInstallabeToApex()
1883func (mod *Module) IsInstallableToApex() bool {
1884 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001885 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001886 return true
1887 }
1888 if _, ok := mod.compiler.(*binaryDecorator); ok {
1889 return true
1890 }
1891 }
1892 return false
1893}
1894
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001895// If a library file has a "lib" prefix, extract the library name without the prefix.
1896func libNameFromFilePath(filepath android.Path) (string, bool) {
1897 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1898 if strings.HasPrefix(libName, "lib") {
1899 libName = libName[3:]
1900 return libName, true
1901 }
1902 return "", false
1903}
1904
Sasha Smundaka76acba2022-04-18 20:12:56 -07001905func kytheExtractRustFactory() android.Singleton {
1906 return &kytheExtractRustSingleton{}
1907}
1908
1909type kytheExtractRustSingleton struct {
1910}
1911
1912func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1913 var xrefTargets android.Paths
1914 ctx.VisitAllModules(func(module android.Module) {
1915 if rustModule, ok := module.(xref); ok {
1916 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1917 }
1918 })
1919 if len(xrefTargets) > 0 {
1920 ctx.Phony("xref_rust", xrefTargets...)
1921 }
1922}
1923
Jihoon Kangf78a8902022-09-01 22:47:07 +00001924func (c *Module) Partition() string {
1925 return ""
1926}
1927
Ivan Lozanoffee3342019-08-27 12:03:00 -07001928var Bool = proptools.Bool
1929var BoolDefault = proptools.BoolDefault
1930var String = proptools.String
1931var StringPtr = proptools.StringPtr