blob: 4edd807ee5f55f791d30d7816e31965202afc635 [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
Ivan Lozanoe6d30982021-02-05 10:57:43 -050083 CoreVariantNeeded bool `blueprint:"mutated"`
84 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070085 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080086 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050087 ExtraVariants []string `blueprint:"mutated"`
88
Ivan Lozanoa2268632021-07-22 10:52:06 -040089 // Allows this module to use non-APEX version of libraries. Useful
90 // for building binaries that are started before APEXes are activated.
91 Bootstrap *bool
92
Ivan Lozano1921e802021-05-20 13:39:16 -040093 // Used by vendor snapshot to record dependencies from snapshot modules.
94 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090095 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040096 SnapshotRlibs []string `blueprint:"mutated"`
97 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040098
Matthew Maurerc6868382021-07-13 14:12:37 -070099 // Make this module available when building for ramdisk.
100 // On device without a dedicated recovery partition, the module is only
101 // available after switching root into
102 // /first_stage_ramdisk. To expose the module before switching root, install
103 // the recovery variant instead.
104 Ramdisk_available *bool
105
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500106 // Make this module available when building for vendor ramdisk.
107 // On device without a dedicated recovery partition, the module is only
108 // available after switching root into
109 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800110 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500111 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400112
Ivan Lozano1921e802021-05-20 13:39:16 -0400113 // Normally Soong uses the directory structure to decide which modules
114 // should be included (framework) or excluded (non-framework) from the
115 // different snapshots (vendor, recovery, etc.), but this property
116 // allows a partner to exclude a module normally thought of as a
117 // framework module from the vendor snapshot.
118 Exclude_from_vendor_snapshot *bool
119
120 // Normally Soong uses the directory structure to decide which modules
121 // should be included (framework) or excluded (non-framework) from the
122 // different snapshots (vendor, recovery, etc.), but this property
123 // allows a partner to exclude a module normally thought of as a
124 // framework module from the recovery snapshot.
125 Exclude_from_recovery_snapshot *bool
126
Matthew Maurer460ee942021-02-11 12:31:46 -0800127 // Make this module available when building for recovery
128 Recovery_available *bool
129
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500130 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
131 Min_sdk_version *string
132
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900133 HideFromMake bool `blueprint:"mutated"`
134 PreventInstall bool `blueprint:"mutated"`
135
136 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137}
138
139type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700140 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
Ivan Lozano6a884432020-12-02 09:15:16 -0500142 VendorProperties cc.VendorProperties
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144 Properties BaseProperties
145
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000146 hod android.HostOrDeviceSupported
147 multilib android.Multilib
148 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149
Ivan Lozano6a884432020-12-02 09:15:16 -0500150 makeLinkType string
151
Yi Kong46c6e592022-01-20 22:55:00 +0800152 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400154 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200155 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500156 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700159 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400160
Ivan Lozano0a468a42024-05-13 21:03:34 -0400161 exportedLinkDirs []string
162
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400163 // Output file to be installed, may be stripped or unstripped.
164 outputFile android.OptionalPath
165
Sasha Smundaka76acba2022-04-18 20:12:56 -0700166 // Cross-reference input file
167 kytheFiles android.Paths
168
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400169 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900170
171 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500172
173 // For apex variants, this is set as apex.min_sdk_version
174 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700175
176 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400177
LaMont Jones0c971852023-12-07 21:56:59 +0000178 // Aconfig files for all transitive deps. Also exposed via TransitiveDeclarationsInfo
179 mergedAconfigFiles map[string]android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180}
181
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500182func (mod *Module) Header() bool {
183 //TODO: If Rust libraries provide header variants, this needs to be updated.
184 return false
185}
186
187func (mod *Module) SetPreventInstall() {
188 mod.Properties.PreventInstall = true
189}
190
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500191func (mod *Module) SetHideFromMake() {
192 mod.Properties.HideFromMake = true
193}
194
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900195func (mod *Module) HiddenFromMake() bool {
196 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400197}
198
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500199func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500200 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
201 // nil since we need compiler to actually sanitize.
202 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500203}
204
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500205func (mod *Module) IsPrebuilt() bool {
206 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
207 return true
208 }
209 return false
210}
211
Ivan Lozano43845682020-07-09 21:03:28 -0400212func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
213 switch tag {
214 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700215 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400216 return mod.sourceProvider.Srcs(), nil
217 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900218 if mod.OutputFile().Valid() {
219 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400220 }
221 return android.Paths{}, nil
222 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500223 case "unstripped":
224 if mod.compiler != nil {
225 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
226 }
227 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400228 default:
229 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
230 }
231}
232
Ivan Lozano52767be2019-10-18 14:49:46 -0700233func (mod *Module) SelectedStl() string {
234 return ""
235}
236
Ivan Lozano2b262972019-11-21 12:30:50 -0800237func (mod *Module) NonCcVariants() bool {
238 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400239 if library, ok := mod.compiler.(libraryInterface); ok {
240 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800241 }
242 }
243 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
244}
245
Ivan Lozano52767be2019-10-18 14:49:46 -0700246func (mod *Module) Static() bool {
247 if mod.compiler != nil {
248 if library, ok := mod.compiler.(libraryInterface); ok {
249 return library.static()
250 }
251 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400252 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700253}
254
255func (mod *Module) Shared() bool {
256 if mod.compiler != nil {
257 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400258 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700259 }
260 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400261 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700262}
263
Ivan Lozanod7586b62021-04-01 09:49:36 -0400264func (mod *Module) Dylib() bool {
265 if mod.compiler != nil {
266 if library, ok := mod.compiler.(libraryInterface); ok {
267 return library.dylib()
268 }
269 }
270 return false
271}
272
Ivan Lozanod106efe2023-09-21 23:30:26 -0400273func (mod *Module) Source() bool {
274 if mod.compiler != nil {
275 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
276 return library.source()
277 }
278 }
279 return false
280}
281
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400282func (mod *Module) RlibStd() bool {
283 if mod.compiler != nil {
284 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
285 return library.rlibStd()
286 }
287 }
288 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
289}
290
Ivan Lozanod7586b62021-04-01 09:49:36 -0400291func (mod *Module) Rlib() bool {
292 if mod.compiler != nil {
293 if library, ok := mod.compiler.(libraryInterface); ok {
294 return library.rlib()
295 }
296 }
297 return false
298}
299
300func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400301 if binary, ok := mod.compiler.(binaryInterface); ok {
302 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400303 }
304 return false
305}
306
Justin Yun5e035862021-06-29 20:50:37 +0900307func (mod *Module) StaticExecutable() bool {
308 if !mod.Binary() {
309 return false
310 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400311 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900312}
313
Ivan Lozanod7586b62021-04-01 09:49:36 -0400314func (mod *Module) Object() bool {
315 // Rust has no modules which produce only object files.
316 return false
317}
318
Ivan Lozano52767be2019-10-18 14:49:46 -0700319func (mod *Module) Toc() android.OptionalPath {
320 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400321 if lib, ok := mod.compiler.(libraryInterface); ok {
322 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700323 }
324 }
325 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
326}
327
Colin Crossc511bc52020-04-07 16:50:32 +0000328func (mod *Module) UseSdk() bool {
329 return false
330}
331
Ivan Lozanod7586b62021-04-01 09:49:36 -0400332func (mod *Module) RelativeInstallPath() string {
333 if mod.compiler != nil {
334 return mod.compiler.relativeInstallPath()
335 }
336 return ""
337}
338
Ivan Lozano52767be2019-10-18 14:49:46 -0700339func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500340 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700341}
342
Jiyong Park7d55b612021-06-11 17:22:09 +0900343func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400344 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900345}
346
Ivan Lozano52767be2019-10-18 14:49:46 -0700347func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400348 return true
349}
350
351func (mod *Module) SubName() string {
352 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700353}
354
355func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500356 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700357 return false
358}
359
Ivan Lozanof9e21722020-12-02 09:00:51 -0500360func (mod *Module) IsVndkExt() bool {
361 return false
362}
363
Ivan Lozanod7586b62021-04-01 09:49:36 -0400364func (mod *Module) IsVndkSp() bool {
365 return false
366}
367
Ivan Lozanof1868af2022-04-12 13:08:36 -0400368func (mod *Module) IsVndkPrebuiltLibrary() bool {
369 // Rust modules do not provide VNDK prebuilts
370 return false
371}
372
373func (mod *Module) IsVendorPublicLibrary() bool {
374 return mod.VendorProperties.IsVendorPublicLibrary
375}
376
377func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
378 // Rust modules to not provide Sdk variants
379 return false
380}
381
Colin Cross127bb8b2020-12-16 16:46:01 -0800382func (c *Module) IsVndkPrivate() bool {
383 return false
384}
385
386func (c *Module) IsLlndk() bool {
387 return false
388}
389
390func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500391 return false
392}
393
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400394func (mod *Module) KernelHeadersDecorator() bool {
395 return false
396}
397
Colin Cross1f3f1302021-04-26 18:37:44 -0700398func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400399 return false
400}
401
Colin Cross5271fea2021-04-27 13:06:04 -0700402func (m *Module) NeedsVendorPublicLibraryVariants() bool {
403 return false
404}
405
Ivan Lozanod7586b62021-04-01 09:49:36 -0400406func (mod *Module) HasLlndkStubs() bool {
407 return false
408}
409
410func (mod *Module) StubsVersion() string {
411 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
412}
413
Ivan Lozano52767be2019-10-18 14:49:46 -0700414func (mod *Module) SdkVersion() string {
415 return ""
416}
417
Colin Crossc511bc52020-04-07 16:50:32 +0000418func (mod *Module) AlwaysSdk() bool {
419 return false
420}
421
Jiyong Park2286afd2020-06-16 21:58:53 +0900422func (mod *Module) IsSdkVariant() bool {
423 return false
424}
425
Colin Cross1348ce32020-10-01 13:37:16 -0700426func (mod *Module) SplitPerApiLevel() bool {
427 return false
428}
429
Sasha Smundaka76acba2022-04-18 20:12:56 -0700430func (mod *Module) XrefRustFiles() android.Paths {
431 return mod.kytheFiles
432}
433
Ivan Lozanoffee3342019-08-27 12:03:00 -0700434type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400435 Dylibs []string
436 Rlibs []string
437 Rustlibs []string
438 Stdlibs []string
439 ProcMacros []string
440 SharedLibs []string
441 StaticLibs []string
442 WholeStaticLibs []string
443 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400445 // Used for data dependencies adjacent to tests
446 DataLibs []string
447 DataBins []string
448
Colin Crossfe605e12022-01-23 20:46:16 -0800449 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700450}
451
452type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700453 DyLibs RustLibraries
454 RLibs RustLibraries
455 SharedLibs android.Paths
456 SharedLibDeps android.Paths
457 StaticLibs android.Paths
458 ProcMacros RustLibraries
459 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500460
461 // depFlags and depLinkFlags are rustc and linker (clang) flags.
462 depFlags []string
463 depLinkFlags []string
464
465 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
466 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000467 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700468 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700469
Ivan Lozano0a468a42024-05-13 21:03:34 -0400470 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
471 // cc_library_static dependants of rlibs.
472 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
473 exportedLinkDirs []string
474
Ivan Lozano45901ed2020-07-24 16:05:01 -0400475 // Used by bindgen modules which call clang
476 depClangFlags []string
477 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400478 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400479 depSystemIncludePaths android.Paths
480
Colin Crossfe605e12022-01-23 20:46:16 -0800481 CrtBegin android.Paths
482 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700483
484 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500485 SrcDeps android.Paths
486 srcProviderFiles android.Paths
Ivan Lozano0a468a42024-05-13 21:03:34 -0400487
488 // Used by Generated Libraries
489 depExportedRlibs []cc.RustRlibDep
Ivan Lozanoffee3342019-08-27 12:03:00 -0700490}
491
492type RustLibraries []RustLibrary
493
494type RustLibrary struct {
495 Path android.Path
496 CrateName string
497}
498
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000500 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700501 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700502}
503
Sasha Smundaka76acba2022-04-18 20:12:56 -0700504type xref interface {
505 XrefRustFiles() android.Paths
506}
507
Matthew Maurerbb3add12020-06-25 09:34:12 -0700508type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000509 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700510 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700511}
512
Wen-yi Chu41326c12023-09-22 03:58:59 +0000513func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
514 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700515}
516
Colin Cross004bd3f2023-10-02 11:39:17 -0700517func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
518 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400519}
520
Colin Cross0de8a1e2020-09-18 14:15:30 -0700521func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800522 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700523 LinkDirs: flagExporter.linkDirs,
524 LinkObjects: flagExporter.linkObjects,
525 })
526}
527
Matthew Maurerbb3add12020-06-25 09:34:12 -0700528var _ exportedFlagsProducer = (*flagExporter)(nil)
529
530func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700531 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700532}
533
Colin Cross0de8a1e2020-09-18 14:15:30 -0700534type FlagExporterInfo struct {
535 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000536 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700537 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700538}
539
Colin Crossbc7d76c2023-12-12 16:39:03 -0800540var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700541
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400542func (mod *Module) isCoverageVariant() bool {
543 return mod.coverage.Properties.IsCoverageVariant
544}
545
546var _ cc.Coverage = (*Module)(nil)
547
Colin Crossf5f4ad32024-01-19 15:41:48 -0800548func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400549 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
550}
551
Ivan Lozanod7586b62021-04-01 09:49:36 -0400552func (mod *Module) VndkVersion() string {
553 return mod.Properties.VndkVersion
554}
555
Ivan Lozano0a468a42024-05-13 21:03:34 -0400556func (mod *Module) ExportedCrateLinkDirs() []string {
557 return mod.exportedLinkDirs
558}
559
Ivan Lozanod7586b62021-04-01 09:49:36 -0400560func (mod *Module) PreventInstall() bool {
561 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400562}
563
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400564func (mod *Module) MarkAsCoverageVariant(coverage bool) {
565 mod.coverage.Properties.IsCoverageVariant = coverage
566}
567
568func (mod *Module) EnableCoverageIfNeeded() {
569 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700570}
571
572func defaultsFactory() android.Module {
573 return DefaultsFactory()
574}
575
576type Defaults struct {
577 android.ModuleBase
578 android.DefaultsModuleBase
579}
580
581func DefaultsFactory(props ...interface{}) android.Module {
582 module := &Defaults{}
583
584 module.AddProperties(props...)
585 module.AddProperties(
586 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800587 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500588 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100589 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400590 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700591 &BaseCompilerProperties{},
592 &BinaryCompilerProperties{},
593 &LibraryCompilerProperties{},
594 &ProcMacroCompilerProperties{},
595 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400596 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700597 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400598 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400599 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200600 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500601 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000602 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700603 )
604
605 android.InitDefaultsModule(module)
606 return module
607}
608
609func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700610 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611}
612
Ivan Lozano183a3212019-10-18 14:18:45 -0700613func (mod *Module) CcLibrary() bool {
614 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500615 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700616 return true
617 }
618 }
619 return false
620}
621
622func (mod *Module) CcLibraryInterface() bool {
623 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400624 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
625 // VariantIs{Static,Shared} is set.
626 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700627 return true
628 }
629 }
630 return false
631}
632
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400633func (mod *Module) RustLibraryInterface() bool {
634 if mod.compiler != nil {
635 if _, ok := mod.compiler.(libraryInterface); ok {
636 return true
637 }
638 }
639 return false
640}
641
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500642func (mod *Module) IsFuzzModule() bool {
643 if _, ok := mod.compiler.(*fuzzDecorator); ok {
644 return true
645 }
646 return false
647}
648
649func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
650 return mod.FuzzModule
651}
652
653func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
654 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
655 return fuzzer.fuzzPackagedModule
656 }
657 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
658}
659
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000660func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500661 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
662 return fuzzer.sharedLibraries
663 }
664 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
665}
666
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400667func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400668 if mod.compiler != nil {
669 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400670 }
671 return nil
672}
673
Ivan Lozano183a3212019-10-18 14:18:45 -0700674func (mod *Module) SetStatic() {
675 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700676 if library, ok := mod.compiler.(libraryInterface); ok {
677 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700678 return
679 }
680 }
681 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
682}
683
684func (mod *Module) SetShared() {
685 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700686 if library, ok := mod.compiler.(libraryInterface); ok {
687 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700688 return
689 }
690 }
691 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
692}
693
Ivan Lozano183a3212019-10-18 14:18:45 -0700694func (mod *Module) BuildStaticVariant() bool {
695 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700696 if library, ok := mod.compiler.(libraryInterface); ok {
697 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700698 }
699 }
700 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
701}
702
703func (mod *Module) BuildSharedVariant() bool {
704 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700705 if library, ok := mod.compiler.(libraryInterface); ok {
706 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700707 }
708 }
709 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
710}
711
Ivan Lozano183a3212019-10-18 14:18:45 -0700712func (mod *Module) Module() android.Module {
713 return mod
714}
715
Ivan Lozano183a3212019-10-18 14:18:45 -0700716func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400717 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700718}
719
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400720func (mod *Module) CoverageFiles() android.Paths {
721 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800722 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400723 }
724 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
725}
726
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400727// Rust does not produce gcno files, and therefore does not produce a coverage archive.
728func (mod *Module) CoverageOutputFile() android.OptionalPath {
729 return android.OptionalPath{}
730}
731
732func (mod *Module) IsNdk(config android.Config) bool {
733 return false
734}
735
736func (mod *Module) IsStubs() bool {
737 return false
738}
739
Jiyong Park459feca2020-12-15 11:02:21 +0900740func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900741 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900742 return false
743 }
744
Jiyong Park459feca2020-12-15 11:02:21 +0900745 // The apex variant is not installable because it is included in the APEX and won't appear
746 // in the system partition as a standalone file.
747 if !apexInfo.IsForPlatform() {
748 return false
749 }
750
Jiyong Parke54f07e2021-04-07 15:08:04 +0900751 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900752}
753
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500754func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800755 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
756 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500757}
758
Ivan Lozano183a3212019-10-18 14:18:45 -0700759var _ cc.LinkableInterface = (*Module)(nil)
760
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761func (mod *Module) Init() android.Module {
762 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500763 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764
Yi Kong46c6e592022-01-20 22:55:00 +0800765 if mod.afdo != nil {
766 mod.AddProperties(mod.afdo.props()...)
767 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768 if mod.compiler != nil {
769 mod.AddProperties(mod.compiler.compilerProps()...)
770 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400771 if mod.coverage != nil {
772 mod.AddProperties(mod.coverage.props()...)
773 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200774 if mod.clippy != nil {
775 mod.AddProperties(mod.clippy.props()...)
776 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400777 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700778 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400779 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500780 if mod.sanitize != nil {
781 mod.AddProperties(mod.sanitize.props()...)
782 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400783
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900785 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786
787 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 return mod
789}
790
791func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
792 return &Module{
793 hod: hod,
794 multilib: multilib,
795 }
796}
797func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
798 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800799 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400800 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200801 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500802 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803 return module
804}
805
806type ModuleContext interface {
807 android.ModuleContext
808 ModuleContextIntf
809}
810
811type BaseModuleContext interface {
812 android.BaseModuleContext
813 ModuleContextIntf
814}
815
816type DepsContext interface {
817 android.BottomUpMutatorContext
818 ModuleContextIntf
819}
820
821type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200822 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824}
825
826type depsContext struct {
827 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828}
829
830type moduleContext struct {
831 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832}
833
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200834type baseModuleContext struct {
835 android.BaseModuleContext
836}
837
838func (ctx *moduleContext) RustModule() *Module {
839 return ctx.Module().(*Module)
840}
841
842func (ctx *moduleContext) toolchain() config.Toolchain {
843 return ctx.RustModule().toolchain(ctx)
844}
845
846func (ctx *depsContext) RustModule() *Module {
847 return ctx.Module().(*Module)
848}
849
850func (ctx *depsContext) toolchain() config.Toolchain {
851 return ctx.RustModule().toolchain(ctx)
852}
853
854func (ctx *baseModuleContext) RustModule() *Module {
855 return ctx.Module().(*Module)
856}
857
858func (ctx *baseModuleContext) toolchain() config.Toolchain {
859 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400860}
861
862func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700863 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
864 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
865 return false
866 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400867 return mod.compiler != nil && mod.compiler.nativeCoverage()
868}
869
Ivan Lozanod7586b62021-04-01 09:49:36 -0400870func (mod *Module) EverInstallable() bool {
871 return mod.compiler != nil &&
872 // Check to see whether the module is actually ever installable.
873 mod.compiler.everInstallable()
874}
875
876func (mod *Module) Installable() *bool {
877 return mod.Properties.Installable
878}
879
Ivan Lozano872d5792022-03-23 17:31:39 -0400880func (mod *Module) ProcMacro() bool {
881 if pm, ok := mod.compiler.(procMacroInterface); ok {
882 return pm.ProcMacro()
883 }
884 return false
885}
886
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
888 if mod.cachedToolchain == nil {
889 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
890 }
891 return mod.cachedToolchain
892}
893
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200894func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
895 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
896}
897
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
899}
900
901func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
902 ctx := &moduleContext{
903 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905
Colin Crossff694a82023-12-13 15:54:49 -0800906 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900907 if !apexInfo.IsForPlatform() {
908 mod.hideApexVariantFromMake = true
909 }
910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500912 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
913
Ivan Lozanof1868af2022-04-12 13:08:36 -0400914 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700915
Ivan Lozanoffee3342019-08-27 12:03:00 -0700916 if !toolchain.Supported() {
917 // This toolchain's unsupported, there's nothing to do for this mod.
918 return
919 }
920
921 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400922 // Export linkDirs for CC rust generatedlibs
923 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
924 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
925
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926 flags := Flags{
927 Toolchain: toolchain,
928 }
929
Ivan Lozano67eada32021-09-23 11:50:33 -0400930 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800931 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500932 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800933 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934 if mod.compiler != nil {
935 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400936 flags = mod.compiler.cfgFlags(ctx, flags)
937 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400938 }
939 if mod.coverage != nil {
940 flags, deps = mod.coverage.flags(ctx, flags, deps)
941 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200942 if mod.clippy != nil {
943 flags, deps = mod.clippy.flags(ctx, flags, deps)
944 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500945 if mod.sanitize != nil {
946 flags, deps = mod.sanitize.flags(ctx, flags, deps)
947 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400948
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200949 // SourceProvider needs to call GenerateSource() before compiler calls
950 // compile() so it can provide the source. A SourceProvider has
951 // multiple variants (e.g. source, rlib, dylib). Only the "source"
952 // variant is responsible for effectively generating the source. The
953 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400954 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200955 if mod.compiler.(libraryInterface).source() {
956 mod.sourceProvider.GenerateSource(ctx, deps)
957 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
958 } else {
959 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
960 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700961 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200962 }
Colin Cross40213022023-12-13 15:19:49 -0800963 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400964 }
965
966 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100967 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700968 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400969 if ctx.Failed() {
970 return
971 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700972 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
973 if buildOutput.kytheFile != nil {
974 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
975 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400976 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900977
Dan Albert06feee92021-03-19 15:06:02 -0700978 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000979 if mod.docTimestampFile.Valid() {
980 ctx.CheckbuildFile(mod.docTimestampFile.Path())
981 }
Dan Albert06feee92021-03-19 15:06:02 -0700982
Colin Crossff694a82023-12-13 15:54:49 -0800983 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400984 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900985 // If the module has been specifically configure to not be installed then
986 // hide from make as otherwise it will break when running inside make as the
987 // output path to install will not be specified. Not all uninstallable
988 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400989 // side dependencies. In particular, proc-macros need to be captured in the
990 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900991 mod.HideFromMake()
992 } else if !mod.installable(apexInfo) {
993 mod.SkipInstall()
994 }
995
996 // Still call install though, the installs will be stored as PackageSpecs to allow
997 // using the outputs in a genrule.
998 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200999 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001000 if ctx.Failed() {
1001 return
1002 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001003 // Export your own directory as a linkDir
1004 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1005
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001006 }
Chris Wailes74be7642021-07-22 16:20:28 -07001007
1008 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001009 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001010 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -08001011 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001012 }
LaMont Jones0c971852023-12-07 21:56:59 +00001013
LaMont Jonesaa005ae2023-12-19 19:01:57 +00001014 android.CollectDependencyAconfigFiles(ctx, &mod.mergedAconfigFiles)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015}
1016
1017func (mod *Module) deps(ctx DepsContext) Deps {
1018 deps := Deps{}
1019
1020 if mod.compiler != nil {
1021 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001022 }
1023 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001024 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 }
1026
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001027 if mod.coverage != nil {
1028 deps = mod.coverage.deps(ctx, deps)
1029 }
1030
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001031 if mod.sanitize != nil {
1032 deps = mod.sanitize.deps(ctx, deps)
1033 }
1034
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1036 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001037 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1039 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1040 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001041 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001042 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 return deps
1044
1045}
1046
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047type dependencyTag struct {
1048 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001049 name string
1050 library bool
1051 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001052 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053}
1054
Jiyong Park65b62242020-11-25 12:44:59 +09001055// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1056// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1057func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001058 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001059}
1060
1061var _ android.InstallNeededDependencyTag = dependencyTag{}
1062
Colin Cross65cb3142021-12-10 23:05:02 +00001063func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1064 if d.library && d.dynamic {
1065 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1066 }
1067 return nil
1068}
1069
Yu Liuc8884602024-03-15 18:48:38 +00001070func (d dependencyTag) PropagateAconfigValidation() bool {
1071 return d == rlibDepTag || d == sourceDepTag
1072}
1073
1074var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1075
Colin Cross65cb3142021-12-10 23:05:02 +00001076var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1077
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001079 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1080 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001081 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001082 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001083 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001084 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001085 dataLibDepTag = dependencyTag{name: "data lib"}
1086 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087)
1088
Jiyong Park99644e92020-11-17 22:21:02 +09001089func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1090 tag, ok := depTag.(dependencyTag)
1091 return ok && tag == dylibDepTag
1092}
1093
Jiyong Park94e22fd2021-04-08 18:19:15 +09001094func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1095 tag, ok := depTag.(dependencyTag)
1096 return ok && tag == rlibDepTag
1097}
1098
Matthew Maurer0f003b12020-06-29 14:34:06 -07001099type autoDep struct {
1100 variation string
1101 depTag dependencyTag
1102}
1103
1104var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001105 rlibVariation = "rlib"
1106 dylibVariation = "dylib"
1107 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1108 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001109)
1110
1111type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001112 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001113}
1114
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001115func (mod *Module) begin(ctx BaseModuleContext) {
1116 if mod.coverage != nil {
1117 mod.coverage.begin(ctx)
1118 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001119 if mod.sanitize != nil {
1120 mod.sanitize.begin(ctx)
1121 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001122}
1123
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001124func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001125 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001126 return p.prebuilt()
1127 }
1128 return nil
1129}
1130
Kiyoung Kim37693d02024-04-04 09:56:15 +09001131func (mod *Module) Symlinks() []string {
1132 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1133 return nil
1134}
1135
Justin Yun24b246a2023-03-16 10:36:16 +09001136func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1137 if rustDep, ok := dep.(*Module); ok {
1138 // Use base module name for snapshots when exporting to Makefile.
1139 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1140 baseName := rustDep.BaseModuleName()
1141 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1142 }
1143 }
1144 return cc.MakeLibName(ctx, c, dep, depName)
1145}
1146
Ivan Lozanod106efe2023-09-21 23:30:26 -04001147func collectIncludedProtos(mod *Module, dep *Module) {
1148 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1149 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1150 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1151 }
1152 }
1153}
Andrew Walbran52533232024-03-19 11:36:04 +00001154
Ivan Lozanoffee3342019-08-27 12:03:00 -07001155func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1156 var depPaths PathDeps
1157
1158 directRlibDeps := []*Module{}
1159 directDylibDeps := []*Module{}
1160 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001161 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001162 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001163 directSrcProvidersDeps := []*Module{}
1164 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001165
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001166 // For the dependency from platform to apex, use the latest stubs
1167 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001168 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001169 if !apexInfo.IsForPlatform() {
1170 mod.apexSdkVersion = apexInfo.MinSdkVersion
1171 }
1172
1173 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1174 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1175 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1176 // (b/144430859)
1177 mod.apexSdkVersion = android.FutureApiLevel
1178 }
1179
Spandan Das604f3762023-03-16 22:51:40 +00001180 skipModuleList := map[string]bool{}
1181
1182 var apiImportInfo multitree.ApiImportInfo
1183 hasApiImportInfo := false
1184
1185 ctx.VisitDirectDeps(func(dep android.Module) {
1186 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001187 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001188 hasApiImportInfo = true
1189 }
1190 })
1191
1192 if hasApiImportInfo {
1193 targetStubModuleList := map[string]string{}
1194 targetOrigModuleList := map[string]string{}
1195
1196 // Search for dependency which both original module and API imported library with APEX stub exists
1197 ctx.VisitDirectDeps(func(dep android.Module) {
1198 depName := ctx.OtherModuleName(dep)
1199 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1200 targetStubModuleList[apiLibrary] = depName
1201 }
1202 })
1203 ctx.VisitDirectDeps(func(dep android.Module) {
1204 depName := ctx.OtherModuleName(dep)
1205 if origLibrary, ok := targetStubModuleList[depName]; ok {
1206 targetOrigModuleList[origLibrary] = depName
1207 }
1208 })
1209
1210 // Decide which library should be used between original and API imported library
1211 ctx.VisitDirectDeps(func(dep android.Module) {
1212 depName := ctx.OtherModuleName(dep)
1213 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1214 if cc.ShouldUseStubForApex(ctx, dep) {
1215 skipModuleList[depName] = true
1216 } else {
1217 skipModuleList[apiLibrary] = true
1218 }
1219 }
1220 })
1221 }
1222
Cole Faustb6e6f992023-08-17 17:42:26 -07001223 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1224 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001225
Ivan Lozanoffee3342019-08-27 12:03:00 -07001226 ctx.VisitDirectDeps(func(dep android.Module) {
1227 depName := ctx.OtherModuleName(dep)
1228 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001229
Spandan Das604f3762023-03-16 22:51:40 +00001230 if _, exists := skipModuleList[depName]; exists {
1231 return
1232 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001233
1234 if depTag == android.DarwinUniversalVariantTag {
1235 return
1236 }
1237
Ivan Lozano0a468a42024-05-13 21:03:34 -04001238 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001239 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001240 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001241
Ivan Lozanoffee3342019-08-27 12:03:00 -07001242 switch depTag {
1243 case dylibDepTag:
1244 dylib, ok := rustDep.compiler.(libraryInterface)
1245 if !ok || !dylib.dylib() {
1246 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1247 return
1248 }
1249 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001250 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001251 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1252
Ivan Lozanoffee3342019-08-27 12:03:00 -07001253 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001254
Ivan Lozanoffee3342019-08-27 12:03:00 -07001255 rlib, ok := rustDep.compiler.(libraryInterface)
1256 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001257 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001258 return
1259 }
1260 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001261 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001262 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1263
Ivan Lozano0a468a42024-05-13 21:03:34 -04001264 // rust_ffi rlibs may export include dirs, so collect those here.
1265 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1266 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1267 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1268
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269 case procMacroDepTag:
1270 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001271 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001272 // proc_macro link dirs need to be exported, so collect those here.
1273 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001274
1275 case sourceDepTag:
1276 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1277 collectIncludedProtos(mod, rustDep)
1278 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001279 }
1280
Cole Faustb6e6f992023-08-17 17:42:26 -07001281 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1282
Paul Duffind5cf92e2021-07-09 17:38:55 +01001283 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001284 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1285 // OS/Arch variant is used.
1286 var helper string
1287 if ctx.Host() {
1288 helper = "missing 'host_supported'?"
1289 } else {
1290 helper = "device module defined?"
1291 }
1292
1293 if dep.Target().Os != ctx.Os() {
1294 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1295 return
1296 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1297 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1298 return
1299 }
1300 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001301 }
1302
Ivan Lozano0a468a42024-05-13 21:03:34 -04001303 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001304 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001305 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001306 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1307 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001308 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001309 }
1310
Ivan Lozanoffee3342019-08-27 12:03:00 -07001311 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001312 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001313 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001314 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001315 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001316 }
1317 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001318
Ivan Lozanod106efe2023-09-21 23:30:26 -04001319 if depTag == sourceDepTag {
1320 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1321 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001322 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001323 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1324 }
1325 }
1326 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001327 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001328 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001329 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001330 if _, ok := ccDep.(*Module); !ok {
1331 if ccDep.Module().Target().Os != ctx.Os() {
1332 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1333 return
1334 }
1335 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1336 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1337 return
1338 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001339 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001340 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001341 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001342 if !ctx.Config().AllowMissingDependencies() {
1343 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1344 } else {
1345 ctx.AddMissingDependencies([]string{depName})
1346 }
1347 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001348 }
1349
Wen-yi Chu41326c12023-09-22 03:58:59 +00001350 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001351
Ivan Lozanoffee3342019-08-27 12:03:00 -07001352 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001353 switch {
1354 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001355 if cc.IsWholeStaticLib(depTag) {
1356 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1357 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001358 if mod.Binary() {
1359 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1360 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1361 // final linkage, pass the args directly to the linker to handle these cases.
1362 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1363 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001364 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001365 } else {
1366 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 -05001367 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001368 }
1369
1370 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1371 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001372 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001373 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1374
Colin Cross313aa542023-12-13 13:47:44 -08001375 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
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...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001380 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001381
1382 // Record baseLibName for snapshots.
1383 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1384
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001385 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001386 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001387 // For the shared lib dependencies, we may link to the stub variant
1388 // of the dependency depending on the context (e.g. if this
1389 // dependency crosses the APEX boundaries).
1390 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1391
1392 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1393 // object (either from stub or non-stub) to use.
1394 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001395 if !linkObject.Valid() {
1396 if !ctx.Config().AllowMissingDependencies() {
1397 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1398 } else {
1399 ctx.AddMissingDependencies([]string{depName})
1400 }
1401 return
1402 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001403 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001404
Ivan Lozanoffee3342019-08-27 12:03:00 -07001405 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001406 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001407 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1408 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1409 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1410 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001411 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001412
1413 // Record baseLibName for snapshots.
1414 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1415
Cole Faustb6e6f992023-08-17 17:42:26 -07001416 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001417 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001418 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001419 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001420 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1421 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1422 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001423 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001424 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001425 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001426 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001427 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001428 }
1429
1430 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001431 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1432 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001433 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001434 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001435 } else {
1436 switch {
1437 case depTag == cc.CrtBeginDepTag:
1438 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1439 case depTag == cc.CrtEndDepTag:
1440 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1441 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001442 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001443
1444 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001445 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001446 // These are usually genrules which don't have per-target variants.
1447 directSrcDeps = append(directSrcDeps, srcDep)
1448 }
1449 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001450 })
1451
Wen-yi Chu41326c12023-09-22 03:58:59 +00001452 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1453
1454 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001455 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001456 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001457 crateName := dep.CrateName()
1458 if alias, aliased := aliases[crateName]; aliased {
1459 crateName = alias
1460 }
1461 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001462 }
1463 var dylibDepFiles RustLibraries
1464 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001465 crateName := dep.CrateName()
1466 if alias, aliased := aliases[crateName]; aliased {
1467 crateName = alias
1468 }
1469 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001470 }
1471 var procMacroDepFiles RustLibraries
1472 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001473 crateName := dep.CrateName()
1474 if alias, aliased := aliases[crateName]; aliased {
1475 crateName = alias
1476 }
1477 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001478 }
1479
Colin Cross004bd3f2023-10-02 11:39:17 -07001480 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001481 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001482 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483 }
1484
Colin Cross004bd3f2023-10-02 11:39:17 -07001485 var sharedLibFiles android.Paths
1486 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001487 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001488 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001489 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001490 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001491 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001492 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001493 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001494 }
1495
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001496 var srcProviderDepFiles android.Paths
1497 for _, dep := range directSrcProvidersDeps {
1498 srcs, _ := dep.OutputFiles("")
1499 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1500 }
1501 for _, dep := range directSrcDeps {
1502 srcs := dep.Srcs()
1503 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1504 }
1505
Wen-yi Chu41326c12023-09-22 03:58:59 +00001506 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1507 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001508 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1509 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1510 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001511 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001512 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001513
1514 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001515 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001516 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001517 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001518 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1519 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1520 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001521
1522 return depPaths
1523}
1524
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001525func (mod *Module) InstallInData() bool {
1526 if mod.compiler == nil {
1527 return false
1528 }
1529 return mod.compiler.inData()
1530}
1531
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001532func (mod *Module) InstallInRamdisk() bool {
1533 return mod.InRamdisk()
1534}
1535
1536func (mod *Module) InstallInVendorRamdisk() bool {
1537 return mod.InVendorRamdisk()
1538}
1539
1540func (mod *Module) InstallInRecovery() bool {
1541 return mod.InRecovery()
1542}
1543
Wen-yi Chu41326c12023-09-22 03:58:59 +00001544func linkPathFromFilePath(filepath android.Path) string {
1545 return strings.Split(filepath.String(), filepath.Base())[0]
1546}
1547
Spandan Das604f3762023-03-16 22:51:40 +00001548// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1549func (r *Module) usePublicApi() bool {
1550 return r.Device() && r.UseSdk()
1551}
1552
1553// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1554func (r *Module) useVendorApi() bool {
1555 return r.Device() && (r.InVendor() || r.InProduct())
1556}
1557
Ivan Lozanoffee3342019-08-27 12:03:00 -07001558func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1559 ctx := &depsContext{
1560 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001561 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001562
1563 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001564 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001565
Kiyoung Kim487689e2022-07-26 09:48:22 +09001566 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001567 if mod.usePublicApi() || mod.useVendorApi() {
1568 for idx, lib := range deps.SharedLibs {
1569 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1570 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001571 }
1572
Ivan Lozano1921e802021-05-20 13:39:16 -04001573 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001574 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001575 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001576
Ivan Lozano2b081132020-09-08 12:46:52 -04001577 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001578 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001579 stdLinkage = "rlib-std"
1580 }
1581
1582 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001583 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001584
Ivan Lozano2b081132020-09-08 12:46:52 -04001585 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1586 rlibDepVariations = append(rlibDepVariations,
1587 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1588 }
1589
Ivan Lozano1921e802021-05-20 13:39:16 -04001590 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001591 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001592 for _, lib := range deps.Rlibs {
1593 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001594 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001595 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001596
1597 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001598 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001599 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1600
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001601 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001602 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001603 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001604
Ivan Lozano1921e802021-05-20 13:39:16 -04001605 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001606 if deps.Rustlibs != nil {
1607 if !mod.compiler.Disabled() {
1608 for _, lib := range deps.Rustlibs {
1609 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1610 if autoDep.depTag == rlibDepTag {
1611 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001612 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1613
Ivan Lozanod106efe2023-09-21 23:30:26 -04001614 } else {
1615 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1616 // Check for the existence of the dylib deptag variant. Select it if available,
1617 // otherwise select the rlib variant.
1618 autoDepVariations := append(commonDepVariations,
1619 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001620 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001621 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1622 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001623
Ivan Lozanod106efe2023-09-21 23:30:26 -04001624 } else {
1625 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001626 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1627
Ivan Lozanod106efe2023-09-21 23:30:26 -04001628 }
1629 }
1630 }
1631 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1632 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001633 srcProviderVariations := append(commonDepVariations,
1634 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001635 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001636
Ivan Lozano0a468a42024-05-13 21:03:34 -04001637 // Only add rustlib dependencies if they're source providers themselves.
1638 // This is used to track which crate names need to be added to the source generated
1639 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001640 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001641 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001642 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001643 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001644 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001645 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001646
Ivan Lozano1921e802021-05-20 13:39:16 -04001647 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001648 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001649 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001650 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001651 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001652 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001653 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001654 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001655 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001656 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1657
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001658 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001659 }
1660 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001661
1662 for _, lib := range deps.SharedLibs {
1663 depTag := cc.SharedDepTag()
1664 name, version := cc.StubsLibNameAndVersion(lib)
1665
1666 variations := []blueprint.Variation{
1667 {Mutator: "link", Variation: "shared"},
1668 }
Spandan Das604f3762023-03-16 22:51:40 +00001669 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1670 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1671 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1672 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1673 }
1674
1675 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1676 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1677 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001678 }
1679
1680 for _, lib := range deps.WholeStaticLibs {
1681 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001682
1683 actx.AddVariationDependencies([]blueprint.Variation{
1684 {Mutator: "link", Variation: "static"},
1685 }, depTag, lib)
1686 }
1687
1688 for _, lib := range deps.StaticLibs {
1689 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001690
1691 actx.AddVariationDependencies([]blueprint.Variation{
1692 {Mutator: "link", Variation: "static"},
1693 }, depTag, lib)
1694 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001695
Zach Johnson3df4e632020-11-06 11:56:27 -08001696 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1697
Colin Cross565cafd2020-09-25 18:47:38 -07001698 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001699 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001700 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001701 }
Colin Crossfe605e12022-01-23 20:46:16 -08001702 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001703 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001704 }
1705
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001706 if mod.sourceProvider != nil {
1707 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1708 bindgen.Properties.Custom_bindgen != "" {
1709 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1710 bindgen.Properties.Custom_bindgen)
1711 }
1712 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001713
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001714 actx.AddVariationDependencies([]blueprint.Variation{
1715 {Mutator: "link", Variation: "shared"},
1716 }, dataLibDepTag, deps.DataLibs...)
1717
1718 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1719
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001720 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001721 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001722
1723 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001724}
1725
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001726func BeginMutator(ctx android.BottomUpMutatorContext) {
1727 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1728 mod.beginMutator(ctx)
1729 }
1730}
1731
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001732func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1733 ctx := &baseModuleContext{
1734 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001735 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001736
1737 mod.begin(ctx)
1738}
1739
Ivan Lozanoffee3342019-08-27 12:03:00 -07001740func (mod *Module) Name() string {
1741 name := mod.ModuleBase.Name()
1742 if p, ok := mod.compiler.(interface {
1743 Name(string) string
1744 }); ok {
1745 name = p.Name(name)
1746 }
1747 return name
1748}
1749
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001750func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001751 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001752 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001753 }
1754}
1755
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001756var _ android.HostToolProvider = (*Module)(nil)
1757
1758func (mod *Module) HostToolPath() android.OptionalPath {
1759 if !mod.Host() {
1760 return android.OptionalPath{}
1761 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001762 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1763 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001764 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1765 // Even though proc-macros aren't strictly "tools", since they target the compiler
1766 // and act as compiler plugins, we treat them similarly.
1767 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001768 }
1769 return android.OptionalPath{}
1770}
1771
Jiyong Park99644e92020-11-17 22:21:02 +09001772var _ android.ApexModule = (*Module)(nil)
1773
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001774func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001775 return String(mod.Properties.Min_sdk_version)
1776}
1777
Jiyong Park45bf82e2020-12-15 22:29:02 +09001778// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001779func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001780 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001781 if minSdkVersion == "apex_inherit" {
1782 return nil
1783 }
1784 if minSdkVersion == "" {
1785 return fmt.Errorf("min_sdk_version is not specificed")
1786 }
1787
1788 // Not using nativeApiLevelFromUser because the context here is not
1789 // necessarily a native context.
1790 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1791 if err != nil {
1792 return err
1793 }
1794
1795 if ver.GreaterThan(sdkVersion) {
1796 return fmt.Errorf("newer SDK(%v)", ver)
1797 }
Jiyong Park99644e92020-11-17 22:21:02 +09001798 return nil
1799}
1800
Jiyong Park45bf82e2020-12-15 22:29:02 +09001801// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001802func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1803 depTag := ctx.OtherModuleDependencyTag(dep)
1804
1805 if ccm, ok := dep.(*cc.Module); ok {
1806 if ccm.HasStubsVariants() {
1807 if cc.IsSharedDepTag(depTag) {
1808 // dynamic dep to a stubs lib crosses APEX boundary
1809 return false
1810 }
1811 if cc.IsRuntimeDepTag(depTag) {
1812 // runtime dep to a stubs lib also crosses APEX boundary
1813 return false
1814 }
1815
1816 if cc.IsHeaderDepTag(depTag) {
1817 return false
1818 }
1819 }
1820 if mod.Static() && cc.IsSharedDepTag(depTag) {
1821 // shared_lib dependency from a static lib is considered as crossing
1822 // the APEX boundary because the dependency doesn't actually is
1823 // linked; the dependency is used only during the compilation phase.
1824 return false
1825 }
1826 }
1827
Matthew Maurer581b6d82022-09-29 16:46:25 -07001828 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001829 return false
1830 }
1831
1832 return true
1833}
1834
1835// Overrides ApexModule.IsInstallabeToApex()
1836func (mod *Module) IsInstallableToApex() bool {
1837 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001838 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001839 return true
1840 }
1841 if _, ok := mod.compiler.(*binaryDecorator); ok {
1842 return true
1843 }
1844 }
1845 return false
1846}
1847
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001848// If a library file has a "lib" prefix, extract the library name without the prefix.
1849func libNameFromFilePath(filepath android.Path) (string, bool) {
1850 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1851 if strings.HasPrefix(libName, "lib") {
1852 libName = libName[3:]
1853 return libName, true
1854 }
1855 return "", false
1856}
1857
Sasha Smundaka76acba2022-04-18 20:12:56 -07001858func kytheExtractRustFactory() android.Singleton {
1859 return &kytheExtractRustSingleton{}
1860}
1861
1862type kytheExtractRustSingleton struct {
1863}
1864
1865func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1866 var xrefTargets android.Paths
1867 ctx.VisitAllModules(func(module android.Module) {
1868 if rustModule, ok := module.(xref); ok {
1869 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1870 }
1871 })
1872 if len(xrefTargets) > 0 {
1873 ctx.Phony("xref_rust", xrefTargets...)
1874 }
1875}
1876
Jihoon Kangf78a8902022-09-01 22:47:07 +00001877func (c *Module) Partition() string {
1878 return ""
1879}
1880
Ivan Lozanoffee3342019-08-27 12:03:00 -07001881var Bool = proptools.Bool
1882var BoolDefault = proptools.BoolDefault
1883var String = proptools.String
1884var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001885
1886var _ android.OutputFileProducer = (*Module)(nil)