blob: 7cd9df4c7132b948a7221692b96740fb5e14388c [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]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177}
178
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179func (mod *Module) Header() bool {
180 //TODO: If Rust libraries provide header variants, this needs to be updated.
181 return false
182}
183
184func (mod *Module) SetPreventInstall() {
185 mod.Properties.PreventInstall = true
186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SetHideFromMake() {
189 mod.Properties.HideFromMake = true
190}
191
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900192func (mod *Module) HiddenFromMake() bool {
193 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400194}
195
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500196func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500197 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
198 // nil since we need compiler to actually sanitize.
199 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500200}
201
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (mod *Module) IsPrebuilt() bool {
203 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
204 return true
205 }
206 return false
207}
208
Ivan Lozano43845682020-07-09 21:03:28 -0400209func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
210 switch tag {
211 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700212 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400213 return mod.sourceProvider.Srcs(), nil
214 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900215 if mod.OutputFile().Valid() {
216 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400217 }
218 return android.Paths{}, nil
219 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500220 case "unstripped":
221 if mod.compiler != nil {
222 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
223 }
224 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400225 default:
226 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
227 }
228}
229
Ivan Lozano52767be2019-10-18 14:49:46 -0700230func (mod *Module) SelectedStl() string {
231 return ""
232}
233
Ivan Lozano2b262972019-11-21 12:30:50 -0800234func (mod *Module) NonCcVariants() bool {
235 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400236 if library, ok := mod.compiler.(libraryInterface); ok {
237 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800238 }
239 }
240 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
241}
242
Ivan Lozano52767be2019-10-18 14:49:46 -0700243func (mod *Module) Static() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
246 return library.static()
247 }
248 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700250}
251
252func (mod *Module) Shared() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400255 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700256 }
257 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400258 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700259}
260
Ivan Lozanod7586b62021-04-01 09:49:36 -0400261func (mod *Module) Dylib() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok {
264 return library.dylib()
265 }
266 }
267 return false
268}
269
Ivan Lozanod106efe2023-09-21 23:30:26 -0400270func (mod *Module) Source() bool {
271 if mod.compiler != nil {
272 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
273 return library.source()
274 }
275 }
276 return false
277}
278
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400279func (mod *Module) RlibStd() bool {
280 if mod.compiler != nil {
281 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
282 return library.rlibStd()
283 }
284 }
285 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
286}
287
Ivan Lozanod7586b62021-04-01 09:49:36 -0400288func (mod *Module) Rlib() bool {
289 if mod.compiler != nil {
290 if library, ok := mod.compiler.(libraryInterface); ok {
291 return library.rlib()
292 }
293 }
294 return false
295}
296
297func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400298 if binary, ok := mod.compiler.(binaryInterface); ok {
299 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400300 }
301 return false
302}
303
Justin Yun5e035862021-06-29 20:50:37 +0900304func (mod *Module) StaticExecutable() bool {
305 if !mod.Binary() {
306 return false
307 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400308 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900309}
310
Ivan Lozanod7586b62021-04-01 09:49:36 -0400311func (mod *Module) Object() bool {
312 // Rust has no modules which produce only object files.
313 return false
314}
315
Ivan Lozano52767be2019-10-18 14:49:46 -0700316func (mod *Module) Toc() android.OptionalPath {
317 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400318 if lib, ok := mod.compiler.(libraryInterface); ok {
319 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700320 }
321 }
322 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
323}
324
Colin Crossc511bc52020-04-07 16:50:32 +0000325func (mod *Module) UseSdk() bool {
326 return false
327}
328
Ivan Lozanod7586b62021-04-01 09:49:36 -0400329func (mod *Module) RelativeInstallPath() string {
330 if mod.compiler != nil {
331 return mod.compiler.relativeInstallPath()
332 }
333 return ""
334}
335
Ivan Lozano52767be2019-10-18 14:49:46 -0700336func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500337 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700338}
339
Jiyong Park7d55b612021-06-11 17:22:09 +0900340func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400341 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900342}
343
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400344func (mod *Module) SubName() string {
345 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700346}
347
Ivan Lozanof1868af2022-04-12 13:08:36 -0400348func (mod *Module) IsVndkPrebuiltLibrary() bool {
349 // Rust modules do not provide VNDK prebuilts
350 return false
351}
352
353func (mod *Module) IsVendorPublicLibrary() bool {
354 return mod.VendorProperties.IsVendorPublicLibrary
355}
356
357func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
358 // Rust modules to not provide Sdk variants
359 return false
360}
361
Colin Cross127bb8b2020-12-16 16:46:01 -0800362func (c *Module) IsVndkPrivate() bool {
363 return false
364}
365
366func (c *Module) IsLlndk() bool {
367 return false
368}
369
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400370func (mod *Module) KernelHeadersDecorator() bool {
371 return false
372}
373
Colin Cross1f3f1302021-04-26 18:37:44 -0700374func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400375 return false
376}
377
Colin Cross5271fea2021-04-27 13:06:04 -0700378func (m *Module) NeedsVendorPublicLibraryVariants() bool {
379 return false
380}
381
Ivan Lozanod7586b62021-04-01 09:49:36 -0400382func (mod *Module) HasLlndkStubs() bool {
383 return false
384}
385
386func (mod *Module) StubsVersion() string {
387 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
388}
389
Ivan Lozano52767be2019-10-18 14:49:46 -0700390func (mod *Module) SdkVersion() string {
391 return ""
392}
393
Colin Crossc511bc52020-04-07 16:50:32 +0000394func (mod *Module) AlwaysSdk() bool {
395 return false
396}
397
Jiyong Park2286afd2020-06-16 21:58:53 +0900398func (mod *Module) IsSdkVariant() bool {
399 return false
400}
401
Colin Cross1348ce32020-10-01 13:37:16 -0700402func (mod *Module) SplitPerApiLevel() bool {
403 return false
404}
405
Sasha Smundaka76acba2022-04-18 20:12:56 -0700406func (mod *Module) XrefRustFiles() android.Paths {
407 return mod.kytheFiles
408}
409
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400411 Dylibs []string
412 Rlibs []string
413 Rustlibs []string
414 Stdlibs []string
415 ProcMacros []string
416 SharedLibs []string
417 StaticLibs []string
418 WholeStaticLibs []string
419 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400421 // Used for data dependencies adjacent to tests
422 DataLibs []string
423 DataBins []string
424
Colin Crossfe605e12022-01-23 20:46:16 -0800425 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700426}
427
428type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700429 DyLibs RustLibraries
430 RLibs RustLibraries
431 SharedLibs android.Paths
432 SharedLibDeps android.Paths
433 StaticLibs android.Paths
434 ProcMacros RustLibraries
435 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500436
437 // depFlags and depLinkFlags are rustc and linker (clang) flags.
438 depFlags []string
439 depLinkFlags []string
440
441 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
442 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000443 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700444 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700445
Ivan Lozano0a468a42024-05-13 21:03:34 -0400446 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
447 // cc_library_static dependants of rlibs.
448 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
449 exportedLinkDirs []string
450
Ivan Lozano45901ed2020-07-24 16:05:01 -0400451 // Used by bindgen modules which call clang
452 depClangFlags []string
453 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400454 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400455 depSystemIncludePaths android.Paths
456
Colin Crossfe605e12022-01-23 20:46:16 -0800457 CrtBegin android.Paths
458 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700459
460 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500461 SrcDeps android.Paths
462 srcProviderFiles android.Paths
Ivan Lozano0a468a42024-05-13 21:03:34 -0400463
464 // Used by Generated Libraries
465 depExportedRlibs []cc.RustRlibDep
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466}
467
468type RustLibraries []RustLibrary
469
470type RustLibrary struct {
471 Path android.Path
472 CrateName string
473}
474
Matthew Maurerbb3add12020-06-25 09:34:12 -0700475type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000476 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700477 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700478}
479
Sasha Smundaka76acba2022-04-18 20:12:56 -0700480type xref interface {
481 XrefRustFiles() android.Paths
482}
483
Matthew Maurerbb3add12020-06-25 09:34:12 -0700484type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000485 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700486 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700487}
488
Wen-yi Chu41326c12023-09-22 03:58:59 +0000489func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
490 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700491}
492
Colin Cross004bd3f2023-10-02 11:39:17 -0700493func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
494 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400495}
496
Colin Cross0de8a1e2020-09-18 14:15:30 -0700497func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800498 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700499 LinkDirs: flagExporter.linkDirs,
500 LinkObjects: flagExporter.linkObjects,
501 })
502}
503
Matthew Maurerbb3add12020-06-25 09:34:12 -0700504var _ exportedFlagsProducer = (*flagExporter)(nil)
505
506func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700507 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700508}
509
Colin Cross0de8a1e2020-09-18 14:15:30 -0700510type FlagExporterInfo struct {
511 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000512 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700513 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700514}
515
Colin Crossbc7d76c2023-12-12 16:39:03 -0800516var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700517
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400518func (mod *Module) isCoverageVariant() bool {
519 return mod.coverage.Properties.IsCoverageVariant
520}
521
522var _ cc.Coverage = (*Module)(nil)
523
Colin Crossf5f4ad32024-01-19 15:41:48 -0800524func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400525 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
526}
527
Ivan Lozanod7586b62021-04-01 09:49:36 -0400528func (mod *Module) VndkVersion() string {
529 return mod.Properties.VndkVersion
530}
531
Ivan Lozano0a468a42024-05-13 21:03:34 -0400532func (mod *Module) ExportedCrateLinkDirs() []string {
533 return mod.exportedLinkDirs
534}
535
Ivan Lozanod7586b62021-04-01 09:49:36 -0400536func (mod *Module) PreventInstall() bool {
537 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400538}
539
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540func (mod *Module) MarkAsCoverageVariant(coverage bool) {
541 mod.coverage.Properties.IsCoverageVariant = coverage
542}
543
544func (mod *Module) EnableCoverageIfNeeded() {
545 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546}
547
548func defaultsFactory() android.Module {
549 return DefaultsFactory()
550}
551
552type Defaults struct {
553 android.ModuleBase
554 android.DefaultsModuleBase
555}
556
557func DefaultsFactory(props ...interface{}) android.Module {
558 module := &Defaults{}
559
560 module.AddProperties(props...)
561 module.AddProperties(
562 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800563 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500564 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100565 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400566 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567 &BaseCompilerProperties{},
568 &BinaryCompilerProperties{},
569 &LibraryCompilerProperties{},
570 &ProcMacroCompilerProperties{},
571 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400572 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700573 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400574 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400575 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200576 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500577 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000578 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579 )
580
581 android.InitDefaultsModule(module)
582 return module
583}
584
585func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700586 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587}
588
Ivan Lozano183a3212019-10-18 14:18:45 -0700589func (mod *Module) CcLibrary() bool {
590 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500591 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700592 return true
593 }
594 }
595 return false
596}
597
598func (mod *Module) CcLibraryInterface() bool {
599 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400600 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
601 // VariantIs{Static,Shared} is set.
602 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700603 return true
604 }
605 }
606 return false
607}
608
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400609func (mod *Module) RustLibraryInterface() bool {
610 if mod.compiler != nil {
611 if _, ok := mod.compiler.(libraryInterface); ok {
612 return true
613 }
614 }
615 return false
616}
617
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500618func (mod *Module) IsFuzzModule() bool {
619 if _, ok := mod.compiler.(*fuzzDecorator); ok {
620 return true
621 }
622 return false
623}
624
625func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
626 return mod.FuzzModule
627}
628
629func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
630 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
631 return fuzzer.fuzzPackagedModule
632 }
633 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
634}
635
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000636func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500637 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
638 return fuzzer.sharedLibraries
639 }
640 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
641}
642
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400643func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400644 if mod.compiler != nil {
645 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400646 }
647 return nil
648}
649
Ivan Lozano183a3212019-10-18 14:18:45 -0700650func (mod *Module) SetStatic() {
651 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700652 if library, ok := mod.compiler.(libraryInterface); ok {
653 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700654 return
655 }
656 }
657 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
658}
659
660func (mod *Module) SetShared() {
661 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700662 if library, ok := mod.compiler.(libraryInterface); ok {
663 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700664 return
665 }
666 }
667 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
668}
669
Ivan Lozano183a3212019-10-18 14:18:45 -0700670func (mod *Module) BuildStaticVariant() bool {
671 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700672 if library, ok := mod.compiler.(libraryInterface); ok {
673 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700674 }
675 }
676 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
677}
678
679func (mod *Module) BuildSharedVariant() bool {
680 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700681 if library, ok := mod.compiler.(libraryInterface); ok {
682 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700683 }
684 }
685 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
686}
687
Ivan Lozano183a3212019-10-18 14:18:45 -0700688func (mod *Module) Module() android.Module {
689 return mod
690}
691
Ivan Lozano183a3212019-10-18 14:18:45 -0700692func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400693 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700694}
695
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400696func (mod *Module) CoverageFiles() android.Paths {
697 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800698 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400699 }
700 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
701}
702
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400703// Rust does not produce gcno files, and therefore does not produce a coverage archive.
704func (mod *Module) CoverageOutputFile() android.OptionalPath {
705 return android.OptionalPath{}
706}
707
708func (mod *Module) IsNdk(config android.Config) bool {
709 return false
710}
711
712func (mod *Module) IsStubs() bool {
713 return false
714}
715
Jiyong Park459feca2020-12-15 11:02:21 +0900716func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900717 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900718 return false
719 }
720
Jiyong Park459feca2020-12-15 11:02:21 +0900721 // The apex variant is not installable because it is included in the APEX and won't appear
722 // in the system partition as a standalone file.
723 if !apexInfo.IsForPlatform() {
724 return false
725 }
726
Jiyong Parke54f07e2021-04-07 15:08:04 +0900727 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900728}
729
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500730func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800731 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
732 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500733}
734
Ivan Lozano183a3212019-10-18 14:18:45 -0700735var _ cc.LinkableInterface = (*Module)(nil)
736
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737func (mod *Module) Init() android.Module {
738 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500739 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740
Yi Kong46c6e592022-01-20 22:55:00 +0800741 if mod.afdo != nil {
742 mod.AddProperties(mod.afdo.props()...)
743 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744 if mod.compiler != nil {
745 mod.AddProperties(mod.compiler.compilerProps()...)
746 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400747 if mod.coverage != nil {
748 mod.AddProperties(mod.coverage.props()...)
749 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200750 if mod.clippy != nil {
751 mod.AddProperties(mod.clippy.props()...)
752 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400753 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700754 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400755 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500756 if mod.sanitize != nil {
757 mod.AddProperties(mod.sanitize.props()...)
758 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400759
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900761 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762
763 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764 return mod
765}
766
767func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
768 return &Module{
769 hod: hod,
770 multilib: multilib,
771 }
772}
773func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
774 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800775 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400776 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200777 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500778 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779 return module
780}
781
782type ModuleContext interface {
783 android.ModuleContext
784 ModuleContextIntf
785}
786
787type BaseModuleContext interface {
788 android.BaseModuleContext
789 ModuleContextIntf
790}
791
792type DepsContext interface {
793 android.BottomUpMutatorContext
794 ModuleContextIntf
795}
796
797type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200798 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800}
801
802type depsContext struct {
803 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804}
805
806type moduleContext struct {
807 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808}
809
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200810type baseModuleContext struct {
811 android.BaseModuleContext
812}
813
814func (ctx *moduleContext) RustModule() *Module {
815 return ctx.Module().(*Module)
816}
817
818func (ctx *moduleContext) toolchain() config.Toolchain {
819 return ctx.RustModule().toolchain(ctx)
820}
821
822func (ctx *depsContext) RustModule() *Module {
823 return ctx.Module().(*Module)
824}
825
826func (ctx *depsContext) toolchain() config.Toolchain {
827 return ctx.RustModule().toolchain(ctx)
828}
829
830func (ctx *baseModuleContext) RustModule() *Module {
831 return ctx.Module().(*Module)
832}
833
834func (ctx *baseModuleContext) toolchain() config.Toolchain {
835 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400836}
837
838func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700839 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
840 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
841 return false
842 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400843 return mod.compiler != nil && mod.compiler.nativeCoverage()
844}
845
Ivan Lozanod7586b62021-04-01 09:49:36 -0400846func (mod *Module) EverInstallable() bool {
847 return mod.compiler != nil &&
848 // Check to see whether the module is actually ever installable.
849 mod.compiler.everInstallable()
850}
851
852func (mod *Module) Installable() *bool {
853 return mod.Properties.Installable
854}
855
Ivan Lozano872d5792022-03-23 17:31:39 -0400856func (mod *Module) ProcMacro() bool {
857 if pm, ok := mod.compiler.(procMacroInterface); ok {
858 return pm.ProcMacro()
859 }
860 return false
861}
862
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
864 if mod.cachedToolchain == nil {
865 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
866 }
867 return mod.cachedToolchain
868}
869
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200870func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
871 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
872}
873
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
875}
876
877func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
878 ctx := &moduleContext{
879 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881
Colin Crossff694a82023-12-13 15:54:49 -0800882 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900883 if !apexInfo.IsForPlatform() {
884 mod.hideApexVariantFromMake = true
885 }
886
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500888 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
889
Ivan Lozanof1868af2022-04-12 13:08:36 -0400890 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700891
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 if !toolchain.Supported() {
893 // This toolchain's unsupported, there's nothing to do for this mod.
894 return
895 }
896
897 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400898 // Export linkDirs for CC rust generatedlibs
899 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
900 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
901
Ivan Lozanoffee3342019-08-27 12:03:00 -0700902 flags := Flags{
903 Toolchain: toolchain,
904 }
905
Ivan Lozano67eada32021-09-23 11:50:33 -0400906 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800907 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500908 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800909 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910 if mod.compiler != nil {
911 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400912 flags = mod.compiler.cfgFlags(ctx, flags)
913 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400914 }
915 if mod.coverage != nil {
916 flags, deps = mod.coverage.flags(ctx, flags, deps)
917 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200918 if mod.clippy != nil {
919 flags, deps = mod.clippy.flags(ctx, flags, deps)
920 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500921 if mod.sanitize != nil {
922 flags, deps = mod.sanitize.flags(ctx, flags, deps)
923 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400924
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200925 // SourceProvider needs to call GenerateSource() before compiler calls
926 // compile() so it can provide the source. A SourceProvider has
927 // multiple variants (e.g. source, rlib, dylib). Only the "source"
928 // variant is responsible for effectively generating the source. The
929 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400930 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200931 if mod.compiler.(libraryInterface).source() {
932 mod.sourceProvider.GenerateSource(ctx, deps)
933 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
934 } else {
935 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
936 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700937 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200938 }
Colin Cross40213022023-12-13 15:19:49 -0800939 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400940 }
941
942 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100943 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700944 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400945 if ctx.Failed() {
946 return
947 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700948 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
949 if buildOutput.kytheFile != nil {
950 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
951 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400952 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900953
Dan Albert06feee92021-03-19 15:06:02 -0700954 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000955 if mod.docTimestampFile.Valid() {
956 ctx.CheckbuildFile(mod.docTimestampFile.Path())
957 }
Dan Albert06feee92021-03-19 15:06:02 -0700958
Colin Crossff694a82023-12-13 15:54:49 -0800959 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400960 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900961 // If the module has been specifically configure to not be installed then
962 // hide from make as otherwise it will break when running inside make as the
963 // output path to install will not be specified. Not all uninstallable
964 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400965 // side dependencies. In particular, proc-macros need to be captured in the
966 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900967 mod.HideFromMake()
968 } else if !mod.installable(apexInfo) {
969 mod.SkipInstall()
970 }
971
972 // Still call install though, the installs will be stored as PackageSpecs to allow
973 // using the outputs in a genrule.
974 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200975 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900976 if ctx.Failed() {
977 return
978 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400979 // Export your own directory as a linkDir
980 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
981
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400982 }
Chris Wailes74be7642021-07-22 16:20:28 -0700983
984 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700985 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000986 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800987 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000988 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700989}
990
991func (mod *Module) deps(ctx DepsContext) Deps {
992 deps := Deps{}
993
994 if mod.compiler != nil {
995 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400996 }
997 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700998 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700999 }
1000
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001001 if mod.coverage != nil {
1002 deps = mod.coverage.deps(ctx, deps)
1003 }
1004
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001005 if mod.sanitize != nil {
1006 deps = mod.sanitize.deps(ctx, deps)
1007 }
1008
Ivan Lozanoffee3342019-08-27 12:03:00 -07001009 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1010 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001011 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001012 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1013 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1014 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001015 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001016 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017 return deps
1018
1019}
1020
Ivan Lozanoffee3342019-08-27 12:03:00 -07001021type dependencyTag struct {
1022 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001023 name string
1024 library bool
1025 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001026 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027}
1028
Jiyong Park65b62242020-11-25 12:44:59 +09001029// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1030// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1031func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001032 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001033}
1034
1035var _ android.InstallNeededDependencyTag = dependencyTag{}
1036
Colin Cross65cb3142021-12-10 23:05:02 +00001037func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1038 if d.library && d.dynamic {
1039 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1040 }
1041 return nil
1042}
1043
Yu Liuc8884602024-03-15 18:48:38 +00001044func (d dependencyTag) PropagateAconfigValidation() bool {
1045 return d == rlibDepTag || d == sourceDepTag
1046}
1047
1048var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1049
Colin Cross65cb3142021-12-10 23:05:02 +00001050var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1051
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001053 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1054 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001055 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001056 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001057 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001058 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001059 dataLibDepTag = dependencyTag{name: "data lib"}
1060 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061)
1062
Jiyong Park99644e92020-11-17 22:21:02 +09001063func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1064 tag, ok := depTag.(dependencyTag)
1065 return ok && tag == dylibDepTag
1066}
1067
Jiyong Park94e22fd2021-04-08 18:19:15 +09001068func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1069 tag, ok := depTag.(dependencyTag)
1070 return ok && tag == rlibDepTag
1071}
1072
Matthew Maurer0f003b12020-06-29 14:34:06 -07001073type autoDep struct {
1074 variation string
1075 depTag dependencyTag
1076}
1077
1078var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001079 rlibVariation = "rlib"
1080 dylibVariation = "dylib"
1081 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1082 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001083)
1084
1085type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001086 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001087}
1088
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001089func (mod *Module) begin(ctx BaseModuleContext) {
1090 if mod.coverage != nil {
1091 mod.coverage.begin(ctx)
1092 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001093 if mod.sanitize != nil {
1094 mod.sanitize.begin(ctx)
1095 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001096}
1097
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001098func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001099 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001100 return p.prebuilt()
1101 }
1102 return nil
1103}
1104
Kiyoung Kim37693d02024-04-04 09:56:15 +09001105func (mod *Module) Symlinks() []string {
1106 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1107 return nil
1108}
1109
Justin Yun24b246a2023-03-16 10:36:16 +09001110func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1111 if rustDep, ok := dep.(*Module); ok {
1112 // Use base module name for snapshots when exporting to Makefile.
1113 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1114 baseName := rustDep.BaseModuleName()
1115 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1116 }
1117 }
1118 return cc.MakeLibName(ctx, c, dep, depName)
1119}
1120
Ivan Lozanod106efe2023-09-21 23:30:26 -04001121func collectIncludedProtos(mod *Module, dep *Module) {
1122 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1123 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1124 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1125 }
1126 }
1127}
Andrew Walbran52533232024-03-19 11:36:04 +00001128
Ivan Lozanoffee3342019-08-27 12:03:00 -07001129func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1130 var depPaths PathDeps
1131
1132 directRlibDeps := []*Module{}
1133 directDylibDeps := []*Module{}
1134 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001135 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001136 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001137 directSrcProvidersDeps := []*Module{}
1138 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001139
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001140 // For the dependency from platform to apex, use the latest stubs
1141 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001142 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001143 if !apexInfo.IsForPlatform() {
1144 mod.apexSdkVersion = apexInfo.MinSdkVersion
1145 }
1146
1147 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1148 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1149 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1150 // (b/144430859)
1151 mod.apexSdkVersion = android.FutureApiLevel
1152 }
1153
Spandan Das604f3762023-03-16 22:51:40 +00001154 skipModuleList := map[string]bool{}
1155
1156 var apiImportInfo multitree.ApiImportInfo
1157 hasApiImportInfo := false
1158
1159 ctx.VisitDirectDeps(func(dep android.Module) {
1160 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001161 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001162 hasApiImportInfo = true
1163 }
1164 })
1165
1166 if hasApiImportInfo {
1167 targetStubModuleList := map[string]string{}
1168 targetOrigModuleList := map[string]string{}
1169
1170 // Search for dependency which both original module and API imported library with APEX stub exists
1171 ctx.VisitDirectDeps(func(dep android.Module) {
1172 depName := ctx.OtherModuleName(dep)
1173 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1174 targetStubModuleList[apiLibrary] = depName
1175 }
1176 })
1177 ctx.VisitDirectDeps(func(dep android.Module) {
1178 depName := ctx.OtherModuleName(dep)
1179 if origLibrary, ok := targetStubModuleList[depName]; ok {
1180 targetOrigModuleList[origLibrary] = depName
1181 }
1182 })
1183
1184 // Decide which library should be used between original and API imported library
1185 ctx.VisitDirectDeps(func(dep android.Module) {
1186 depName := ctx.OtherModuleName(dep)
1187 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1188 if cc.ShouldUseStubForApex(ctx, dep) {
1189 skipModuleList[depName] = true
1190 } else {
1191 skipModuleList[apiLibrary] = true
1192 }
1193 }
1194 })
1195 }
1196
Cole Faustb6e6f992023-08-17 17:42:26 -07001197 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1198 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001199
Ivan Lozanoffee3342019-08-27 12:03:00 -07001200 ctx.VisitDirectDeps(func(dep android.Module) {
1201 depName := ctx.OtherModuleName(dep)
1202 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001203
Spandan Das604f3762023-03-16 22:51:40 +00001204 if _, exists := skipModuleList[depName]; exists {
1205 return
1206 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001207
1208 if depTag == android.DarwinUniversalVariantTag {
1209 return
1210 }
1211
Ivan Lozano0a468a42024-05-13 21:03:34 -04001212 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001213 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001214 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001215
Ivan Lozanoffee3342019-08-27 12:03:00 -07001216 switch depTag {
1217 case dylibDepTag:
1218 dylib, ok := rustDep.compiler.(libraryInterface)
1219 if !ok || !dylib.dylib() {
1220 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1221 return
1222 }
1223 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001224 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001225 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1226
Ivan Lozanoffee3342019-08-27 12:03:00 -07001227 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001228
Ivan Lozanoffee3342019-08-27 12:03:00 -07001229 rlib, ok := rustDep.compiler.(libraryInterface)
1230 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001231 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001232 return
1233 }
1234 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001235 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001236 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1237
Ivan Lozano0a468a42024-05-13 21:03:34 -04001238 // rust_ffi rlibs may export include dirs, so collect those here.
1239 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1240 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1241 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1242
Ivan Lozanoffee3342019-08-27 12:03:00 -07001243 case procMacroDepTag:
1244 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001245 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001246 // proc_macro link dirs need to be exported, so collect those here.
1247 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001248
1249 case sourceDepTag:
1250 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1251 collectIncludedProtos(mod, rustDep)
1252 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001253 }
1254
Cole Faustb6e6f992023-08-17 17:42:26 -07001255 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1256
Paul Duffind5cf92e2021-07-09 17:38:55 +01001257 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001258 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1259 // OS/Arch variant is used.
1260 var helper string
1261 if ctx.Host() {
1262 helper = "missing 'host_supported'?"
1263 } else {
1264 helper = "device module defined?"
1265 }
1266
1267 if dep.Target().Os != ctx.Os() {
1268 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1269 return
1270 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1271 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1272 return
1273 }
1274 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275 }
1276
Ivan Lozano0a468a42024-05-13 21:03:34 -04001277 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001278 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001279 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001280 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1281 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001282 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 }
1284
Ivan Lozanoffee3342019-08-27 12:03:00 -07001285 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001286 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001287 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001288 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001289 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001290 }
1291 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001292
Ivan Lozanod106efe2023-09-21 23:30:26 -04001293 if depTag == sourceDepTag {
1294 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1295 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001296 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001297 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1298 }
1299 }
1300 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001301 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001302 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001303 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001304 if _, ok := ccDep.(*Module); !ok {
1305 if ccDep.Module().Target().Os != ctx.Os() {
1306 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1307 return
1308 }
1309 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1310 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1311 return
1312 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001313 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001314 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001315 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001316 if !ctx.Config().AllowMissingDependencies() {
1317 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1318 } else {
1319 ctx.AddMissingDependencies([]string{depName})
1320 }
1321 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001322 }
1323
Wen-yi Chu41326c12023-09-22 03:58:59 +00001324 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001325
Ivan Lozanoffee3342019-08-27 12:03:00 -07001326 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001327 switch {
1328 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001329 if cc.IsWholeStaticLib(depTag) {
1330 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1331 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001332 if mod.Binary() {
1333 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1334 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1335 // final linkage, pass the args directly to the linker to handle these cases.
1336 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1337 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001338 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001339 } else {
1340 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 -05001341 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001342 }
1343
1344 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1345 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001346 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001347 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1348
Colin Cross313aa542023-12-13 13:47:44 -08001349 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001350 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1351 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1352 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1353 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001354 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001355
1356 // Record baseLibName for snapshots.
1357 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1358
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001359 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001360 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001361 // For the shared lib dependencies, we may link to the stub variant
1362 // of the dependency depending on the context (e.g. if this
1363 // dependency crosses the APEX boundaries).
1364 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1365
1366 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1367 // object (either from stub or non-stub) to use.
1368 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001369 if !linkObject.Valid() {
1370 if !ctx.Config().AllowMissingDependencies() {
1371 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1372 } else {
1373 ctx.AddMissingDependencies([]string{depName})
1374 }
1375 return
1376 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001377 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001378
Ivan Lozanoffee3342019-08-27 12:03:00 -07001379 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001380 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001381 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1382 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1383 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1384 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001385 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001386
1387 // Record baseLibName for snapshots.
1388 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1389
Cole Faustb6e6f992023-08-17 17:42:26 -07001390 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001391 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001392 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001393 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001394 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1395 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1396 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001397 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001398 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001399 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001400 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001401 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001402 }
1403
1404 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001405 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1406 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001407 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001408 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001409 } else {
1410 switch {
1411 case depTag == cc.CrtBeginDepTag:
1412 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1413 case depTag == cc.CrtEndDepTag:
1414 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1415 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001416 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001417
1418 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001419 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001420 // These are usually genrules which don't have per-target variants.
1421 directSrcDeps = append(directSrcDeps, srcDep)
1422 }
1423 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001424 })
1425
Wen-yi Chu41326c12023-09-22 03:58:59 +00001426 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1427
1428 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001429 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001430 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001431 crateName := dep.CrateName()
1432 if alias, aliased := aliases[crateName]; aliased {
1433 crateName = alias
1434 }
1435 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001436 }
1437 var dylibDepFiles RustLibraries
1438 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001439 crateName := dep.CrateName()
1440 if alias, aliased := aliases[crateName]; aliased {
1441 crateName = alias
1442 }
1443 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001444 }
1445 var procMacroDepFiles RustLibraries
1446 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001447 crateName := dep.CrateName()
1448 if alias, aliased := aliases[crateName]; aliased {
1449 crateName = alias
1450 }
1451 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001452 }
1453
Colin Cross004bd3f2023-10-02 11:39:17 -07001454 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001456 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001457 }
1458
Colin Cross004bd3f2023-10-02 11:39:17 -07001459 var sharedLibFiles android.Paths
1460 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001461 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001462 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001463 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001464 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001465 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001466 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001467 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001468 }
1469
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001470 var srcProviderDepFiles android.Paths
1471 for _, dep := range directSrcProvidersDeps {
1472 srcs, _ := dep.OutputFiles("")
1473 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1474 }
1475 for _, dep := range directSrcDeps {
1476 srcs := dep.Srcs()
1477 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1478 }
1479
Wen-yi Chu41326c12023-09-22 03:58:59 +00001480 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1481 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001482 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1483 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1484 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001485 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001486 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001487
1488 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001489 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001490 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001491 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001492 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1493 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1494 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001495
1496 return depPaths
1497}
1498
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001499func (mod *Module) InstallInData() bool {
1500 if mod.compiler == nil {
1501 return false
1502 }
1503 return mod.compiler.inData()
1504}
1505
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001506func (mod *Module) InstallInRamdisk() bool {
1507 return mod.InRamdisk()
1508}
1509
1510func (mod *Module) InstallInVendorRamdisk() bool {
1511 return mod.InVendorRamdisk()
1512}
1513
1514func (mod *Module) InstallInRecovery() bool {
1515 return mod.InRecovery()
1516}
1517
Wen-yi Chu41326c12023-09-22 03:58:59 +00001518func linkPathFromFilePath(filepath android.Path) string {
1519 return strings.Split(filepath.String(), filepath.Base())[0]
1520}
1521
Spandan Das604f3762023-03-16 22:51:40 +00001522// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1523func (r *Module) usePublicApi() bool {
1524 return r.Device() && r.UseSdk()
1525}
1526
1527// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1528func (r *Module) useVendorApi() bool {
1529 return r.Device() && (r.InVendor() || r.InProduct())
1530}
1531
Ivan Lozanoffee3342019-08-27 12:03:00 -07001532func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1533 ctx := &depsContext{
1534 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001535 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001536
1537 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001538 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001539
Kiyoung Kim487689e2022-07-26 09:48:22 +09001540 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001541 if mod.usePublicApi() || mod.useVendorApi() {
1542 for idx, lib := range deps.SharedLibs {
1543 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1544 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001545 }
1546
Ivan Lozano1921e802021-05-20 13:39:16 -04001547 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001548 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001549 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001550
Ivan Lozano2b081132020-09-08 12:46:52 -04001551 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001552 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001553 stdLinkage = "rlib-std"
1554 }
1555
1556 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001557 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001558
Ivan Lozano2b081132020-09-08 12:46:52 -04001559 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1560 rlibDepVariations = append(rlibDepVariations,
1561 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1562 }
1563
Ivan Lozano1921e802021-05-20 13:39:16 -04001564 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001565 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001566 for _, lib := range deps.Rlibs {
1567 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001568 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001569 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001570
1571 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001572 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001573 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1574
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001575 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001576 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001577 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001578
Ivan Lozano1921e802021-05-20 13:39:16 -04001579 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001580 if deps.Rustlibs != nil {
1581 if !mod.compiler.Disabled() {
1582 for _, lib := range deps.Rustlibs {
1583 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1584 if autoDep.depTag == rlibDepTag {
1585 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001586 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1587
Ivan Lozanod106efe2023-09-21 23:30:26 -04001588 } else {
1589 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1590 // Check for the existence of the dylib deptag variant. Select it if available,
1591 // otherwise select the rlib variant.
1592 autoDepVariations := append(commonDepVariations,
1593 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001594 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001595 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1596 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001597
Ivan Lozanod106efe2023-09-21 23:30:26 -04001598 } else {
1599 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001600 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1601
Ivan Lozanod106efe2023-09-21 23:30:26 -04001602 }
1603 }
1604 }
1605 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1606 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001607 srcProviderVariations := append(commonDepVariations,
1608 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001609 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001610
Ivan Lozano0a468a42024-05-13 21:03:34 -04001611 // Only add rustlib dependencies if they're source providers themselves.
1612 // This is used to track which crate names need to be added to the source generated
1613 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001614 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001615 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001616 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001617 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001618 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001619 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001620
Ivan Lozano1921e802021-05-20 13:39:16 -04001621 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001622 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001623 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001624 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001625 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001626 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001627 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001628 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001629 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001630 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1631
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001632 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001633 }
1634 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001635
1636 for _, lib := range deps.SharedLibs {
1637 depTag := cc.SharedDepTag()
1638 name, version := cc.StubsLibNameAndVersion(lib)
1639
1640 variations := []blueprint.Variation{
1641 {Mutator: "link", Variation: "shared"},
1642 }
Spandan Das604f3762023-03-16 22:51:40 +00001643 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1644 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1645 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1646 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1647 }
1648
1649 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1650 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1651 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001652 }
1653
1654 for _, lib := range deps.WholeStaticLibs {
1655 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001656
1657 actx.AddVariationDependencies([]blueprint.Variation{
1658 {Mutator: "link", Variation: "static"},
1659 }, depTag, lib)
1660 }
1661
1662 for _, lib := range deps.StaticLibs {
1663 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001664
1665 actx.AddVariationDependencies([]blueprint.Variation{
1666 {Mutator: "link", Variation: "static"},
1667 }, depTag, lib)
1668 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001669
Zach Johnson3df4e632020-11-06 11:56:27 -08001670 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1671
Colin Cross565cafd2020-09-25 18:47:38 -07001672 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001673 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001674 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001675 }
Colin Crossfe605e12022-01-23 20:46:16 -08001676 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001677 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001678 }
1679
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001680 if mod.sourceProvider != nil {
1681 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1682 bindgen.Properties.Custom_bindgen != "" {
1683 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1684 bindgen.Properties.Custom_bindgen)
1685 }
1686 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001687
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001688 actx.AddVariationDependencies([]blueprint.Variation{
1689 {Mutator: "link", Variation: "shared"},
1690 }, dataLibDepTag, deps.DataLibs...)
1691
1692 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1693
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001694 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001695 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001696
1697 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001698}
1699
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001700func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001701 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001702 mod.beginMutator(ctx)
1703 }
1704}
1705
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001706func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1707 ctx := &baseModuleContext{
1708 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001709 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001710
1711 mod.begin(ctx)
1712}
1713
Ivan Lozanoffee3342019-08-27 12:03:00 -07001714func (mod *Module) Name() string {
1715 name := mod.ModuleBase.Name()
1716 if p, ok := mod.compiler.(interface {
1717 Name(string) string
1718 }); ok {
1719 name = p.Name(name)
1720 }
1721 return name
1722}
1723
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001724func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001725 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001726 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001727 }
1728}
1729
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001730var _ android.HostToolProvider = (*Module)(nil)
1731
1732func (mod *Module) HostToolPath() android.OptionalPath {
1733 if !mod.Host() {
1734 return android.OptionalPath{}
1735 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001736 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1737 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001738 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1739 // Even though proc-macros aren't strictly "tools", since they target the compiler
1740 // and act as compiler plugins, we treat them similarly.
1741 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001742 }
1743 return android.OptionalPath{}
1744}
1745
Jiyong Park99644e92020-11-17 22:21:02 +09001746var _ android.ApexModule = (*Module)(nil)
1747
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001748func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001749 return String(mod.Properties.Min_sdk_version)
1750}
1751
Jiyong Park45bf82e2020-12-15 22:29:02 +09001752// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001753func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001754 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001755 if minSdkVersion == "apex_inherit" {
1756 return nil
1757 }
1758 if minSdkVersion == "" {
1759 return fmt.Errorf("min_sdk_version is not specificed")
1760 }
1761
1762 // Not using nativeApiLevelFromUser because the context here is not
1763 // necessarily a native context.
1764 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1765 if err != nil {
1766 return err
1767 }
1768
1769 if ver.GreaterThan(sdkVersion) {
1770 return fmt.Errorf("newer SDK(%v)", ver)
1771 }
Jiyong Park99644e92020-11-17 22:21:02 +09001772 return nil
1773}
1774
Jiyong Park45bf82e2020-12-15 22:29:02 +09001775// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001776func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1777 depTag := ctx.OtherModuleDependencyTag(dep)
1778
1779 if ccm, ok := dep.(*cc.Module); ok {
1780 if ccm.HasStubsVariants() {
1781 if cc.IsSharedDepTag(depTag) {
1782 // dynamic dep to a stubs lib crosses APEX boundary
1783 return false
1784 }
1785 if cc.IsRuntimeDepTag(depTag) {
1786 // runtime dep to a stubs lib also crosses APEX boundary
1787 return false
1788 }
1789
1790 if cc.IsHeaderDepTag(depTag) {
1791 return false
1792 }
1793 }
1794 if mod.Static() && cc.IsSharedDepTag(depTag) {
1795 // shared_lib dependency from a static lib is considered as crossing
1796 // the APEX boundary because the dependency doesn't actually is
1797 // linked; the dependency is used only during the compilation phase.
1798 return false
1799 }
1800 }
1801
Matthew Maurer581b6d82022-09-29 16:46:25 -07001802 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001803 return false
1804 }
1805
1806 return true
1807}
1808
1809// Overrides ApexModule.IsInstallabeToApex()
1810func (mod *Module) IsInstallableToApex() bool {
1811 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001812 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001813 return true
1814 }
1815 if _, ok := mod.compiler.(*binaryDecorator); ok {
1816 return true
1817 }
1818 }
1819 return false
1820}
1821
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001822// If a library file has a "lib" prefix, extract the library name without the prefix.
1823func libNameFromFilePath(filepath android.Path) (string, bool) {
1824 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1825 if strings.HasPrefix(libName, "lib") {
1826 libName = libName[3:]
1827 return libName, true
1828 }
1829 return "", false
1830}
1831
Sasha Smundaka76acba2022-04-18 20:12:56 -07001832func kytheExtractRustFactory() android.Singleton {
1833 return &kytheExtractRustSingleton{}
1834}
1835
1836type kytheExtractRustSingleton struct {
1837}
1838
1839func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1840 var xrefTargets android.Paths
1841 ctx.VisitAllModules(func(module android.Module) {
1842 if rustModule, ok := module.(xref); ok {
1843 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1844 }
1845 })
1846 if len(xrefTargets) > 0 {
1847 ctx.Phony("xref_rust", xrefTargets...)
1848 }
1849}
1850
Jihoon Kangf78a8902022-09-01 22:47:07 +00001851func (c *Module) Partition() string {
1852 return ""
1853}
1854
Ivan Lozanoffee3342019-08-27 12:03:00 -07001855var Bool = proptools.Bool
1856var BoolDefault = proptools.BoolDefault
1857var String = proptools.String
1858var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001859
1860var _ android.OutputFileProducer = (*Module)(nil)