blob: 3a3ca4d336c508aab51656c88b835ed85c91ffdb [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
423 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
424 // 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 Lozano0a468a42024-05-13 21:03:34 -0400445
446 // Used by Generated Libraries
447 depExportedRlibs []cc.RustRlibDep
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448}
449
450type RustLibraries []RustLibrary
451
452type RustLibrary struct {
453 Path android.Path
454 CrateName string
455}
456
Matthew Maurerbb3add12020-06-25 09:34:12 -0700457type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000458 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700459 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700460}
461
Sasha Smundaka76acba2022-04-18 20:12:56 -0700462type xref interface {
463 XrefRustFiles() android.Paths
464}
465
Matthew Maurerbb3add12020-06-25 09:34:12 -0700466type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000467 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700468 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700469}
470
Wen-yi Chu41326c12023-09-22 03:58:59 +0000471func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
472 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700473}
474
Colin Cross004bd3f2023-10-02 11:39:17 -0700475func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
476 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400477}
478
Colin Cross0de8a1e2020-09-18 14:15:30 -0700479func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800480 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700481 LinkDirs: flagExporter.linkDirs,
482 LinkObjects: flagExporter.linkObjects,
483 })
484}
485
Matthew Maurerbb3add12020-06-25 09:34:12 -0700486var _ exportedFlagsProducer = (*flagExporter)(nil)
487
488func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700489 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700490}
491
Colin Cross0de8a1e2020-09-18 14:15:30 -0700492type FlagExporterInfo struct {
493 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000494 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700495 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700496}
497
Colin Crossbc7d76c2023-12-12 16:39:03 -0800498var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700499
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400500func (mod *Module) isCoverageVariant() bool {
501 return mod.coverage.Properties.IsCoverageVariant
502}
503
504var _ cc.Coverage = (*Module)(nil)
505
Colin Crossf5f4ad32024-01-19 15:41:48 -0800506func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400507 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
508}
509
Ivan Lozanod7586b62021-04-01 09:49:36 -0400510func (mod *Module) VndkVersion() string {
511 return mod.Properties.VndkVersion
512}
513
Ivan Lozano0a468a42024-05-13 21:03:34 -0400514func (mod *Module) ExportedCrateLinkDirs() []string {
515 return mod.exportedLinkDirs
516}
517
Ivan Lozanod7586b62021-04-01 09:49:36 -0400518func (mod *Module) PreventInstall() bool {
519 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400520}
521
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400522func (mod *Module) MarkAsCoverageVariant(coverage bool) {
523 mod.coverage.Properties.IsCoverageVariant = coverage
524}
525
526func (mod *Module) EnableCoverageIfNeeded() {
527 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700528}
529
530func defaultsFactory() android.Module {
531 return DefaultsFactory()
532}
533
534type Defaults struct {
535 android.ModuleBase
536 android.DefaultsModuleBase
537}
538
539func DefaultsFactory(props ...interface{}) android.Module {
540 module := &Defaults{}
541
542 module.AddProperties(props...)
543 module.AddProperties(
544 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800545 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500546 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100547 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400548 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700549 &BaseCompilerProperties{},
550 &BinaryCompilerProperties{},
551 &LibraryCompilerProperties{},
552 &ProcMacroCompilerProperties{},
553 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400554 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700555 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400556 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400557 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200558 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500559 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000560 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700561 )
562
563 android.InitDefaultsModule(module)
564 return module
565}
566
567func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700568 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569}
570
Ivan Lozano183a3212019-10-18 14:18:45 -0700571func (mod *Module) CcLibrary() bool {
572 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500573 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700574 return true
575 }
576 }
577 return false
578}
579
580func (mod *Module) CcLibraryInterface() bool {
581 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400582 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
583 // VariantIs{Static,Shared} is set.
584 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700585 return true
586 }
587 }
588 return false
589}
590
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400591func (mod *Module) RustLibraryInterface() bool {
592 if mod.compiler != nil {
593 if _, ok := mod.compiler.(libraryInterface); ok {
594 return true
595 }
596 }
597 return false
598}
599
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500600func (mod *Module) IsFuzzModule() bool {
601 if _, ok := mod.compiler.(*fuzzDecorator); ok {
602 return true
603 }
604 return false
605}
606
607func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
608 return mod.FuzzModule
609}
610
611func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
612 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
613 return fuzzer.fuzzPackagedModule
614 }
615 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
616}
617
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000618func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500619 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
620 return fuzzer.sharedLibraries
621 }
622 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
623}
624
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400625func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400626 if mod.compiler != nil {
627 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400628 }
629 return nil
630}
631
Ivan Lozano183a3212019-10-18 14:18:45 -0700632func (mod *Module) SetStatic() {
633 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700634 if library, ok := mod.compiler.(libraryInterface); ok {
635 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700636 return
637 }
638 }
639 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
640}
641
642func (mod *Module) SetShared() {
643 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700644 if library, ok := mod.compiler.(libraryInterface); ok {
645 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700646 return
647 }
648 }
649 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
650}
651
Ivan Lozano183a3212019-10-18 14:18:45 -0700652func (mod *Module) BuildStaticVariant() bool {
653 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700654 if library, ok := mod.compiler.(libraryInterface); ok {
655 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700656 }
657 }
658 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
659}
660
661func (mod *Module) BuildSharedVariant() bool {
662 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700663 if library, ok := mod.compiler.(libraryInterface); ok {
664 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700665 }
666 }
667 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
668}
669
Ivan Lozano183a3212019-10-18 14:18:45 -0700670func (mod *Module) Module() android.Module {
671 return mod
672}
673
Ivan Lozano183a3212019-10-18 14:18:45 -0700674func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400675 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700676}
677
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400678func (mod *Module) CoverageFiles() android.Paths {
679 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800680 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400681 }
682 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
683}
684
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400685// Rust does not produce gcno files, and therefore does not produce a coverage archive.
686func (mod *Module) CoverageOutputFile() android.OptionalPath {
687 return android.OptionalPath{}
688}
689
690func (mod *Module) IsNdk(config android.Config) bool {
691 return false
692}
693
694func (mod *Module) IsStubs() bool {
695 return false
696}
697
Jiyong Park459feca2020-12-15 11:02:21 +0900698func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900699 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900700 return false
701 }
702
Jiyong Park459feca2020-12-15 11:02:21 +0900703 // The apex variant is not installable because it is included in the APEX and won't appear
704 // in the system partition as a standalone file.
705 if !apexInfo.IsForPlatform() {
706 return false
707 }
708
Jiyong Parke54f07e2021-04-07 15:08:04 +0900709 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900710}
711
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500712func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800713 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
714 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500715}
716
Ivan Lozano183a3212019-10-18 14:18:45 -0700717var _ cc.LinkableInterface = (*Module)(nil)
718
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719func (mod *Module) Init() android.Module {
720 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500721 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700722
Yi Kong46c6e592022-01-20 22:55:00 +0800723 if mod.afdo != nil {
724 mod.AddProperties(mod.afdo.props()...)
725 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726 if mod.compiler != nil {
727 mod.AddProperties(mod.compiler.compilerProps()...)
728 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400729 if mod.coverage != nil {
730 mod.AddProperties(mod.coverage.props()...)
731 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200732 if mod.clippy != nil {
733 mod.AddProperties(mod.clippy.props()...)
734 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400735 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700736 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400737 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500738 if mod.sanitize != nil {
739 mod.AddProperties(mod.sanitize.props()...)
740 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400741
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900743 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744
745 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 return mod
747}
748
749func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
750 return &Module{
751 hod: hod,
752 multilib: multilib,
753 }
754}
755func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
756 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800757 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400758 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200759 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500760 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 return module
762}
763
764type ModuleContext interface {
765 android.ModuleContext
766 ModuleContextIntf
767}
768
769type BaseModuleContext interface {
770 android.BaseModuleContext
771 ModuleContextIntf
772}
773
774type DepsContext interface {
775 android.BottomUpMutatorContext
776 ModuleContextIntf
777}
778
779type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200780 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782}
783
784type depsContext struct {
785 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786}
787
788type moduleContext struct {
789 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790}
791
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200792type baseModuleContext struct {
793 android.BaseModuleContext
794}
795
796func (ctx *moduleContext) RustModule() *Module {
797 return ctx.Module().(*Module)
798}
799
800func (ctx *moduleContext) toolchain() config.Toolchain {
801 return ctx.RustModule().toolchain(ctx)
802}
803
804func (ctx *depsContext) RustModule() *Module {
805 return ctx.Module().(*Module)
806}
807
808func (ctx *depsContext) toolchain() config.Toolchain {
809 return ctx.RustModule().toolchain(ctx)
810}
811
812func (ctx *baseModuleContext) RustModule() *Module {
813 return ctx.Module().(*Module)
814}
815
816func (ctx *baseModuleContext) toolchain() config.Toolchain {
817 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400818}
819
820func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700821 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
822 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
823 return false
824 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400825 return mod.compiler != nil && mod.compiler.nativeCoverage()
826}
827
Ivan Lozanod7586b62021-04-01 09:49:36 -0400828func (mod *Module) EverInstallable() bool {
829 return mod.compiler != nil &&
830 // Check to see whether the module is actually ever installable.
831 mod.compiler.everInstallable()
832}
833
834func (mod *Module) Installable() *bool {
835 return mod.Properties.Installable
836}
837
Ivan Lozano872d5792022-03-23 17:31:39 -0400838func (mod *Module) ProcMacro() bool {
839 if pm, ok := mod.compiler.(procMacroInterface); ok {
840 return pm.ProcMacro()
841 }
842 return false
843}
844
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
846 if mod.cachedToolchain == nil {
847 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
848 }
849 return mod.cachedToolchain
850}
851
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200852func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
853 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
854}
855
Ivan Lozanoffee3342019-08-27 12:03:00 -0700856func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
857}
858
859func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
860 ctx := &moduleContext{
861 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863
Colin Crossff694a82023-12-13 15:54:49 -0800864 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900865 if !apexInfo.IsForPlatform() {
866 mod.hideApexVariantFromMake = true
867 }
868
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500870 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
871
Ivan Lozanof1868af2022-04-12 13:08:36 -0400872 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700873
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 if !toolchain.Supported() {
875 // This toolchain's unsupported, there's nothing to do for this mod.
876 return
877 }
878
879 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400880 // Export linkDirs for CC rust generatedlibs
881 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
882 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
883
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 flags := Flags{
885 Toolchain: toolchain,
886 }
887
Ivan Lozano67eada32021-09-23 11:50:33 -0400888 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800889 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500890 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800891 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 if mod.compiler != nil {
893 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400894 flags = mod.compiler.cfgFlags(ctx, flags)
895 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400896 }
897 if mod.coverage != nil {
898 flags, deps = mod.coverage.flags(ctx, flags, deps)
899 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200900 if mod.clippy != nil {
901 flags, deps = mod.clippy.flags(ctx, flags, deps)
902 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500903 if mod.sanitize != nil {
904 flags, deps = mod.sanitize.flags(ctx, flags, deps)
905 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400906
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200907 // SourceProvider needs to call GenerateSource() before compiler calls
908 // compile() so it can provide the source. A SourceProvider has
909 // multiple variants (e.g. source, rlib, dylib). Only the "source"
910 // variant is responsible for effectively generating the source. The
911 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400912 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200913 if mod.compiler.(libraryInterface).source() {
914 mod.sourceProvider.GenerateSource(ctx, deps)
915 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
916 } else {
917 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
918 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700919 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200920 }
Colin Cross40213022023-12-13 15:19:49 -0800921 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400922 }
923
924 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100925 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700926 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400927 if ctx.Failed() {
928 return
929 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700930 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
931 if buildOutput.kytheFile != nil {
932 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
933 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400934 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900935
Dan Albert06feee92021-03-19 15:06:02 -0700936 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000937 if mod.docTimestampFile.Valid() {
938 ctx.CheckbuildFile(mod.docTimestampFile.Path())
939 }
Dan Albert06feee92021-03-19 15:06:02 -0700940
Colin Crossff694a82023-12-13 15:54:49 -0800941 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400942 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900943 // If the module has been specifically configure to not be installed then
944 // hide from make as otherwise it will break when running inside make as the
945 // output path to install will not be specified. Not all uninstallable
946 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400947 // side dependencies. In particular, proc-macros need to be captured in the
948 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900949 mod.HideFromMake()
950 } else if !mod.installable(apexInfo) {
951 mod.SkipInstall()
952 }
953
954 // Still call install though, the installs will be stored as PackageSpecs to allow
955 // using the outputs in a genrule.
956 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200957 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900958 if ctx.Failed() {
959 return
960 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400961 // Export your own directory as a linkDir
962 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
963
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400964 }
Chris Wailes74be7642021-07-22 16:20:28 -0700965
966 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000968 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800969 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000970 }
Wei Lia1aa2972024-06-21 13:08:51 -0700971
mrziwang0cbd3b02024-06-20 16:39:25 -0700972 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -0700973
974 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -0700975}
976
977func (mod *Module) setOutputFiles(ctx ModuleContext) {
978 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
979 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
980 } else if mod.OutputFile().Valid() {
981 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
982 } else {
983 ctx.SetOutputFiles(android.Paths{}, "")
984 }
985 if mod.compiler != nil {
986 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
987 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700988}
989
Wei Lia1aa2972024-06-21 13:08:51 -0700990func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
991 // Dump metadata that can not be done in android/compliance-metadata.go
992 metadataInfo := ctx.ComplianceMetadataInfo()
993 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
994 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
995
996 // Static libs
997 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
998 staticDepNames := make([]string, 0, len(staticDeps))
999 for _, dep := range staticDeps {
1000 staticDepNames = append(staticDepNames, dep.Name())
1001 }
1002 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1003 for _, dep := range ccStaticDeps {
1004 staticDepNames = append(staticDepNames, dep.Name())
1005 }
1006
1007 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1008 // C static libraries
1009 for _, dep := range deps.StaticLibs {
1010 staticDepPaths = append(staticDepPaths, dep.String())
1011 }
1012 // Rust static libraries
1013 for _, dep := range deps.RLibs {
1014 staticDepPaths = append(staticDepPaths, dep.Path.String())
1015 }
1016 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1017 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1018
1019 // C Whole static libs
1020 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1021 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1022 for _, dep := range ccStaticDeps {
1023 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1024 }
1025 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1026}
1027
Ivan Lozanoffee3342019-08-27 12:03:00 -07001028func (mod *Module) deps(ctx DepsContext) Deps {
1029 deps := Deps{}
1030
1031 if mod.compiler != nil {
1032 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001033 }
1034 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001035 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001036 }
1037
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001038 if mod.coverage != nil {
1039 deps = mod.coverage.deps(ctx, deps)
1040 }
1041
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001042 if mod.sanitize != nil {
1043 deps = mod.sanitize.deps(ctx, deps)
1044 }
1045
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1047 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001048 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1050 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1051 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001052 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001053 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054 return deps
1055
1056}
1057
Ivan Lozanoffee3342019-08-27 12:03:00 -07001058type dependencyTag struct {
1059 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001060 name string
1061 library bool
1062 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001063 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001064}
1065
Jiyong Park65b62242020-11-25 12:44:59 +09001066// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1067// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1068func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001069 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001070}
1071
1072var _ android.InstallNeededDependencyTag = dependencyTag{}
1073
Colin Cross65cb3142021-12-10 23:05:02 +00001074func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1075 if d.library && d.dynamic {
1076 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1077 }
1078 return nil
1079}
1080
Yu Liuc8884602024-03-15 18:48:38 +00001081func (d dependencyTag) PropagateAconfigValidation() bool {
1082 return d == rlibDepTag || d == sourceDepTag
1083}
1084
1085var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1086
Colin Cross65cb3142021-12-10 23:05:02 +00001087var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1088
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001090 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1091 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001092 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001093 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001094 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001095 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001096 dataLibDepTag = dependencyTag{name: "data lib"}
1097 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001098)
1099
Jiyong Park99644e92020-11-17 22:21:02 +09001100func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1101 tag, ok := depTag.(dependencyTag)
1102 return ok && tag == dylibDepTag
1103}
1104
Jiyong Park94e22fd2021-04-08 18:19:15 +09001105func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1106 tag, ok := depTag.(dependencyTag)
1107 return ok && tag == rlibDepTag
1108}
1109
Matthew Maurer0f003b12020-06-29 14:34:06 -07001110type autoDep struct {
1111 variation string
1112 depTag dependencyTag
1113}
1114
1115var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001116 rlibVariation = "rlib"
1117 dylibVariation = "dylib"
1118 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1119 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001120)
1121
1122type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001123 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001124}
1125
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001126func (mod *Module) begin(ctx BaseModuleContext) {
1127 if mod.coverage != nil {
1128 mod.coverage.begin(ctx)
1129 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001130 if mod.sanitize != nil {
1131 mod.sanitize.begin(ctx)
1132 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001133}
1134
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001135func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001136 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001137 return p.prebuilt()
1138 }
1139 return nil
1140}
1141
Kiyoung Kim37693d02024-04-04 09:56:15 +09001142func (mod *Module) Symlinks() []string {
1143 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1144 return nil
1145}
1146
Justin Yun24b246a2023-03-16 10:36:16 +09001147func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1148 if rustDep, ok := dep.(*Module); ok {
1149 // Use base module name for snapshots when exporting to Makefile.
1150 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1151 baseName := rustDep.BaseModuleName()
1152 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1153 }
1154 }
1155 return cc.MakeLibName(ctx, c, dep, depName)
1156}
1157
Ivan Lozanod106efe2023-09-21 23:30:26 -04001158func collectIncludedProtos(mod *Module, dep *Module) {
1159 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1160 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1161 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1162 }
1163 }
1164}
Andrew Walbran52533232024-03-19 11:36:04 +00001165
Ivan Lozanoffee3342019-08-27 12:03:00 -07001166func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1167 var depPaths PathDeps
1168
1169 directRlibDeps := []*Module{}
1170 directDylibDeps := []*Module{}
1171 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001172 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001173 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001174 directSrcProvidersDeps := []*Module{}
1175 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001176
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001177 // For the dependency from platform to apex, use the latest stubs
1178 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001179 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001180 if !apexInfo.IsForPlatform() {
1181 mod.apexSdkVersion = apexInfo.MinSdkVersion
1182 }
1183
1184 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1185 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1186 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1187 // (b/144430859)
1188 mod.apexSdkVersion = android.FutureApiLevel
1189 }
1190
Spandan Das604f3762023-03-16 22:51:40 +00001191 skipModuleList := map[string]bool{}
1192
1193 var apiImportInfo multitree.ApiImportInfo
1194 hasApiImportInfo := false
1195
1196 ctx.VisitDirectDeps(func(dep android.Module) {
1197 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001198 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001199 hasApiImportInfo = true
1200 }
1201 })
1202
1203 if hasApiImportInfo {
1204 targetStubModuleList := map[string]string{}
1205 targetOrigModuleList := map[string]string{}
1206
1207 // Search for dependency which both original module and API imported library with APEX stub exists
1208 ctx.VisitDirectDeps(func(dep android.Module) {
1209 depName := ctx.OtherModuleName(dep)
1210 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1211 targetStubModuleList[apiLibrary] = depName
1212 }
1213 })
1214 ctx.VisitDirectDeps(func(dep android.Module) {
1215 depName := ctx.OtherModuleName(dep)
1216 if origLibrary, ok := targetStubModuleList[depName]; ok {
1217 targetOrigModuleList[origLibrary] = depName
1218 }
1219 })
1220
1221 // Decide which library should be used between original and API imported library
1222 ctx.VisitDirectDeps(func(dep android.Module) {
1223 depName := ctx.OtherModuleName(dep)
1224 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1225 if cc.ShouldUseStubForApex(ctx, dep) {
1226 skipModuleList[depName] = true
1227 } else {
1228 skipModuleList[apiLibrary] = true
1229 }
1230 }
1231 })
1232 }
1233
Cole Faustb6e6f992023-08-17 17:42:26 -07001234 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1235 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001236
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237 ctx.VisitDirectDeps(func(dep android.Module) {
1238 depName := ctx.OtherModuleName(dep)
1239 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001240
Spandan Das604f3762023-03-16 22:51:40 +00001241 if _, exists := skipModuleList[depName]; exists {
1242 return
1243 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001244
1245 if depTag == android.DarwinUniversalVariantTag {
1246 return
1247 }
1248
Ivan Lozano0a468a42024-05-13 21:03:34 -04001249 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001250 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001251 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001252
Ivan Lozanoffee3342019-08-27 12:03:00 -07001253 switch depTag {
1254 case dylibDepTag:
1255 dylib, ok := rustDep.compiler.(libraryInterface)
1256 if !ok || !dylib.dylib() {
1257 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1258 return
1259 }
1260 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001261 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001262 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1263
Ivan Lozanoffee3342019-08-27 12:03:00 -07001264 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001265
Ivan Lozanoffee3342019-08-27 12:03:00 -07001266 rlib, ok := rustDep.compiler.(libraryInterface)
1267 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001268 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269 return
1270 }
1271 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001272 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001273 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1274
Ivan Lozano0a468a42024-05-13 21:03:34 -04001275 // rust_ffi rlibs may export include dirs, so collect those here.
1276 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1277 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1278 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1279
Ivan Lozanoffee3342019-08-27 12:03:00 -07001280 case procMacroDepTag:
1281 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001282 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001283 // proc_macro link dirs need to be exported, so collect those here.
1284 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001285
1286 case sourceDepTag:
1287 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1288 collectIncludedProtos(mod, rustDep)
1289 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001290 }
1291
Cole Faustb6e6f992023-08-17 17:42:26 -07001292 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1293
Paul Duffind5cf92e2021-07-09 17:38:55 +01001294 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001295 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1296 // OS/Arch variant is used.
1297 var helper string
1298 if ctx.Host() {
1299 helper = "missing 'host_supported'?"
1300 } else {
1301 helper = "device module defined?"
1302 }
1303
1304 if dep.Target().Os != ctx.Os() {
1305 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1306 return
1307 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1308 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1309 return
1310 }
1311 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001312 }
1313
Ivan Lozano0a468a42024-05-13 21:03:34 -04001314 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001315 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001316 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001317 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1318 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001319 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001320 }
1321
Ivan Lozanoffee3342019-08-27 12:03:00 -07001322 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001323 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001324 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001325 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001326 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001327 }
1328 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001329
Ivan Lozanod106efe2023-09-21 23:30:26 -04001330 if depTag == sourceDepTag {
1331 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1332 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001333 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001334 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1335 }
1336 }
1337 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001338 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001339 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001340 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001341 if _, ok := ccDep.(*Module); !ok {
1342 if ccDep.Module().Target().Os != ctx.Os() {
1343 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1344 return
1345 }
1346 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1347 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1348 return
1349 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001350 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001351 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001352 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001353 if !ctx.Config().AllowMissingDependencies() {
1354 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1355 } else {
1356 ctx.AddMissingDependencies([]string{depName})
1357 }
1358 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001359 }
1360
Wen-yi Chu41326c12023-09-22 03:58:59 +00001361 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001362
Ivan Lozanoffee3342019-08-27 12:03:00 -07001363 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001364 switch {
1365 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001366 if cc.IsWholeStaticLib(depTag) {
1367 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1368 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001369 if mod.Binary() {
1370 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1371 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1372 // final linkage, pass the args directly to the linker to handle these cases.
1373 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1374 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001375 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001376 } else {
1377 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 -05001378 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001379 }
1380
1381 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1382 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001383 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001384 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1385
Colin Cross313aa542023-12-13 13:47:44 -08001386 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001387 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1388 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1389 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1390 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001391 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001392
1393 // Record baseLibName for snapshots.
1394 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1395
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001396 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001397 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001398 // For the shared lib dependencies, we may link to the stub variant
1399 // of the dependency depending on the context (e.g. if this
1400 // dependency crosses the APEX boundaries).
1401 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1402
1403 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1404 // object (either from stub or non-stub) to use.
1405 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001406 if !linkObject.Valid() {
1407 if !ctx.Config().AllowMissingDependencies() {
1408 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1409 } else {
1410 ctx.AddMissingDependencies([]string{depName})
1411 }
1412 return
1413 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001414 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001415
Ivan Lozanoffee3342019-08-27 12:03:00 -07001416 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001417 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001418 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1419 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1420 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1421 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001422 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001423
1424 // Record baseLibName for snapshots.
1425 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1426
Cole Faustb6e6f992023-08-17 17:42:26 -07001427 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001428 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001429 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001430 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001431 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1432 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1433 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001434 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001435 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001436 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001437 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001438 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001439 }
1440
1441 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001442 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1443 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001444 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001445 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001446 } else {
1447 switch {
1448 case depTag == cc.CrtBeginDepTag:
1449 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1450 case depTag == cc.CrtEndDepTag:
1451 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1452 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001453 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001454
1455 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001456 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001457 // These are usually genrules which don't have per-target variants.
1458 directSrcDeps = append(directSrcDeps, srcDep)
1459 }
1460 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001461 })
1462
Wen-yi Chu41326c12023-09-22 03:58:59 +00001463 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1464
1465 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001466 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001467 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001468 crateName := dep.CrateName()
1469 if alias, aliased := aliases[crateName]; aliased {
1470 crateName = alias
1471 }
1472 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001473 }
1474 var dylibDepFiles RustLibraries
1475 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001476 crateName := dep.CrateName()
1477 if alias, aliased := aliases[crateName]; aliased {
1478 crateName = alias
1479 }
1480 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001481 }
1482 var procMacroDepFiles RustLibraries
1483 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001484 crateName := dep.CrateName()
1485 if alias, aliased := aliases[crateName]; aliased {
1486 crateName = alias
1487 }
1488 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001489 }
1490
Colin Cross004bd3f2023-10-02 11:39:17 -07001491 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001492 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001493 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001494 }
1495
Colin Cross004bd3f2023-10-02 11:39:17 -07001496 var sharedLibFiles android.Paths
1497 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001498 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001499 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001500 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001501 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001502 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001503 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001504 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001505 }
1506
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001507 var srcProviderDepFiles android.Paths
1508 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001509 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001510 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1511 }
1512 for _, dep := range directSrcDeps {
1513 srcs := dep.Srcs()
1514 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1515 }
1516
Wen-yi Chu41326c12023-09-22 03:58:59 +00001517 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1518 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001519 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1520 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1521 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001522 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001523 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001524
1525 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001526 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001527 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001528 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001529 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1530 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1531 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001532
1533 return depPaths
1534}
1535
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001536func (mod *Module) InstallInData() bool {
1537 if mod.compiler == nil {
1538 return false
1539 }
1540 return mod.compiler.inData()
1541}
1542
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001543func (mod *Module) InstallInRamdisk() bool {
1544 return mod.InRamdisk()
1545}
1546
1547func (mod *Module) InstallInVendorRamdisk() bool {
1548 return mod.InVendorRamdisk()
1549}
1550
1551func (mod *Module) InstallInRecovery() bool {
1552 return mod.InRecovery()
1553}
1554
Wen-yi Chu41326c12023-09-22 03:58:59 +00001555func linkPathFromFilePath(filepath android.Path) string {
1556 return strings.Split(filepath.String(), filepath.Base())[0]
1557}
1558
Spandan Das604f3762023-03-16 22:51:40 +00001559// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1560func (r *Module) usePublicApi() bool {
1561 return r.Device() && r.UseSdk()
1562}
1563
1564// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1565func (r *Module) useVendorApi() bool {
1566 return r.Device() && (r.InVendor() || r.InProduct())
1567}
1568
Ivan Lozanoffee3342019-08-27 12:03:00 -07001569func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1570 ctx := &depsContext{
1571 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001572 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001573
1574 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001575 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001576
Kiyoung Kim487689e2022-07-26 09:48:22 +09001577 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001578 if mod.usePublicApi() || mod.useVendorApi() {
1579 for idx, lib := range deps.SharedLibs {
1580 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1581 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001582 }
1583
Ivan Lozano1921e802021-05-20 13:39:16 -04001584 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001585 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001586 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001587
Ivan Lozano2b081132020-09-08 12:46:52 -04001588 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001589 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001590 stdLinkage = "rlib-std"
1591 }
1592
1593 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001594 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001595
Ivan Lozano2b081132020-09-08 12:46:52 -04001596 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1597 rlibDepVariations = append(rlibDepVariations,
1598 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1599 }
1600
Ivan Lozano1921e802021-05-20 13:39:16 -04001601 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001602 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001603 for _, lib := range deps.Rlibs {
1604 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001605 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001606 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001607
1608 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001609 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001610 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1611
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001612 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001613 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001614 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001615
Ivan Lozano1921e802021-05-20 13:39:16 -04001616 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001617 if deps.Rustlibs != nil {
1618 if !mod.compiler.Disabled() {
1619 for _, lib := range deps.Rustlibs {
1620 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1621 if autoDep.depTag == rlibDepTag {
1622 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001623 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1624
Ivan Lozanod106efe2023-09-21 23:30:26 -04001625 } else {
1626 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1627 // Check for the existence of the dylib deptag variant. Select it if available,
1628 // otherwise select the rlib variant.
1629 autoDepVariations := append(commonDepVariations,
1630 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001631 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001632 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1633 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001634
Ivan Lozanod106efe2023-09-21 23:30:26 -04001635 } else {
1636 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001637 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1638
Ivan Lozanod106efe2023-09-21 23:30:26 -04001639 }
1640 }
1641 }
1642 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1643 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001644 srcProviderVariations := append(commonDepVariations,
1645 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001646 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001647
Ivan Lozano0a468a42024-05-13 21:03:34 -04001648 // Only add rustlib dependencies if they're source providers themselves.
1649 // This is used to track which crate names need to be added to the source generated
1650 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001651 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001652 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001653 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001654 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001655 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001656 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001657
Ivan Lozano1921e802021-05-20 13:39:16 -04001658 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001659 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001660 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001661 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001662 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001663 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001664 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001665 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001666 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001667 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1668
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001669 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001670 }
1671 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001672
1673 for _, lib := range deps.SharedLibs {
1674 depTag := cc.SharedDepTag()
1675 name, version := cc.StubsLibNameAndVersion(lib)
1676
1677 variations := []blueprint.Variation{
1678 {Mutator: "link", Variation: "shared"},
1679 }
Spandan Das604f3762023-03-16 22:51:40 +00001680 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1681 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1682 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1683 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1684 }
1685
1686 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1687 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1688 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001689 }
1690
1691 for _, lib := range deps.WholeStaticLibs {
1692 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001693
1694 actx.AddVariationDependencies([]blueprint.Variation{
1695 {Mutator: "link", Variation: "static"},
1696 }, depTag, lib)
1697 }
1698
1699 for _, lib := range deps.StaticLibs {
1700 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001701
1702 actx.AddVariationDependencies([]blueprint.Variation{
1703 {Mutator: "link", Variation: "static"},
1704 }, depTag, lib)
1705 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001706
Zach Johnson3df4e632020-11-06 11:56:27 -08001707 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1708
Colin Cross565cafd2020-09-25 18:47:38 -07001709 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001710 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001711 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001712 }
Colin Crossfe605e12022-01-23 20:46:16 -08001713 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001714 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001715 }
1716
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001717 if mod.sourceProvider != nil {
1718 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1719 bindgen.Properties.Custom_bindgen != "" {
1720 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1721 bindgen.Properties.Custom_bindgen)
1722 }
1723 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001724
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001725 actx.AddVariationDependencies([]blueprint.Variation{
1726 {Mutator: "link", Variation: "shared"},
1727 }, dataLibDepTag, deps.DataLibs...)
1728
1729 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1730
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001731 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001732 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001733
1734 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001735}
1736
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001737func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001738 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001739 mod.beginMutator(ctx)
1740 }
1741}
1742
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001743func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1744 ctx := &baseModuleContext{
1745 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001746 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001747
1748 mod.begin(ctx)
1749}
1750
Ivan Lozanoffee3342019-08-27 12:03:00 -07001751func (mod *Module) Name() string {
1752 name := mod.ModuleBase.Name()
1753 if p, ok := mod.compiler.(interface {
1754 Name(string) string
1755 }); ok {
1756 name = p.Name(name)
1757 }
1758 return name
1759}
1760
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001761func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001762 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001763 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001764 }
1765}
1766
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001767var _ android.HostToolProvider = (*Module)(nil)
1768
1769func (mod *Module) HostToolPath() android.OptionalPath {
1770 if !mod.Host() {
1771 return android.OptionalPath{}
1772 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001773 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1774 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001775 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1776 // Even though proc-macros aren't strictly "tools", since they target the compiler
1777 // and act as compiler plugins, we treat them similarly.
1778 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001779 }
1780 return android.OptionalPath{}
1781}
1782
Jiyong Park99644e92020-11-17 22:21:02 +09001783var _ android.ApexModule = (*Module)(nil)
1784
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001785func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001786 return String(mod.Properties.Min_sdk_version)
1787}
1788
Jiyong Park45bf82e2020-12-15 22:29:02 +09001789// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001790func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001791 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001792 if minSdkVersion == "apex_inherit" {
1793 return nil
1794 }
1795 if minSdkVersion == "" {
1796 return fmt.Errorf("min_sdk_version is not specificed")
1797 }
1798
1799 // Not using nativeApiLevelFromUser because the context here is not
1800 // necessarily a native context.
1801 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1802 if err != nil {
1803 return err
1804 }
1805
1806 if ver.GreaterThan(sdkVersion) {
1807 return fmt.Errorf("newer SDK(%v)", ver)
1808 }
Jiyong Park99644e92020-11-17 22:21:02 +09001809 return nil
1810}
1811
Jiyong Park45bf82e2020-12-15 22:29:02 +09001812// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001813func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1814 depTag := ctx.OtherModuleDependencyTag(dep)
1815
1816 if ccm, ok := dep.(*cc.Module); ok {
1817 if ccm.HasStubsVariants() {
1818 if cc.IsSharedDepTag(depTag) {
1819 // dynamic dep to a stubs lib crosses APEX boundary
1820 return false
1821 }
1822 if cc.IsRuntimeDepTag(depTag) {
1823 // runtime dep to a stubs lib also crosses APEX boundary
1824 return false
1825 }
1826
1827 if cc.IsHeaderDepTag(depTag) {
1828 return false
1829 }
1830 }
1831 if mod.Static() && cc.IsSharedDepTag(depTag) {
1832 // shared_lib dependency from a static lib is considered as crossing
1833 // the APEX boundary because the dependency doesn't actually is
1834 // linked; the dependency is used only during the compilation phase.
1835 return false
1836 }
1837 }
1838
Matthew Maurer581b6d82022-09-29 16:46:25 -07001839 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001840 return false
1841 }
1842
1843 return true
1844}
1845
1846// Overrides ApexModule.IsInstallabeToApex()
1847func (mod *Module) IsInstallableToApex() bool {
1848 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001849 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001850 return true
1851 }
1852 if _, ok := mod.compiler.(*binaryDecorator); ok {
1853 return true
1854 }
1855 }
1856 return false
1857}
1858
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001859// If a library file has a "lib" prefix, extract the library name without the prefix.
1860func libNameFromFilePath(filepath android.Path) (string, bool) {
1861 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1862 if strings.HasPrefix(libName, "lib") {
1863 libName = libName[3:]
1864 return libName, true
1865 }
1866 return "", false
1867}
1868
Sasha Smundaka76acba2022-04-18 20:12:56 -07001869func kytheExtractRustFactory() android.Singleton {
1870 return &kytheExtractRustSingleton{}
1871}
1872
1873type kytheExtractRustSingleton struct {
1874}
1875
1876func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1877 var xrefTargets android.Paths
1878 ctx.VisitAllModules(func(module android.Module) {
1879 if rustModule, ok := module.(xref); ok {
1880 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1881 }
1882 })
1883 if len(xrefTargets) > 0 {
1884 ctx.Phony("xref_rust", xrefTargets...)
1885 }
1886}
1887
Jihoon Kangf78a8902022-09-01 22:47:07 +00001888func (c *Module) Partition() string {
1889 return ""
1890}
1891
Ivan Lozanoffee3342019-08-27 12:03:00 -07001892var Bool = proptools.Bool
1893var BoolDefault = proptools.BoolDefault
1894var String = proptools.String
1895var StringPtr = proptools.StringPtr