blob: 8dc52f07046692e8bf37598deb274b780d55f97b [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"
19 "strings"
20
Sasha Smundaka76acba2022-04-18 20:12:56 -070021 "android/soong/bloaty"
Aditya Choudhary87b2ab22023-11-17 15:27:06 +000022 "android/soong/testing"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090023
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "github.com/google/blueprint"
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020029 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070030 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000031 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
33)
34
35var pctx = android.NewPackageContext("android/soong/rust")
36
37func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070038 android.RegisterModuleType("rust_defaults", defaultsFactory)
39 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
40 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040041 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040042 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050043 })
44 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070046 })
Inseob Kim3b244062023-07-11 13:31:36 +090047 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020049 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000050 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070051}
52
53type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040054 GlobalRustFlags []string // Flags that apply globally to rust
55 GlobalLinkFlags []string // Flags that apply globally to linker
56 RustFlags []string // Flags that apply to rust
57 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070059 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070060 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070063 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070064}
65
66type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050067 AndroidMkRlibs []string `blueprint:"mutated"`
68 AndroidMkDylibs []string `blueprint:"mutated"`
69 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050070 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +000071 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040072
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090073 ImageVariation string `blueprint:"mutated"`
74 VndkVersion string `blueprint:"mutated"`
75 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -050076
Ivan Lozanoc08897c2021-04-02 12:41:32 -040077 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
78 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
79 // appended before SubName.
80 RustSubName string `blueprint:"mutated"`
81
Ivan Lozano6a884432020-12-02 09:15:16 -050082 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +000083 ProductVariantNeeded bool `blueprint:"mutated"`
84 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 CoreVariantNeeded bool `blueprint:"mutated"`
86 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070087 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080088 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050089 ExtraVariants []string `blueprint:"mutated"`
90
Ivan Lozanoa2268632021-07-22 10:52:06 -040091 // Allows this module to use non-APEX version of libraries. Useful
92 // for building binaries that are started before APEXes are activated.
93 Bootstrap *bool
94
Ivan Lozano1921e802021-05-20 13:39:16 -040095 // Used by vendor snapshot to record dependencies from snapshot modules.
96 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090097 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040098 SnapshotRlibs []string `blueprint:"mutated"`
99 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400100
Matthew Maurerc6868382021-07-13 14:12:37 -0700101 // Make this module available when building for ramdisk.
102 // On device without a dedicated recovery partition, the module is only
103 // available after switching root into
104 // /first_stage_ramdisk. To expose the module before switching root, install
105 // the recovery variant instead.
106 Ramdisk_available *bool
107
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500108 // Make this module available when building for vendor ramdisk.
109 // On device without a dedicated recovery partition, the module is only
110 // available after switching root into
111 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800112 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500113 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400114
Ivan Lozano1921e802021-05-20 13:39:16 -0400115 // Normally Soong uses the directory structure to decide which modules
116 // should be included (framework) or excluded (non-framework) from the
117 // different snapshots (vendor, recovery, etc.), but this property
118 // allows a partner to exclude a module normally thought of as a
119 // framework module from the vendor snapshot.
120 Exclude_from_vendor_snapshot *bool
121
122 // Normally Soong uses the directory structure to decide which modules
123 // should be included (framework) or excluded (non-framework) from the
124 // different snapshots (vendor, recovery, etc.), but this property
125 // allows a partner to exclude a module normally thought of as a
126 // framework module from the recovery snapshot.
127 Exclude_from_recovery_snapshot *bool
128
Matthew Maurer460ee942021-02-11 12:31:46 -0800129 // Make this module available when building for recovery
130 Recovery_available *bool
131
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500132 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
133 Min_sdk_version *string
134
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900135 HideFromMake bool `blueprint:"mutated"`
136 PreventInstall bool `blueprint:"mutated"`
137
138 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139}
140
141type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700142 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143
Ivan Lozano6a884432020-12-02 09:15:16 -0500144 VendorProperties cc.VendorProperties
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 Properties BaseProperties
147
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000148 hod android.HostOrDeviceSupported
149 multilib android.Multilib
150 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151
Ivan Lozano6a884432020-12-02 09:15:16 -0500152 makeLinkType string
153
Yi Kong46c6e592022-01-20 22:55:00 +0800154 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400156 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200157 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500158 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400160 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700161 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400162
Ivan Lozano0a468a42024-05-13 21:03:34 -0400163 exportedLinkDirs []string
164
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400165 // Output file to be installed, may be stripped or unstripped.
166 outputFile android.OptionalPath
167
Sasha Smundaka76acba2022-04-18 20:12:56 -0700168 // Cross-reference input file
169 kytheFiles android.Paths
170
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400171 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900172
173 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500174
175 // For apex variants, this is set as apex.min_sdk_version
176 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700177
178 transitiveAndroidMkSharedLibs *android.DepSet[string]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179}
180
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500181func (mod *Module) Header() bool {
182 //TODO: If Rust libraries provide header variants, this needs to be updated.
183 return false
184}
185
186func (mod *Module) SetPreventInstall() {
187 mod.Properties.PreventInstall = true
188}
189
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500190func (mod *Module) SetHideFromMake() {
191 mod.Properties.HideFromMake = true
192}
193
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900194func (mod *Module) HiddenFromMake() bool {
195 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400196}
197
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500198func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500199 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
200 // nil since we need compiler to actually sanitize.
201 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202}
203
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500204func (mod *Module) IsPrebuilt() bool {
205 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
206 return true
207 }
208 return false
209}
210
Ivan Lozano52767be2019-10-18 14:49:46 -0700211func (mod *Module) SelectedStl() string {
212 return ""
213}
214
Ivan Lozano2b262972019-11-21 12:30:50 -0800215func (mod *Module) NonCcVariants() bool {
216 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400217 if library, ok := mod.compiler.(libraryInterface); ok {
218 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800219 }
220 }
221 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
222}
223
Ivan Lozano52767be2019-10-18 14:49:46 -0700224func (mod *Module) Static() bool {
225 if mod.compiler != nil {
226 if library, ok := mod.compiler.(libraryInterface); ok {
227 return library.static()
228 }
229 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400230 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700231}
232
233func (mod *Module) Shared() bool {
234 if mod.compiler != nil {
235 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400236 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700237 }
238 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400239 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700240}
241
Ivan Lozanod7586b62021-04-01 09:49:36 -0400242func (mod *Module) Dylib() bool {
243 if mod.compiler != nil {
244 if library, ok := mod.compiler.(libraryInterface); ok {
245 return library.dylib()
246 }
247 }
248 return false
249}
250
Ivan Lozanod106efe2023-09-21 23:30:26 -0400251func (mod *Module) Source() bool {
252 if mod.compiler != nil {
253 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
254 return library.source()
255 }
256 }
257 return false
258}
259
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400260func (mod *Module) RlibStd() bool {
261 if mod.compiler != nil {
262 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
263 return library.rlibStd()
264 }
265 }
266 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
267}
268
Ivan Lozanod7586b62021-04-01 09:49:36 -0400269func (mod *Module) Rlib() bool {
270 if mod.compiler != nil {
271 if library, ok := mod.compiler.(libraryInterface); ok {
272 return library.rlib()
273 }
274 }
275 return false
276}
277
278func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400279 if binary, ok := mod.compiler.(binaryInterface); ok {
280 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400281 }
282 return false
283}
284
Justin Yun5e035862021-06-29 20:50:37 +0900285func (mod *Module) StaticExecutable() bool {
286 if !mod.Binary() {
287 return false
288 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400289 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900290}
291
Ivan Lozanod7586b62021-04-01 09:49:36 -0400292func (mod *Module) Object() bool {
293 // Rust has no modules which produce only object files.
294 return false
295}
296
Ivan Lozano52767be2019-10-18 14:49:46 -0700297func (mod *Module) Toc() android.OptionalPath {
298 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400299 if lib, ok := mod.compiler.(libraryInterface); ok {
300 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 }
302 }
303 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
304}
305
Colin Crossc511bc52020-04-07 16:50:32 +0000306func (mod *Module) UseSdk() bool {
307 return false
308}
309
Ivan Lozanod7586b62021-04-01 09:49:36 -0400310func (mod *Module) RelativeInstallPath() string {
311 if mod.compiler != nil {
312 return mod.compiler.relativeInstallPath()
313 }
314 return ""
315}
316
Ivan Lozano52767be2019-10-18 14:49:46 -0700317func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500318 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700319}
320
Jiyong Park7d55b612021-06-11 17:22:09 +0900321func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400322 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900323}
324
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400325func (mod *Module) SubName() string {
326 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700327}
328
Ivan Lozanof1868af2022-04-12 13:08:36 -0400329func (mod *Module) IsVndkPrebuiltLibrary() bool {
330 // Rust modules do not provide VNDK prebuilts
331 return false
332}
333
334func (mod *Module) IsVendorPublicLibrary() bool {
335 return mod.VendorProperties.IsVendorPublicLibrary
336}
337
338func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
339 // Rust modules to not provide Sdk variants
340 return false
341}
342
Colin Cross127bb8b2020-12-16 16:46:01 -0800343func (c *Module) IsVndkPrivate() bool {
344 return false
345}
346
347func (c *Module) IsLlndk() bool {
348 return false
349}
350
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400351func (mod *Module) KernelHeadersDecorator() bool {
352 return false
353}
354
Colin Cross1f3f1302021-04-26 18:37:44 -0700355func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400356 return false
357}
358
Colin Cross5271fea2021-04-27 13:06:04 -0700359func (m *Module) NeedsVendorPublicLibraryVariants() bool {
360 return false
361}
362
Ivan Lozanod7586b62021-04-01 09:49:36 -0400363func (mod *Module) HasLlndkStubs() bool {
364 return false
365}
366
367func (mod *Module) StubsVersion() string {
368 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
369}
370
Ivan Lozano52767be2019-10-18 14:49:46 -0700371func (mod *Module) SdkVersion() string {
372 return ""
373}
374
Colin Crossc511bc52020-04-07 16:50:32 +0000375func (mod *Module) AlwaysSdk() bool {
376 return false
377}
378
Jiyong Park2286afd2020-06-16 21:58:53 +0900379func (mod *Module) IsSdkVariant() bool {
380 return false
381}
382
Colin Cross1348ce32020-10-01 13:37:16 -0700383func (mod *Module) SplitPerApiLevel() bool {
384 return false
385}
386
Sasha Smundaka76acba2022-04-18 20:12:56 -0700387func (mod *Module) XrefRustFiles() android.Paths {
388 return mod.kytheFiles
389}
390
Ivan Lozanoffee3342019-08-27 12:03:00 -0700391type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400392 Dylibs []string
393 Rlibs []string
394 Rustlibs []string
395 Stdlibs []string
396 ProcMacros []string
397 SharedLibs []string
398 StaticLibs []string
399 WholeStaticLibs []string
400 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700401
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400402 // Used for data dependencies adjacent to tests
403 DataLibs []string
404 DataBins []string
405
Colin Crossfe605e12022-01-23 20:46:16 -0800406 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407}
408
409type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700410 DyLibs RustLibraries
411 RLibs RustLibraries
412 SharedLibs android.Paths
413 SharedLibDeps android.Paths
414 StaticLibs android.Paths
415 ProcMacros RustLibraries
416 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500417
418 // depFlags and depLinkFlags are rustc and linker (clang) flags.
419 depFlags []string
420 depLinkFlags []string
421
422 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
423 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000424 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700425 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700426
Ivan Lozano0a468a42024-05-13 21:03:34 -0400427 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
428 // cc_library_static dependants of rlibs.
429 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
430 exportedLinkDirs []string
431
Ivan Lozano45901ed2020-07-24 16:05:01 -0400432 // Used by bindgen modules which call clang
433 depClangFlags []string
434 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400435 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400436 depSystemIncludePaths android.Paths
437
Colin Crossfe605e12022-01-23 20:46:16 -0800438 CrtBegin android.Paths
439 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700440
441 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500442 SrcDeps android.Paths
443 srcProviderFiles android.Paths
Ivan Lozano0a468a42024-05-13 21:03:34 -0400444
445 // Used by Generated Libraries
446 depExportedRlibs []cc.RustRlibDep
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447}
448
449type RustLibraries []RustLibrary
450
451type RustLibrary struct {
452 Path android.Path
453 CrateName string
454}
455
Matthew Maurerbb3add12020-06-25 09:34:12 -0700456type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000457 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700458 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700459}
460
Sasha Smundaka76acba2022-04-18 20:12:56 -0700461type xref interface {
462 XrefRustFiles() android.Paths
463}
464
Matthew Maurerbb3add12020-06-25 09:34:12 -0700465type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000466 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700467 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700468}
469
Wen-yi Chu41326c12023-09-22 03:58:59 +0000470func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
471 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700472}
473
Colin Cross004bd3f2023-10-02 11:39:17 -0700474func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
475 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400476}
477
Colin Cross0de8a1e2020-09-18 14:15:30 -0700478func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800479 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700480 LinkDirs: flagExporter.linkDirs,
481 LinkObjects: flagExporter.linkObjects,
482 })
483}
484
Matthew Maurerbb3add12020-06-25 09:34:12 -0700485var _ exportedFlagsProducer = (*flagExporter)(nil)
486
487func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700488 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700489}
490
Colin Cross0de8a1e2020-09-18 14:15:30 -0700491type FlagExporterInfo struct {
492 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000493 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700494 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700495}
496
Colin Crossbc7d76c2023-12-12 16:39:03 -0800497var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700498
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400499func (mod *Module) isCoverageVariant() bool {
500 return mod.coverage.Properties.IsCoverageVariant
501}
502
503var _ cc.Coverage = (*Module)(nil)
504
Colin Crossf5f4ad32024-01-19 15:41:48 -0800505func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400506 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
507}
508
Ivan Lozanod7586b62021-04-01 09:49:36 -0400509func (mod *Module) VndkVersion() string {
510 return mod.Properties.VndkVersion
511}
512
Ivan Lozano0a468a42024-05-13 21:03:34 -0400513func (mod *Module) ExportedCrateLinkDirs() []string {
514 return mod.exportedLinkDirs
515}
516
Ivan Lozanod7586b62021-04-01 09:49:36 -0400517func (mod *Module) PreventInstall() bool {
518 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400519}
520
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400521func (mod *Module) MarkAsCoverageVariant(coverage bool) {
522 mod.coverage.Properties.IsCoverageVariant = coverage
523}
524
525func (mod *Module) EnableCoverageIfNeeded() {
526 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527}
528
529func defaultsFactory() android.Module {
530 return DefaultsFactory()
531}
532
533type Defaults struct {
534 android.ModuleBase
535 android.DefaultsModuleBase
536}
537
538func DefaultsFactory(props ...interface{}) android.Module {
539 module := &Defaults{}
540
541 module.AddProperties(props...)
542 module.AddProperties(
543 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800544 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500545 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100546 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400547 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548 &BaseCompilerProperties{},
549 &BinaryCompilerProperties{},
550 &LibraryCompilerProperties{},
551 &ProcMacroCompilerProperties{},
552 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400553 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700554 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400555 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400556 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200557 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500558 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000559 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560 )
561
562 android.InitDefaultsModule(module)
563 return module
564}
565
566func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700567 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568}
569
Ivan Lozano183a3212019-10-18 14:18:45 -0700570func (mod *Module) CcLibrary() bool {
571 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500572 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700573 return true
574 }
575 }
576 return false
577}
578
579func (mod *Module) CcLibraryInterface() bool {
580 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400581 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
582 // VariantIs{Static,Shared} is set.
583 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700584 return true
585 }
586 }
587 return false
588}
589
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400590func (mod *Module) RustLibraryInterface() bool {
591 if mod.compiler != nil {
592 if _, ok := mod.compiler.(libraryInterface); ok {
593 return true
594 }
595 }
596 return false
597}
598
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500599func (mod *Module) IsFuzzModule() bool {
600 if _, ok := mod.compiler.(*fuzzDecorator); ok {
601 return true
602 }
603 return false
604}
605
606func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
607 return mod.FuzzModule
608}
609
610func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
611 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
612 return fuzzer.fuzzPackagedModule
613 }
614 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
615}
616
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000617func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500618 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
619 return fuzzer.sharedLibraries
620 }
621 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
622}
623
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400624func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400625 if mod.compiler != nil {
626 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400627 }
628 return nil
629}
630
Ivan Lozano183a3212019-10-18 14:18:45 -0700631func (mod *Module) SetStatic() {
632 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700633 if library, ok := mod.compiler.(libraryInterface); ok {
634 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700635 return
636 }
637 }
638 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
639}
640
641func (mod *Module) SetShared() {
642 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700643 if library, ok := mod.compiler.(libraryInterface); ok {
644 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700645 return
646 }
647 }
648 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
649}
650
Ivan Lozano183a3212019-10-18 14:18:45 -0700651func (mod *Module) BuildStaticVariant() bool {
652 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700653 if library, ok := mod.compiler.(libraryInterface); ok {
654 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700655 }
656 }
657 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
658}
659
660func (mod *Module) BuildSharedVariant() bool {
661 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700662 if library, ok := mod.compiler.(libraryInterface); ok {
663 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700664 }
665 }
666 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
667}
668
Ivan Lozano183a3212019-10-18 14:18:45 -0700669func (mod *Module) Module() android.Module {
670 return mod
671}
672
Ivan Lozano183a3212019-10-18 14:18:45 -0700673func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400674 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700675}
676
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400677func (mod *Module) CoverageFiles() android.Paths {
678 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800679 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400680 }
681 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
682}
683
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400684// Rust does not produce gcno files, and therefore does not produce a coverage archive.
685func (mod *Module) CoverageOutputFile() android.OptionalPath {
686 return android.OptionalPath{}
687}
688
689func (mod *Module) IsNdk(config android.Config) bool {
690 return false
691}
692
693func (mod *Module) IsStubs() bool {
694 return false
695}
696
Jiyong Park459feca2020-12-15 11:02:21 +0900697func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900698 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900699 return false
700 }
701
Jiyong Park459feca2020-12-15 11:02:21 +0900702 // The apex variant is not installable because it is included in the APEX and won't appear
703 // in the system partition as a standalone file.
704 if !apexInfo.IsForPlatform() {
705 return false
706 }
707
Jiyong Parke54f07e2021-04-07 15:08:04 +0900708 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900709}
710
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500711func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800712 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
713 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500714}
715
Ivan Lozano183a3212019-10-18 14:18:45 -0700716var _ cc.LinkableInterface = (*Module)(nil)
717
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718func (mod *Module) Init() android.Module {
719 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500720 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721
Yi Kong46c6e592022-01-20 22:55:00 +0800722 if mod.afdo != nil {
723 mod.AddProperties(mod.afdo.props()...)
724 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725 if mod.compiler != nil {
726 mod.AddProperties(mod.compiler.compilerProps()...)
727 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400728 if mod.coverage != nil {
729 mod.AddProperties(mod.coverage.props()...)
730 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200731 if mod.clippy != nil {
732 mod.AddProperties(mod.clippy.props()...)
733 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400734 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700735 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400736 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500737 if mod.sanitize != nil {
738 mod.AddProperties(mod.sanitize.props()...)
739 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400740
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900742 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743
744 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745 return mod
746}
747
748func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
749 return &Module{
750 hod: hod,
751 multilib: multilib,
752 }
753}
754func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
755 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800756 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400757 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200758 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500759 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760 return module
761}
762
763type ModuleContext interface {
764 android.ModuleContext
765 ModuleContextIntf
766}
767
768type BaseModuleContext interface {
769 android.BaseModuleContext
770 ModuleContextIntf
771}
772
773type DepsContext interface {
774 android.BottomUpMutatorContext
775 ModuleContextIntf
776}
777
778type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200779 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781}
782
783type depsContext struct {
784 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785}
786
787type moduleContext struct {
788 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789}
790
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200791type baseModuleContext struct {
792 android.BaseModuleContext
793}
794
795func (ctx *moduleContext) RustModule() *Module {
796 return ctx.Module().(*Module)
797}
798
799func (ctx *moduleContext) toolchain() config.Toolchain {
800 return ctx.RustModule().toolchain(ctx)
801}
802
803func (ctx *depsContext) RustModule() *Module {
804 return ctx.Module().(*Module)
805}
806
807func (ctx *depsContext) toolchain() config.Toolchain {
808 return ctx.RustModule().toolchain(ctx)
809}
810
811func (ctx *baseModuleContext) RustModule() *Module {
812 return ctx.Module().(*Module)
813}
814
815func (ctx *baseModuleContext) toolchain() config.Toolchain {
816 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400817}
818
819func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700820 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
821 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
822 return false
823 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400824 return mod.compiler != nil && mod.compiler.nativeCoverage()
825}
826
Ivan Lozanod7586b62021-04-01 09:49:36 -0400827func (mod *Module) EverInstallable() bool {
828 return mod.compiler != nil &&
829 // Check to see whether the module is actually ever installable.
830 mod.compiler.everInstallable()
831}
832
833func (mod *Module) Installable() *bool {
834 return mod.Properties.Installable
835}
836
Ivan Lozano872d5792022-03-23 17:31:39 -0400837func (mod *Module) ProcMacro() bool {
838 if pm, ok := mod.compiler.(procMacroInterface); ok {
839 return pm.ProcMacro()
840 }
841 return false
842}
843
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
845 if mod.cachedToolchain == nil {
846 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
847 }
848 return mod.cachedToolchain
849}
850
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200851func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
852 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
853}
854
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
856}
857
858func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
859 ctx := &moduleContext{
860 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862
Colin Crossff694a82023-12-13 15:54:49 -0800863 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900864 if !apexInfo.IsForPlatform() {
865 mod.hideApexVariantFromMake = true
866 }
867
Ivan Lozanoffee3342019-08-27 12:03:00 -0700868 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500869 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
870
Ivan Lozanof1868af2022-04-12 13:08:36 -0400871 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700872
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 if !toolchain.Supported() {
874 // This toolchain's unsupported, there's nothing to do for this mod.
875 return
876 }
877
878 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400879 // Export linkDirs for CC rust generatedlibs
880 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
881 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
882
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 flags := Flags{
884 Toolchain: toolchain,
885 }
886
Ivan Lozano67eada32021-09-23 11:50:33 -0400887 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800888 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500889 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800890 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 if mod.compiler != nil {
892 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400893 flags = mod.compiler.cfgFlags(ctx, flags)
894 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400895 }
896 if mod.coverage != nil {
897 flags, deps = mod.coverage.flags(ctx, flags, deps)
898 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200899 if mod.clippy != nil {
900 flags, deps = mod.clippy.flags(ctx, flags, deps)
901 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500902 if mod.sanitize != nil {
903 flags, deps = mod.sanitize.flags(ctx, flags, deps)
904 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400905
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200906 // SourceProvider needs to call GenerateSource() before compiler calls
907 // compile() so it can provide the source. A SourceProvider has
908 // multiple variants (e.g. source, rlib, dylib). Only the "source"
909 // variant is responsible for effectively generating the source. The
910 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400911 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200912 if mod.compiler.(libraryInterface).source() {
913 mod.sourceProvider.GenerateSource(ctx, deps)
914 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
915 } else {
916 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
917 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700918 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200919 }
Colin Cross40213022023-12-13 15:19:49 -0800920 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400921 }
922
923 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100924 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700925 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400926 if ctx.Failed() {
927 return
928 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700929 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
930 if buildOutput.kytheFile != nil {
931 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
932 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400933 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900934
Dan Albert06feee92021-03-19 15:06:02 -0700935 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000936 if mod.docTimestampFile.Valid() {
937 ctx.CheckbuildFile(mod.docTimestampFile.Path())
938 }
Dan Albert06feee92021-03-19 15:06:02 -0700939
Colin Crossff694a82023-12-13 15:54:49 -0800940 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400941 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900942 // If the module has been specifically configure to not be installed then
943 // hide from make as otherwise it will break when running inside make as the
944 // output path to install will not be specified. Not all uninstallable
945 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400946 // side dependencies. In particular, proc-macros need to be captured in the
947 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900948 mod.HideFromMake()
949 } else if !mod.installable(apexInfo) {
950 mod.SkipInstall()
951 }
952
953 // Still call install though, the installs will be stored as PackageSpecs to allow
954 // using the outputs in a genrule.
955 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200956 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900957 if ctx.Failed() {
958 return
959 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400960 // Export your own directory as a linkDir
961 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
962
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400963 }
Chris Wailes74be7642021-07-22 16:20:28 -0700964
965 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700966 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000967 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800968 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000969 }
mrziwang0cbd3b02024-06-20 16:39:25 -0700970 mod.setOutputFiles(ctx)
971}
972
973func (mod *Module) setOutputFiles(ctx ModuleContext) {
974 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
975 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
976 } else if mod.OutputFile().Valid() {
977 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
978 } else {
979 ctx.SetOutputFiles(android.Paths{}, "")
980 }
981 if mod.compiler != nil {
982 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
983 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700984}
985
986func (mod *Module) deps(ctx DepsContext) Deps {
987 deps := Deps{}
988
989 if mod.compiler != nil {
990 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400991 }
992 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700993 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994 }
995
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400996 if mod.coverage != nil {
997 deps = mod.coverage.deps(ctx, deps)
998 }
999
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001000 if mod.sanitize != nil {
1001 deps = mod.sanitize.deps(ctx, deps)
1002 }
1003
Ivan Lozanoffee3342019-08-27 12:03:00 -07001004 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1005 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001006 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001007 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1008 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1009 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001010 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001011 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001012 return deps
1013
1014}
1015
Ivan Lozanoffee3342019-08-27 12:03:00 -07001016type dependencyTag struct {
1017 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001018 name string
1019 library bool
1020 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001021 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022}
1023
Jiyong Park65b62242020-11-25 12:44:59 +09001024// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1025// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1026func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001027 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001028}
1029
1030var _ android.InstallNeededDependencyTag = dependencyTag{}
1031
Colin Cross65cb3142021-12-10 23:05:02 +00001032func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1033 if d.library && d.dynamic {
1034 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1035 }
1036 return nil
1037}
1038
Yu Liuc8884602024-03-15 18:48:38 +00001039func (d dependencyTag) PropagateAconfigValidation() bool {
1040 return d == rlibDepTag || d == sourceDepTag
1041}
1042
1043var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1044
Colin Cross65cb3142021-12-10 23:05:02 +00001045var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1046
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001048 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1049 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001050 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001051 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001052 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001053 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001054 dataLibDepTag = dependencyTag{name: "data lib"}
1055 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056)
1057
Jiyong Park99644e92020-11-17 22:21:02 +09001058func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1059 tag, ok := depTag.(dependencyTag)
1060 return ok && tag == dylibDepTag
1061}
1062
Jiyong Park94e22fd2021-04-08 18:19:15 +09001063func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1064 tag, ok := depTag.(dependencyTag)
1065 return ok && tag == rlibDepTag
1066}
1067
Matthew Maurer0f003b12020-06-29 14:34:06 -07001068type autoDep struct {
1069 variation string
1070 depTag dependencyTag
1071}
1072
1073var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001074 rlibVariation = "rlib"
1075 dylibVariation = "dylib"
1076 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1077 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001078)
1079
1080type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001081 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001082}
1083
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001084func (mod *Module) begin(ctx BaseModuleContext) {
1085 if mod.coverage != nil {
1086 mod.coverage.begin(ctx)
1087 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001088 if mod.sanitize != nil {
1089 mod.sanitize.begin(ctx)
1090 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001091}
1092
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001093func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001094 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001095 return p.prebuilt()
1096 }
1097 return nil
1098}
1099
Kiyoung Kim37693d02024-04-04 09:56:15 +09001100func (mod *Module) Symlinks() []string {
1101 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1102 return nil
1103}
1104
Justin Yun24b246a2023-03-16 10:36:16 +09001105func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1106 if rustDep, ok := dep.(*Module); ok {
1107 // Use base module name for snapshots when exporting to Makefile.
1108 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1109 baseName := rustDep.BaseModuleName()
1110 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1111 }
1112 }
1113 return cc.MakeLibName(ctx, c, dep, depName)
1114}
1115
Ivan Lozanod106efe2023-09-21 23:30:26 -04001116func collectIncludedProtos(mod *Module, dep *Module) {
1117 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1118 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1119 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1120 }
1121 }
1122}
Andrew Walbran52533232024-03-19 11:36:04 +00001123
Ivan Lozanoffee3342019-08-27 12:03:00 -07001124func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1125 var depPaths PathDeps
1126
1127 directRlibDeps := []*Module{}
1128 directDylibDeps := []*Module{}
1129 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001130 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001131 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001132 directSrcProvidersDeps := []*Module{}
1133 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001135 // For the dependency from platform to apex, use the latest stubs
1136 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001137 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001138 if !apexInfo.IsForPlatform() {
1139 mod.apexSdkVersion = apexInfo.MinSdkVersion
1140 }
1141
1142 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1143 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1144 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1145 // (b/144430859)
1146 mod.apexSdkVersion = android.FutureApiLevel
1147 }
1148
Spandan Das604f3762023-03-16 22:51:40 +00001149 skipModuleList := map[string]bool{}
1150
1151 var apiImportInfo multitree.ApiImportInfo
1152 hasApiImportInfo := false
1153
1154 ctx.VisitDirectDeps(func(dep android.Module) {
1155 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001156 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001157 hasApiImportInfo = true
1158 }
1159 })
1160
1161 if hasApiImportInfo {
1162 targetStubModuleList := map[string]string{}
1163 targetOrigModuleList := map[string]string{}
1164
1165 // Search for dependency which both original module and API imported library with APEX stub exists
1166 ctx.VisitDirectDeps(func(dep android.Module) {
1167 depName := ctx.OtherModuleName(dep)
1168 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1169 targetStubModuleList[apiLibrary] = depName
1170 }
1171 })
1172 ctx.VisitDirectDeps(func(dep android.Module) {
1173 depName := ctx.OtherModuleName(dep)
1174 if origLibrary, ok := targetStubModuleList[depName]; ok {
1175 targetOrigModuleList[origLibrary] = depName
1176 }
1177 })
1178
1179 // Decide which library should be used between original and API imported library
1180 ctx.VisitDirectDeps(func(dep android.Module) {
1181 depName := ctx.OtherModuleName(dep)
1182 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1183 if cc.ShouldUseStubForApex(ctx, dep) {
1184 skipModuleList[depName] = true
1185 } else {
1186 skipModuleList[apiLibrary] = true
1187 }
1188 }
1189 })
1190 }
1191
Cole Faustb6e6f992023-08-17 17:42:26 -07001192 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1193 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001194
Ivan Lozanoffee3342019-08-27 12:03:00 -07001195 ctx.VisitDirectDeps(func(dep android.Module) {
1196 depName := ctx.OtherModuleName(dep)
1197 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001198
Spandan Das604f3762023-03-16 22:51:40 +00001199 if _, exists := skipModuleList[depName]; exists {
1200 return
1201 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001202
1203 if depTag == android.DarwinUniversalVariantTag {
1204 return
1205 }
1206
Ivan Lozano0a468a42024-05-13 21:03:34 -04001207 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001208 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001209 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001210
Ivan Lozanoffee3342019-08-27 12:03:00 -07001211 switch depTag {
1212 case dylibDepTag:
1213 dylib, ok := rustDep.compiler.(libraryInterface)
1214 if !ok || !dylib.dylib() {
1215 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1216 return
1217 }
1218 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001219 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001220 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1221
Ivan Lozanoffee3342019-08-27 12:03:00 -07001222 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001223
Ivan Lozanoffee3342019-08-27 12:03:00 -07001224 rlib, ok := rustDep.compiler.(libraryInterface)
1225 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001226 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001227 return
1228 }
1229 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001230 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001231 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1232
Ivan Lozano0a468a42024-05-13 21:03:34 -04001233 // rust_ffi rlibs may export include dirs, so collect those here.
1234 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1235 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1236 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1237
Ivan Lozanoffee3342019-08-27 12:03:00 -07001238 case procMacroDepTag:
1239 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001240 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001241 // proc_macro link dirs need to be exported, so collect those here.
1242 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001243
1244 case sourceDepTag:
1245 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1246 collectIncludedProtos(mod, rustDep)
1247 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001248 }
1249
Cole Faustb6e6f992023-08-17 17:42:26 -07001250 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1251
Paul Duffind5cf92e2021-07-09 17:38:55 +01001252 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001253 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1254 // OS/Arch variant is used.
1255 var helper string
1256 if ctx.Host() {
1257 helper = "missing 'host_supported'?"
1258 } else {
1259 helper = "device module defined?"
1260 }
1261
1262 if dep.Target().Os != ctx.Os() {
1263 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1264 return
1265 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1266 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1267 return
1268 }
1269 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001270 }
1271
Ivan Lozano0a468a42024-05-13 21:03:34 -04001272 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001273 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001274 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001275 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1276 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001277 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001278 }
1279
Ivan Lozanoffee3342019-08-27 12:03:00 -07001280 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001281 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001282 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001283 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001284 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001285 }
1286 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001287
Ivan Lozanod106efe2023-09-21 23:30:26 -04001288 if depTag == sourceDepTag {
1289 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1290 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001291 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001292 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1293 }
1294 }
1295 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001296 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001297 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001298 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001299 if _, ok := ccDep.(*Module); !ok {
1300 if ccDep.Module().Target().Os != ctx.Os() {
1301 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1302 return
1303 }
1304 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1305 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1306 return
1307 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001308 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001309 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001310 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001311 if !ctx.Config().AllowMissingDependencies() {
1312 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1313 } else {
1314 ctx.AddMissingDependencies([]string{depName})
1315 }
1316 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001317 }
1318
Wen-yi Chu41326c12023-09-22 03:58:59 +00001319 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001320
Ivan Lozanoffee3342019-08-27 12:03:00 -07001321 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001322 switch {
1323 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001324 if cc.IsWholeStaticLib(depTag) {
1325 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1326 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001327 if mod.Binary() {
1328 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1329 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1330 // final linkage, pass the args directly to the linker to handle these cases.
1331 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1332 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001333 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001334 } else {
1335 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 -05001336 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001337 }
1338
1339 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1340 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001341 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001342 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1343
Colin Cross313aa542023-12-13 13:47:44 -08001344 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001345 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1346 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1347 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1348 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001349 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001350
1351 // Record baseLibName for snapshots.
1352 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1353
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001354 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001355 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001356 // For the shared lib dependencies, we may link to the stub variant
1357 // of the dependency depending on the context (e.g. if this
1358 // dependency crosses the APEX boundaries).
1359 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1360
1361 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1362 // object (either from stub or non-stub) to use.
1363 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001364 if !linkObject.Valid() {
1365 if !ctx.Config().AllowMissingDependencies() {
1366 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1367 } else {
1368 ctx.AddMissingDependencies([]string{depName})
1369 }
1370 return
1371 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001372 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001373
Ivan Lozanoffee3342019-08-27 12:03:00 -07001374 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001375 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001376 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1377 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1378 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1379 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001380 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001381
1382 // Record baseLibName for snapshots.
1383 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1384
Cole Faustb6e6f992023-08-17 17:42:26 -07001385 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001386 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001387 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001388 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001389 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1390 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1391 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001392 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001393 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001394 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001395 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001396 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001397 }
1398
1399 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001400 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1401 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001402 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001403 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001404 } else {
1405 switch {
1406 case depTag == cc.CrtBeginDepTag:
1407 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1408 case depTag == cc.CrtEndDepTag:
1409 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1410 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001411 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001412
1413 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001414 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001415 // These are usually genrules which don't have per-target variants.
1416 directSrcDeps = append(directSrcDeps, srcDep)
1417 }
1418 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001419 })
1420
Wen-yi Chu41326c12023-09-22 03:58:59 +00001421 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1422
1423 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001424 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001425 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001426 crateName := dep.CrateName()
1427 if alias, aliased := aliases[crateName]; aliased {
1428 crateName = alias
1429 }
1430 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001431 }
1432 var dylibDepFiles RustLibraries
1433 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001434 crateName := dep.CrateName()
1435 if alias, aliased := aliases[crateName]; aliased {
1436 crateName = alias
1437 }
1438 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001439 }
1440 var procMacroDepFiles RustLibraries
1441 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001442 crateName := dep.CrateName()
1443 if alias, aliased := aliases[crateName]; aliased {
1444 crateName = alias
1445 }
1446 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001447 }
1448
Colin Cross004bd3f2023-10-02 11:39:17 -07001449 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001450 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001451 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001452 }
1453
Colin Cross004bd3f2023-10-02 11:39:17 -07001454 var sharedLibFiles android.Paths
1455 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001456 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001457 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001458 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001459 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001460 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001461 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001462 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463 }
1464
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001465 var srcProviderDepFiles android.Paths
1466 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001467 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001468 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1469 }
1470 for _, dep := range directSrcDeps {
1471 srcs := dep.Srcs()
1472 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1473 }
1474
Wen-yi Chu41326c12023-09-22 03:58:59 +00001475 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1476 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001477 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1478 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1479 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001480 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001481 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001482
1483 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001484 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001485 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001486 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001487 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1488 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1489 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001490
1491 return depPaths
1492}
1493
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001494func (mod *Module) InstallInData() bool {
1495 if mod.compiler == nil {
1496 return false
1497 }
1498 return mod.compiler.inData()
1499}
1500
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001501func (mod *Module) InstallInRamdisk() bool {
1502 return mod.InRamdisk()
1503}
1504
1505func (mod *Module) InstallInVendorRamdisk() bool {
1506 return mod.InVendorRamdisk()
1507}
1508
1509func (mod *Module) InstallInRecovery() bool {
1510 return mod.InRecovery()
1511}
1512
Wen-yi Chu41326c12023-09-22 03:58:59 +00001513func linkPathFromFilePath(filepath android.Path) string {
1514 return strings.Split(filepath.String(), filepath.Base())[0]
1515}
1516
Spandan Das604f3762023-03-16 22:51:40 +00001517// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1518func (r *Module) usePublicApi() bool {
1519 return r.Device() && r.UseSdk()
1520}
1521
1522// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1523func (r *Module) useVendorApi() bool {
1524 return r.Device() && (r.InVendor() || r.InProduct())
1525}
1526
Ivan Lozanoffee3342019-08-27 12:03:00 -07001527func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1528 ctx := &depsContext{
1529 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001530 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001531
1532 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001533 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001534
Kiyoung Kim487689e2022-07-26 09:48:22 +09001535 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001536 if mod.usePublicApi() || mod.useVendorApi() {
1537 for idx, lib := range deps.SharedLibs {
1538 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1539 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001540 }
1541
Ivan Lozano1921e802021-05-20 13:39:16 -04001542 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001543 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001544 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001545
Ivan Lozano2b081132020-09-08 12:46:52 -04001546 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001547 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001548 stdLinkage = "rlib-std"
1549 }
1550
1551 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001552 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001553
Ivan Lozano2b081132020-09-08 12:46:52 -04001554 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1555 rlibDepVariations = append(rlibDepVariations,
1556 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1557 }
1558
Ivan Lozano1921e802021-05-20 13:39:16 -04001559 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001560 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001561 for _, lib := range deps.Rlibs {
1562 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001563 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001564 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001565
1566 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001567 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001568 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1569
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001570 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001571 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001572 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001573
Ivan Lozano1921e802021-05-20 13:39:16 -04001574 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001575 if deps.Rustlibs != nil {
1576 if !mod.compiler.Disabled() {
1577 for _, lib := range deps.Rustlibs {
1578 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1579 if autoDep.depTag == rlibDepTag {
1580 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001581 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1582
Ivan Lozanod106efe2023-09-21 23:30:26 -04001583 } else {
1584 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1585 // Check for the existence of the dylib deptag variant. Select it if available,
1586 // otherwise select the rlib variant.
1587 autoDepVariations := append(commonDepVariations,
1588 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001589 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001590 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1591 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001592
Ivan Lozanod106efe2023-09-21 23:30:26 -04001593 } else {
1594 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001595 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1596
Ivan Lozanod106efe2023-09-21 23:30:26 -04001597 }
1598 }
1599 }
1600 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1601 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001602 srcProviderVariations := append(commonDepVariations,
1603 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001604 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001605
Ivan Lozano0a468a42024-05-13 21:03:34 -04001606 // Only add rustlib dependencies if they're source providers themselves.
1607 // This is used to track which crate names need to be added to the source generated
1608 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001609 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001610 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001611 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001612 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001613 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001614 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001615
Ivan Lozano1921e802021-05-20 13:39:16 -04001616 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001617 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001618 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001619 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001620 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001621 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001622 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001623 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001624 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001625 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1626
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001627 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001628 }
1629 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001630
1631 for _, lib := range deps.SharedLibs {
1632 depTag := cc.SharedDepTag()
1633 name, version := cc.StubsLibNameAndVersion(lib)
1634
1635 variations := []blueprint.Variation{
1636 {Mutator: "link", Variation: "shared"},
1637 }
Spandan Das604f3762023-03-16 22:51:40 +00001638 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1639 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1640 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1641 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1642 }
1643
1644 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1645 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1646 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001647 }
1648
1649 for _, lib := range deps.WholeStaticLibs {
1650 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001651
1652 actx.AddVariationDependencies([]blueprint.Variation{
1653 {Mutator: "link", Variation: "static"},
1654 }, depTag, lib)
1655 }
1656
1657 for _, lib := range deps.StaticLibs {
1658 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001659
1660 actx.AddVariationDependencies([]blueprint.Variation{
1661 {Mutator: "link", Variation: "static"},
1662 }, depTag, lib)
1663 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001664
Zach Johnson3df4e632020-11-06 11:56:27 -08001665 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1666
Colin Cross565cafd2020-09-25 18:47:38 -07001667 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001668 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001669 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001670 }
Colin Crossfe605e12022-01-23 20:46:16 -08001671 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001672 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001673 }
1674
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001675 if mod.sourceProvider != nil {
1676 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1677 bindgen.Properties.Custom_bindgen != "" {
1678 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1679 bindgen.Properties.Custom_bindgen)
1680 }
1681 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001682
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001683 actx.AddVariationDependencies([]blueprint.Variation{
1684 {Mutator: "link", Variation: "shared"},
1685 }, dataLibDepTag, deps.DataLibs...)
1686
1687 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1688
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001689 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001690 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001691
1692 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001693}
1694
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001695func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001696 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001697 mod.beginMutator(ctx)
1698 }
1699}
1700
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001701func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1702 ctx := &baseModuleContext{
1703 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001704 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001705
1706 mod.begin(ctx)
1707}
1708
Ivan Lozanoffee3342019-08-27 12:03:00 -07001709func (mod *Module) Name() string {
1710 name := mod.ModuleBase.Name()
1711 if p, ok := mod.compiler.(interface {
1712 Name(string) string
1713 }); ok {
1714 name = p.Name(name)
1715 }
1716 return name
1717}
1718
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001719func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001720 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001721 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001722 }
1723}
1724
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001725var _ android.HostToolProvider = (*Module)(nil)
1726
1727func (mod *Module) HostToolPath() android.OptionalPath {
1728 if !mod.Host() {
1729 return android.OptionalPath{}
1730 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001731 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1732 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001733 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1734 // Even though proc-macros aren't strictly "tools", since they target the compiler
1735 // and act as compiler plugins, we treat them similarly.
1736 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001737 }
1738 return android.OptionalPath{}
1739}
1740
Jiyong Park99644e92020-11-17 22:21:02 +09001741var _ android.ApexModule = (*Module)(nil)
1742
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001743func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001744 return String(mod.Properties.Min_sdk_version)
1745}
1746
Jiyong Park45bf82e2020-12-15 22:29:02 +09001747// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001748func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001749 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001750 if minSdkVersion == "apex_inherit" {
1751 return nil
1752 }
1753 if minSdkVersion == "" {
1754 return fmt.Errorf("min_sdk_version is not specificed")
1755 }
1756
1757 // Not using nativeApiLevelFromUser because the context here is not
1758 // necessarily a native context.
1759 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1760 if err != nil {
1761 return err
1762 }
1763
1764 if ver.GreaterThan(sdkVersion) {
1765 return fmt.Errorf("newer SDK(%v)", ver)
1766 }
Jiyong Park99644e92020-11-17 22:21:02 +09001767 return nil
1768}
1769
Jiyong Park45bf82e2020-12-15 22:29:02 +09001770// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001771func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1772 depTag := ctx.OtherModuleDependencyTag(dep)
1773
1774 if ccm, ok := dep.(*cc.Module); ok {
1775 if ccm.HasStubsVariants() {
1776 if cc.IsSharedDepTag(depTag) {
1777 // dynamic dep to a stubs lib crosses APEX boundary
1778 return false
1779 }
1780 if cc.IsRuntimeDepTag(depTag) {
1781 // runtime dep to a stubs lib also crosses APEX boundary
1782 return false
1783 }
1784
1785 if cc.IsHeaderDepTag(depTag) {
1786 return false
1787 }
1788 }
1789 if mod.Static() && cc.IsSharedDepTag(depTag) {
1790 // shared_lib dependency from a static lib is considered as crossing
1791 // the APEX boundary because the dependency doesn't actually is
1792 // linked; the dependency is used only during the compilation phase.
1793 return false
1794 }
1795 }
1796
Matthew Maurer581b6d82022-09-29 16:46:25 -07001797 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001798 return false
1799 }
1800
1801 return true
1802}
1803
1804// Overrides ApexModule.IsInstallabeToApex()
1805func (mod *Module) IsInstallableToApex() bool {
1806 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001807 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001808 return true
1809 }
1810 if _, ok := mod.compiler.(*binaryDecorator); ok {
1811 return true
1812 }
1813 }
1814 return false
1815}
1816
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001817// If a library file has a "lib" prefix, extract the library name without the prefix.
1818func libNameFromFilePath(filepath android.Path) (string, bool) {
1819 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1820 if strings.HasPrefix(libName, "lib") {
1821 libName = libName[3:]
1822 return libName, true
1823 }
1824 return "", false
1825}
1826
Sasha Smundaka76acba2022-04-18 20:12:56 -07001827func kytheExtractRustFactory() android.Singleton {
1828 return &kytheExtractRustSingleton{}
1829}
1830
1831type kytheExtractRustSingleton struct {
1832}
1833
1834func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1835 var xrefTargets android.Paths
1836 ctx.VisitAllModules(func(module android.Module) {
1837 if rustModule, ok := module.(xref); ok {
1838 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1839 }
1840 })
1841 if len(xrefTargets) > 0 {
1842 ctx.Phony("xref_rust", xrefTargets...)
1843 }
1844}
1845
Jihoon Kangf78a8902022-09-01 22:47:07 +00001846func (c *Module) Partition() string {
1847 return ""
1848}
1849
Ivan Lozanoffee3342019-08-27 12:03:00 -07001850var Bool = proptools.Bool
1851var BoolDefault = proptools.BoolDefault
1852var String = proptools.String
1853var StringPtr = proptools.StringPtr