blob: 9dae75ee5624b4071b91f71b03626a63e1a527d3 [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 Crosse1a85552024-06-14 12:17:37 -0700504func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) 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}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001110 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001111 dataLibDepTag = dependencyTag{name: "data lib"}
1112 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001113)
1114
Jiyong Park99644e92020-11-17 22:21:02 +09001115func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1116 tag, ok := depTag.(dependencyTag)
1117 return ok && tag == dylibDepTag
1118}
1119
Jiyong Park94e22fd2021-04-08 18:19:15 +09001120func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1121 tag, ok := depTag.(dependencyTag)
1122 return ok && tag == rlibDepTag
1123}
1124
Matthew Maurer0f003b12020-06-29 14:34:06 -07001125type autoDep struct {
1126 variation string
1127 depTag dependencyTag
1128}
1129
1130var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001131 rlibVariation = "rlib"
1132 dylibVariation = "dylib"
1133 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1134 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001135)
1136
1137type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001138 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001139}
1140
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001141func (mod *Module) begin(ctx BaseModuleContext) {
1142 if mod.coverage != nil {
1143 mod.coverage.begin(ctx)
1144 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001145 if mod.sanitize != nil {
1146 mod.sanitize.begin(ctx)
1147 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001148}
1149
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001150func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001151 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001152 return p.prebuilt()
1153 }
1154 return nil
1155}
1156
Kiyoung Kim37693d02024-04-04 09:56:15 +09001157func (mod *Module) Symlinks() []string {
1158 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1159 return nil
1160}
1161
Justin Yun24b246a2023-03-16 10:36:16 +09001162func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1163 if rustDep, ok := dep.(*Module); ok {
1164 // Use base module name for snapshots when exporting to Makefile.
1165 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1166 baseName := rustDep.BaseModuleName()
1167 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1168 }
1169 }
1170 return cc.MakeLibName(ctx, c, dep, depName)
1171}
1172
Ivan Lozanod106efe2023-09-21 23:30:26 -04001173func collectIncludedProtos(mod *Module, dep *Module) {
1174 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1175 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1176 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1177 }
1178 }
1179}
Andrew Walbran52533232024-03-19 11:36:04 +00001180
Ivan Lozanoffee3342019-08-27 12:03:00 -07001181func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1182 var depPaths PathDeps
1183
1184 directRlibDeps := []*Module{}
1185 directDylibDeps := []*Module{}
1186 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001187 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001188 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001189 directSrcProvidersDeps := []*Module{}
1190 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001191
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001192 // For the dependency from platform to apex, use the latest stubs
1193 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001194 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001195 if !apexInfo.IsForPlatform() {
1196 mod.apexSdkVersion = apexInfo.MinSdkVersion
1197 }
1198
1199 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1200 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1201 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1202 // (b/144430859)
1203 mod.apexSdkVersion = android.FutureApiLevel
1204 }
1205
Spandan Das604f3762023-03-16 22:51:40 +00001206 skipModuleList := map[string]bool{}
1207
1208 var apiImportInfo multitree.ApiImportInfo
1209 hasApiImportInfo := false
1210
1211 ctx.VisitDirectDeps(func(dep android.Module) {
1212 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001213 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001214 hasApiImportInfo = true
1215 }
1216 })
1217
1218 if hasApiImportInfo {
1219 targetStubModuleList := map[string]string{}
1220 targetOrigModuleList := map[string]string{}
1221
1222 // Search for dependency which both original module and API imported library with APEX stub exists
1223 ctx.VisitDirectDeps(func(dep android.Module) {
1224 depName := ctx.OtherModuleName(dep)
1225 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1226 targetStubModuleList[apiLibrary] = depName
1227 }
1228 })
1229 ctx.VisitDirectDeps(func(dep android.Module) {
1230 depName := ctx.OtherModuleName(dep)
1231 if origLibrary, ok := targetStubModuleList[depName]; ok {
1232 targetOrigModuleList[origLibrary] = depName
1233 }
1234 })
1235
1236 // Decide which library should be used between original and API imported library
1237 ctx.VisitDirectDeps(func(dep android.Module) {
1238 depName := ctx.OtherModuleName(dep)
1239 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1240 if cc.ShouldUseStubForApex(ctx, dep) {
1241 skipModuleList[depName] = true
1242 } else {
1243 skipModuleList[apiLibrary] = true
1244 }
1245 }
1246 })
1247 }
1248
Cole Faustb6e6f992023-08-17 17:42:26 -07001249 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1250 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001251
Ivan Lozanoffee3342019-08-27 12:03:00 -07001252 ctx.VisitDirectDeps(func(dep android.Module) {
1253 depName := ctx.OtherModuleName(dep)
1254 depTag := ctx.OtherModuleDependencyTag(dep)
Spandan Das604f3762023-03-16 22:51:40 +00001255 if _, exists := skipModuleList[depName]; exists {
1256 return
1257 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001258
1259 if depTag == android.DarwinUniversalVariantTag {
1260 return
1261 }
1262
Ivan Lozano0a468a42024-05-13 21:03:34 -04001263 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001264 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001265 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001266
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001267 switch {
1268 case depTag == dylibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269 dylib, ok := rustDep.compiler.(libraryInterface)
1270 if !ok || !dylib.dylib() {
1271 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1272 return
1273 }
1274 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001275 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001276 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1277
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001278 case depTag == rlibDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001279 rlib, ok := rustDep.compiler.(libraryInterface)
1280 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001281 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001282 return
1283 }
1284 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001285 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001286 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1287
Ivan Lozano0a468a42024-05-13 21:03:34 -04001288 // rust_ffi rlibs may export include dirs, so collect those here.
1289 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1290 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1291 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1292
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001293 case depTag == procMacroDepTag:
Ivan Lozanoffee3342019-08-27 12:03:00 -07001294 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001295 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001296 // proc_macro link dirs need to be exported, so collect those here.
1297 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001298
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001299 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001300 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1301 collectIncludedProtos(mod, rustDep)
1302 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001303 case cc.IsStaticDepTag(depTag):
1304 // Rust FFI rlibs should not be declared in a Rust modules
1305 // "static_libs" list as we can't handle them properly at the
1306 // moment (for example, they only produce an rlib-std variant).
1307 // Instead, a normal rust_library variant should be used.
1308 ctx.PropertyErrorf("static_libs",
1309 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1310 depName)
1311
Paul Duffind5cf92e2021-07-09 17:38:55 +01001312 }
1313
Cole Faustb6e6f992023-08-17 17:42:26 -07001314 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1315
Paul Duffind5cf92e2021-07-09 17:38:55 +01001316 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001317 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1318 // OS/Arch variant is used.
1319 var helper string
1320 if ctx.Host() {
1321 helper = "missing 'host_supported'?"
1322 } else {
1323 helper = "device module defined?"
1324 }
1325
1326 if dep.Target().Os != ctx.Os() {
1327 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1328 return
1329 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1330 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1331 return
1332 }
1333 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001334 }
1335
Ivan Lozano0a468a42024-05-13 21:03:34 -04001336 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001337 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001338 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001339 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1340 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001341 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001342 }
1343
Ivan Lozanoffee3342019-08-27 12:03:00 -07001344 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001345 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001346 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001347 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001348 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001349 }
1350 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001351
Ivan Lozanod106efe2023-09-21 23:30:26 -04001352 if depTag == sourceDepTag {
1353 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1354 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001355 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001356 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1357 }
1358 }
1359 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001360 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001361 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001362 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001363 if _, ok := ccDep.(*Module); !ok {
1364 if ccDep.Module().Target().Os != ctx.Os() {
1365 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1366 return
1367 }
1368 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1369 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1370 return
1371 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001372 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001373 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001374 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001375 if !ctx.Config().AllowMissingDependencies() {
1376 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1377 } else {
1378 ctx.AddMissingDependencies([]string{depName})
1379 }
1380 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001381 }
1382
Wen-yi Chu41326c12023-09-22 03:58:59 +00001383 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001384
Ivan Lozanoffee3342019-08-27 12:03:00 -07001385 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001386 switch {
1387 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001388 if cc.IsWholeStaticLib(depTag) {
1389 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1390 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001391 if mod.Binary() {
1392 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1393 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1394 // final linkage, pass the args directly to the linker to handle these cases.
1395 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1396 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001397 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001398 } else {
1399 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 -05001400 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001401 }
1402
1403 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1404 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001405 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001406 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1407
Colin Cross313aa542023-12-13 13:47:44 -08001408 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001409 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1410 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1411 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1412 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001413 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001414
1415 // Record baseLibName for snapshots.
1416 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1417
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001418 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001419 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001420 // For the shared lib dependencies, we may link to the stub variant
1421 // of the dependency depending on the context (e.g. if this
1422 // dependency crosses the APEX boundaries).
1423 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1424
1425 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1426 // object (either from stub or non-stub) to use.
1427 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001428 if !linkObject.Valid() {
1429 if !ctx.Config().AllowMissingDependencies() {
1430 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1431 } else {
1432 ctx.AddMissingDependencies([]string{depName})
1433 }
1434 return
1435 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001436 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001437
Ivan Lozanoffee3342019-08-27 12:03:00 -07001438 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001439 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001440 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1441 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1442 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1443 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001444 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001445
1446 // Record baseLibName for snapshots.
1447 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1448
Cole Faustb6e6f992023-08-17 17:42:26 -07001449 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001450 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001451 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001452 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001453 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1454 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1455 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001456 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001457 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001458 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001459 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001460 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001461 }
1462
1463 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001464 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1465 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001466 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001467 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001468 } else {
1469 switch {
1470 case depTag == cc.CrtBeginDepTag:
1471 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1472 case depTag == cc.CrtEndDepTag:
1473 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1474 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001475 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001476
1477 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001478 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001479 // These are usually genrules which don't have per-target variants.
1480 directSrcDeps = append(directSrcDeps, srcDep)
1481 }
1482 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483 })
1484
Wen-yi Chu41326c12023-09-22 03:58:59 +00001485 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1486
1487 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001488 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001489 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001490 crateName := dep.CrateName()
1491 if alias, aliased := aliases[crateName]; aliased {
1492 crateName = alias
1493 }
1494 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001495 }
1496 var dylibDepFiles RustLibraries
1497 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001498 crateName := dep.CrateName()
1499 if alias, aliased := aliases[crateName]; aliased {
1500 crateName = alias
1501 }
1502 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001503 }
1504 var procMacroDepFiles RustLibraries
1505 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001506 crateName := dep.CrateName()
1507 if alias, aliased := aliases[crateName]; aliased {
1508 crateName = alias
1509 }
1510 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001511 }
1512
Colin Cross004bd3f2023-10-02 11:39:17 -07001513 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001514 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001515 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001516 }
1517
Colin Cross004bd3f2023-10-02 11:39:17 -07001518 var sharedLibFiles android.Paths
1519 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001520 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001521 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001522 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001523 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001524 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001525 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001526 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001527 }
1528
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001529 var srcProviderDepFiles android.Paths
1530 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001531 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001532 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1533 }
1534 for _, dep := range directSrcDeps {
1535 srcs := dep.Srcs()
1536 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1537 }
1538
Wen-yi Chu41326c12023-09-22 03:58:59 +00001539 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1540 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001541 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1542 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1543 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001544 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001545 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001546
1547 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001548 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001549 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001550 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001551 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1552 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1553 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001554
1555 return depPaths
1556}
1557
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001558func (mod *Module) InstallInData() bool {
1559 if mod.compiler == nil {
1560 return false
1561 }
1562 return mod.compiler.inData()
1563}
1564
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001565func (mod *Module) InstallInRamdisk() bool {
1566 return mod.InRamdisk()
1567}
1568
1569func (mod *Module) InstallInVendorRamdisk() bool {
1570 return mod.InVendorRamdisk()
1571}
1572
1573func (mod *Module) InstallInRecovery() bool {
1574 return mod.InRecovery()
1575}
1576
Wen-yi Chu41326c12023-09-22 03:58:59 +00001577func linkPathFromFilePath(filepath android.Path) string {
1578 return strings.Split(filepath.String(), filepath.Base())[0]
1579}
1580
Spandan Das604f3762023-03-16 22:51:40 +00001581// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1582func (r *Module) usePublicApi() bool {
1583 return r.Device() && r.UseSdk()
1584}
1585
1586// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1587func (r *Module) useVendorApi() bool {
1588 return r.Device() && (r.InVendor() || r.InProduct())
1589}
1590
Ivan Lozanoffee3342019-08-27 12:03:00 -07001591func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1592 ctx := &depsContext{
1593 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001594 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001595
1596 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001597 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001598
Kiyoung Kim487689e2022-07-26 09:48:22 +09001599 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001600 if mod.usePublicApi() || mod.useVendorApi() {
1601 for idx, lib := range deps.SharedLibs {
1602 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1603 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001604 }
1605
Ivan Lozano1921e802021-05-20 13:39:16 -04001606 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001607 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001608 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001609
Ivan Lozano2b081132020-09-08 12:46:52 -04001610 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001611 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001612 stdLinkage = "rlib-std"
1613 }
1614
1615 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001616 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001617
Ivan Lozano2b081132020-09-08 12:46:52 -04001618 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1619 rlibDepVariations = append(rlibDepVariations,
1620 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1621 }
1622
Ivan Lozano1921e802021-05-20 13:39:16 -04001623 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001624 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001625 for _, lib := range deps.Rlibs {
1626 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001627 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001628 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001629
1630 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001631 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001632 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1633
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001634 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001635 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001636 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001637
Ivan Lozano1921e802021-05-20 13:39:16 -04001638 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001639 if deps.Rustlibs != nil {
1640 if !mod.compiler.Disabled() {
1641 for _, lib := range deps.Rustlibs {
1642 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1643 if autoDep.depTag == rlibDepTag {
1644 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001645 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1646
Ivan Lozanod106efe2023-09-21 23:30:26 -04001647 } else {
1648 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1649 // Check for the existence of the dylib deptag variant. Select it if available,
1650 // otherwise select the rlib variant.
1651 autoDepVariations := append(commonDepVariations,
1652 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001653 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001654 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1655 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001656
Ivan Lozanod106efe2023-09-21 23:30:26 -04001657 } else {
1658 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001659 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1660
Ivan Lozanod106efe2023-09-21 23:30:26 -04001661 }
1662 }
1663 }
1664 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1665 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001666 srcProviderVariations := append(commonDepVariations,
1667 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001668 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001669
Ivan Lozano0a468a42024-05-13 21:03:34 -04001670 // Only add rustlib dependencies if they're source providers themselves.
1671 // This is used to track which crate names need to be added to the source generated
1672 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001673 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001674 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001675 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001676 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001677 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001678 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001679
Ivan Lozano1921e802021-05-20 13:39:16 -04001680 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001681 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001682 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001683 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001684 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001685 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001686 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001687 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001688 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001689 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1690
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001691 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001692 }
1693 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001694
1695 for _, lib := range deps.SharedLibs {
1696 depTag := cc.SharedDepTag()
1697 name, version := cc.StubsLibNameAndVersion(lib)
1698
1699 variations := []blueprint.Variation{
1700 {Mutator: "link", Variation: "shared"},
1701 }
Spandan Das604f3762023-03-16 22:51:40 +00001702 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1703 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1704 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1705 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1706 }
1707
1708 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1709 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1710 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001711 }
1712
1713 for _, lib := range deps.WholeStaticLibs {
1714 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001715
1716 actx.AddVariationDependencies([]blueprint.Variation{
1717 {Mutator: "link", Variation: "static"},
1718 }, depTag, lib)
1719 }
1720
1721 for _, lib := range deps.StaticLibs {
1722 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001723
1724 actx.AddVariationDependencies([]blueprint.Variation{
1725 {Mutator: "link", Variation: "static"},
1726 }, depTag, lib)
1727 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001728
Zach Johnson3df4e632020-11-06 11:56:27 -08001729 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1730
Colin Cross565cafd2020-09-25 18:47:38 -07001731 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001732 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001733 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001734 }
Colin Crossfe605e12022-01-23 20:46:16 -08001735 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001736 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001737 }
1738
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001739 if mod.sourceProvider != nil {
1740 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1741 bindgen.Properties.Custom_bindgen != "" {
1742 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1743 bindgen.Properties.Custom_bindgen)
1744 }
1745 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001746
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001747 actx.AddVariationDependencies([]blueprint.Variation{
1748 {Mutator: "link", Variation: "shared"},
1749 }, dataLibDepTag, deps.DataLibs...)
1750
1751 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1752
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001753 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001754 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001755
1756 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001757}
1758
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001759func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001760 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001761 mod.beginMutator(ctx)
1762 }
1763}
1764
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001765func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1766 ctx := &baseModuleContext{
1767 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001768 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001769
1770 mod.begin(ctx)
1771}
1772
Ivan Lozanoffee3342019-08-27 12:03:00 -07001773func (mod *Module) Name() string {
1774 name := mod.ModuleBase.Name()
1775 if p, ok := mod.compiler.(interface {
1776 Name(string) string
1777 }); ok {
1778 name = p.Name(name)
1779 }
1780 return name
1781}
1782
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001783func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001784 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001785 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001786 }
1787}
1788
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001789var _ android.HostToolProvider = (*Module)(nil)
1790
1791func (mod *Module) HostToolPath() android.OptionalPath {
1792 if !mod.Host() {
1793 return android.OptionalPath{}
1794 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001795 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1796 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001797 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1798 // Even though proc-macros aren't strictly "tools", since they target the compiler
1799 // and act as compiler plugins, we treat them similarly.
1800 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001801 }
1802 return android.OptionalPath{}
1803}
1804
Jiyong Park99644e92020-11-17 22:21:02 +09001805var _ android.ApexModule = (*Module)(nil)
1806
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001807func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001808 return String(mod.Properties.Min_sdk_version)
1809}
1810
Jiyong Park45bf82e2020-12-15 22:29:02 +09001811// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001812func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001813 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001814 if minSdkVersion == "apex_inherit" {
1815 return nil
1816 }
1817 if minSdkVersion == "" {
1818 return fmt.Errorf("min_sdk_version is not specificed")
1819 }
1820
1821 // Not using nativeApiLevelFromUser because the context here is not
1822 // necessarily a native context.
1823 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1824 if err != nil {
1825 return err
1826 }
1827
1828 if ver.GreaterThan(sdkVersion) {
1829 return fmt.Errorf("newer SDK(%v)", ver)
1830 }
Jiyong Park99644e92020-11-17 22:21:02 +09001831 return nil
1832}
1833
Jiyong Park45bf82e2020-12-15 22:29:02 +09001834// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001835func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1836 depTag := ctx.OtherModuleDependencyTag(dep)
1837
1838 if ccm, ok := dep.(*cc.Module); ok {
1839 if ccm.HasStubsVariants() {
1840 if cc.IsSharedDepTag(depTag) {
1841 // dynamic dep to a stubs lib crosses APEX boundary
1842 return false
1843 }
1844 if cc.IsRuntimeDepTag(depTag) {
1845 // runtime dep to a stubs lib also crosses APEX boundary
1846 return false
1847 }
1848
1849 if cc.IsHeaderDepTag(depTag) {
1850 return false
1851 }
1852 }
1853 if mod.Static() && cc.IsSharedDepTag(depTag) {
1854 // shared_lib dependency from a static lib is considered as crossing
1855 // the APEX boundary because the dependency doesn't actually is
1856 // linked; the dependency is used only during the compilation phase.
1857 return false
1858 }
1859 }
1860
Matthew Maurer581b6d82022-09-29 16:46:25 -07001861 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001862 return false
1863 }
1864
1865 return true
1866}
1867
1868// Overrides ApexModule.IsInstallabeToApex()
1869func (mod *Module) IsInstallableToApex() bool {
1870 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001871 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001872 return true
1873 }
1874 if _, ok := mod.compiler.(*binaryDecorator); ok {
1875 return true
1876 }
1877 }
1878 return false
1879}
1880
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001881// If a library file has a "lib" prefix, extract the library name without the prefix.
1882func libNameFromFilePath(filepath android.Path) (string, bool) {
1883 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1884 if strings.HasPrefix(libName, "lib") {
1885 libName = libName[3:]
1886 return libName, true
1887 }
1888 return "", false
1889}
1890
Sasha Smundaka76acba2022-04-18 20:12:56 -07001891func kytheExtractRustFactory() android.Singleton {
1892 return &kytheExtractRustSingleton{}
1893}
1894
1895type kytheExtractRustSingleton struct {
1896}
1897
1898func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1899 var xrefTargets android.Paths
1900 ctx.VisitAllModules(func(module android.Module) {
1901 if rustModule, ok := module.(xref); ok {
1902 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1903 }
1904 })
1905 if len(xrefTargets) > 0 {
1906 ctx.Phony("xref_rust", xrefTargets...)
1907 }
1908}
1909
Jihoon Kangf78a8902022-09-01 22:47:07 +00001910func (c *Module) Partition() string {
1911 return ""
1912}
1913
Ivan Lozanoffee3342019-08-27 12:03:00 -07001914var Bool = proptools.Bool
1915var BoolDefault = proptools.BoolDefault
1916var String = proptools.String
1917var StringPtr = proptools.StringPtr