blob: 8a053c1842dea2f1d82f8813afca37b4b03570b1 [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 Lozano43845682020-07-09 21:03:28 -0400211func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
212 switch tag {
213 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700214 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400215 return mod.sourceProvider.Srcs(), nil
216 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900217 if mod.OutputFile().Valid() {
218 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400219 }
220 return android.Paths{}, nil
221 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500222 case "unstripped":
223 if mod.compiler != nil {
224 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
225 }
226 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400227 default:
228 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
229 }
230}
231
Ivan Lozano52767be2019-10-18 14:49:46 -0700232func (mod *Module) SelectedStl() string {
233 return ""
234}
235
Ivan Lozano2b262972019-11-21 12:30:50 -0800236func (mod *Module) NonCcVariants() bool {
237 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400238 if library, ok := mod.compiler.(libraryInterface); ok {
239 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800240 }
241 }
242 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
243}
244
Ivan Lozano52767be2019-10-18 14:49:46 -0700245func (mod *Module) Static() bool {
246 if mod.compiler != nil {
247 if library, ok := mod.compiler.(libraryInterface); ok {
248 return library.static()
249 }
250 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400251 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700252}
253
254func (mod *Module) Shared() bool {
255 if mod.compiler != nil {
256 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400257 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700258 }
259 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400260 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700261}
262
Ivan Lozanod7586b62021-04-01 09:49:36 -0400263func (mod *Module) Dylib() bool {
264 if mod.compiler != nil {
265 if library, ok := mod.compiler.(libraryInterface); ok {
266 return library.dylib()
267 }
268 }
269 return false
270}
271
Ivan Lozanod106efe2023-09-21 23:30:26 -0400272func (mod *Module) Source() bool {
273 if mod.compiler != nil {
274 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
275 return library.source()
276 }
277 }
278 return false
279}
280
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400281func (mod *Module) RlibStd() bool {
282 if mod.compiler != nil {
283 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
284 return library.rlibStd()
285 }
286 }
287 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
288}
289
Ivan Lozanod7586b62021-04-01 09:49:36 -0400290func (mod *Module) Rlib() bool {
291 if mod.compiler != nil {
292 if library, ok := mod.compiler.(libraryInterface); ok {
293 return library.rlib()
294 }
295 }
296 return false
297}
298
299func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400300 if binary, ok := mod.compiler.(binaryInterface); ok {
301 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302 }
303 return false
304}
305
Justin Yun5e035862021-06-29 20:50:37 +0900306func (mod *Module) StaticExecutable() bool {
307 if !mod.Binary() {
308 return false
309 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400310 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900311}
312
Ivan Lozanod7586b62021-04-01 09:49:36 -0400313func (mod *Module) Object() bool {
314 // Rust has no modules which produce only object files.
315 return false
316}
317
Ivan Lozano52767be2019-10-18 14:49:46 -0700318func (mod *Module) Toc() android.OptionalPath {
319 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400320 if lib, ok := mod.compiler.(libraryInterface); ok {
321 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 }
323 }
324 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
325}
326
Colin Crossc511bc52020-04-07 16:50:32 +0000327func (mod *Module) UseSdk() bool {
328 return false
329}
330
Ivan Lozanod7586b62021-04-01 09:49:36 -0400331func (mod *Module) RelativeInstallPath() string {
332 if mod.compiler != nil {
333 return mod.compiler.relativeInstallPath()
334 }
335 return ""
336}
337
Ivan Lozano52767be2019-10-18 14:49:46 -0700338func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500339 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700340}
341
Jiyong Park7d55b612021-06-11 17:22:09 +0900342func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400343 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900344}
345
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400346func (mod *Module) SubName() string {
347 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700348}
349
Ivan Lozanof1868af2022-04-12 13:08:36 -0400350func (mod *Module) IsVndkPrebuiltLibrary() bool {
351 // Rust modules do not provide VNDK prebuilts
352 return false
353}
354
355func (mod *Module) IsVendorPublicLibrary() bool {
356 return mod.VendorProperties.IsVendorPublicLibrary
357}
358
359func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
360 // Rust modules to not provide Sdk variants
361 return false
362}
363
Colin Cross127bb8b2020-12-16 16:46:01 -0800364func (c *Module) IsVndkPrivate() bool {
365 return false
366}
367
368func (c *Module) IsLlndk() bool {
369 return false
370}
371
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400372func (mod *Module) KernelHeadersDecorator() bool {
373 return false
374}
375
Colin Cross1f3f1302021-04-26 18:37:44 -0700376func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400377 return false
378}
379
Colin Cross5271fea2021-04-27 13:06:04 -0700380func (m *Module) NeedsVendorPublicLibraryVariants() bool {
381 return false
382}
383
Ivan Lozanod7586b62021-04-01 09:49:36 -0400384func (mod *Module) HasLlndkStubs() bool {
385 return false
386}
387
388func (mod *Module) StubsVersion() string {
389 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
390}
391
Ivan Lozano52767be2019-10-18 14:49:46 -0700392func (mod *Module) SdkVersion() string {
393 return ""
394}
395
Colin Crossc511bc52020-04-07 16:50:32 +0000396func (mod *Module) AlwaysSdk() bool {
397 return false
398}
399
Jiyong Park2286afd2020-06-16 21:58:53 +0900400func (mod *Module) IsSdkVariant() bool {
401 return false
402}
403
Colin Cross1348ce32020-10-01 13:37:16 -0700404func (mod *Module) SplitPerApiLevel() bool {
405 return false
406}
407
Sasha Smundaka76acba2022-04-18 20:12:56 -0700408func (mod *Module) XrefRustFiles() android.Paths {
409 return mod.kytheFiles
410}
411
Ivan Lozanoffee3342019-08-27 12:03:00 -0700412type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400413 Dylibs []string
414 Rlibs []string
415 Rustlibs []string
416 Stdlibs []string
417 ProcMacros []string
418 SharedLibs []string
419 StaticLibs []string
420 WholeStaticLibs []string
421 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700422
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400423 // Used for data dependencies adjacent to tests
424 DataLibs []string
425 DataBins []string
426
Colin Crossfe605e12022-01-23 20:46:16 -0800427 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428}
429
430type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700431 DyLibs RustLibraries
432 RLibs RustLibraries
433 SharedLibs android.Paths
434 SharedLibDeps android.Paths
435 StaticLibs android.Paths
436 ProcMacros RustLibraries
437 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500438
439 // depFlags and depLinkFlags are rustc and linker (clang) flags.
440 depFlags []string
441 depLinkFlags []string
442
443 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
444 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000445 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700446 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700447
Ivan Lozano0a468a42024-05-13 21:03:34 -0400448 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
449 // cc_library_static dependants of rlibs.
450 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
451 exportedLinkDirs []string
452
Ivan Lozano45901ed2020-07-24 16:05:01 -0400453 // Used by bindgen modules which call clang
454 depClangFlags []string
455 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400456 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400457 depSystemIncludePaths android.Paths
458
Colin Crossfe605e12022-01-23 20:46:16 -0800459 CrtBegin android.Paths
460 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700461
462 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500463 SrcDeps android.Paths
464 srcProviderFiles android.Paths
Ivan Lozano0a468a42024-05-13 21:03:34 -0400465
466 // Used by Generated Libraries
467 depExportedRlibs []cc.RustRlibDep
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468}
469
470type RustLibraries []RustLibrary
471
472type RustLibrary struct {
473 Path android.Path
474 CrateName string
475}
476
Matthew Maurerbb3add12020-06-25 09:34:12 -0700477type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000478 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700479 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700480}
481
Sasha Smundaka76acba2022-04-18 20:12:56 -0700482type xref interface {
483 XrefRustFiles() android.Paths
484}
485
Matthew Maurerbb3add12020-06-25 09:34:12 -0700486type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000487 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700488 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700489}
490
Wen-yi Chu41326c12023-09-22 03:58:59 +0000491func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
492 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700493}
494
Colin Cross004bd3f2023-10-02 11:39:17 -0700495func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
496 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400497}
498
Colin Cross0de8a1e2020-09-18 14:15:30 -0700499func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800500 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700501 LinkDirs: flagExporter.linkDirs,
502 LinkObjects: flagExporter.linkObjects,
503 })
504}
505
Matthew Maurerbb3add12020-06-25 09:34:12 -0700506var _ exportedFlagsProducer = (*flagExporter)(nil)
507
508func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700509 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700510}
511
Colin Cross0de8a1e2020-09-18 14:15:30 -0700512type FlagExporterInfo struct {
513 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000514 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700515 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700516}
517
Colin Crossbc7d76c2023-12-12 16:39:03 -0800518var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700519
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400520func (mod *Module) isCoverageVariant() bool {
521 return mod.coverage.Properties.IsCoverageVariant
522}
523
524var _ cc.Coverage = (*Module)(nil)
525
Colin Crossf5f4ad32024-01-19 15:41:48 -0800526func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400527 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
528}
529
Ivan Lozanod7586b62021-04-01 09:49:36 -0400530func (mod *Module) VndkVersion() string {
531 return mod.Properties.VndkVersion
532}
533
Ivan Lozano0a468a42024-05-13 21:03:34 -0400534func (mod *Module) ExportedCrateLinkDirs() []string {
535 return mod.exportedLinkDirs
536}
537
Ivan Lozanod7586b62021-04-01 09:49:36 -0400538func (mod *Module) PreventInstall() bool {
539 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540}
541
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400542func (mod *Module) MarkAsCoverageVariant(coverage bool) {
543 mod.coverage.Properties.IsCoverageVariant = coverage
544}
545
546func (mod *Module) EnableCoverageIfNeeded() {
547 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548}
549
550func defaultsFactory() android.Module {
551 return DefaultsFactory()
552}
553
554type Defaults struct {
555 android.ModuleBase
556 android.DefaultsModuleBase
557}
558
559func DefaultsFactory(props ...interface{}) android.Module {
560 module := &Defaults{}
561
562 module.AddProperties(props...)
563 module.AddProperties(
564 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800565 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500566 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100567 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400568 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 &BaseCompilerProperties{},
570 &BinaryCompilerProperties{},
571 &LibraryCompilerProperties{},
572 &ProcMacroCompilerProperties{},
573 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400574 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700575 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400576 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400577 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200578 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500579 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000580 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581 )
582
583 android.InitDefaultsModule(module)
584 return module
585}
586
587func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700588 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589}
590
Ivan Lozano183a3212019-10-18 14:18:45 -0700591func (mod *Module) CcLibrary() bool {
592 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500593 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700594 return true
595 }
596 }
597 return false
598}
599
600func (mod *Module) CcLibraryInterface() bool {
601 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400602 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
603 // VariantIs{Static,Shared} is set.
604 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700605 return true
606 }
607 }
608 return false
609}
610
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400611func (mod *Module) RustLibraryInterface() bool {
612 if mod.compiler != nil {
613 if _, ok := mod.compiler.(libraryInterface); ok {
614 return true
615 }
616 }
617 return false
618}
619
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500620func (mod *Module) IsFuzzModule() bool {
621 if _, ok := mod.compiler.(*fuzzDecorator); ok {
622 return true
623 }
624 return false
625}
626
627func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
628 return mod.FuzzModule
629}
630
631func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
632 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
633 return fuzzer.fuzzPackagedModule
634 }
635 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
636}
637
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000638func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500639 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
640 return fuzzer.sharedLibraries
641 }
642 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
643}
644
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400645func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400646 if mod.compiler != nil {
647 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400648 }
649 return nil
650}
651
Ivan Lozano183a3212019-10-18 14:18:45 -0700652func (mod *Module) SetStatic() {
653 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700654 if library, ok := mod.compiler.(libraryInterface); ok {
655 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700656 return
657 }
658 }
659 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
660}
661
662func (mod *Module) SetShared() {
663 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700664 if library, ok := mod.compiler.(libraryInterface); ok {
665 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700666 return
667 }
668 }
669 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
670}
671
Ivan Lozano183a3212019-10-18 14:18:45 -0700672func (mod *Module) BuildStaticVariant() bool {
673 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700674 if library, ok := mod.compiler.(libraryInterface); ok {
675 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700676 }
677 }
678 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
679}
680
681func (mod *Module) BuildSharedVariant() bool {
682 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700683 if library, ok := mod.compiler.(libraryInterface); ok {
684 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700685 }
686 }
687 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
688}
689
Ivan Lozano183a3212019-10-18 14:18:45 -0700690func (mod *Module) Module() android.Module {
691 return mod
692}
693
Ivan Lozano183a3212019-10-18 14:18:45 -0700694func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400695 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700696}
697
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400698func (mod *Module) CoverageFiles() android.Paths {
699 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800700 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400701 }
702 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
703}
704
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400705// Rust does not produce gcno files, and therefore does not produce a coverage archive.
706func (mod *Module) CoverageOutputFile() android.OptionalPath {
707 return android.OptionalPath{}
708}
709
710func (mod *Module) IsNdk(config android.Config) bool {
711 return false
712}
713
714func (mod *Module) IsStubs() bool {
715 return false
716}
717
Jiyong Park459feca2020-12-15 11:02:21 +0900718func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900719 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900720 return false
721 }
722
Jiyong Park459feca2020-12-15 11:02:21 +0900723 // The apex variant is not installable because it is included in the APEX and won't appear
724 // in the system partition as a standalone file.
725 if !apexInfo.IsForPlatform() {
726 return false
727 }
728
Jiyong Parke54f07e2021-04-07 15:08:04 +0900729 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900730}
731
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500732func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800733 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
734 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500735}
736
Ivan Lozano183a3212019-10-18 14:18:45 -0700737var _ cc.LinkableInterface = (*Module)(nil)
738
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739func (mod *Module) Init() android.Module {
740 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500741 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742
Yi Kong46c6e592022-01-20 22:55:00 +0800743 if mod.afdo != nil {
744 mod.AddProperties(mod.afdo.props()...)
745 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746 if mod.compiler != nil {
747 mod.AddProperties(mod.compiler.compilerProps()...)
748 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400749 if mod.coverage != nil {
750 mod.AddProperties(mod.coverage.props()...)
751 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200752 if mod.clippy != nil {
753 mod.AddProperties(mod.clippy.props()...)
754 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400755 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700756 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400757 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500758 if mod.sanitize != nil {
759 mod.AddProperties(mod.sanitize.props()...)
760 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400761
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900763 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764
765 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700766 return mod
767}
768
769func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
770 return &Module{
771 hod: hod,
772 multilib: multilib,
773 }
774}
775func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
776 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800777 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200779 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500780 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781 return module
782}
783
784type ModuleContext interface {
785 android.ModuleContext
786 ModuleContextIntf
787}
788
789type BaseModuleContext interface {
790 android.BaseModuleContext
791 ModuleContextIntf
792}
793
794type DepsContext interface {
795 android.BottomUpMutatorContext
796 ModuleContextIntf
797}
798
799type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200800 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802}
803
804type depsContext struct {
805 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806}
807
808type moduleContext struct {
809 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810}
811
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200812type baseModuleContext struct {
813 android.BaseModuleContext
814}
815
816func (ctx *moduleContext) RustModule() *Module {
817 return ctx.Module().(*Module)
818}
819
820func (ctx *moduleContext) toolchain() config.Toolchain {
821 return ctx.RustModule().toolchain(ctx)
822}
823
824func (ctx *depsContext) RustModule() *Module {
825 return ctx.Module().(*Module)
826}
827
828func (ctx *depsContext) toolchain() config.Toolchain {
829 return ctx.RustModule().toolchain(ctx)
830}
831
832func (ctx *baseModuleContext) RustModule() *Module {
833 return ctx.Module().(*Module)
834}
835
836func (ctx *baseModuleContext) toolchain() config.Toolchain {
837 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400838}
839
840func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700841 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
842 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
843 return false
844 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400845 return mod.compiler != nil && mod.compiler.nativeCoverage()
846}
847
Ivan Lozanod7586b62021-04-01 09:49:36 -0400848func (mod *Module) EverInstallable() bool {
849 return mod.compiler != nil &&
850 // Check to see whether the module is actually ever installable.
851 mod.compiler.everInstallable()
852}
853
854func (mod *Module) Installable() *bool {
855 return mod.Properties.Installable
856}
857
Ivan Lozano872d5792022-03-23 17:31:39 -0400858func (mod *Module) ProcMacro() bool {
859 if pm, ok := mod.compiler.(procMacroInterface); ok {
860 return pm.ProcMacro()
861 }
862 return false
863}
864
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
866 if mod.cachedToolchain == nil {
867 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
868 }
869 return mod.cachedToolchain
870}
871
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200872func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
873 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
874}
875
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
877}
878
879func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
880 ctx := &moduleContext{
881 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883
Colin Crossff694a82023-12-13 15:54:49 -0800884 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900885 if !apexInfo.IsForPlatform() {
886 mod.hideApexVariantFromMake = true
887 }
888
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500890 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
891
Ivan Lozanof1868af2022-04-12 13:08:36 -0400892 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700893
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 if !toolchain.Supported() {
895 // This toolchain's unsupported, there's nothing to do for this mod.
896 return
897 }
898
899 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400900 // Export linkDirs for CC rust generatedlibs
901 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
902 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
903
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 flags := Flags{
905 Toolchain: toolchain,
906 }
907
Ivan Lozano67eada32021-09-23 11:50:33 -0400908 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800909 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500910 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800911 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 if mod.compiler != nil {
913 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400914 flags = mod.compiler.cfgFlags(ctx, flags)
915 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400916 }
917 if mod.coverage != nil {
918 flags, deps = mod.coverage.flags(ctx, flags, deps)
919 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200920 if mod.clippy != nil {
921 flags, deps = mod.clippy.flags(ctx, flags, deps)
922 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500923 if mod.sanitize != nil {
924 flags, deps = mod.sanitize.flags(ctx, flags, deps)
925 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400926
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200927 // SourceProvider needs to call GenerateSource() before compiler calls
928 // compile() so it can provide the source. A SourceProvider has
929 // multiple variants (e.g. source, rlib, dylib). Only the "source"
930 // variant is responsible for effectively generating the source. The
931 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400932 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200933 if mod.compiler.(libraryInterface).source() {
934 mod.sourceProvider.GenerateSource(ctx, deps)
935 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
936 } else {
937 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
938 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700939 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200940 }
Colin Cross40213022023-12-13 15:19:49 -0800941 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400942 }
943
944 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100945 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700946 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400947 if ctx.Failed() {
948 return
949 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700950 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
951 if buildOutput.kytheFile != nil {
952 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
953 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400954 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900955
Dan Albert06feee92021-03-19 15:06:02 -0700956 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000957 if mod.docTimestampFile.Valid() {
958 ctx.CheckbuildFile(mod.docTimestampFile.Path())
959 }
Dan Albert06feee92021-03-19 15:06:02 -0700960
Colin Crossff694a82023-12-13 15:54:49 -0800961 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400962 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900963 // If the module has been specifically configure to not be installed then
964 // hide from make as otherwise it will break when running inside make as the
965 // output path to install will not be specified. Not all uninstallable
966 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400967 // side dependencies. In particular, proc-macros need to be captured in the
968 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900969 mod.HideFromMake()
970 } else if !mod.installable(apexInfo) {
971 mod.SkipInstall()
972 }
973
974 // Still call install though, the installs will be stored as PackageSpecs to allow
975 // using the outputs in a genrule.
976 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200977 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900978 if ctx.Failed() {
979 return
980 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400981 // Export your own directory as a linkDir
982 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
983
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400984 }
Chris Wailes74be7642021-07-22 16:20:28 -0700985
986 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700987 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000988 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800989 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000990 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700991}
992
993func (mod *Module) deps(ctx DepsContext) Deps {
994 deps := Deps{}
995
996 if mod.compiler != nil {
997 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400998 }
999 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001000 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001001 }
1002
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001003 if mod.coverage != nil {
1004 deps = mod.coverage.deps(ctx, deps)
1005 }
1006
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001007 if mod.sanitize != nil {
1008 deps = mod.sanitize.deps(ctx, deps)
1009 }
1010
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1012 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001013 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1015 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1016 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001017 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001018 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019 return deps
1020
1021}
1022
Ivan Lozanoffee3342019-08-27 12:03:00 -07001023type dependencyTag struct {
1024 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001025 name string
1026 library bool
1027 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001028 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029}
1030
Jiyong Park65b62242020-11-25 12:44:59 +09001031// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1032// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1033func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001034 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001035}
1036
1037var _ android.InstallNeededDependencyTag = dependencyTag{}
1038
Colin Cross65cb3142021-12-10 23:05:02 +00001039func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1040 if d.library && d.dynamic {
1041 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1042 }
1043 return nil
1044}
1045
Yu Liuc8884602024-03-15 18:48:38 +00001046func (d dependencyTag) PropagateAconfigValidation() bool {
1047 return d == rlibDepTag || d == sourceDepTag
1048}
1049
1050var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1051
Colin Cross65cb3142021-12-10 23:05:02 +00001052var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1053
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001055 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1056 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001057 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001058 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001059 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001060 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001061 dataLibDepTag = dependencyTag{name: "data lib"}
1062 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001063)
1064
Jiyong Park99644e92020-11-17 22:21:02 +09001065func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1066 tag, ok := depTag.(dependencyTag)
1067 return ok && tag == dylibDepTag
1068}
1069
Jiyong Park94e22fd2021-04-08 18:19:15 +09001070func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1071 tag, ok := depTag.(dependencyTag)
1072 return ok && tag == rlibDepTag
1073}
1074
Matthew Maurer0f003b12020-06-29 14:34:06 -07001075type autoDep struct {
1076 variation string
1077 depTag dependencyTag
1078}
1079
1080var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001081 rlibVariation = "rlib"
1082 dylibVariation = "dylib"
1083 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1084 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001085)
1086
1087type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001088 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001089}
1090
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001091func (mod *Module) begin(ctx BaseModuleContext) {
1092 if mod.coverage != nil {
1093 mod.coverage.begin(ctx)
1094 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001095 if mod.sanitize != nil {
1096 mod.sanitize.begin(ctx)
1097 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001098}
1099
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001100func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001101 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001102 return p.prebuilt()
1103 }
1104 return nil
1105}
1106
Kiyoung Kim37693d02024-04-04 09:56:15 +09001107func (mod *Module) Symlinks() []string {
1108 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1109 return nil
1110}
1111
Justin Yun24b246a2023-03-16 10:36:16 +09001112func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1113 if rustDep, ok := dep.(*Module); ok {
1114 // Use base module name for snapshots when exporting to Makefile.
1115 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1116 baseName := rustDep.BaseModuleName()
1117 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1118 }
1119 }
1120 return cc.MakeLibName(ctx, c, dep, depName)
1121}
1122
Ivan Lozanod106efe2023-09-21 23:30:26 -04001123func collectIncludedProtos(mod *Module, dep *Module) {
1124 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1125 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1126 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1127 }
1128 }
1129}
Andrew Walbran52533232024-03-19 11:36:04 +00001130
Ivan Lozanoffee3342019-08-27 12:03:00 -07001131func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1132 var depPaths PathDeps
1133
1134 directRlibDeps := []*Module{}
1135 directDylibDeps := []*Module{}
1136 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001137 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001138 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001139 directSrcProvidersDeps := []*Module{}
1140 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001141
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001142 // For the dependency from platform to apex, use the latest stubs
1143 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001144 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001145 if !apexInfo.IsForPlatform() {
1146 mod.apexSdkVersion = apexInfo.MinSdkVersion
1147 }
1148
1149 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1150 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1151 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1152 // (b/144430859)
1153 mod.apexSdkVersion = android.FutureApiLevel
1154 }
1155
Spandan Das604f3762023-03-16 22:51:40 +00001156 skipModuleList := map[string]bool{}
1157
1158 var apiImportInfo multitree.ApiImportInfo
1159 hasApiImportInfo := false
1160
1161 ctx.VisitDirectDeps(func(dep android.Module) {
1162 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001163 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001164 hasApiImportInfo = true
1165 }
1166 })
1167
1168 if hasApiImportInfo {
1169 targetStubModuleList := map[string]string{}
1170 targetOrigModuleList := map[string]string{}
1171
1172 // Search for dependency which both original module and API imported library with APEX stub exists
1173 ctx.VisitDirectDeps(func(dep android.Module) {
1174 depName := ctx.OtherModuleName(dep)
1175 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1176 targetStubModuleList[apiLibrary] = depName
1177 }
1178 })
1179 ctx.VisitDirectDeps(func(dep android.Module) {
1180 depName := ctx.OtherModuleName(dep)
1181 if origLibrary, ok := targetStubModuleList[depName]; ok {
1182 targetOrigModuleList[origLibrary] = depName
1183 }
1184 })
1185
1186 // Decide which library should be used between original and API imported library
1187 ctx.VisitDirectDeps(func(dep android.Module) {
1188 depName := ctx.OtherModuleName(dep)
1189 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1190 if cc.ShouldUseStubForApex(ctx, dep) {
1191 skipModuleList[depName] = true
1192 } else {
1193 skipModuleList[apiLibrary] = true
1194 }
1195 }
1196 })
1197 }
1198
Cole Faustb6e6f992023-08-17 17:42:26 -07001199 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1200 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001201
Ivan Lozanoffee3342019-08-27 12:03:00 -07001202 ctx.VisitDirectDeps(func(dep android.Module) {
1203 depName := ctx.OtherModuleName(dep)
1204 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001205
Spandan Das604f3762023-03-16 22:51:40 +00001206 if _, exists := skipModuleList[depName]; exists {
1207 return
1208 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001209
1210 if depTag == android.DarwinUniversalVariantTag {
1211 return
1212 }
1213
Ivan Lozano0a468a42024-05-13 21:03:34 -04001214 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001215 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001216 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001217
Ivan Lozanoffee3342019-08-27 12:03:00 -07001218 switch depTag {
1219 case dylibDepTag:
1220 dylib, ok := rustDep.compiler.(libraryInterface)
1221 if !ok || !dylib.dylib() {
1222 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1223 return
1224 }
1225 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001226 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001227 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1228
Ivan Lozanoffee3342019-08-27 12:03:00 -07001229 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001230
Ivan Lozanoffee3342019-08-27 12:03:00 -07001231 rlib, ok := rustDep.compiler.(libraryInterface)
1232 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001233 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001234 return
1235 }
1236 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001237 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001238 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1239
Ivan Lozano0a468a42024-05-13 21:03:34 -04001240 // rust_ffi rlibs may export include dirs, so collect those here.
1241 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1242 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1243 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1244
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 case procMacroDepTag:
1246 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001247 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001248 // proc_macro link dirs need to be exported, so collect those here.
1249 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001250
1251 case sourceDepTag:
1252 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1253 collectIncludedProtos(mod, rustDep)
1254 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001255 }
1256
Cole Faustb6e6f992023-08-17 17:42:26 -07001257 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1258
Paul Duffind5cf92e2021-07-09 17:38:55 +01001259 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001260 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1261 // OS/Arch variant is used.
1262 var helper string
1263 if ctx.Host() {
1264 helper = "missing 'host_supported'?"
1265 } else {
1266 helper = "device module defined?"
1267 }
1268
1269 if dep.Target().Os != ctx.Os() {
1270 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1271 return
1272 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1273 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1274 return
1275 }
1276 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001277 }
1278
Ivan Lozano0a468a42024-05-13 21:03:34 -04001279 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001280 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001281 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001282 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1283 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001284 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001285 }
1286
Ivan Lozanoffee3342019-08-27 12:03:00 -07001287 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001288 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001289 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001290 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001291 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 }
1293 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001294
Ivan Lozanod106efe2023-09-21 23:30:26 -04001295 if depTag == sourceDepTag {
1296 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1297 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001298 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001299 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1300 }
1301 }
1302 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001303 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001304 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001305 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001306 if _, ok := ccDep.(*Module); !ok {
1307 if ccDep.Module().Target().Os != ctx.Os() {
1308 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1309 return
1310 }
1311 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1312 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1313 return
1314 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001315 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001316 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001317 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001318 if !ctx.Config().AllowMissingDependencies() {
1319 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1320 } else {
1321 ctx.AddMissingDependencies([]string{depName})
1322 }
1323 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001324 }
1325
Wen-yi Chu41326c12023-09-22 03:58:59 +00001326 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001327
Ivan Lozanoffee3342019-08-27 12:03:00 -07001328 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001329 switch {
1330 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001331 if cc.IsWholeStaticLib(depTag) {
1332 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1333 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001334 if mod.Binary() {
1335 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1336 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1337 // final linkage, pass the args directly to the linker to handle these cases.
1338 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1339 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001340 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001341 } else {
1342 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 -05001343 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001344 }
1345
1346 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1347 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001348 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001349 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1350
Colin Cross313aa542023-12-13 13:47:44 -08001351 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001352 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1353 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1354 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1355 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001356 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001357
1358 // Record baseLibName for snapshots.
1359 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1360
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001361 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001362 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001363 // For the shared lib dependencies, we may link to the stub variant
1364 // of the dependency depending on the context (e.g. if this
1365 // dependency crosses the APEX boundaries).
1366 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1367
1368 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1369 // object (either from stub or non-stub) to use.
1370 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001371 if !linkObject.Valid() {
1372 if !ctx.Config().AllowMissingDependencies() {
1373 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1374 } else {
1375 ctx.AddMissingDependencies([]string{depName})
1376 }
1377 return
1378 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001379 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001380
Ivan Lozanoffee3342019-08-27 12:03:00 -07001381 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001382 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001383 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1384 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1385 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1386 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001387 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001388
1389 // Record baseLibName for snapshots.
1390 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1391
Cole Faustb6e6f992023-08-17 17:42:26 -07001392 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001393 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001394 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001395 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001396 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1397 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1398 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001399 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001400 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001401 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001402 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001403 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001404 }
1405
1406 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001407 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1408 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001409 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001410 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001411 } else {
1412 switch {
1413 case depTag == cc.CrtBeginDepTag:
1414 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1415 case depTag == cc.CrtEndDepTag:
1416 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1417 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001418 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001419
1420 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001421 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001422 // These are usually genrules which don't have per-target variants.
1423 directSrcDeps = append(directSrcDeps, srcDep)
1424 }
1425 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001426 })
1427
Wen-yi Chu41326c12023-09-22 03:58:59 +00001428 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1429
1430 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001431 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001432 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001433 crateName := dep.CrateName()
1434 if alias, aliased := aliases[crateName]; aliased {
1435 crateName = alias
1436 }
1437 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001438 }
1439 var dylibDepFiles RustLibraries
1440 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001441 crateName := dep.CrateName()
1442 if alias, aliased := aliases[crateName]; aliased {
1443 crateName = alias
1444 }
1445 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001446 }
1447 var procMacroDepFiles RustLibraries
1448 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001449 crateName := dep.CrateName()
1450 if alias, aliased := aliases[crateName]; aliased {
1451 crateName = alias
1452 }
1453 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001454 }
1455
Colin Cross004bd3f2023-10-02 11:39:17 -07001456 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001457 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001458 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001459 }
1460
Colin Cross004bd3f2023-10-02 11:39:17 -07001461 var sharedLibFiles android.Paths
1462 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001464 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001465 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001466 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001467 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001468 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001469 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001470 }
1471
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001472 var srcProviderDepFiles android.Paths
1473 for _, dep := range directSrcProvidersDeps {
1474 srcs, _ := dep.OutputFiles("")
1475 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1476 }
1477 for _, dep := range directSrcDeps {
1478 srcs := dep.Srcs()
1479 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1480 }
1481
Wen-yi Chu41326c12023-09-22 03:58:59 +00001482 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1483 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001484 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1485 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1486 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001487 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001488 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001489
1490 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001491 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001492 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001493 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001494 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1495 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1496 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001497
1498 return depPaths
1499}
1500
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001501func (mod *Module) InstallInData() bool {
1502 if mod.compiler == nil {
1503 return false
1504 }
1505 return mod.compiler.inData()
1506}
1507
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001508func (mod *Module) InstallInRamdisk() bool {
1509 return mod.InRamdisk()
1510}
1511
1512func (mod *Module) InstallInVendorRamdisk() bool {
1513 return mod.InVendorRamdisk()
1514}
1515
1516func (mod *Module) InstallInRecovery() bool {
1517 return mod.InRecovery()
1518}
1519
Wen-yi Chu41326c12023-09-22 03:58:59 +00001520func linkPathFromFilePath(filepath android.Path) string {
1521 return strings.Split(filepath.String(), filepath.Base())[0]
1522}
1523
Spandan Das604f3762023-03-16 22:51:40 +00001524// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1525func (r *Module) usePublicApi() bool {
1526 return r.Device() && r.UseSdk()
1527}
1528
1529// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1530func (r *Module) useVendorApi() bool {
1531 return r.Device() && (r.InVendor() || r.InProduct())
1532}
1533
Ivan Lozanoffee3342019-08-27 12:03:00 -07001534func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1535 ctx := &depsContext{
1536 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001537 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001538
1539 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001540 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001541
Kiyoung Kim487689e2022-07-26 09:48:22 +09001542 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001543 if mod.usePublicApi() || mod.useVendorApi() {
1544 for idx, lib := range deps.SharedLibs {
1545 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1546 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001547 }
1548
Ivan Lozano1921e802021-05-20 13:39:16 -04001549 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001550 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001551 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001552
Ivan Lozano2b081132020-09-08 12:46:52 -04001553 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001554 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001555 stdLinkage = "rlib-std"
1556 }
1557
1558 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001559 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001560
Ivan Lozano2b081132020-09-08 12:46:52 -04001561 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1562 rlibDepVariations = append(rlibDepVariations,
1563 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1564 }
1565
Ivan Lozano1921e802021-05-20 13:39:16 -04001566 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001567 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001568 for _, lib := range deps.Rlibs {
1569 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001570 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001571 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001572
1573 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001574 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001575 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1576
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001577 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001578 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001579 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001580
Ivan Lozano1921e802021-05-20 13:39:16 -04001581 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001582 if deps.Rustlibs != nil {
1583 if !mod.compiler.Disabled() {
1584 for _, lib := range deps.Rustlibs {
1585 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1586 if autoDep.depTag == rlibDepTag {
1587 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001588 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1589
Ivan Lozanod106efe2023-09-21 23:30:26 -04001590 } else {
1591 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1592 // Check for the existence of the dylib deptag variant. Select it if available,
1593 // otherwise select the rlib variant.
1594 autoDepVariations := append(commonDepVariations,
1595 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001596 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001597 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1598 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001599
Ivan Lozanod106efe2023-09-21 23:30:26 -04001600 } else {
1601 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001602 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1603
Ivan Lozanod106efe2023-09-21 23:30:26 -04001604 }
1605 }
1606 }
1607 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1608 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001609 srcProviderVariations := append(commonDepVariations,
1610 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001611 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001612
Ivan Lozano0a468a42024-05-13 21:03:34 -04001613 // Only add rustlib dependencies if they're source providers themselves.
1614 // This is used to track which crate names need to be added to the source generated
1615 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001616 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001617 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001618 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001619 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001620 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001621 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001622
Ivan Lozano1921e802021-05-20 13:39:16 -04001623 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001624 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001625 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001626 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001627 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001628 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001629 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001630 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001631 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001632 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1633
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001634 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001635 }
1636 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001637
1638 for _, lib := range deps.SharedLibs {
1639 depTag := cc.SharedDepTag()
1640 name, version := cc.StubsLibNameAndVersion(lib)
1641
1642 variations := []blueprint.Variation{
1643 {Mutator: "link", Variation: "shared"},
1644 }
Spandan Das604f3762023-03-16 22:51:40 +00001645 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1646 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1647 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1648 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1649 }
1650
1651 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1652 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1653 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001654 }
1655
1656 for _, lib := range deps.WholeStaticLibs {
1657 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001658
1659 actx.AddVariationDependencies([]blueprint.Variation{
1660 {Mutator: "link", Variation: "static"},
1661 }, depTag, lib)
1662 }
1663
1664 for _, lib := range deps.StaticLibs {
1665 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001666
1667 actx.AddVariationDependencies([]blueprint.Variation{
1668 {Mutator: "link", Variation: "static"},
1669 }, depTag, lib)
1670 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001671
Zach Johnson3df4e632020-11-06 11:56:27 -08001672 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1673
Colin Cross565cafd2020-09-25 18:47:38 -07001674 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001675 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001676 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001677 }
Colin Crossfe605e12022-01-23 20:46:16 -08001678 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001679 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001680 }
1681
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001682 if mod.sourceProvider != nil {
1683 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1684 bindgen.Properties.Custom_bindgen != "" {
1685 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1686 bindgen.Properties.Custom_bindgen)
1687 }
1688 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001689
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001690 actx.AddVariationDependencies([]blueprint.Variation{
1691 {Mutator: "link", Variation: "shared"},
1692 }, dataLibDepTag, deps.DataLibs...)
1693
1694 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1695
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001696 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001697 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001698
1699 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001700}
1701
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001702func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001703 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001704 mod.beginMutator(ctx)
1705 }
1706}
1707
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001708func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1709 ctx := &baseModuleContext{
1710 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001711 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001712
1713 mod.begin(ctx)
1714}
1715
Ivan Lozanoffee3342019-08-27 12:03:00 -07001716func (mod *Module) Name() string {
1717 name := mod.ModuleBase.Name()
1718 if p, ok := mod.compiler.(interface {
1719 Name(string) string
1720 }); ok {
1721 name = p.Name(name)
1722 }
1723 return name
1724}
1725
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001726func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001727 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001728 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001729 }
1730}
1731
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001732var _ android.HostToolProvider = (*Module)(nil)
1733
1734func (mod *Module) HostToolPath() android.OptionalPath {
1735 if !mod.Host() {
1736 return android.OptionalPath{}
1737 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001738 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1739 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001740 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1741 // Even though proc-macros aren't strictly "tools", since they target the compiler
1742 // and act as compiler plugins, we treat them similarly.
1743 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001744 }
1745 return android.OptionalPath{}
1746}
1747
Jiyong Park99644e92020-11-17 22:21:02 +09001748var _ android.ApexModule = (*Module)(nil)
1749
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001750func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001751 return String(mod.Properties.Min_sdk_version)
1752}
1753
Jiyong Park45bf82e2020-12-15 22:29:02 +09001754// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001755func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001756 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001757 if minSdkVersion == "apex_inherit" {
1758 return nil
1759 }
1760 if minSdkVersion == "" {
1761 return fmt.Errorf("min_sdk_version is not specificed")
1762 }
1763
1764 // Not using nativeApiLevelFromUser because the context here is not
1765 // necessarily a native context.
1766 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1767 if err != nil {
1768 return err
1769 }
1770
1771 if ver.GreaterThan(sdkVersion) {
1772 return fmt.Errorf("newer SDK(%v)", ver)
1773 }
Jiyong Park99644e92020-11-17 22:21:02 +09001774 return nil
1775}
1776
Jiyong Park45bf82e2020-12-15 22:29:02 +09001777// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001778func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1779 depTag := ctx.OtherModuleDependencyTag(dep)
1780
1781 if ccm, ok := dep.(*cc.Module); ok {
1782 if ccm.HasStubsVariants() {
1783 if cc.IsSharedDepTag(depTag) {
1784 // dynamic dep to a stubs lib crosses APEX boundary
1785 return false
1786 }
1787 if cc.IsRuntimeDepTag(depTag) {
1788 // runtime dep to a stubs lib also crosses APEX boundary
1789 return false
1790 }
1791
1792 if cc.IsHeaderDepTag(depTag) {
1793 return false
1794 }
1795 }
1796 if mod.Static() && cc.IsSharedDepTag(depTag) {
1797 // shared_lib dependency from a static lib is considered as crossing
1798 // the APEX boundary because the dependency doesn't actually is
1799 // linked; the dependency is used only during the compilation phase.
1800 return false
1801 }
1802 }
1803
Matthew Maurer581b6d82022-09-29 16:46:25 -07001804 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001805 return false
1806 }
1807
1808 return true
1809}
1810
1811// Overrides ApexModule.IsInstallabeToApex()
1812func (mod *Module) IsInstallableToApex() bool {
1813 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001814 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001815 return true
1816 }
1817 if _, ok := mod.compiler.(*binaryDecorator); ok {
1818 return true
1819 }
1820 }
1821 return false
1822}
1823
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001824// If a library file has a "lib" prefix, extract the library name without the prefix.
1825func libNameFromFilePath(filepath android.Path) (string, bool) {
1826 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1827 if strings.HasPrefix(libName, "lib") {
1828 libName = libName[3:]
1829 return libName, true
1830 }
1831 return "", false
1832}
1833
Sasha Smundaka76acba2022-04-18 20:12:56 -07001834func kytheExtractRustFactory() android.Singleton {
1835 return &kytheExtractRustSingleton{}
1836}
1837
1838type kytheExtractRustSingleton struct {
1839}
1840
1841func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1842 var xrefTargets android.Paths
1843 ctx.VisitAllModules(func(module android.Module) {
1844 if rustModule, ok := module.(xref); ok {
1845 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1846 }
1847 })
1848 if len(xrefTargets) > 0 {
1849 ctx.Phony("xref_rust", xrefTargets...)
1850 }
1851}
1852
Jihoon Kangf78a8902022-09-01 22:47:07 +00001853func (c *Module) Partition() string {
1854 return ""
1855}
1856
Ivan Lozanoffee3342019-08-27 12:03:00 -07001857var Bool = proptools.Bool
1858var BoolDefault = proptools.BoolDefault
1859var String = proptools.String
1860var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001861
1862var _ android.OutputFileProducer = (*Module)(nil)