blob: 6b7b2fbaeff844a7891a96fb306281eabd2fd6a0 [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)
40 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
41 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040042 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040043 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050044 })
45 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
46 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070047 })
Inseob Kim3b244062023-07-11 13:31:36 +090048 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020050 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000051 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040055 GlobalRustFlags []string // Flags that apply globally to rust
56 GlobalLinkFlags []string // Flags that apply globally to linker
57 RustFlags []string // Flags that apply to rust
58 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070060 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070061 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040062 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020063 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070064 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050068 AndroidMkRlibs []string `blueprint:"mutated"`
69 AndroidMkDylibs []string `blueprint:"mutated"`
70 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050071 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +000072 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040073
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090074 ImageVariation string `blueprint:"mutated"`
75 VndkVersion string `blueprint:"mutated"`
76 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -050077
Ivan Lozanoc08897c2021-04-02 12:41:32 -040078 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
79 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
80 // appended before SubName.
81 RustSubName string `blueprint:"mutated"`
82
Ivan Lozano6a884432020-12-02 09:15:16 -050083 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +000084 ProductVariantNeeded bool `blueprint:"mutated"`
85 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050086 CoreVariantNeeded bool `blueprint:"mutated"`
87 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070088 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080089 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050090 ExtraVariants []string `blueprint:"mutated"`
91
Ivan Lozanoa2268632021-07-22 10:52:06 -040092 // Allows this module to use non-APEX version of libraries. Useful
93 // for building binaries that are started before APEXes are activated.
94 Bootstrap *bool
95
Ivan Lozano1921e802021-05-20 13:39:16 -040096 // Used by vendor snapshot to record dependencies from snapshot modules.
97 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090098 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040099 SnapshotRlibs []string `blueprint:"mutated"`
100 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400101
Matthew Maurerc6868382021-07-13 14:12:37 -0700102 // Make this module available when building for ramdisk.
103 // On device without a dedicated recovery partition, the module is only
104 // available after switching root into
105 // /first_stage_ramdisk. To expose the module before switching root, install
106 // the recovery variant instead.
107 Ramdisk_available *bool
108
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500109 // Make this module available when building for vendor ramdisk.
110 // On device without a dedicated recovery partition, the module is only
111 // available after switching root into
112 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800113 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500114 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400115
Ivan Lozano1921e802021-05-20 13:39:16 -0400116 // Normally Soong uses the directory structure to decide which modules
117 // should be included (framework) or excluded (non-framework) from the
118 // different snapshots (vendor, recovery, etc.), but this property
119 // allows a partner to exclude a module normally thought of as a
120 // framework module from the vendor snapshot.
121 Exclude_from_vendor_snapshot *bool
122
123 // Normally Soong uses the directory structure to decide which modules
124 // should be included (framework) or excluded (non-framework) from the
125 // different snapshots (vendor, recovery, etc.), but this property
126 // allows a partner to exclude a module normally thought of as a
127 // framework module from the recovery snapshot.
128 Exclude_from_recovery_snapshot *bool
129
Matthew Maurer460ee942021-02-11 12:31:46 -0800130 // Make this module available when building for recovery
131 Recovery_available *bool
132
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500133 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
134 Min_sdk_version *string
135
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900136 HideFromMake bool `blueprint:"mutated"`
137 PreventInstall bool `blueprint:"mutated"`
138
139 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140}
141
142type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700143 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144
Ivan Lozano6a884432020-12-02 09:15:16 -0500145 VendorProperties cc.VendorProperties
146
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147 Properties BaseProperties
148
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000149 hod android.HostOrDeviceSupported
150 multilib android.Multilib
151 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152
Ivan Lozano6a884432020-12-02 09:15:16 -0500153 makeLinkType string
154
Yi Kong46c6e592022-01-20 22:55:00 +0800155 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400157 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200158 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500159 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700160 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400161 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700162 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400163
Ivan Lozano0a468a42024-05-13 21:03:34 -0400164 exportedLinkDirs []string
165
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400166 // Output file to be installed, may be stripped or unstripped.
167 outputFile android.OptionalPath
168
Sasha Smundaka76acba2022-04-18 20:12:56 -0700169 // Cross-reference input file
170 kytheFiles android.Paths
171
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400172 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900173
174 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500175
176 // For apex variants, this is set as apex.min_sdk_version
177 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700178
179 transitiveAndroidMkSharedLibs *android.DepSet[string]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180}
181
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500182func (mod *Module) Header() bool {
183 //TODO: If Rust libraries provide header variants, this needs to be updated.
184 return false
185}
186
187func (mod *Module) SetPreventInstall() {
188 mod.Properties.PreventInstall = true
189}
190
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500191func (mod *Module) SetHideFromMake() {
192 mod.Properties.HideFromMake = true
193}
194
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900195func (mod *Module) HiddenFromMake() bool {
196 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400197}
198
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500199func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500200 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
201 // nil since we need compiler to actually sanitize.
202 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500203}
204
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500205func (mod *Module) IsPrebuilt() bool {
206 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
207 return true
208 }
209 return false
210}
211
Ivan Lozano52767be2019-10-18 14:49:46 -0700212func (mod *Module) SelectedStl() string {
213 return ""
214}
215
Ivan Lozano2b262972019-11-21 12:30:50 -0800216func (mod *Module) NonCcVariants() bool {
217 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400218 if library, ok := mod.compiler.(libraryInterface); ok {
219 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800220 }
221 }
222 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
223}
224
Ivan Lozano52767be2019-10-18 14:49:46 -0700225func (mod *Module) Static() bool {
226 if mod.compiler != nil {
227 if library, ok := mod.compiler.(libraryInterface); ok {
228 return library.static()
229 }
230 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400231 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700232}
233
234func (mod *Module) Shared() bool {
235 if mod.compiler != nil {
236 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400237 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700238 }
239 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400240 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700241}
242
Ivan Lozanod7586b62021-04-01 09:49:36 -0400243func (mod *Module) Dylib() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
246 return library.dylib()
247 }
248 }
249 return false
250}
251
Ivan Lozanod106efe2023-09-21 23:30:26 -0400252func (mod *Module) Source() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
255 return library.source()
256 }
257 }
258 return false
259}
260
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400261func (mod *Module) RlibStd() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
264 return library.rlibStd()
265 }
266 }
267 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
268}
269
Ivan Lozanod7586b62021-04-01 09:49:36 -0400270func (mod *Module) Rlib() bool {
271 if mod.compiler != nil {
272 if library, ok := mod.compiler.(libraryInterface); ok {
273 return library.rlib()
274 }
275 }
276 return false
277}
278
279func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400280 if binary, ok := mod.compiler.(binaryInterface); ok {
281 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400282 }
283 return false
284}
285
Justin Yun5e035862021-06-29 20:50:37 +0900286func (mod *Module) StaticExecutable() bool {
287 if !mod.Binary() {
288 return false
289 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400290 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900291}
292
Ivan Lozanod7586b62021-04-01 09:49:36 -0400293func (mod *Module) Object() bool {
294 // Rust has no modules which produce only object files.
295 return false
296}
297
Ivan Lozano52767be2019-10-18 14:49:46 -0700298func (mod *Module) Toc() android.OptionalPath {
299 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400300 if lib, ok := mod.compiler.(libraryInterface); ok {
301 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700302 }
303 }
304 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
305}
306
Colin Crossc511bc52020-04-07 16:50:32 +0000307func (mod *Module) UseSdk() bool {
308 return false
309}
310
Ivan Lozanod7586b62021-04-01 09:49:36 -0400311func (mod *Module) RelativeInstallPath() string {
312 if mod.compiler != nil {
313 return mod.compiler.relativeInstallPath()
314 }
315 return ""
316}
317
Ivan Lozano52767be2019-10-18 14:49:46 -0700318func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500319 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700320}
321
Jiyong Park7d55b612021-06-11 17:22:09 +0900322func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400323 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900324}
325
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400326func (mod *Module) SubName() string {
327 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700328}
329
Ivan Lozanof1868af2022-04-12 13:08:36 -0400330func (mod *Module) IsVndkPrebuiltLibrary() bool {
331 // Rust modules do not provide VNDK prebuilts
332 return false
333}
334
335func (mod *Module) IsVendorPublicLibrary() bool {
336 return mod.VendorProperties.IsVendorPublicLibrary
337}
338
339func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
340 // Rust modules to not provide Sdk variants
341 return false
342}
343
Colin Cross127bb8b2020-12-16 16:46:01 -0800344func (c *Module) IsVndkPrivate() bool {
345 return false
346}
347
348func (c *Module) IsLlndk() bool {
349 return false
350}
351
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400352func (mod *Module) KernelHeadersDecorator() bool {
353 return false
354}
355
Colin Cross1f3f1302021-04-26 18:37:44 -0700356func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400357 return false
358}
359
Colin Cross5271fea2021-04-27 13:06:04 -0700360func (m *Module) NeedsVendorPublicLibraryVariants() bool {
361 return false
362}
363
Ivan Lozanod7586b62021-04-01 09:49:36 -0400364func (mod *Module) HasLlndkStubs() bool {
365 return false
366}
367
368func (mod *Module) StubsVersion() string {
369 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
370}
371
Ivan Lozano52767be2019-10-18 14:49:46 -0700372func (mod *Module) SdkVersion() string {
373 return ""
374}
375
Colin Crossc511bc52020-04-07 16:50:32 +0000376func (mod *Module) AlwaysSdk() bool {
377 return false
378}
379
Jiyong Park2286afd2020-06-16 21:58:53 +0900380func (mod *Module) IsSdkVariant() bool {
381 return false
382}
383
Colin Cross1348ce32020-10-01 13:37:16 -0700384func (mod *Module) SplitPerApiLevel() bool {
385 return false
386}
387
Sasha Smundaka76acba2022-04-18 20:12:56 -0700388func (mod *Module) XrefRustFiles() android.Paths {
389 return mod.kytheFiles
390}
391
Ivan Lozanoffee3342019-08-27 12:03:00 -0700392type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400393 Dylibs []string
394 Rlibs []string
395 Rustlibs []string
396 Stdlibs []string
397 ProcMacros []string
398 SharedLibs []string
399 StaticLibs []string
400 WholeStaticLibs []string
401 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700402
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400403 // Used for data dependencies adjacent to tests
404 DataLibs []string
405 DataBins []string
406
Colin Crossfe605e12022-01-23 20:46:16 -0800407 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700408}
409
410type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700411 DyLibs RustLibraries
412 RLibs RustLibraries
413 SharedLibs android.Paths
414 SharedLibDeps android.Paths
415 StaticLibs android.Paths
416 ProcMacros RustLibraries
417 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500418
419 // depFlags and depLinkFlags are rustc and linker (clang) flags.
420 depFlags []string
421 depLinkFlags []string
422
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400423 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500424 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000425 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700426 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700427
Ivan Lozano0a468a42024-05-13 21:03:34 -0400428 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
429 // cc_library_static dependants of rlibs.
430 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
431 exportedLinkDirs []string
432
Ivan Lozano45901ed2020-07-24 16:05:01 -0400433 // Used by bindgen modules which call clang
434 depClangFlags []string
435 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400436 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400437 depSystemIncludePaths android.Paths
438
Colin Crossfe605e12022-01-23 20:46:16 -0800439 CrtBegin android.Paths
440 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700441
442 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500443 SrcDeps android.Paths
444 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445}
446
447type RustLibraries []RustLibrary
448
449type RustLibrary struct {
450 Path android.Path
451 CrateName string
452}
453
Matthew Maurerbb3add12020-06-25 09:34:12 -0700454type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000455 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700456 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700457}
458
Sasha Smundaka76acba2022-04-18 20:12:56 -0700459type xref interface {
460 XrefRustFiles() android.Paths
461}
462
Matthew Maurerbb3add12020-06-25 09:34:12 -0700463type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000464 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400465 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700466 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700467}
468
Wen-yi Chu41326c12023-09-22 03:58:59 +0000469func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
470 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700471}
472
Colin Cross004bd3f2023-10-02 11:39:17 -0700473func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
474 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400475}
476
Colin Cross0de8a1e2020-09-18 14:15:30 -0700477func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800478 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700479 LinkDirs: flagExporter.linkDirs,
480 LinkObjects: flagExporter.linkObjects,
481 })
482}
483
Matthew Maurerbb3add12020-06-25 09:34:12 -0700484var _ exportedFlagsProducer = (*flagExporter)(nil)
485
486func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700487 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700488}
489
Colin Cross0de8a1e2020-09-18 14:15:30 -0700490type FlagExporterInfo struct {
491 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000492 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700493 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700494}
495
Colin Crossbc7d76c2023-12-12 16:39:03 -0800496var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700497
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400498func (mod *Module) isCoverageVariant() bool {
499 return mod.coverage.Properties.IsCoverageVariant
500}
501
502var _ cc.Coverage = (*Module)(nil)
503
Colin Crossf5f4ad32024-01-19 15:41:48 -0800504func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400505 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
506}
507
Ivan Lozanod7586b62021-04-01 09:49:36 -0400508func (mod *Module) VndkVersion() string {
509 return mod.Properties.VndkVersion
510}
511
Ivan Lozano0a468a42024-05-13 21:03:34 -0400512func (mod *Module) ExportedCrateLinkDirs() []string {
513 return mod.exportedLinkDirs
514}
515
Ivan Lozanod7586b62021-04-01 09:49:36 -0400516func (mod *Module) PreventInstall() bool {
517 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400518}
519
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400520func (mod *Module) MarkAsCoverageVariant(coverage bool) {
521 mod.coverage.Properties.IsCoverageVariant = coverage
522}
523
524func (mod *Module) EnableCoverageIfNeeded() {
525 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700526}
527
528func defaultsFactory() android.Module {
529 return DefaultsFactory()
530}
531
532type Defaults struct {
533 android.ModuleBase
534 android.DefaultsModuleBase
535}
536
537func DefaultsFactory(props ...interface{}) android.Module {
538 module := &Defaults{}
539
540 module.AddProperties(props...)
541 module.AddProperties(
542 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800543 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500544 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100545 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400546 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547 &BaseCompilerProperties{},
548 &BinaryCompilerProperties{},
549 &LibraryCompilerProperties{},
550 &ProcMacroCompilerProperties{},
551 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400552 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700553 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400554 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400555 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200556 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500557 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000558 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700559 )
560
561 android.InitDefaultsModule(module)
562 return module
563}
564
565func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700566 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567}
568
Ivan Lozano183a3212019-10-18 14:18:45 -0700569func (mod *Module) CcLibrary() bool {
570 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500571 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700572 return true
573 }
574 }
575 return false
576}
577
578func (mod *Module) CcLibraryInterface() bool {
579 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400580 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
581 // VariantIs{Static,Shared} is set.
582 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700583 return true
584 }
585 }
586 return false
587}
588
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400589func (mod *Module) RustLibraryInterface() bool {
590 if mod.compiler != nil {
591 if _, ok := mod.compiler.(libraryInterface); ok {
592 return true
593 }
594 }
595 return false
596}
597
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500598func (mod *Module) IsFuzzModule() bool {
599 if _, ok := mod.compiler.(*fuzzDecorator); ok {
600 return true
601 }
602 return false
603}
604
605func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
606 return mod.FuzzModule
607}
608
609func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
610 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
611 return fuzzer.fuzzPackagedModule
612 }
613 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
614}
615
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000616func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500617 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
618 return fuzzer.sharedLibraries
619 }
620 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
621}
622
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400623func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400624 if mod.compiler != nil {
625 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400626 }
627 return nil
628}
629
Ivan Lozano183a3212019-10-18 14:18:45 -0700630func (mod *Module) SetStatic() {
631 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700632 if library, ok := mod.compiler.(libraryInterface); ok {
633 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700634 return
635 }
636 }
637 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
638}
639
640func (mod *Module) SetShared() {
641 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700642 if library, ok := mod.compiler.(libraryInterface); ok {
643 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700644 return
645 }
646 }
647 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
648}
649
Ivan Lozano183a3212019-10-18 14:18:45 -0700650func (mod *Module) BuildStaticVariant() bool {
651 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700652 if library, ok := mod.compiler.(libraryInterface); ok {
653 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700654 }
655 }
656 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
657}
658
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400659func (mod *Module) BuildRlibVariant() bool {
660 if mod.compiler != nil {
661 if library, ok := mod.compiler.(libraryInterface); ok {
662 return library.buildRlib()
663 }
664 }
665 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
666}
667
668func (mod *Module) IsRustFFI() bool {
669 if mod.compiler != nil {
670 if library, ok := mod.compiler.(libraryInterface); ok {
671 return library.isFFILibrary()
672 }
673 }
674 return false
675}
676
Ivan Lozano183a3212019-10-18 14:18:45 -0700677func (mod *Module) BuildSharedVariant() bool {
678 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700679 if library, ok := mod.compiler.(libraryInterface); ok {
680 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700681 }
682 }
683 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
684}
685
Ivan Lozano183a3212019-10-18 14:18:45 -0700686func (mod *Module) Module() android.Module {
687 return mod
688}
689
Ivan Lozano183a3212019-10-18 14:18:45 -0700690func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400691 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700692}
693
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400694func (mod *Module) CoverageFiles() android.Paths {
695 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800696 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400697 }
698 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
699}
700
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400701// Rust does not produce gcno files, and therefore does not produce a coverage archive.
702func (mod *Module) CoverageOutputFile() android.OptionalPath {
703 return android.OptionalPath{}
704}
705
706func (mod *Module) IsNdk(config android.Config) bool {
707 return false
708}
709
710func (mod *Module) IsStubs() bool {
711 return false
712}
713
Jiyong Park459feca2020-12-15 11:02:21 +0900714func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900715 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900716 return false
717 }
718
Jiyong Park459feca2020-12-15 11:02:21 +0900719 // The apex variant is not installable because it is included in the APEX and won't appear
720 // in the system partition as a standalone file.
721 if !apexInfo.IsForPlatform() {
722 return false
723 }
724
Jiyong Parke54f07e2021-04-07 15:08:04 +0900725 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900726}
727
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500728func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800729 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
730 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500731}
732
Ivan Lozano183a3212019-10-18 14:18:45 -0700733var _ cc.LinkableInterface = (*Module)(nil)
734
Ivan Lozanoffee3342019-08-27 12:03:00 -0700735func (mod *Module) Init() android.Module {
736 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500737 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738
Yi Kong46c6e592022-01-20 22:55:00 +0800739 if mod.afdo != nil {
740 mod.AddProperties(mod.afdo.props()...)
741 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 if mod.compiler != nil {
743 mod.AddProperties(mod.compiler.compilerProps()...)
744 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400745 if mod.coverage != nil {
746 mod.AddProperties(mod.coverage.props()...)
747 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200748 if mod.clippy != nil {
749 mod.AddProperties(mod.clippy.props()...)
750 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400751 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700752 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400753 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500754 if mod.sanitize != nil {
755 mod.AddProperties(mod.sanitize.props()...)
756 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400757
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900759 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760
761 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 return mod
763}
764
765func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
766 return &Module{
767 hod: hod,
768 multilib: multilib,
769 }
770}
771func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
772 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800773 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400774 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200775 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500776 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777 return module
778}
779
780type ModuleContext interface {
781 android.ModuleContext
782 ModuleContextIntf
783}
784
785type BaseModuleContext interface {
786 android.BaseModuleContext
787 ModuleContextIntf
788}
789
790type DepsContext interface {
791 android.BottomUpMutatorContext
792 ModuleContextIntf
793}
794
795type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200796 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798}
799
800type depsContext struct {
801 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802}
803
804type moduleContext struct {
805 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806}
807
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200808type baseModuleContext struct {
809 android.BaseModuleContext
810}
811
812func (ctx *moduleContext) RustModule() *Module {
813 return ctx.Module().(*Module)
814}
815
816func (ctx *moduleContext) toolchain() config.Toolchain {
817 return ctx.RustModule().toolchain(ctx)
818}
819
820func (ctx *depsContext) RustModule() *Module {
821 return ctx.Module().(*Module)
822}
823
824func (ctx *depsContext) toolchain() config.Toolchain {
825 return ctx.RustModule().toolchain(ctx)
826}
827
828func (ctx *baseModuleContext) RustModule() *Module {
829 return ctx.Module().(*Module)
830}
831
832func (ctx *baseModuleContext) toolchain() config.Toolchain {
833 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400834}
835
836func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700837 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
838 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
839 return false
840 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400841 return mod.compiler != nil && mod.compiler.nativeCoverage()
842}
843
Ivan Lozanod7586b62021-04-01 09:49:36 -0400844func (mod *Module) EverInstallable() bool {
845 return mod.compiler != nil &&
846 // Check to see whether the module is actually ever installable.
847 mod.compiler.everInstallable()
848}
849
850func (mod *Module) Installable() *bool {
851 return mod.Properties.Installable
852}
853
Ivan Lozano872d5792022-03-23 17:31:39 -0400854func (mod *Module) ProcMacro() bool {
855 if pm, ok := mod.compiler.(procMacroInterface); ok {
856 return pm.ProcMacro()
857 }
858 return false
859}
860
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
862 if mod.cachedToolchain == nil {
863 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
864 }
865 return mod.cachedToolchain
866}
867
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200868func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
869 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
870}
871
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
873}
874
875func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
876 ctx := &moduleContext{
877 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879
Colin Crossff694a82023-12-13 15:54:49 -0800880 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900881 if !apexInfo.IsForPlatform() {
882 mod.hideApexVariantFromMake = true
883 }
884
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500886 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
887
Ivan Lozanof1868af2022-04-12 13:08:36 -0400888 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700889
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 if !toolchain.Supported() {
891 // This toolchain's unsupported, there's nothing to do for this mod.
892 return
893 }
894
895 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400896 // Export linkDirs for CC rust generatedlibs
897 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
898 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
899
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 flags := Flags{
901 Toolchain: toolchain,
902 }
903
Ivan Lozano67eada32021-09-23 11:50:33 -0400904 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800905 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500906 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800907 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908 if mod.compiler != nil {
909 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400910 flags = mod.compiler.cfgFlags(ctx, flags)
911 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400912 }
913 if mod.coverage != nil {
914 flags, deps = mod.coverage.flags(ctx, flags, deps)
915 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200916 if mod.clippy != nil {
917 flags, deps = mod.clippy.flags(ctx, flags, deps)
918 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500919 if mod.sanitize != nil {
920 flags, deps = mod.sanitize.flags(ctx, flags, deps)
921 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400922
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200923 // SourceProvider needs to call GenerateSource() before compiler calls
924 // compile() so it can provide the source. A SourceProvider has
925 // multiple variants (e.g. source, rlib, dylib). Only the "source"
926 // variant is responsible for effectively generating the source. The
927 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400928 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200929 if mod.compiler.(libraryInterface).source() {
930 mod.sourceProvider.GenerateSource(ctx, deps)
931 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
932 } else {
933 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
934 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700935 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200936 }
Colin Cross40213022023-12-13 15:19:49 -0800937 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400938 }
939
940 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100941 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700942 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400943 if ctx.Failed() {
944 return
945 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700946 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
947 if buildOutput.kytheFile != nil {
948 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
949 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400950 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900951
Dan Albert06feee92021-03-19 15:06:02 -0700952 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000953 if mod.docTimestampFile.Valid() {
954 ctx.CheckbuildFile(mod.docTimestampFile.Path())
955 }
Dan Albert06feee92021-03-19 15:06:02 -0700956
Colin Crossff694a82023-12-13 15:54:49 -0800957 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400958 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900959 // If the module has been specifically configure to not be installed then
960 // hide from make as otherwise it will break when running inside make as the
961 // output path to install will not be specified. Not all uninstallable
962 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400963 // side dependencies. In particular, proc-macros need to be captured in the
964 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900965 mod.HideFromMake()
966 } else if !mod.installable(apexInfo) {
967 mod.SkipInstall()
968 }
969
970 // Still call install though, the installs will be stored as PackageSpecs to allow
971 // using the outputs in a genrule.
972 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200973 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900974 if ctx.Failed() {
975 return
976 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400977 // Export your own directory as a linkDir
978 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
979
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400980 }
Chris Wailes74be7642021-07-22 16:20:28 -0700981
982 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700983 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000984 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800985 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000986 }
Wei Lia1aa2972024-06-21 13:08:51 -0700987
mrziwang0cbd3b02024-06-20 16:39:25 -0700988 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -0700989
990 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -0700991}
992
993func (mod *Module) setOutputFiles(ctx ModuleContext) {
994 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
995 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
996 } else if mod.OutputFile().Valid() {
997 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
998 } else {
999 ctx.SetOutputFiles(android.Paths{}, "")
1000 }
1001 if mod.compiler != nil {
1002 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1003 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001004}
1005
Wei Lia1aa2972024-06-21 13:08:51 -07001006func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1007 // Dump metadata that can not be done in android/compliance-metadata.go
1008 metadataInfo := ctx.ComplianceMetadataInfo()
1009 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1010 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1011
1012 // Static libs
1013 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1014 staticDepNames := make([]string, 0, len(staticDeps))
1015 for _, dep := range staticDeps {
1016 staticDepNames = append(staticDepNames, dep.Name())
1017 }
1018 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1019 for _, dep := range ccStaticDeps {
1020 staticDepNames = append(staticDepNames, dep.Name())
1021 }
1022
1023 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1024 // C static libraries
1025 for _, dep := range deps.StaticLibs {
1026 staticDepPaths = append(staticDepPaths, dep.String())
1027 }
1028 // Rust static libraries
1029 for _, dep := range deps.RLibs {
1030 staticDepPaths = append(staticDepPaths, dep.Path.String())
1031 }
1032 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1033 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1034
1035 // C Whole static libs
1036 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1037 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1038 for _, dep := range ccStaticDeps {
1039 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1040 }
1041 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1042}
1043
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044func (mod *Module) deps(ctx DepsContext) Deps {
1045 deps := Deps{}
1046
1047 if mod.compiler != nil {
1048 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001049 }
1050 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001051 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052 }
1053
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001054 if mod.coverage != nil {
1055 deps = mod.coverage.deps(ctx, deps)
1056 }
1057
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001058 if mod.sanitize != nil {
1059 deps = mod.sanitize.deps(ctx, deps)
1060 }
1061
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1063 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001064 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1066 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1067 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001068 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001069 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070 return deps
1071
1072}
1073
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074type dependencyTag struct {
1075 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001076 name string
1077 library bool
1078 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001079 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001080}
1081
Jiyong Park65b62242020-11-25 12:44:59 +09001082// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1083// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1084func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001085 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001086}
1087
1088var _ android.InstallNeededDependencyTag = dependencyTag{}
1089
Colin Cross65cb3142021-12-10 23:05:02 +00001090func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1091 if d.library && d.dynamic {
1092 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1093 }
1094 return nil
1095}
1096
Yu Liuc8884602024-03-15 18:48:38 +00001097func (d dependencyTag) PropagateAconfigValidation() bool {
1098 return d == rlibDepTag || d == sourceDepTag
1099}
1100
1101var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1102
Colin Cross65cb3142021-12-10 23:05:02 +00001103var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1104
Ivan Lozanoffee3342019-08-27 12:03:00 -07001105var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001106 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1107 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001108 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001109 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001110 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001111 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001112 dataLibDepTag = dependencyTag{name: "data lib"}
1113 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001114)
1115
Jiyong Park99644e92020-11-17 22:21:02 +09001116func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1117 tag, ok := depTag.(dependencyTag)
1118 return ok && tag == dylibDepTag
1119}
1120
Jiyong Park94e22fd2021-04-08 18:19:15 +09001121func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1122 tag, ok := depTag.(dependencyTag)
1123 return ok && tag == rlibDepTag
1124}
1125
Matthew Maurer0f003b12020-06-29 14:34:06 -07001126type autoDep struct {
1127 variation string
1128 depTag dependencyTag
1129}
1130
1131var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001132 rlibVariation = "rlib"
1133 dylibVariation = "dylib"
1134 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1135 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001136)
1137
1138type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001139 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001140}
1141
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001142func (mod *Module) begin(ctx BaseModuleContext) {
1143 if mod.coverage != nil {
1144 mod.coverage.begin(ctx)
1145 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001146 if mod.sanitize != nil {
1147 mod.sanitize.begin(ctx)
1148 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001149}
1150
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001151func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001152 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001153 return p.prebuilt()
1154 }
1155 return nil
1156}
1157
Kiyoung Kim37693d02024-04-04 09:56:15 +09001158func (mod *Module) Symlinks() []string {
1159 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1160 return nil
1161}
1162
Justin Yun24b246a2023-03-16 10:36:16 +09001163func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1164 if rustDep, ok := dep.(*Module); ok {
1165 // Use base module name for snapshots when exporting to Makefile.
1166 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1167 baseName := rustDep.BaseModuleName()
1168 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1169 }
1170 }
1171 return cc.MakeLibName(ctx, c, dep, depName)
1172}
1173
Ivan Lozanod106efe2023-09-21 23:30:26 -04001174func collectIncludedProtos(mod *Module, dep *Module) {
1175 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1176 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1177 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1178 }
1179 }
1180}
Andrew Walbran52533232024-03-19 11:36:04 +00001181
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1183 var depPaths PathDeps
1184
1185 directRlibDeps := []*Module{}
1186 directDylibDeps := []*Module{}
1187 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001188 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001189 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001190 directSrcProvidersDeps := []*Module{}
1191 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001192
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001193 // For the dependency from platform to apex, use the latest stubs
1194 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001195 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001196 if !apexInfo.IsForPlatform() {
1197 mod.apexSdkVersion = apexInfo.MinSdkVersion
1198 }
1199
1200 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1201 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1202 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1203 // (b/144430859)
1204 mod.apexSdkVersion = android.FutureApiLevel
1205 }
1206
Spandan Das604f3762023-03-16 22:51:40 +00001207 skipModuleList := map[string]bool{}
1208
1209 var apiImportInfo multitree.ApiImportInfo
1210 hasApiImportInfo := false
1211
1212 ctx.VisitDirectDeps(func(dep android.Module) {
1213 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001214 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001215 hasApiImportInfo = true
1216 }
1217 })
1218
1219 if hasApiImportInfo {
1220 targetStubModuleList := map[string]string{}
1221 targetOrigModuleList := map[string]string{}
1222
1223 // Search for dependency which both original module and API imported library with APEX stub exists
1224 ctx.VisitDirectDeps(func(dep android.Module) {
1225 depName := ctx.OtherModuleName(dep)
1226 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1227 targetStubModuleList[apiLibrary] = depName
1228 }
1229 })
1230 ctx.VisitDirectDeps(func(dep android.Module) {
1231 depName := ctx.OtherModuleName(dep)
1232 if origLibrary, ok := targetStubModuleList[depName]; ok {
1233 targetOrigModuleList[origLibrary] = depName
1234 }
1235 })
1236
1237 // Decide which library should be used between original and API imported library
1238 ctx.VisitDirectDeps(func(dep android.Module) {
1239 depName := ctx.OtherModuleName(dep)
1240 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1241 if cc.ShouldUseStubForApex(ctx, dep) {
1242 skipModuleList[depName] = true
1243 } else {
1244 skipModuleList[apiLibrary] = true
1245 }
1246 }
1247 })
1248 }
1249
Cole Faustb6e6f992023-08-17 17:42:26 -07001250 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1251 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001252
Ivan Lozanoffee3342019-08-27 12:03:00 -07001253 ctx.VisitDirectDeps(func(dep android.Module) {
1254 depName := ctx.OtherModuleName(dep)
1255 depTag := ctx.OtherModuleDependencyTag(dep)
Spandan Das604f3762023-03-16 22:51:40 +00001256 if _, exists := skipModuleList[depName]; exists {
1257 return
1258 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001259
1260 if depTag == android.DarwinUniversalVariantTag {
1261 return
1262 }
1263
Ivan Lozano0a468a42024-05-13 21:03:34 -04001264 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001265 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001266 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001267
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001268 switch {
1269 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001270 dylib, ok := rustDep.compiler.(libraryInterface)
1271 if !ok || !dylib.dylib() {
1272 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1273 return
1274 }
1275 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001276 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001277 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1278
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001279 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001280 rlib, ok := rustDep.compiler.(libraryInterface)
1281 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001282 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 return
1284 }
1285 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001286 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001287 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1288
Ivan Lozano0a468a42024-05-13 21:03:34 -04001289 // rust_ffi rlibs may export include dirs, so collect those here.
1290 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1291 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1292 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1293
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001294 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001295 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001296 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001297 // proc_macro link dirs need to be exported, so collect those here.
1298 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001299
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001300 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001301 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1302 collectIncludedProtos(mod, rustDep)
1303 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001304 case cc.IsStaticDepTag(depTag):
1305 // Rust FFI rlibs should not be declared in a Rust modules
1306 // "static_libs" list as we can't handle them properly at the
1307 // moment (for example, they only produce an rlib-std variant).
1308 // Instead, a normal rust_library variant should be used.
1309 ctx.PropertyErrorf("static_libs",
1310 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1311 depName)
1312
Paul Duffind5cf92e2021-07-09 17:38:55 +01001313 }
1314
Cole Faustb6e6f992023-08-17 17:42:26 -07001315 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1316
Paul Duffind5cf92e2021-07-09 17:38:55 +01001317 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001318 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1319 // OS/Arch variant is used.
1320 var helper string
1321 if ctx.Host() {
1322 helper = "missing 'host_supported'?"
1323 } else {
1324 helper = "device module defined?"
1325 }
1326
1327 if dep.Target().Os != ctx.Os() {
1328 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1329 return
1330 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1331 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1332 return
1333 }
1334 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001335 }
1336
Ivan Lozano0a468a42024-05-13 21:03:34 -04001337 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001338 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001339 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001340 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1341 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001342 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001343 }
1344
Ivan Lozanoffee3342019-08-27 12:03:00 -07001345 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001346 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001347 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001348 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001349 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001350 }
1351 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001352
Ivan Lozanod106efe2023-09-21 23:30:26 -04001353 if depTag == sourceDepTag {
1354 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1355 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001356 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001357 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1358 }
1359 }
1360 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001361 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001362 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001363 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001364 if _, ok := ccDep.(*Module); !ok {
1365 if ccDep.Module().Target().Os != ctx.Os() {
1366 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1367 return
1368 }
1369 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1370 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1371 return
1372 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001373 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001374 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001375 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001376 if !ctx.Config().AllowMissingDependencies() {
1377 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1378 } else {
1379 ctx.AddMissingDependencies([]string{depName})
1380 }
1381 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001382 }
1383
Wen-yi Chu41326c12023-09-22 03:58:59 +00001384 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001385
Ivan Lozanoffee3342019-08-27 12:03:00 -07001386 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001387 switch {
1388 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001389 if cc.IsWholeStaticLib(depTag) {
1390 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1391 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001392 if mod.Binary() {
1393 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1394 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1395 // final linkage, pass the args directly to the linker to handle these cases.
1396 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1397 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001398 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001399 } else {
1400 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 -05001401 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001402 }
1403
1404 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1405 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001406 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001407 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1408
Colin Cross313aa542023-12-13 13:47:44 -08001409 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001410 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1411 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1412 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1413 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001414 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001415
1416 // Record baseLibName for snapshots.
1417 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1418
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001419 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001420 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001421 // For the shared lib dependencies, we may link to the stub variant
1422 // of the dependency depending on the context (e.g. if this
1423 // dependency crosses the APEX boundaries).
1424 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1425
1426 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1427 // object (either from stub or non-stub) to use.
1428 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001429 if !linkObject.Valid() {
1430 if !ctx.Config().AllowMissingDependencies() {
1431 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1432 } else {
1433 ctx.AddMissingDependencies([]string{depName})
1434 }
1435 return
1436 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001437 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001438
Ivan Lozanoffee3342019-08-27 12:03:00 -07001439 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001440 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001441 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1442 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1443 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1444 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001445 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001446
1447 // Record baseLibName for snapshots.
1448 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1449
Cole Faustb6e6f992023-08-17 17:42:26 -07001450 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001451 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001452 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001453 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001454 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1455 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1456 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001457 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001458 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001459 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001460 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001461 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001462 }
1463
1464 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001465 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1466 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001467 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001468 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001469 } else {
1470 switch {
1471 case depTag == cc.CrtBeginDepTag:
1472 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1473 case depTag == cc.CrtEndDepTag:
1474 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1475 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001477
1478 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001479 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001480 // These are usually genrules which don't have per-target variants.
1481 directSrcDeps = append(directSrcDeps, srcDep)
1482 }
1483 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001484 })
1485
Wen-yi Chu41326c12023-09-22 03:58:59 +00001486 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1487
1488 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001489 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001490 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001491 crateName := dep.CrateName()
1492 if alias, aliased := aliases[crateName]; aliased {
1493 crateName = alias
1494 }
1495 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001496 }
1497 var dylibDepFiles RustLibraries
1498 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001499 crateName := dep.CrateName()
1500 if alias, aliased := aliases[crateName]; aliased {
1501 crateName = alias
1502 }
1503 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001504 }
1505 var procMacroDepFiles RustLibraries
1506 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001507 crateName := dep.CrateName()
1508 if alias, aliased := aliases[crateName]; aliased {
1509 crateName = alias
1510 }
1511 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001512 }
1513
Colin Cross004bd3f2023-10-02 11:39:17 -07001514 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001515 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001516 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001517 }
1518
Colin Cross004bd3f2023-10-02 11:39:17 -07001519 var sharedLibFiles android.Paths
1520 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001521 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001522 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001523 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001524 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001525 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001526 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001527 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001528 }
1529
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001530 var srcProviderDepFiles android.Paths
1531 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001532 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001533 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1534 }
1535 for _, dep := range directSrcDeps {
1536 srcs := dep.Srcs()
1537 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1538 }
1539
Wen-yi Chu41326c12023-09-22 03:58:59 +00001540 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1541 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001542 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1543 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1544 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001545 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001546 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001547
1548 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001549 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001550 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001551 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001552 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1553 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1554 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001555
1556 return depPaths
1557}
1558
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001559func (mod *Module) InstallInData() bool {
1560 if mod.compiler == nil {
1561 return false
1562 }
1563 return mod.compiler.inData()
1564}
1565
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001566func (mod *Module) InstallInRamdisk() bool {
1567 return mod.InRamdisk()
1568}
1569
1570func (mod *Module) InstallInVendorRamdisk() bool {
1571 return mod.InVendorRamdisk()
1572}
1573
1574func (mod *Module) InstallInRecovery() bool {
1575 return mod.InRecovery()
1576}
1577
Wen-yi Chu41326c12023-09-22 03:58:59 +00001578func linkPathFromFilePath(filepath android.Path) string {
1579 return strings.Split(filepath.String(), filepath.Base())[0]
1580}
1581
Spandan Das604f3762023-03-16 22:51:40 +00001582// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1583func (r *Module) usePublicApi() bool {
1584 return r.Device() && r.UseSdk()
1585}
1586
1587// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1588func (r *Module) useVendorApi() bool {
1589 return r.Device() && (r.InVendor() || r.InProduct())
1590}
1591
Ivan Lozanoffee3342019-08-27 12:03:00 -07001592func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1593 ctx := &depsContext{
1594 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001595 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001596
1597 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001598 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001599
Kiyoung Kim487689e2022-07-26 09:48:22 +09001600 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001601 if mod.usePublicApi() || mod.useVendorApi() {
1602 for idx, lib := range deps.SharedLibs {
1603 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1604 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001605 }
1606
Ivan Lozano1921e802021-05-20 13:39:16 -04001607 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001608 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001609 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001610
Ivan Lozano2b081132020-09-08 12:46:52 -04001611 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001612 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001613 stdLinkage = "rlib-std"
1614 }
1615
1616 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001617 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001618
Ivan Lozano2b081132020-09-08 12:46:52 -04001619 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1620 rlibDepVariations = append(rlibDepVariations,
1621 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1622 }
1623
Ivan Lozano1921e802021-05-20 13:39:16 -04001624 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001625 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001626 for _, lib := range deps.Rlibs {
1627 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001628 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001629 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001630
1631 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001632 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001633 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1634
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001635 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001636 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001637 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001638
Ivan Lozano1921e802021-05-20 13:39:16 -04001639 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001640 if deps.Rustlibs != nil {
1641 if !mod.compiler.Disabled() {
1642 for _, lib := range deps.Rustlibs {
1643 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1644 if autoDep.depTag == rlibDepTag {
1645 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001646 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1647
Ivan Lozanod106efe2023-09-21 23:30:26 -04001648 } else {
1649 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1650 // Check for the existence of the dylib deptag variant. Select it if available,
1651 // otherwise select the rlib variant.
1652 autoDepVariations := append(commonDepVariations,
1653 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001654 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001655 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1656 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001657
Ivan Lozanod106efe2023-09-21 23:30:26 -04001658 } else {
1659 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001660 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1661
Ivan Lozanod106efe2023-09-21 23:30:26 -04001662 }
1663 }
1664 }
1665 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1666 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001667 srcProviderVariations := append(commonDepVariations,
1668 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001669 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
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 {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001685 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
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