blob: 3402adcc503df84f65fba007f110995da48141c9 [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
Ivan Lozanod7586b62021-04-01 09:49:36 -0400297func (mod *Module) Object() bool {
298 // Rust has no modules which produce only object files.
299 return false
300}
301
Ivan Lozano52767be2019-10-18 14:49:46 -0700302func (mod *Module) Toc() android.OptionalPath {
303 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400304 if lib, ok := mod.compiler.(libraryInterface); ok {
305 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700306 }
307 }
308 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
309}
310
Colin Crossc511bc52020-04-07 16:50:32 +0000311func (mod *Module) UseSdk() bool {
312 return false
313}
314
Ivan Lozanod7586b62021-04-01 09:49:36 -0400315func (mod *Module) RelativeInstallPath() string {
316 if mod.compiler != nil {
317 return mod.compiler.relativeInstallPath()
318 }
319 return ""
320}
321
Ivan Lozano52767be2019-10-18 14:49:46 -0700322func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500323 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700324}
325
Jiyong Park7d55b612021-06-11 17:22:09 +0900326func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400327 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900328}
329
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400330func (mod *Module) SubName() string {
331 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700332}
333
Ivan Lozanof1868af2022-04-12 13:08:36 -0400334func (mod *Module) IsVndkPrebuiltLibrary() bool {
335 // Rust modules do not provide VNDK prebuilts
336 return false
337}
338
339func (mod *Module) IsVendorPublicLibrary() bool {
340 return mod.VendorProperties.IsVendorPublicLibrary
341}
342
343func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
344 // Rust modules to not provide Sdk variants
345 return false
346}
347
Colin Cross127bb8b2020-12-16 16:46:01 -0800348func (c *Module) IsVndkPrivate() bool {
349 return false
350}
351
352func (c *Module) IsLlndk() bool {
353 return false
354}
355
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400356func (mod *Module) KernelHeadersDecorator() bool {
357 return false
358}
359
Colin Cross1f3f1302021-04-26 18:37:44 -0700360func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400361 return false
362}
363
Colin Cross5271fea2021-04-27 13:06:04 -0700364func (m *Module) NeedsVendorPublicLibraryVariants() bool {
365 return false
366}
367
Ivan Lozanod7586b62021-04-01 09:49:36 -0400368func (mod *Module) HasLlndkStubs() bool {
369 return false
370}
371
372func (mod *Module) StubsVersion() string {
373 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
374}
375
Ivan Lozano52767be2019-10-18 14:49:46 -0700376func (mod *Module) SdkVersion() string {
377 return ""
378}
379
Colin Crossc511bc52020-04-07 16:50:32 +0000380func (mod *Module) AlwaysSdk() bool {
381 return false
382}
383
Jiyong Park2286afd2020-06-16 21:58:53 +0900384func (mod *Module) IsSdkVariant() bool {
385 return false
386}
387
Colin Cross1348ce32020-10-01 13:37:16 -0700388func (mod *Module) SplitPerApiLevel() bool {
389 return false
390}
391
Sasha Smundaka76acba2022-04-18 20:12:56 -0700392func (mod *Module) XrefRustFiles() android.Paths {
393 return mod.kytheFiles
394}
395
Ivan Lozanoffee3342019-08-27 12:03:00 -0700396type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400397 Dylibs []string
398 Rlibs []string
399 Rustlibs []string
400 Stdlibs []string
401 ProcMacros []string
402 SharedLibs []string
403 StaticLibs []string
404 WholeStaticLibs []string
405 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700406
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400407 // Used for data dependencies adjacent to tests
408 DataLibs []string
409 DataBins []string
410
Colin Crossfe605e12022-01-23 20:46:16 -0800411 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700412}
413
414type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700415 DyLibs RustLibraries
416 RLibs RustLibraries
417 SharedLibs android.Paths
418 SharedLibDeps android.Paths
419 StaticLibs android.Paths
420 ProcMacros RustLibraries
421 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500422
423 // depFlags and depLinkFlags are rustc and linker (clang) flags.
424 depFlags []string
425 depLinkFlags []string
426
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400427 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500428 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000429 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700430 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700431
Ivan Lozano0a468a42024-05-13 21:03:34 -0400432 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
433 // cc_library_static dependants of rlibs.
434 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
435 exportedLinkDirs []string
436
Ivan Lozano45901ed2020-07-24 16:05:01 -0400437 // Used by bindgen modules which call clang
438 depClangFlags []string
439 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400440 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400441 depSystemIncludePaths android.Paths
442
Colin Crossfe605e12022-01-23 20:46:16 -0800443 CrtBegin android.Paths
444 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700445
446 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500447 SrcDeps android.Paths
448 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449}
450
451type RustLibraries []RustLibrary
452
453type RustLibrary struct {
454 Path android.Path
455 CrateName string
456}
457
Matthew Maurerbb3add12020-06-25 09:34:12 -0700458type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000459 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700460 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700461}
462
Sasha Smundaka76acba2022-04-18 20:12:56 -0700463type xref interface {
464 XrefRustFiles() android.Paths
465}
466
Matthew Maurerbb3add12020-06-25 09:34:12 -0700467type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000468 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400469 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700470 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700471}
472
Wen-yi Chu41326c12023-09-22 03:58:59 +0000473func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
474 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700475}
476
Colin Cross004bd3f2023-10-02 11:39:17 -0700477func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
478 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400479}
480
Colin Cross0de8a1e2020-09-18 14:15:30 -0700481func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800482 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700483 LinkDirs: flagExporter.linkDirs,
484 LinkObjects: flagExporter.linkObjects,
485 })
486}
487
Matthew Maurerbb3add12020-06-25 09:34:12 -0700488var _ exportedFlagsProducer = (*flagExporter)(nil)
489
490func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700491 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700492}
493
Colin Cross0de8a1e2020-09-18 14:15:30 -0700494type FlagExporterInfo struct {
495 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000496 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700497 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700498}
499
Colin Crossbc7d76c2023-12-12 16:39:03 -0800500var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700501
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400502func (mod *Module) isCoverageVariant() bool {
503 return mod.coverage.Properties.IsCoverageVariant
504}
505
506var _ cc.Coverage = (*Module)(nil)
507
Colin Crosse1a85552024-06-14 12:17:37 -0700508func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400509 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
510}
511
Ivan Lozanod7586b62021-04-01 09:49:36 -0400512func (mod *Module) VndkVersion() string {
513 return mod.Properties.VndkVersion
514}
515
Ivan Lozano0a468a42024-05-13 21:03:34 -0400516func (mod *Module) ExportedCrateLinkDirs() []string {
517 return mod.exportedLinkDirs
518}
519
Ivan Lozanod7586b62021-04-01 09:49:36 -0400520func (mod *Module) PreventInstall() bool {
521 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400522}
523
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400524func (mod *Module) MarkAsCoverageVariant(coverage bool) {
525 mod.coverage.Properties.IsCoverageVariant = coverage
526}
527
528func (mod *Module) EnableCoverageIfNeeded() {
529 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530}
531
532func defaultsFactory() android.Module {
533 return DefaultsFactory()
534}
535
536type Defaults struct {
537 android.ModuleBase
538 android.DefaultsModuleBase
539}
540
541func DefaultsFactory(props ...interface{}) android.Module {
542 module := &Defaults{}
543
544 module.AddProperties(props...)
545 module.AddProperties(
546 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800547 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500548 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100549 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400550 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551 &BaseCompilerProperties{},
552 &BinaryCompilerProperties{},
553 &LibraryCompilerProperties{},
554 &ProcMacroCompilerProperties{},
555 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400556 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700557 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400558 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400559 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200560 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500561 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000562 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563 )
564
565 android.InitDefaultsModule(module)
566 return module
567}
568
569func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700570 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571}
572
Ivan Lozano183a3212019-10-18 14:18:45 -0700573func (mod *Module) CcLibrary() bool {
574 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500575 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700576 return true
577 }
578 }
579 return false
580}
581
582func (mod *Module) CcLibraryInterface() bool {
583 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400584 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
585 // VariantIs{Static,Shared} is set.
586 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700587 return true
588 }
589 }
590 return false
591}
592
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400593func (mod *Module) RustLibraryInterface() bool {
594 if mod.compiler != nil {
595 if _, ok := mod.compiler.(libraryInterface); ok {
596 return true
597 }
598 }
599 return false
600}
601
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500602func (mod *Module) IsFuzzModule() bool {
603 if _, ok := mod.compiler.(*fuzzDecorator); ok {
604 return true
605 }
606 return false
607}
608
609func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
610 return mod.FuzzModule
611}
612
613func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
614 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
615 return fuzzer.fuzzPackagedModule
616 }
617 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
618}
619
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000620func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500621 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
622 return fuzzer.sharedLibraries
623 }
624 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
625}
626
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400627func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400628 if mod.compiler != nil {
629 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400630 }
631 return nil
632}
633
Ivan Lozano183a3212019-10-18 14:18:45 -0700634func (mod *Module) SetStatic() {
635 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700636 if library, ok := mod.compiler.(libraryInterface); ok {
637 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700638 return
639 }
640 }
641 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
642}
643
644func (mod *Module) SetShared() {
645 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700646 if library, ok := mod.compiler.(libraryInterface); ok {
647 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700648 return
649 }
650 }
651 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
652}
653
Ivan Lozano183a3212019-10-18 14:18:45 -0700654func (mod *Module) BuildStaticVariant() bool {
655 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700656 if library, ok := mod.compiler.(libraryInterface); ok {
657 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700658 }
659 }
660 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
661}
662
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400663func (mod *Module) BuildRlibVariant() bool {
664 if mod.compiler != nil {
665 if library, ok := mod.compiler.(libraryInterface); ok {
666 return library.buildRlib()
667 }
668 }
669 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
670}
671
672func (mod *Module) IsRustFFI() bool {
673 if mod.compiler != nil {
674 if library, ok := mod.compiler.(libraryInterface); ok {
675 return library.isFFILibrary()
676 }
677 }
678 return false
679}
680
Ivan Lozano183a3212019-10-18 14:18:45 -0700681func (mod *Module) BuildSharedVariant() bool {
682 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700683 if library, ok := mod.compiler.(libraryInterface); ok {
684 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700685 }
686 }
687 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
688}
689
Ivan Lozano183a3212019-10-18 14:18:45 -0700690func (mod *Module) Module() android.Module {
691 return mod
692}
693
Ivan Lozano183a3212019-10-18 14:18:45 -0700694func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400695 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700696}
697
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400698func (mod *Module) CoverageFiles() android.Paths {
699 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800700 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400701 }
702 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
703}
704
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400705// Rust does not produce gcno files, and therefore does not produce a coverage archive.
706func (mod *Module) CoverageOutputFile() android.OptionalPath {
707 return android.OptionalPath{}
708}
709
710func (mod *Module) IsNdk(config android.Config) bool {
711 return false
712}
713
714func (mod *Module) IsStubs() bool {
715 return false
716}
717
Jiyong Park459feca2020-12-15 11:02:21 +0900718func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900719 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900720 return false
721 }
722
Jiyong Park459feca2020-12-15 11:02:21 +0900723 // The apex variant is not installable because it is included in the APEX and won't appear
724 // in the system partition as a standalone file.
725 if !apexInfo.IsForPlatform() {
726 return false
727 }
728
Jiyong Parke54f07e2021-04-07 15:08:04 +0900729 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900730}
731
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500732func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800733 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
734 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500735}
736
Ivan Lozano183a3212019-10-18 14:18:45 -0700737var _ cc.LinkableInterface = (*Module)(nil)
738
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739func (mod *Module) Init() android.Module {
740 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500741 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742
Yi Kong46c6e592022-01-20 22:55:00 +0800743 if mod.afdo != nil {
744 mod.AddProperties(mod.afdo.props()...)
745 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 if mod.compiler != nil {
747 mod.AddProperties(mod.compiler.compilerProps()...)
748 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400749 if mod.coverage != nil {
750 mod.AddProperties(mod.coverage.props()...)
751 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200752 if mod.clippy != nil {
753 mod.AddProperties(mod.clippy.props()...)
754 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400755 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700756 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400757 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500758 if mod.sanitize != nil {
759 mod.AddProperties(mod.sanitize.props()...)
760 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400761
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900763 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764
765 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700766 return mod
767}
768
769func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
770 return &Module{
771 hod: hod,
772 multilib: multilib,
773 }
774}
775func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
776 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800777 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200779 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500780 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781 return module
782}
783
784type ModuleContext interface {
785 android.ModuleContext
786 ModuleContextIntf
787}
788
789type BaseModuleContext interface {
790 android.BaseModuleContext
791 ModuleContextIntf
792}
793
794type DepsContext interface {
795 android.BottomUpMutatorContext
796 ModuleContextIntf
797}
798
799type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200800 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802}
803
804type depsContext struct {
805 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806}
807
808type moduleContext struct {
809 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810}
811
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200812type baseModuleContext struct {
813 android.BaseModuleContext
814}
815
816func (ctx *moduleContext) RustModule() *Module {
817 return ctx.Module().(*Module)
818}
819
820func (ctx *moduleContext) toolchain() config.Toolchain {
821 return ctx.RustModule().toolchain(ctx)
822}
823
824func (ctx *depsContext) RustModule() *Module {
825 return ctx.Module().(*Module)
826}
827
828func (ctx *depsContext) toolchain() config.Toolchain {
829 return ctx.RustModule().toolchain(ctx)
830}
831
832func (ctx *baseModuleContext) RustModule() *Module {
833 return ctx.Module().(*Module)
834}
835
836func (ctx *baseModuleContext) toolchain() config.Toolchain {
837 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400838}
839
840func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700841 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
842 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
843 return false
844 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400845 return mod.compiler != nil && mod.compiler.nativeCoverage()
846}
847
Ivan Lozanod7586b62021-04-01 09:49:36 -0400848func (mod *Module) EverInstallable() bool {
849 return mod.compiler != nil &&
850 // Check to see whether the module is actually ever installable.
851 mod.compiler.everInstallable()
852}
853
854func (mod *Module) Installable() *bool {
855 return mod.Properties.Installable
856}
857
Ivan Lozano872d5792022-03-23 17:31:39 -0400858func (mod *Module) ProcMacro() bool {
859 if pm, ok := mod.compiler.(procMacroInterface); ok {
860 return pm.ProcMacro()
861 }
862 return false
863}
864
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
866 if mod.cachedToolchain == nil {
867 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
868 }
869 return mod.cachedToolchain
870}
871
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200872func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
873 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
874}
875
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
877}
878
879func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
880 ctx := &moduleContext{
881 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883
Colin Crossff694a82023-12-13 15:54:49 -0800884 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900885 if !apexInfo.IsForPlatform() {
886 mod.hideApexVariantFromMake = true
887 }
888
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500890 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
891
Ivan Lozanof1868af2022-04-12 13:08:36 -0400892 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700893
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 if !toolchain.Supported() {
895 // This toolchain's unsupported, there's nothing to do for this mod.
896 return
897 }
898
899 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400900 // Export linkDirs for CC rust generatedlibs
901 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
902 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
903
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 flags := Flags{
905 Toolchain: toolchain,
906 }
907
Ivan Lozano67eada32021-09-23 11:50:33 -0400908 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800909 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500910 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800911 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 if mod.compiler != nil {
913 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400914 flags = mod.compiler.cfgFlags(ctx, flags)
915 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400916 }
917 if mod.coverage != nil {
918 flags, deps = mod.coverage.flags(ctx, flags, deps)
919 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200920 if mod.clippy != nil {
921 flags, deps = mod.clippy.flags(ctx, flags, deps)
922 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500923 if mod.sanitize != nil {
924 flags, deps = mod.sanitize.flags(ctx, flags, deps)
925 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400926
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200927 // SourceProvider needs to call GenerateSource() before compiler calls
928 // compile() so it can provide the source. A SourceProvider has
929 // multiple variants (e.g. source, rlib, dylib). Only the "source"
930 // variant is responsible for effectively generating the source. The
931 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400932 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200933 if mod.compiler.(libraryInterface).source() {
934 mod.sourceProvider.GenerateSource(ctx, deps)
935 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
936 } else {
937 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
938 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700939 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200940 }
Colin Cross40213022023-12-13 15:19:49 -0800941 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400942 }
943
944 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100945 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700946 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400947 if ctx.Failed() {
948 return
949 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700950 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
951 if buildOutput.kytheFile != nil {
952 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
953 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400954 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900955
Dan Albert06feee92021-03-19 15:06:02 -0700956 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000957 if mod.docTimestampFile.Valid() {
958 ctx.CheckbuildFile(mod.docTimestampFile.Path())
959 }
Dan Albert06feee92021-03-19 15:06:02 -0700960
Colin Crossff694a82023-12-13 15:54:49 -0800961 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400962 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900963 // If the module has been specifically configure to not be installed then
964 // hide from make as otherwise it will break when running inside make as the
965 // output path to install will not be specified. Not all uninstallable
966 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400967 // side dependencies. In particular, proc-macros need to be captured in the
968 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900969 mod.HideFromMake()
970 } else if !mod.installable(apexInfo) {
971 mod.SkipInstall()
972 }
973
974 // Still call install though, the installs will be stored as PackageSpecs to allow
975 // using the outputs in a genrule.
976 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200977 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900978 if ctx.Failed() {
979 return
980 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400981 // Export your own directory as a linkDir
982 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
983
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400984 }
Chris Wailes74be7642021-07-22 16:20:28 -0700985
986 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700987 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000988 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800989 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000990 }
Wei Lia1aa2972024-06-21 13:08:51 -0700991
mrziwang0cbd3b02024-06-20 16:39:25 -0700992 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -0700993
994 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -0700995}
996
997func (mod *Module) setOutputFiles(ctx ModuleContext) {
998 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
999 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1000 } else if mod.OutputFile().Valid() {
1001 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1002 } else {
1003 ctx.SetOutputFiles(android.Paths{}, "")
1004 }
1005 if mod.compiler != nil {
1006 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1007 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001008}
1009
Wei Lia1aa2972024-06-21 13:08:51 -07001010func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1011 // Dump metadata that can not be done in android/compliance-metadata.go
1012 metadataInfo := ctx.ComplianceMetadataInfo()
1013 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1014 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1015
1016 // Static libs
1017 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1018 staticDepNames := make([]string, 0, len(staticDeps))
1019 for _, dep := range staticDeps {
1020 staticDepNames = append(staticDepNames, dep.Name())
1021 }
1022 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1023 for _, dep := range ccStaticDeps {
1024 staticDepNames = append(staticDepNames, dep.Name())
1025 }
1026
1027 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1028 // C static libraries
1029 for _, dep := range deps.StaticLibs {
1030 staticDepPaths = append(staticDepPaths, dep.String())
1031 }
1032 // Rust static libraries
1033 for _, dep := range deps.RLibs {
1034 staticDepPaths = append(staticDepPaths, dep.Path.String())
1035 }
1036 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1037 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1038
1039 // C Whole static libs
1040 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1041 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1042 for _, dep := range ccStaticDeps {
1043 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1044 }
1045 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1046}
1047
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048func (mod *Module) deps(ctx DepsContext) Deps {
1049 deps := Deps{}
1050
1051 if mod.compiler != nil {
1052 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001053 }
1054 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001055 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 }
1057
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001058 if mod.coverage != nil {
1059 deps = mod.coverage.deps(ctx, deps)
1060 }
1061
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001062 if mod.sanitize != nil {
1063 deps = mod.sanitize.deps(ctx, deps)
1064 }
1065
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1067 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001068 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1070 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1071 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001072 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001073 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074 return deps
1075
1076}
1077
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078type dependencyTag struct {
1079 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001080 name string
1081 library bool
1082 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001083 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001084}
1085
Jiyong Park65b62242020-11-25 12:44:59 +09001086// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1087// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1088func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001089 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001090}
1091
1092var _ android.InstallNeededDependencyTag = dependencyTag{}
1093
Colin Cross65cb3142021-12-10 23:05:02 +00001094func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1095 if d.library && d.dynamic {
1096 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1097 }
1098 return nil
1099}
1100
Yu Liuc8884602024-03-15 18:48:38 +00001101func (d dependencyTag) PropagateAconfigValidation() bool {
1102 return d == rlibDepTag || d == sourceDepTag
1103}
1104
1105var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1106
Colin Cross65cb3142021-12-10 23:05:02 +00001107var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1108
Ivan Lozanoffee3342019-08-27 12:03:00 -07001109var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001110 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1111 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001112 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001113 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001114 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001115 dataLibDepTag = dependencyTag{name: "data lib"}
1116 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001117)
1118
Jiyong Park99644e92020-11-17 22:21:02 +09001119func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1120 tag, ok := depTag.(dependencyTag)
1121 return ok && tag == dylibDepTag
1122}
1123
Jiyong Park94e22fd2021-04-08 18:19:15 +09001124func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1125 tag, ok := depTag.(dependencyTag)
1126 return ok && tag == rlibDepTag
1127}
1128
Matthew Maurer0f003b12020-06-29 14:34:06 -07001129type autoDep struct {
1130 variation string
1131 depTag dependencyTag
1132}
1133
1134var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001135 sourceVariation = "source"
1136 rlibVariation = "rlib"
1137 dylibVariation = "dylib"
1138 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1139 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001140)
1141
1142type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001143 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001144}
1145
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001146func (mod *Module) begin(ctx BaseModuleContext) {
1147 if mod.coverage != nil {
1148 mod.coverage.begin(ctx)
1149 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001150 if mod.sanitize != nil {
1151 mod.sanitize.begin(ctx)
1152 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001153}
1154
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001155func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001156 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001157 return p.prebuilt()
1158 }
1159 return nil
1160}
1161
Kiyoung Kim37693d02024-04-04 09:56:15 +09001162func (mod *Module) Symlinks() []string {
1163 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1164 return nil
1165}
1166
Justin Yun24b246a2023-03-16 10:36:16 +09001167func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1168 if rustDep, ok := dep.(*Module); ok {
1169 // Use base module name for snapshots when exporting to Makefile.
1170 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1171 baseName := rustDep.BaseModuleName()
1172 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1173 }
1174 }
1175 return cc.MakeLibName(ctx, c, dep, depName)
1176}
1177
Ivan Lozanod106efe2023-09-21 23:30:26 -04001178func collectIncludedProtos(mod *Module, dep *Module) {
1179 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1180 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1181 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1182 }
1183 }
1184}
Andrew Walbran52533232024-03-19 11:36:04 +00001185
Ivan Lozanoffee3342019-08-27 12:03:00 -07001186func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1187 var depPaths PathDeps
1188
1189 directRlibDeps := []*Module{}
1190 directDylibDeps := []*Module{}
1191 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001192 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001193 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001194 directSrcProvidersDeps := []*Module{}
1195 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001196
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001197 // For the dependency from platform to apex, use the latest stubs
1198 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001199 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001200 if !apexInfo.IsForPlatform() {
1201 mod.apexSdkVersion = apexInfo.MinSdkVersion
1202 }
1203
1204 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1205 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1206 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1207 // (b/144430859)
1208 mod.apexSdkVersion = android.FutureApiLevel
1209 }
1210
Spandan Das604f3762023-03-16 22:51:40 +00001211 skipModuleList := map[string]bool{}
1212
1213 var apiImportInfo multitree.ApiImportInfo
1214 hasApiImportInfo := false
1215
1216 ctx.VisitDirectDeps(func(dep android.Module) {
1217 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001218 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001219 hasApiImportInfo = true
1220 }
1221 })
1222
1223 if hasApiImportInfo {
1224 targetStubModuleList := map[string]string{}
1225 targetOrigModuleList := map[string]string{}
1226
1227 // Search for dependency which both original module and API imported library with APEX stub exists
1228 ctx.VisitDirectDeps(func(dep android.Module) {
1229 depName := ctx.OtherModuleName(dep)
1230 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1231 targetStubModuleList[apiLibrary] = depName
1232 }
1233 })
1234 ctx.VisitDirectDeps(func(dep android.Module) {
1235 depName := ctx.OtherModuleName(dep)
1236 if origLibrary, ok := targetStubModuleList[depName]; ok {
1237 targetOrigModuleList[origLibrary] = depName
1238 }
1239 })
1240
1241 // Decide which library should be used between original and API imported library
1242 ctx.VisitDirectDeps(func(dep android.Module) {
1243 depName := ctx.OtherModuleName(dep)
1244 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1245 if cc.ShouldUseStubForApex(ctx, dep) {
1246 skipModuleList[depName] = true
1247 } else {
1248 skipModuleList[apiLibrary] = true
1249 }
1250 }
1251 })
1252 }
1253
Cole Faustb6e6f992023-08-17 17:42:26 -07001254 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1255 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001256
Ivan Lozanoffee3342019-08-27 12:03:00 -07001257 ctx.VisitDirectDeps(func(dep android.Module) {
1258 depName := ctx.OtherModuleName(dep)
1259 depTag := ctx.OtherModuleDependencyTag(dep)
Spandan Das604f3762023-03-16 22:51:40 +00001260 if _, exists := skipModuleList[depName]; exists {
1261 return
1262 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001263
1264 if depTag == android.DarwinUniversalVariantTag {
1265 return
1266 }
1267
Ivan Lozano0a468a42024-05-13 21:03:34 -04001268 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001270 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001271
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001272 switch {
1273 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001274 dylib, ok := rustDep.compiler.(libraryInterface)
1275 if !ok || !dylib.dylib() {
1276 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1277 return
1278 }
1279 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001280 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001281 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1282
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001283 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001284 rlib, ok := rustDep.compiler.(libraryInterface)
1285 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001286 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001287 return
1288 }
1289 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001290 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001291 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1292
Ivan Lozano0a468a42024-05-13 21:03:34 -04001293 // rust_ffi rlibs may export include dirs, so collect those here.
1294 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1295 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1296 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1297
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001298 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001299 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001300 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001301 // proc_macro link dirs need to be exported, so collect those here.
1302 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001303
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001304 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001305 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1306 collectIncludedProtos(mod, rustDep)
1307 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001308 case cc.IsStaticDepTag(depTag):
1309 // Rust FFI rlibs should not be declared in a Rust modules
1310 // "static_libs" list as we can't handle them properly at the
1311 // moment (for example, they only produce an rlib-std variant).
1312 // Instead, a normal rust_library variant should be used.
1313 ctx.PropertyErrorf("static_libs",
1314 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1315 depName)
1316
Paul Duffind5cf92e2021-07-09 17:38:55 +01001317 }
1318
Cole Faustb6e6f992023-08-17 17:42:26 -07001319 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1320
Paul Duffind5cf92e2021-07-09 17:38:55 +01001321 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001322 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1323 // OS/Arch variant is used.
1324 var helper string
1325 if ctx.Host() {
1326 helper = "missing 'host_supported'?"
1327 } else {
1328 helper = "device module defined?"
1329 }
1330
1331 if dep.Target().Os != ctx.Os() {
1332 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1333 return
1334 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1335 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1336 return
1337 }
1338 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001339 }
1340
Ivan Lozano0a468a42024-05-13 21:03:34 -04001341 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001342 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001343 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001344 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1345 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001346 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001347 }
1348
Ivan Lozanoffee3342019-08-27 12:03:00 -07001349 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001350 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001351 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001352 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001353 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001354 }
1355 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001356
Ivan Lozanod106efe2023-09-21 23:30:26 -04001357 if depTag == sourceDepTag {
1358 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1359 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001360 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001361 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1362 }
1363 }
1364 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001365 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001366 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001367 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001368 if _, ok := ccDep.(*Module); !ok {
1369 if ccDep.Module().Target().Os != ctx.Os() {
1370 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1371 return
1372 }
1373 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1374 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1375 return
1376 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001377 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001378 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001379 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001380 if !ctx.Config().AllowMissingDependencies() {
1381 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1382 } else {
1383 ctx.AddMissingDependencies([]string{depName})
1384 }
1385 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001386 }
1387
Wen-yi Chu41326c12023-09-22 03:58:59 +00001388 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001389
Ivan Lozanoffee3342019-08-27 12:03:00 -07001390 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001391 switch {
1392 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001393 if cc.IsWholeStaticLib(depTag) {
1394 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1395 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001396 if mod.Binary() {
1397 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1398 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1399 // final linkage, pass the args directly to the linker to handle these cases.
1400 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1401 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001402 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001403 } else {
1404 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 -05001405 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001406 }
1407
1408 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1409 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001410 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001411 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1412
Colin Cross313aa542023-12-13 13:47:44 -08001413 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001414 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1415 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1416 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1417 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001418 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001419
1420 // Record baseLibName for snapshots.
1421 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1422
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001423 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001424 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001425 // For the shared lib dependencies, we may link to the stub variant
1426 // of the dependency depending on the context (e.g. if this
1427 // dependency crosses the APEX boundaries).
1428 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1429
1430 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1431 // object (either from stub or non-stub) to use.
1432 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001433 if !linkObject.Valid() {
1434 if !ctx.Config().AllowMissingDependencies() {
1435 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1436 } else {
1437 ctx.AddMissingDependencies([]string{depName})
1438 }
1439 return
1440 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001441 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001442
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001444 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001445 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1446 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1447 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1448 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001449 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001450
1451 // Record baseLibName for snapshots.
1452 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1453
Cole Faustb6e6f992023-08-17 17:42:26 -07001454 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001456 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001457 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001458 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1459 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1460 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001461 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001462 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001463 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001464 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001465 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001466 }
1467
1468 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001469 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1470 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001471 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001473 } else {
1474 switch {
1475 case depTag == cc.CrtBeginDepTag:
1476 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1477 case depTag == cc.CrtEndDepTag:
1478 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1479 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001481
1482 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001483 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001484 // These are usually genrules which don't have per-target variants.
1485 directSrcDeps = append(directSrcDeps, srcDep)
1486 }
1487 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001488 })
1489
Wen-yi Chu41326c12023-09-22 03:58:59 +00001490 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1491
1492 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001493 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001494 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001495 crateName := dep.CrateName()
1496 if alias, aliased := aliases[crateName]; aliased {
1497 crateName = alias
1498 }
1499 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001500 }
1501 var dylibDepFiles RustLibraries
1502 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001503 crateName := dep.CrateName()
1504 if alias, aliased := aliases[crateName]; aliased {
1505 crateName = alias
1506 }
1507 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001508 }
1509 var procMacroDepFiles RustLibraries
1510 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001511 crateName := dep.CrateName()
1512 if alias, aliased := aliases[crateName]; aliased {
1513 crateName = alias
1514 }
1515 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001516 }
1517
Colin Cross004bd3f2023-10-02 11:39:17 -07001518 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001519 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001520 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001521 }
1522
Colin Cross004bd3f2023-10-02 11:39:17 -07001523 var sharedLibFiles android.Paths
1524 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001525 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001526 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001527 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001528 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001529 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001530 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001531 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001532 }
1533
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001534 var srcProviderDepFiles android.Paths
1535 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001536 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001537 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1538 }
1539 for _, dep := range directSrcDeps {
1540 srcs := dep.Srcs()
1541 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1542 }
1543
Wen-yi Chu41326c12023-09-22 03:58:59 +00001544 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1545 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001546 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1547 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1548 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001549 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001550 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001551
1552 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001553 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001554 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001555 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001556 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1557 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1558 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001559
1560 return depPaths
1561}
1562
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001563func (mod *Module) InstallInData() bool {
1564 if mod.compiler == nil {
1565 return false
1566 }
1567 return mod.compiler.inData()
1568}
1569
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001570func (mod *Module) InstallInRamdisk() bool {
1571 return mod.InRamdisk()
1572}
1573
1574func (mod *Module) InstallInVendorRamdisk() bool {
1575 return mod.InVendorRamdisk()
1576}
1577
1578func (mod *Module) InstallInRecovery() bool {
1579 return mod.InRecovery()
1580}
1581
Wen-yi Chu41326c12023-09-22 03:58:59 +00001582func linkPathFromFilePath(filepath android.Path) string {
1583 return strings.Split(filepath.String(), filepath.Base())[0]
1584}
1585
Spandan Das604f3762023-03-16 22:51:40 +00001586// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1587func (r *Module) usePublicApi() bool {
1588 return r.Device() && r.UseSdk()
1589}
1590
1591// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1592func (r *Module) useVendorApi() bool {
1593 return r.Device() && (r.InVendor() || r.InProduct())
1594}
1595
Ivan Lozanoffee3342019-08-27 12:03:00 -07001596func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1597 ctx := &depsContext{
1598 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001599 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001600
1601 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001602 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001603
Kiyoung Kim487689e2022-07-26 09:48:22 +09001604 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001605 if mod.usePublicApi() || mod.useVendorApi() {
1606 for idx, lib := range deps.SharedLibs {
1607 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1608 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001609 }
1610
Ivan Lozano1921e802021-05-20 13:39:16 -04001611 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001612 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001613 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001614
Ivan Lozano2b081132020-09-08 12:46:52 -04001615 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001616 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001617 stdLinkage = "rlib-std"
1618 }
1619
1620 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001621
Ivan Lozano2b081132020-09-08 12:46:52 -04001622 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1623 rlibDepVariations = append(rlibDepVariations,
1624 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1625 }
1626
Ivan Lozano1921e802021-05-20 13:39:16 -04001627 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001628 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001629 for _, lib := range deps.Rlibs {
1630 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001631 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001632 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001633
1634 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001635 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001636
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001637 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001638 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001639 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001640
Ivan Lozano1921e802021-05-20 13:39:16 -04001641 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001642 if deps.Rustlibs != nil {
1643 if !mod.compiler.Disabled() {
1644 for _, lib := range deps.Rustlibs {
1645 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1646 if autoDep.depTag == rlibDepTag {
1647 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001648 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1649
Ivan Lozanod106efe2023-09-21 23:30:26 -04001650 } else {
1651 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1652 // Check for the existence of the dylib deptag variant. Select it if available,
1653 // otherwise select the rlib variant.
1654 autoDepVariations := append(commonDepVariations,
1655 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001656 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1657 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001658
Ivan Lozanod106efe2023-09-21 23:30:26 -04001659 } else {
1660 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001661 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1662
Ivan Lozanod106efe2023-09-21 23:30:26 -04001663 }
1664 }
1665 }
1666 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1667 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001668 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001669 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001670
Ivan Lozano0a468a42024-05-13 21:03:34 -04001671 // Only add rustlib dependencies if they're source providers themselves.
1672 // This is used to track which crate names need to be added to the source generated
1673 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001674 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001675 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001676 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001677 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001678 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001679 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001680
Ivan Lozano1921e802021-05-20 13:39:16 -04001681 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001682 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001683 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001684 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001685 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001686 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001687 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001688 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001689 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001690 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1691
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001692 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001693 }
1694 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001695
1696 for _, lib := range deps.SharedLibs {
1697 depTag := cc.SharedDepTag()
1698 name, version := cc.StubsLibNameAndVersion(lib)
1699
1700 variations := []blueprint.Variation{
1701 {Mutator: "link", Variation: "shared"},
1702 }
Spandan Das604f3762023-03-16 22:51:40 +00001703 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1704 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1705 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1706 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1707 }
1708
1709 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1710 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1711 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001712 }
1713
1714 for _, lib := range deps.WholeStaticLibs {
1715 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001716
1717 actx.AddVariationDependencies([]blueprint.Variation{
1718 {Mutator: "link", Variation: "static"},
1719 }, depTag, lib)
1720 }
1721
1722 for _, lib := range deps.StaticLibs {
1723 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001724
1725 actx.AddVariationDependencies([]blueprint.Variation{
1726 {Mutator: "link", Variation: "static"},
1727 }, depTag, lib)
1728 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001729
Zach Johnson3df4e632020-11-06 11:56:27 -08001730 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1731
Colin Cross565cafd2020-09-25 18:47:38 -07001732 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001733 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001734 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001735 }
Colin Crossfe605e12022-01-23 20:46:16 -08001736 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001737 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001738 }
1739
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001740 if mod.sourceProvider != nil {
1741 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1742 bindgen.Properties.Custom_bindgen != "" {
1743 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1744 bindgen.Properties.Custom_bindgen)
1745 }
1746 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001747
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001748 actx.AddVariationDependencies([]blueprint.Variation{
1749 {Mutator: "link", Variation: "shared"},
1750 }, dataLibDepTag, deps.DataLibs...)
1751
1752 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1753
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001754 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001755 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001756
1757 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001758}
1759
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001760func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001761 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001762 mod.beginMutator(ctx)
1763 }
1764}
1765
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001766func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1767 ctx := &baseModuleContext{
1768 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001769 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001770
1771 mod.begin(ctx)
1772}
1773
Ivan Lozanoffee3342019-08-27 12:03:00 -07001774func (mod *Module) Name() string {
1775 name := mod.ModuleBase.Name()
1776 if p, ok := mod.compiler.(interface {
1777 Name(string) string
1778 }); ok {
1779 name = p.Name(name)
1780 }
1781 return name
1782}
1783
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001784func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001785 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001786 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001787 }
1788}
1789
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001790var _ android.HostToolProvider = (*Module)(nil)
1791
1792func (mod *Module) HostToolPath() android.OptionalPath {
1793 if !mod.Host() {
1794 return android.OptionalPath{}
1795 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001796 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1797 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001798 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1799 // Even though proc-macros aren't strictly "tools", since they target the compiler
1800 // and act as compiler plugins, we treat them similarly.
1801 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001802 }
1803 return android.OptionalPath{}
1804}
1805
Jiyong Park99644e92020-11-17 22:21:02 +09001806var _ android.ApexModule = (*Module)(nil)
1807
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001808func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001809 return String(mod.Properties.Min_sdk_version)
1810}
1811
Jiyong Park45bf82e2020-12-15 22:29:02 +09001812// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001813func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001814 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001815 if minSdkVersion == "apex_inherit" {
1816 return nil
1817 }
1818 if minSdkVersion == "" {
1819 return fmt.Errorf("min_sdk_version is not specificed")
1820 }
1821
1822 // Not using nativeApiLevelFromUser because the context here is not
1823 // necessarily a native context.
1824 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1825 if err != nil {
1826 return err
1827 }
1828
1829 if ver.GreaterThan(sdkVersion) {
1830 return fmt.Errorf("newer SDK(%v)", ver)
1831 }
Jiyong Park99644e92020-11-17 22:21:02 +09001832 return nil
1833}
1834
Jiyong Park45bf82e2020-12-15 22:29:02 +09001835// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001836func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1837 depTag := ctx.OtherModuleDependencyTag(dep)
1838
1839 if ccm, ok := dep.(*cc.Module); ok {
1840 if ccm.HasStubsVariants() {
1841 if cc.IsSharedDepTag(depTag) {
1842 // dynamic dep to a stubs lib crosses APEX boundary
1843 return false
1844 }
1845 if cc.IsRuntimeDepTag(depTag) {
1846 // runtime dep to a stubs lib also crosses APEX boundary
1847 return false
1848 }
1849
1850 if cc.IsHeaderDepTag(depTag) {
1851 return false
1852 }
1853 }
1854 if mod.Static() && cc.IsSharedDepTag(depTag) {
1855 // shared_lib dependency from a static lib is considered as crossing
1856 // the APEX boundary because the dependency doesn't actually is
1857 // linked; the dependency is used only during the compilation phase.
1858 return false
1859 }
1860 }
1861
Matthew Maurer581b6d82022-09-29 16:46:25 -07001862 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001863 return false
1864 }
1865
1866 return true
1867}
1868
1869// Overrides ApexModule.IsInstallabeToApex()
1870func (mod *Module) IsInstallableToApex() bool {
1871 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001872 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001873 return true
1874 }
1875 if _, ok := mod.compiler.(*binaryDecorator); ok {
1876 return true
1877 }
1878 }
1879 return false
1880}
1881
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001882// If a library file has a "lib" prefix, extract the library name without the prefix.
1883func libNameFromFilePath(filepath android.Path) (string, bool) {
1884 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1885 if strings.HasPrefix(libName, "lib") {
1886 libName = libName[3:]
1887 return libName, true
1888 }
1889 return "", false
1890}
1891
Sasha Smundaka76acba2022-04-18 20:12:56 -07001892func kytheExtractRustFactory() android.Singleton {
1893 return &kytheExtractRustSingleton{}
1894}
1895
1896type kytheExtractRustSingleton struct {
1897}
1898
1899func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1900 var xrefTargets android.Paths
1901 ctx.VisitAllModules(func(module android.Module) {
1902 if rustModule, ok := module.(xref); ok {
1903 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1904 }
1905 })
1906 if len(xrefTargets) > 0 {
1907 ctx.Phony("xref_rust", xrefTargets...)
1908 }
1909}
1910
Jihoon Kangf78a8902022-09-01 22:47:07 +00001911func (c *Module) Partition() string {
1912 return ""
1913}
1914
Ivan Lozanoffee3342019-08-27 12:03:00 -07001915var Bool = proptools.Bool
1916var BoolDefault = proptools.BoolDefault
1917var String = proptools.String
1918var StringPtr = proptools.StringPtr