blob: 240c2217876780edb3b8c5ce5d126e71764eaefc [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 Crossa6182ab2024-08-21 10:47:44 -0700950 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Colin Cross40213022023-12-13 15:19:49 -0800951 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400952 }
953
954 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100955 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700956 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400957 if ctx.Failed() {
958 return
959 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700960 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -0700961 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700962 if buildOutput.kytheFile != nil {
963 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
964 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400965 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900966
Dan Albert06feee92021-03-19 15:06:02 -0700967 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
968
Colin Crossff694a82023-12-13 15:54:49 -0800969 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400970 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900971 // If the module has been specifically configure to not be installed then
972 // hide from make as otherwise it will break when running inside make as the
973 // output path to install will not be specified. Not all uninstallable
974 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400975 // side dependencies. In particular, proc-macros need to be captured in the
976 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900977 mod.HideFromMake()
978 } else if !mod.installable(apexInfo) {
979 mod.SkipInstall()
980 }
981
982 // Still call install though, the installs will be stored as PackageSpecs to allow
983 // using the outputs in a genrule.
984 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200985 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900986 if ctx.Failed() {
987 return
988 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400989 // Export your own directory as a linkDir
990 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
991
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400992 }
Chris Wailes74be7642021-07-22 16:20:28 -0700993
994 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700995 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000996 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800997 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000998 }
Wei Lia1aa2972024-06-21 13:08:51 -0700999
mrziwang0cbd3b02024-06-20 16:39:25 -07001000 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001001
1002 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001003}
1004
1005func (mod *Module) setOutputFiles(ctx ModuleContext) {
1006 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1007 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1008 } else if mod.OutputFile().Valid() {
1009 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1010 } else {
1011 ctx.SetOutputFiles(android.Paths{}, "")
1012 }
1013 if mod.compiler != nil {
1014 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1015 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001016}
1017
Wei Lia1aa2972024-06-21 13:08:51 -07001018func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1019 // Dump metadata that can not be done in android/compliance-metadata.go
1020 metadataInfo := ctx.ComplianceMetadataInfo()
1021 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1022 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1023
1024 // Static libs
1025 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1026 staticDepNames := make([]string, 0, len(staticDeps))
1027 for _, dep := range staticDeps {
1028 staticDepNames = append(staticDepNames, dep.Name())
1029 }
1030 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1031 for _, dep := range ccStaticDeps {
1032 staticDepNames = append(staticDepNames, dep.Name())
1033 }
1034
1035 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1036 // C static libraries
1037 for _, dep := range deps.StaticLibs {
1038 staticDepPaths = append(staticDepPaths, dep.String())
1039 }
1040 // Rust static libraries
1041 for _, dep := range deps.RLibs {
1042 staticDepPaths = append(staticDepPaths, dep.Path.String())
1043 }
1044 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1045 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1046
1047 // C Whole static libs
1048 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1049 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1050 for _, dep := range ccStaticDeps {
1051 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1052 }
1053 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1054}
1055
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056func (mod *Module) deps(ctx DepsContext) Deps {
1057 deps := Deps{}
1058
1059 if mod.compiler != nil {
1060 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001061 }
1062 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001063 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001064 }
1065
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001066 if mod.coverage != nil {
1067 deps = mod.coverage.deps(ctx, deps)
1068 }
1069
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001070 if mod.sanitize != nil {
1071 deps = mod.sanitize.deps(ctx, deps)
1072 }
1073
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1075 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001076 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001077 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1078 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1079 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001080 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001081 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001082 return deps
1083
1084}
1085
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086type dependencyTag struct {
1087 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001088 name string
1089 library bool
1090 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001091 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001092}
1093
Jiyong Park65b62242020-11-25 12:44:59 +09001094// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1095// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1096func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001097 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001098}
1099
1100var _ android.InstallNeededDependencyTag = dependencyTag{}
1101
Colin Cross65cb3142021-12-10 23:05:02 +00001102func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1103 if d.library && d.dynamic {
1104 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1105 }
1106 return nil
1107}
1108
Yu Liuc8884602024-03-15 18:48:38 +00001109func (d dependencyTag) PropagateAconfigValidation() bool {
1110 return d == rlibDepTag || d == sourceDepTag
1111}
1112
1113var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1114
Colin Cross65cb3142021-12-10 23:05:02 +00001115var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1116
Ivan Lozanoffee3342019-08-27 12:03:00 -07001117var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001118 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1119 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001120 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001121 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001122 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001123 dataLibDepTag = dependencyTag{name: "data lib"}
1124 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001125)
1126
Jiyong Park99644e92020-11-17 22:21:02 +09001127func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1128 tag, ok := depTag.(dependencyTag)
1129 return ok && tag == dylibDepTag
1130}
1131
Jiyong Park94e22fd2021-04-08 18:19:15 +09001132func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1133 tag, ok := depTag.(dependencyTag)
1134 return ok && tag == rlibDepTag
1135}
1136
Matthew Maurer0f003b12020-06-29 14:34:06 -07001137type autoDep struct {
1138 variation string
1139 depTag dependencyTag
1140}
1141
1142var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001143 sourceVariation = "source"
1144 rlibVariation = "rlib"
1145 dylibVariation = "dylib"
1146 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1147 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001148)
1149
1150type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001151 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001152}
1153
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001154func (mod *Module) begin(ctx BaseModuleContext) {
1155 if mod.coverage != nil {
1156 mod.coverage.begin(ctx)
1157 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001158 if mod.sanitize != nil {
1159 mod.sanitize.begin(ctx)
1160 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001161}
1162
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001163func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001164 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001165 return p.prebuilt()
1166 }
1167 return nil
1168}
1169
Kiyoung Kim37693d02024-04-04 09:56:15 +09001170func (mod *Module) Symlinks() []string {
1171 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1172 return nil
1173}
1174
Justin Yun24b246a2023-03-16 10:36:16 +09001175func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1176 if rustDep, ok := dep.(*Module); ok {
1177 // Use base module name for snapshots when exporting to Makefile.
1178 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1179 baseName := rustDep.BaseModuleName()
1180 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1181 }
1182 }
1183 return cc.MakeLibName(ctx, c, dep, depName)
1184}
1185
Ivan Lozanod106efe2023-09-21 23:30:26 -04001186func collectIncludedProtos(mod *Module, dep *Module) {
1187 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1188 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1189 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1190 }
1191 }
1192}
Andrew Walbran52533232024-03-19 11:36:04 +00001193
Ivan Lozanoffee3342019-08-27 12:03:00 -07001194func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1195 var depPaths PathDeps
1196
1197 directRlibDeps := []*Module{}
1198 directDylibDeps := []*Module{}
1199 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001200 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001201 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001202 directSrcProvidersDeps := []*Module{}
1203 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001204
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001205 // For the dependency from platform to apex, use the latest stubs
1206 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001207 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001208 if !apexInfo.IsForPlatform() {
1209 mod.apexSdkVersion = apexInfo.MinSdkVersion
1210 }
1211
1212 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1213 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1214 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1215 // (b/144430859)
1216 mod.apexSdkVersion = android.FutureApiLevel
1217 }
1218
Spandan Das604f3762023-03-16 22:51:40 +00001219 skipModuleList := map[string]bool{}
1220
1221 var apiImportInfo multitree.ApiImportInfo
1222 hasApiImportInfo := false
1223
1224 ctx.VisitDirectDeps(func(dep android.Module) {
1225 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001226 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001227 hasApiImportInfo = true
1228 }
1229 })
1230
1231 if hasApiImportInfo {
1232 targetStubModuleList := map[string]string{}
1233 targetOrigModuleList := map[string]string{}
1234
1235 // Search for dependency which both original module and API imported library with APEX stub exists
1236 ctx.VisitDirectDeps(func(dep android.Module) {
1237 depName := ctx.OtherModuleName(dep)
1238 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1239 targetStubModuleList[apiLibrary] = depName
1240 }
1241 })
1242 ctx.VisitDirectDeps(func(dep android.Module) {
1243 depName := ctx.OtherModuleName(dep)
1244 if origLibrary, ok := targetStubModuleList[depName]; ok {
1245 targetOrigModuleList[origLibrary] = depName
1246 }
1247 })
1248
1249 // Decide which library should be used between original and API imported library
1250 ctx.VisitDirectDeps(func(dep android.Module) {
1251 depName := ctx.OtherModuleName(dep)
1252 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1253 if cc.ShouldUseStubForApex(ctx, dep) {
1254 skipModuleList[depName] = true
1255 } else {
1256 skipModuleList[apiLibrary] = true
1257 }
1258 }
1259 })
1260 }
1261
Cole Faustb6e6f992023-08-17 17:42:26 -07001262 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1263 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001264
Ivan Lozanoffee3342019-08-27 12:03:00 -07001265 ctx.VisitDirectDeps(func(dep android.Module) {
1266 depName := ctx.OtherModuleName(dep)
1267 depTag := ctx.OtherModuleDependencyTag(dep)
Spandan Das604f3762023-03-16 22:51:40 +00001268 if _, exists := skipModuleList[depName]; exists {
1269 return
1270 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001271
1272 if depTag == android.DarwinUniversalVariantTag {
1273 return
1274 }
1275
Ivan Lozano0a468a42024-05-13 21:03:34 -04001276 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001277 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001278 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001279
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001280 switch {
1281 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001282 dylib, ok := rustDep.compiler.(libraryInterface)
1283 if !ok || !dylib.dylib() {
1284 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1285 return
1286 }
1287 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001288 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001289 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1290
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001291 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 rlib, ok := rustDep.compiler.(libraryInterface)
1293 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001294 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001295 return
1296 }
1297 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001298 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001299 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1300
Ivan Lozano0a468a42024-05-13 21:03:34 -04001301 // rust_ffi rlibs may export include dirs, so collect those here.
1302 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1303 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1304 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1305
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001306 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001307 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001308 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001309 // proc_macro link dirs need to be exported, so collect those here.
1310 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001311
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001312 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001313 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1314 collectIncludedProtos(mod, rustDep)
1315 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001316 case cc.IsStaticDepTag(depTag):
1317 // Rust FFI rlibs should not be declared in a Rust modules
1318 // "static_libs" list as we can't handle them properly at the
1319 // moment (for example, they only produce an rlib-std variant).
1320 // Instead, a normal rust_library variant should be used.
1321 ctx.PropertyErrorf("static_libs",
1322 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1323 depName)
1324
Paul Duffind5cf92e2021-07-09 17:38:55 +01001325 }
1326
Cole Faustb6e6f992023-08-17 17:42:26 -07001327 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1328
Paul Duffind5cf92e2021-07-09 17:38:55 +01001329 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001330 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1331 // OS/Arch variant is used.
1332 var helper string
1333 if ctx.Host() {
1334 helper = "missing 'host_supported'?"
1335 } else {
1336 helper = "device module defined?"
1337 }
1338
1339 if dep.Target().Os != ctx.Os() {
1340 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1341 return
1342 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1343 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1344 return
1345 }
1346 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001347 }
1348
Ivan Lozano0a468a42024-05-13 21:03:34 -04001349 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001350 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001351 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001352 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1353 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001354 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001355 }
1356
Ivan Lozanoffee3342019-08-27 12:03:00 -07001357 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001358 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001359 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001360 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001361 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001362 }
1363 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001364
Ivan Lozanod106efe2023-09-21 23:30:26 -04001365 if depTag == sourceDepTag {
1366 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1367 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001368 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001369 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1370 }
1371 }
1372 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001373 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001374 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001375 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001376 if _, ok := ccDep.(*Module); !ok {
1377 if ccDep.Module().Target().Os != ctx.Os() {
1378 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1379 return
1380 }
1381 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1382 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1383 return
1384 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001385 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001386 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001387 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001388 if !ctx.Config().AllowMissingDependencies() {
1389 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1390 } else {
1391 ctx.AddMissingDependencies([]string{depName})
1392 }
1393 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001394 }
1395
Wen-yi Chu41326c12023-09-22 03:58:59 +00001396 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001397
Ivan Lozanoffee3342019-08-27 12:03:00 -07001398 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001399 switch {
1400 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001401 if cc.IsWholeStaticLib(depTag) {
1402 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1403 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001404 if mod.Binary() {
1405 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1406 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1407 // final linkage, pass the args directly to the linker to handle these cases.
1408 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1409 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001410 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001411 } else {
1412 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 -05001413 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001414 }
1415
1416 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1417 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001418 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001419 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1420
Colin Cross313aa542023-12-13 13:47:44 -08001421 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001422 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1423 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1424 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1425 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001426 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001427
1428 // Record baseLibName for snapshots.
1429 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1430
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001431 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001432 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001433 // For the shared lib dependencies, we may link to the stub variant
1434 // of the dependency depending on the context (e.g. if this
1435 // dependency crosses the APEX boundaries).
1436 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1437
1438 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1439 // object (either from stub or non-stub) to use.
1440 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001441 if !linkObject.Valid() {
1442 if !ctx.Config().AllowMissingDependencies() {
1443 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1444 } else {
1445 ctx.AddMissingDependencies([]string{depName})
1446 }
1447 return
1448 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001449 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001450
Ivan Lozanoffee3342019-08-27 12:03:00 -07001451 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001452 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001453 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1454 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1455 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1456 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001457 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001458
1459 // Record baseLibName for snapshots.
1460 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1461
Cole Faustb6e6f992023-08-17 17:42:26 -07001462 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001464 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001465 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001466 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1467 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1468 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001469 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001470 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001471 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001472 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001473 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001474 }
1475
1476 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001477 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1478 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001479 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001481 } else {
1482 switch {
1483 case depTag == cc.CrtBeginDepTag:
1484 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1485 case depTag == cc.CrtEndDepTag:
1486 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1487 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001488 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001489
1490 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001491 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001492 // These are usually genrules which don't have per-target variants.
1493 directSrcDeps = append(directSrcDeps, srcDep)
1494 }
1495 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001496 })
1497
Wen-yi Chu41326c12023-09-22 03:58:59 +00001498 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1499
1500 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001501 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001502 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001503 crateName := dep.CrateName()
1504 if alias, aliased := aliases[crateName]; aliased {
1505 crateName = alias
1506 }
1507 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001508 }
1509 var dylibDepFiles RustLibraries
1510 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001511 crateName := dep.CrateName()
1512 if alias, aliased := aliases[crateName]; aliased {
1513 crateName = alias
1514 }
1515 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001516 }
1517 var procMacroDepFiles RustLibraries
1518 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001519 crateName := dep.CrateName()
1520 if alias, aliased := aliases[crateName]; aliased {
1521 crateName = alias
1522 }
1523 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001524 }
1525
Colin Cross004bd3f2023-10-02 11:39:17 -07001526 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001527 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001528 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001529 }
1530
Colin Cross004bd3f2023-10-02 11:39:17 -07001531 var sharedLibFiles android.Paths
1532 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001533 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001534 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001535 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001536 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001537 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001538 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001539 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001540 }
1541
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001542 var srcProviderDepFiles android.Paths
1543 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001544 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001545 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1546 }
1547 for _, dep := range directSrcDeps {
1548 srcs := dep.Srcs()
1549 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1550 }
1551
Wen-yi Chu41326c12023-09-22 03:58:59 +00001552 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1553 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001554 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1555 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1556 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001557 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001558 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001559
1560 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001561 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001562 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001563 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001564 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1565 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1566 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001567
1568 return depPaths
1569}
1570
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001571func (mod *Module) InstallInData() bool {
1572 if mod.compiler == nil {
1573 return false
1574 }
1575 return mod.compiler.inData()
1576}
1577
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001578func (mod *Module) InstallInRamdisk() bool {
1579 return mod.InRamdisk()
1580}
1581
1582func (mod *Module) InstallInVendorRamdisk() bool {
1583 return mod.InVendorRamdisk()
1584}
1585
1586func (mod *Module) InstallInRecovery() bool {
1587 return mod.InRecovery()
1588}
1589
Wen-yi Chu41326c12023-09-22 03:58:59 +00001590func linkPathFromFilePath(filepath android.Path) string {
1591 return strings.Split(filepath.String(), filepath.Base())[0]
1592}
1593
Spandan Das604f3762023-03-16 22:51:40 +00001594// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1595func (r *Module) usePublicApi() bool {
1596 return r.Device() && r.UseSdk()
1597}
1598
1599// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1600func (r *Module) useVendorApi() bool {
1601 return r.Device() && (r.InVendor() || r.InProduct())
1602}
1603
Ivan Lozanoffee3342019-08-27 12:03:00 -07001604func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1605 ctx := &depsContext{
1606 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001607 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001608
1609 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001610 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001611
Kiyoung Kim487689e2022-07-26 09:48:22 +09001612 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001613 if mod.usePublicApi() || mod.useVendorApi() {
1614 for idx, lib := range deps.SharedLibs {
1615 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1616 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001617 }
1618
Ivan Lozano1921e802021-05-20 13:39:16 -04001619 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001620 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001621 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001622
Ivan Lozano2b081132020-09-08 12:46:52 -04001623 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001624 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001625 stdLinkage = "rlib-std"
1626 }
1627
1628 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001629
Ivan Lozano2b081132020-09-08 12:46:52 -04001630 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1631 rlibDepVariations = append(rlibDepVariations,
1632 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1633 }
1634
Ivan Lozano1921e802021-05-20 13:39:16 -04001635 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001636 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001637 for _, lib := range deps.Rlibs {
1638 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001639 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001640 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001641
1642 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001643 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001644
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001645 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001646 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001647 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001648
Ivan Lozano1921e802021-05-20 13:39:16 -04001649 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001650 if deps.Rustlibs != nil {
1651 if !mod.compiler.Disabled() {
1652 for _, lib := range deps.Rustlibs {
1653 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1654 if autoDep.depTag == rlibDepTag {
1655 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001656 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1657
Ivan Lozanod106efe2023-09-21 23:30:26 -04001658 } else {
1659 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1660 // Check for the existence of the dylib deptag variant. Select it if available,
1661 // otherwise select the rlib variant.
1662 autoDepVariations := append(commonDepVariations,
1663 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001664 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1665 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001666
Ivan Lozanod106efe2023-09-21 23:30:26 -04001667 } else {
1668 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001669 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1670
Ivan Lozanod106efe2023-09-21 23:30:26 -04001671 }
1672 }
1673 }
1674 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1675 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001676 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001677 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001678
Ivan Lozano0a468a42024-05-13 21:03:34 -04001679 // Only add rustlib dependencies if they're source providers themselves.
1680 // This is used to track which crate names need to be added to the source generated
1681 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001682 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001683 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001684 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001685 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001686 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001687 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001688
Ivan Lozano1921e802021-05-20 13:39:16 -04001689 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001690 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001691 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001692 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001693 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001694 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001695 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001696 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001697 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001698 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1699
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001700 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001701 }
1702 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001703
1704 for _, lib := range deps.SharedLibs {
1705 depTag := cc.SharedDepTag()
1706 name, version := cc.StubsLibNameAndVersion(lib)
1707
1708 variations := []blueprint.Variation{
1709 {Mutator: "link", Variation: "shared"},
1710 }
Spandan Das604f3762023-03-16 22:51:40 +00001711 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1712 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1713 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1714 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1715 }
1716
1717 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1718 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1719 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001720 }
1721
1722 for _, lib := range deps.WholeStaticLibs {
1723 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001724
1725 actx.AddVariationDependencies([]blueprint.Variation{
1726 {Mutator: "link", Variation: "static"},
1727 }, depTag, lib)
1728 }
1729
1730 for _, lib := range deps.StaticLibs {
1731 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001732
1733 actx.AddVariationDependencies([]blueprint.Variation{
1734 {Mutator: "link", Variation: "static"},
1735 }, depTag, lib)
1736 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001737
Zach Johnson3df4e632020-11-06 11:56:27 -08001738 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1739
Colin Cross565cafd2020-09-25 18:47:38 -07001740 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001741 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001742 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001743 }
Colin Crossfe605e12022-01-23 20:46:16 -08001744 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001745 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001746 }
1747
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001748 if mod.sourceProvider != nil {
1749 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1750 bindgen.Properties.Custom_bindgen != "" {
1751 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1752 bindgen.Properties.Custom_bindgen)
1753 }
1754 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001755
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001756 actx.AddVariationDependencies([]blueprint.Variation{
1757 {Mutator: "link", Variation: "shared"},
1758 }, dataLibDepTag, deps.DataLibs...)
1759
1760 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1761
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001762 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001763 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001764
1765 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001766}
1767
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001768func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001769 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001770 mod.beginMutator(ctx)
1771 }
1772}
1773
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001774func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1775 ctx := &baseModuleContext{
1776 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001777 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001778
1779 mod.begin(ctx)
1780}
1781
Ivan Lozanoffee3342019-08-27 12:03:00 -07001782func (mod *Module) Name() string {
1783 name := mod.ModuleBase.Name()
1784 if p, ok := mod.compiler.(interface {
1785 Name(string) string
1786 }); ok {
1787 name = p.Name(name)
1788 }
1789 return name
1790}
1791
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001792func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001793 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001794 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001795 }
1796}
1797
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001798var _ android.HostToolProvider = (*Module)(nil)
1799
1800func (mod *Module) HostToolPath() android.OptionalPath {
1801 if !mod.Host() {
1802 return android.OptionalPath{}
1803 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001804 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1805 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001806 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1807 // Even though proc-macros aren't strictly "tools", since they target the compiler
1808 // and act as compiler plugins, we treat them similarly.
1809 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001810 }
1811 return android.OptionalPath{}
1812}
1813
Jiyong Park99644e92020-11-17 22:21:02 +09001814var _ android.ApexModule = (*Module)(nil)
1815
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001816func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001817 return String(mod.Properties.Min_sdk_version)
1818}
1819
Jiyong Park45bf82e2020-12-15 22:29:02 +09001820// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001821func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001822 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001823 if minSdkVersion == "apex_inherit" {
1824 return nil
1825 }
1826 if minSdkVersion == "" {
1827 return fmt.Errorf("min_sdk_version is not specificed")
1828 }
1829
1830 // Not using nativeApiLevelFromUser because the context here is not
1831 // necessarily a native context.
1832 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1833 if err != nil {
1834 return err
1835 }
1836
1837 if ver.GreaterThan(sdkVersion) {
1838 return fmt.Errorf("newer SDK(%v)", ver)
1839 }
Jiyong Park99644e92020-11-17 22:21:02 +09001840 return nil
1841}
1842
Jiyong Park45bf82e2020-12-15 22:29:02 +09001843// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001844func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1845 depTag := ctx.OtherModuleDependencyTag(dep)
1846
1847 if ccm, ok := dep.(*cc.Module); ok {
1848 if ccm.HasStubsVariants() {
1849 if cc.IsSharedDepTag(depTag) {
1850 // dynamic dep to a stubs lib crosses APEX boundary
1851 return false
1852 }
1853 if cc.IsRuntimeDepTag(depTag) {
1854 // runtime dep to a stubs lib also crosses APEX boundary
1855 return false
1856 }
1857
1858 if cc.IsHeaderDepTag(depTag) {
1859 return false
1860 }
1861 }
1862 if mod.Static() && cc.IsSharedDepTag(depTag) {
1863 // shared_lib dependency from a static lib is considered as crossing
1864 // the APEX boundary because the dependency doesn't actually is
1865 // linked; the dependency is used only during the compilation phase.
1866 return false
1867 }
1868 }
1869
Matthew Maurer581b6d82022-09-29 16:46:25 -07001870 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001871 return false
1872 }
1873
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +00001874 if rustDep, ok := dep.(*Module); ok && rustDep.ApexExclude() {
1875 return false
1876 }
1877
Jiyong Park99644e92020-11-17 22:21:02 +09001878 return true
1879}
1880
1881// Overrides ApexModule.IsInstallabeToApex()
1882func (mod *Module) IsInstallableToApex() bool {
1883 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001884 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001885 return true
1886 }
1887 if _, ok := mod.compiler.(*binaryDecorator); ok {
1888 return true
1889 }
1890 }
1891 return false
1892}
1893
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001894// If a library file has a "lib" prefix, extract the library name without the prefix.
1895func libNameFromFilePath(filepath android.Path) (string, bool) {
1896 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1897 if strings.HasPrefix(libName, "lib") {
1898 libName = libName[3:]
1899 return libName, true
1900 }
1901 return "", false
1902}
1903
Sasha Smundaka76acba2022-04-18 20:12:56 -07001904func kytheExtractRustFactory() android.Singleton {
1905 return &kytheExtractRustSingleton{}
1906}
1907
1908type kytheExtractRustSingleton struct {
1909}
1910
1911func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1912 var xrefTargets android.Paths
1913 ctx.VisitAllModules(func(module android.Module) {
1914 if rustModule, ok := module.(xref); ok {
1915 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1916 }
1917 })
1918 if len(xrefTargets) > 0 {
1919 ctx.Phony("xref_rust", xrefTargets...)
1920 }
1921}
1922
Jihoon Kangf78a8902022-09-01 22:47:07 +00001923func (c *Module) Partition() string {
1924 return ""
1925}
1926
Ivan Lozanoffee3342019-08-27 12:03:00 -07001927var Bool = proptools.Bool
1928var BoolDefault = proptools.BoolDefault
1929var String = proptools.String
1930var StringPtr = proptools.StringPtr