blob: 042ba8e171577f767939fe19ab20f732f0154815 [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 Lozano52767be2019-10-18 14:49:46 -0700209func (mod *Module) SelectedStl() string {
210 return ""
211}
212
Ivan Lozano2b262972019-11-21 12:30:50 -0800213func (mod *Module) NonCcVariants() bool {
214 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400215 if library, ok := mod.compiler.(libraryInterface); ok {
216 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800217 }
218 }
219 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
220}
221
Ivan Lozano52767be2019-10-18 14:49:46 -0700222func (mod *Module) Static() bool {
223 if mod.compiler != nil {
224 if library, ok := mod.compiler.(libraryInterface); ok {
225 return library.static()
226 }
227 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400228 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700229}
230
231func (mod *Module) Shared() bool {
232 if mod.compiler != nil {
233 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400234 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700235 }
236 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400237 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700238}
239
Ivan Lozanod7586b62021-04-01 09:49:36 -0400240func (mod *Module) Dylib() bool {
241 if mod.compiler != nil {
242 if library, ok := mod.compiler.(libraryInterface); ok {
243 return library.dylib()
244 }
245 }
246 return false
247}
248
Ivan Lozanod106efe2023-09-21 23:30:26 -0400249func (mod *Module) Source() bool {
250 if mod.compiler != nil {
251 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
252 return library.source()
253 }
254 }
255 return false
256}
257
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400258func (mod *Module) RlibStd() bool {
259 if mod.compiler != nil {
260 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
261 return library.rlibStd()
262 }
263 }
264 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
265}
266
Ivan Lozanod7586b62021-04-01 09:49:36 -0400267func (mod *Module) Rlib() bool {
268 if mod.compiler != nil {
269 if library, ok := mod.compiler.(libraryInterface); ok {
270 return library.rlib()
271 }
272 }
273 return false
274}
275
276func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400277 if binary, ok := mod.compiler.(binaryInterface); ok {
278 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400279 }
280 return false
281}
282
Justin Yun5e035862021-06-29 20:50:37 +0900283func (mod *Module) StaticExecutable() bool {
284 if !mod.Binary() {
285 return false
286 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400287 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900288}
289
Ivan Lozanod7586b62021-04-01 09:49:36 -0400290func (mod *Module) Object() bool {
291 // Rust has no modules which produce only object files.
292 return false
293}
294
Ivan Lozano52767be2019-10-18 14:49:46 -0700295func (mod *Module) Toc() android.OptionalPath {
296 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400297 if lib, ok := mod.compiler.(libraryInterface); ok {
298 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700299 }
300 }
301 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
302}
303
Colin Crossc511bc52020-04-07 16:50:32 +0000304func (mod *Module) UseSdk() bool {
305 return false
306}
307
Ivan Lozanod7586b62021-04-01 09:49:36 -0400308func (mod *Module) RelativeInstallPath() string {
309 if mod.compiler != nil {
310 return mod.compiler.relativeInstallPath()
311 }
312 return ""
313}
314
Ivan Lozano52767be2019-10-18 14:49:46 -0700315func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500316 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700317}
318
Jiyong Park7d55b612021-06-11 17:22:09 +0900319func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400320 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900321}
322
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400323func (mod *Module) SubName() string {
324 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700325}
326
Ivan Lozanof1868af2022-04-12 13:08:36 -0400327func (mod *Module) IsVndkPrebuiltLibrary() bool {
328 // Rust modules do not provide VNDK prebuilts
329 return false
330}
331
332func (mod *Module) IsVendorPublicLibrary() bool {
333 return mod.VendorProperties.IsVendorPublicLibrary
334}
335
336func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
337 // Rust modules to not provide Sdk variants
338 return false
339}
340
Colin Cross127bb8b2020-12-16 16:46:01 -0800341func (c *Module) IsVndkPrivate() bool {
342 return false
343}
344
345func (c *Module) IsLlndk() bool {
346 return false
347}
348
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400349func (mod *Module) KernelHeadersDecorator() bool {
350 return false
351}
352
Colin Cross1f3f1302021-04-26 18:37:44 -0700353func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400354 return false
355}
356
Colin Cross5271fea2021-04-27 13:06:04 -0700357func (m *Module) NeedsVendorPublicLibraryVariants() bool {
358 return false
359}
360
Ivan Lozanod7586b62021-04-01 09:49:36 -0400361func (mod *Module) HasLlndkStubs() bool {
362 return false
363}
364
365func (mod *Module) StubsVersion() string {
366 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
367}
368
Ivan Lozano52767be2019-10-18 14:49:46 -0700369func (mod *Module) SdkVersion() string {
370 return ""
371}
372
Colin Crossc511bc52020-04-07 16:50:32 +0000373func (mod *Module) AlwaysSdk() bool {
374 return false
375}
376
Jiyong Park2286afd2020-06-16 21:58:53 +0900377func (mod *Module) IsSdkVariant() bool {
378 return false
379}
380
Colin Cross1348ce32020-10-01 13:37:16 -0700381func (mod *Module) SplitPerApiLevel() bool {
382 return false
383}
384
Sasha Smundaka76acba2022-04-18 20:12:56 -0700385func (mod *Module) XrefRustFiles() android.Paths {
386 return mod.kytheFiles
387}
388
Ivan Lozanoffee3342019-08-27 12:03:00 -0700389type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400390 Dylibs []string
391 Rlibs []string
392 Rustlibs []string
393 Stdlibs []string
394 ProcMacros []string
395 SharedLibs []string
396 StaticLibs []string
397 WholeStaticLibs []string
398 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400400 // Used for data dependencies adjacent to tests
401 DataLibs []string
402 DataBins []string
403
Colin Crossfe605e12022-01-23 20:46:16 -0800404 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700405}
406
407type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700408 DyLibs RustLibraries
409 RLibs RustLibraries
410 SharedLibs android.Paths
411 SharedLibDeps android.Paths
412 StaticLibs android.Paths
413 ProcMacros RustLibraries
414 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500415
416 // depFlags and depLinkFlags are rustc and linker (clang) flags.
417 depFlags []string
418 depLinkFlags []string
419
420 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
421 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000422 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700423 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700424
Ivan Lozano0a468a42024-05-13 21:03:34 -0400425 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
426 // cc_library_static dependants of rlibs.
427 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
428 exportedLinkDirs []string
429
Ivan Lozano45901ed2020-07-24 16:05:01 -0400430 // Used by bindgen modules which call clang
431 depClangFlags []string
432 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400433 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400434 depSystemIncludePaths android.Paths
435
Colin Crossfe605e12022-01-23 20:46:16 -0800436 CrtBegin android.Paths
437 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700438
439 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500440 SrcDeps android.Paths
441 srcProviderFiles android.Paths
Ivan Lozano0a468a42024-05-13 21:03:34 -0400442
443 // Used by Generated Libraries
444 depExportedRlibs []cc.RustRlibDep
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445}
446
447type RustLibraries []RustLibrary
448
449type RustLibrary struct {
450 Path android.Path
451 CrateName string
452}
453
Matthew Maurerbb3add12020-06-25 09:34:12 -0700454type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000455 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700456 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700457}
458
Sasha Smundaka76acba2022-04-18 20:12:56 -0700459type xref interface {
460 XrefRustFiles() android.Paths
461}
462
Matthew Maurerbb3add12020-06-25 09:34:12 -0700463type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000464 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700465 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700466}
467
Wen-yi Chu41326c12023-09-22 03:58:59 +0000468func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
469 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700470}
471
Colin Cross004bd3f2023-10-02 11:39:17 -0700472func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
473 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400474}
475
Colin Cross0de8a1e2020-09-18 14:15:30 -0700476func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800477 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700478 LinkDirs: flagExporter.linkDirs,
479 LinkObjects: flagExporter.linkObjects,
480 })
481}
482
Matthew Maurerbb3add12020-06-25 09:34:12 -0700483var _ exportedFlagsProducer = (*flagExporter)(nil)
484
485func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700486 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700487}
488
Colin Cross0de8a1e2020-09-18 14:15:30 -0700489type FlagExporterInfo struct {
490 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000491 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700492 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700493}
494
Colin Crossbc7d76c2023-12-12 16:39:03 -0800495var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700496
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400497func (mod *Module) isCoverageVariant() bool {
498 return mod.coverage.Properties.IsCoverageVariant
499}
500
501var _ cc.Coverage = (*Module)(nil)
502
Colin Crossf5f4ad32024-01-19 15:41:48 -0800503func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400504 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
505}
506
Ivan Lozanod7586b62021-04-01 09:49:36 -0400507func (mod *Module) VndkVersion() string {
508 return mod.Properties.VndkVersion
509}
510
Ivan Lozano0a468a42024-05-13 21:03:34 -0400511func (mod *Module) ExportedCrateLinkDirs() []string {
512 return mod.exportedLinkDirs
513}
514
Ivan Lozanod7586b62021-04-01 09:49:36 -0400515func (mod *Module) PreventInstall() bool {
516 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400517}
518
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400519func (mod *Module) MarkAsCoverageVariant(coverage bool) {
520 mod.coverage.Properties.IsCoverageVariant = coverage
521}
522
523func (mod *Module) EnableCoverageIfNeeded() {
524 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525}
526
527func defaultsFactory() android.Module {
528 return DefaultsFactory()
529}
530
531type Defaults struct {
532 android.ModuleBase
533 android.DefaultsModuleBase
534}
535
536func DefaultsFactory(props ...interface{}) android.Module {
537 module := &Defaults{}
538
539 module.AddProperties(props...)
540 module.AddProperties(
541 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800542 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500543 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100544 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400545 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546 &BaseCompilerProperties{},
547 &BinaryCompilerProperties{},
548 &LibraryCompilerProperties{},
549 &ProcMacroCompilerProperties{},
550 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400551 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700552 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400553 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400554 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200555 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500556 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000557 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558 )
559
560 android.InitDefaultsModule(module)
561 return module
562}
563
564func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700565 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700566}
567
Ivan Lozano183a3212019-10-18 14:18:45 -0700568func (mod *Module) CcLibrary() bool {
569 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500570 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700571 return true
572 }
573 }
574 return false
575}
576
577func (mod *Module) CcLibraryInterface() bool {
578 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400579 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
580 // VariantIs{Static,Shared} is set.
581 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700582 return true
583 }
584 }
585 return false
586}
587
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400588func (mod *Module) RustLibraryInterface() bool {
589 if mod.compiler != nil {
590 if _, ok := mod.compiler.(libraryInterface); ok {
591 return true
592 }
593 }
594 return false
595}
596
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500597func (mod *Module) IsFuzzModule() bool {
598 if _, ok := mod.compiler.(*fuzzDecorator); ok {
599 return true
600 }
601 return false
602}
603
604func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
605 return mod.FuzzModule
606}
607
608func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
609 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
610 return fuzzer.fuzzPackagedModule
611 }
612 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
613}
614
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000615func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500616 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
617 return fuzzer.sharedLibraries
618 }
619 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
620}
621
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400622func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400623 if mod.compiler != nil {
624 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400625 }
626 return nil
627}
628
Ivan Lozano183a3212019-10-18 14:18:45 -0700629func (mod *Module) SetStatic() {
630 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700631 if library, ok := mod.compiler.(libraryInterface); ok {
632 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700633 return
634 }
635 }
636 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
637}
638
639func (mod *Module) SetShared() {
640 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700641 if library, ok := mod.compiler.(libraryInterface); ok {
642 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700643 return
644 }
645 }
646 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
647}
648
Ivan Lozano183a3212019-10-18 14:18:45 -0700649func (mod *Module) BuildStaticVariant() bool {
650 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700651 if library, ok := mod.compiler.(libraryInterface); ok {
652 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700653 }
654 }
655 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
656}
657
658func (mod *Module) BuildSharedVariant() bool {
659 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700660 if library, ok := mod.compiler.(libraryInterface); ok {
661 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700662 }
663 }
664 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
665}
666
Ivan Lozano183a3212019-10-18 14:18:45 -0700667func (mod *Module) Module() android.Module {
668 return mod
669}
670
Ivan Lozano183a3212019-10-18 14:18:45 -0700671func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400672 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700673}
674
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400675func (mod *Module) CoverageFiles() android.Paths {
676 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800677 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400678 }
679 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
680}
681
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400682// Rust does not produce gcno files, and therefore does not produce a coverage archive.
683func (mod *Module) CoverageOutputFile() android.OptionalPath {
684 return android.OptionalPath{}
685}
686
687func (mod *Module) IsNdk(config android.Config) bool {
688 return false
689}
690
691func (mod *Module) IsStubs() bool {
692 return false
693}
694
Jiyong Park459feca2020-12-15 11:02:21 +0900695func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900696 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900697 return false
698 }
699
Jiyong Park459feca2020-12-15 11:02:21 +0900700 // The apex variant is not installable because it is included in the APEX and won't appear
701 // in the system partition as a standalone file.
702 if !apexInfo.IsForPlatform() {
703 return false
704 }
705
Jiyong Parke54f07e2021-04-07 15:08:04 +0900706 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900707}
708
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500709func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800710 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
711 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500712}
713
Ivan Lozano183a3212019-10-18 14:18:45 -0700714var _ cc.LinkableInterface = (*Module)(nil)
715
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716func (mod *Module) Init() android.Module {
717 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500718 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719
Yi Kong46c6e592022-01-20 22:55:00 +0800720 if mod.afdo != nil {
721 mod.AddProperties(mod.afdo.props()...)
722 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723 if mod.compiler != nil {
724 mod.AddProperties(mod.compiler.compilerProps()...)
725 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400726 if mod.coverage != nil {
727 mod.AddProperties(mod.coverage.props()...)
728 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200729 if mod.clippy != nil {
730 mod.AddProperties(mod.clippy.props()...)
731 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400732 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700733 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400734 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500735 if mod.sanitize != nil {
736 mod.AddProperties(mod.sanitize.props()...)
737 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400738
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900740 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741
742 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743 return mod
744}
745
746func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
747 return &Module{
748 hod: hod,
749 multilib: multilib,
750 }
751}
752func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
753 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800754 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400755 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200756 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500757 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758 return module
759}
760
761type ModuleContext interface {
762 android.ModuleContext
763 ModuleContextIntf
764}
765
766type BaseModuleContext interface {
767 android.BaseModuleContext
768 ModuleContextIntf
769}
770
771type DepsContext interface {
772 android.BottomUpMutatorContext
773 ModuleContextIntf
774}
775
776type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200777 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779}
780
781type depsContext struct {
782 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783}
784
785type moduleContext struct {
786 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787}
788
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200789type baseModuleContext struct {
790 android.BaseModuleContext
791}
792
793func (ctx *moduleContext) RustModule() *Module {
794 return ctx.Module().(*Module)
795}
796
797func (ctx *moduleContext) toolchain() config.Toolchain {
798 return ctx.RustModule().toolchain(ctx)
799}
800
801func (ctx *depsContext) RustModule() *Module {
802 return ctx.Module().(*Module)
803}
804
805func (ctx *depsContext) toolchain() config.Toolchain {
806 return ctx.RustModule().toolchain(ctx)
807}
808
809func (ctx *baseModuleContext) RustModule() *Module {
810 return ctx.Module().(*Module)
811}
812
813func (ctx *baseModuleContext) toolchain() config.Toolchain {
814 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400815}
816
817func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700818 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
819 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
820 return false
821 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400822 return mod.compiler != nil && mod.compiler.nativeCoverage()
823}
824
Ivan Lozanod7586b62021-04-01 09:49:36 -0400825func (mod *Module) EverInstallable() bool {
826 return mod.compiler != nil &&
827 // Check to see whether the module is actually ever installable.
828 mod.compiler.everInstallable()
829}
830
831func (mod *Module) Installable() *bool {
832 return mod.Properties.Installable
833}
834
Ivan Lozano872d5792022-03-23 17:31:39 -0400835func (mod *Module) ProcMacro() bool {
836 if pm, ok := mod.compiler.(procMacroInterface); ok {
837 return pm.ProcMacro()
838 }
839 return false
840}
841
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
843 if mod.cachedToolchain == nil {
844 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
845 }
846 return mod.cachedToolchain
847}
848
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200849func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
850 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
851}
852
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
854}
855
856func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
857 ctx := &moduleContext{
858 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860
Colin Crossff694a82023-12-13 15:54:49 -0800861 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900862 if !apexInfo.IsForPlatform() {
863 mod.hideApexVariantFromMake = true
864 }
865
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500867 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
868
Ivan Lozanof1868af2022-04-12 13:08:36 -0400869 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700870
Ivan Lozanoffee3342019-08-27 12:03:00 -0700871 if !toolchain.Supported() {
872 // This toolchain's unsupported, there's nothing to do for this mod.
873 return
874 }
875
876 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400877 // Export linkDirs for CC rust generatedlibs
878 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
879 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
880
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 flags := Flags{
882 Toolchain: toolchain,
883 }
884
Ivan Lozano67eada32021-09-23 11:50:33 -0400885 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800886 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500887 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800888 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 if mod.compiler != nil {
890 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400891 flags = mod.compiler.cfgFlags(ctx, flags)
892 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400893 }
894 if mod.coverage != nil {
895 flags, deps = mod.coverage.flags(ctx, flags, deps)
896 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200897 if mod.clippy != nil {
898 flags, deps = mod.clippy.flags(ctx, flags, deps)
899 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500900 if mod.sanitize != nil {
901 flags, deps = mod.sanitize.flags(ctx, flags, deps)
902 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400903
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200904 // SourceProvider needs to call GenerateSource() before compiler calls
905 // compile() so it can provide the source. A SourceProvider has
906 // multiple variants (e.g. source, rlib, dylib). Only the "source"
907 // variant is responsible for effectively generating the source. The
908 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400909 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200910 if mod.compiler.(libraryInterface).source() {
911 mod.sourceProvider.GenerateSource(ctx, deps)
912 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
913 } else {
914 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
915 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700916 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200917 }
Colin Cross40213022023-12-13 15:19:49 -0800918 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400919 }
920
921 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100922 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700923 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400924 if ctx.Failed() {
925 return
926 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700927 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
928 if buildOutput.kytheFile != nil {
929 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
930 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400931 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900932
Dan Albert06feee92021-03-19 15:06:02 -0700933 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000934 if mod.docTimestampFile.Valid() {
935 ctx.CheckbuildFile(mod.docTimestampFile.Path())
936 }
Dan Albert06feee92021-03-19 15:06:02 -0700937
Colin Crossff694a82023-12-13 15:54:49 -0800938 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400939 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900940 // If the module has been specifically configure to not be installed then
941 // hide from make as otherwise it will break when running inside make as the
942 // output path to install will not be specified. Not all uninstallable
943 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400944 // side dependencies. In particular, proc-macros need to be captured in the
945 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900946 mod.HideFromMake()
947 } else if !mod.installable(apexInfo) {
948 mod.SkipInstall()
949 }
950
951 // Still call install though, the installs will be stored as PackageSpecs to allow
952 // using the outputs in a genrule.
953 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200954 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900955 if ctx.Failed() {
956 return
957 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400958 // Export your own directory as a linkDir
959 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
960
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400961 }
Chris Wailes74be7642021-07-22 16:20:28 -0700962
963 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700964 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000965 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800966 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000967 }
mrziwang0cbd3b02024-06-20 16:39:25 -0700968 mod.setOutputFiles(ctx)
969}
970
971func (mod *Module) setOutputFiles(ctx ModuleContext) {
972 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
973 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
974 } else if mod.OutputFile().Valid() {
975 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
976 } else {
977 ctx.SetOutputFiles(android.Paths{}, "")
978 }
979 if mod.compiler != nil {
980 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
981 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982}
983
984func (mod *Module) deps(ctx DepsContext) Deps {
985 deps := Deps{}
986
987 if mod.compiler != nil {
988 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400989 }
990 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700991 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700992 }
993
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400994 if mod.coverage != nil {
995 deps = mod.coverage.deps(ctx, deps)
996 }
997
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500998 if mod.sanitize != nil {
999 deps = mod.sanitize.deps(ctx, deps)
1000 }
1001
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1003 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001004 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001005 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1006 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1007 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001008 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001009 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001010 return deps
1011
1012}
1013
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014type dependencyTag struct {
1015 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001016 name string
1017 library bool
1018 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001019 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020}
1021
Jiyong Park65b62242020-11-25 12:44:59 +09001022// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1023// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1024func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001025 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001026}
1027
1028var _ android.InstallNeededDependencyTag = dependencyTag{}
1029
Colin Cross65cb3142021-12-10 23:05:02 +00001030func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1031 if d.library && d.dynamic {
1032 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1033 }
1034 return nil
1035}
1036
Yu Liuc8884602024-03-15 18:48:38 +00001037func (d dependencyTag) PropagateAconfigValidation() bool {
1038 return d == rlibDepTag || d == sourceDepTag
1039}
1040
1041var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1042
Colin Cross65cb3142021-12-10 23:05:02 +00001043var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1044
Ivan Lozanoffee3342019-08-27 12:03:00 -07001045var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001046 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1047 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001048 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001049 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001050 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001051 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001052 dataLibDepTag = dependencyTag{name: "data lib"}
1053 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054)
1055
Jiyong Park99644e92020-11-17 22:21:02 +09001056func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1057 tag, ok := depTag.(dependencyTag)
1058 return ok && tag == dylibDepTag
1059}
1060
Jiyong Park94e22fd2021-04-08 18:19:15 +09001061func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1062 tag, ok := depTag.(dependencyTag)
1063 return ok && tag == rlibDepTag
1064}
1065
Matthew Maurer0f003b12020-06-29 14:34:06 -07001066type autoDep struct {
1067 variation string
1068 depTag dependencyTag
1069}
1070
1071var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001072 rlibVariation = "rlib"
1073 dylibVariation = "dylib"
1074 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1075 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001076)
1077
1078type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001079 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001080}
1081
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001082func (mod *Module) begin(ctx BaseModuleContext) {
1083 if mod.coverage != nil {
1084 mod.coverage.begin(ctx)
1085 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001086 if mod.sanitize != nil {
1087 mod.sanitize.begin(ctx)
1088 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001089}
1090
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001091func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001092 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001093 return p.prebuilt()
1094 }
1095 return nil
1096}
1097
Kiyoung Kim37693d02024-04-04 09:56:15 +09001098func (mod *Module) Symlinks() []string {
1099 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1100 return nil
1101}
1102
Justin Yun24b246a2023-03-16 10:36:16 +09001103func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1104 if rustDep, ok := dep.(*Module); ok {
1105 // Use base module name for snapshots when exporting to Makefile.
1106 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1107 baseName := rustDep.BaseModuleName()
1108 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1109 }
1110 }
1111 return cc.MakeLibName(ctx, c, dep, depName)
1112}
1113
Ivan Lozanod106efe2023-09-21 23:30:26 -04001114func collectIncludedProtos(mod *Module, dep *Module) {
1115 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1116 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1117 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1118 }
1119 }
1120}
Andrew Walbran52533232024-03-19 11:36:04 +00001121
Ivan Lozanoffee3342019-08-27 12:03:00 -07001122func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1123 var depPaths PathDeps
1124
1125 directRlibDeps := []*Module{}
1126 directDylibDeps := []*Module{}
1127 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001128 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001129 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001130 directSrcProvidersDeps := []*Module{}
1131 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001132
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001133 // For the dependency from platform to apex, use the latest stubs
1134 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001135 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001136 if !apexInfo.IsForPlatform() {
1137 mod.apexSdkVersion = apexInfo.MinSdkVersion
1138 }
1139
1140 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1141 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1142 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1143 // (b/144430859)
1144 mod.apexSdkVersion = android.FutureApiLevel
1145 }
1146
Spandan Das604f3762023-03-16 22:51:40 +00001147 skipModuleList := map[string]bool{}
1148
1149 var apiImportInfo multitree.ApiImportInfo
1150 hasApiImportInfo := false
1151
1152 ctx.VisitDirectDeps(func(dep android.Module) {
1153 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001154 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001155 hasApiImportInfo = true
1156 }
1157 })
1158
1159 if hasApiImportInfo {
1160 targetStubModuleList := map[string]string{}
1161 targetOrigModuleList := map[string]string{}
1162
1163 // Search for dependency which both original module and API imported library with APEX stub exists
1164 ctx.VisitDirectDeps(func(dep android.Module) {
1165 depName := ctx.OtherModuleName(dep)
1166 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1167 targetStubModuleList[apiLibrary] = depName
1168 }
1169 })
1170 ctx.VisitDirectDeps(func(dep android.Module) {
1171 depName := ctx.OtherModuleName(dep)
1172 if origLibrary, ok := targetStubModuleList[depName]; ok {
1173 targetOrigModuleList[origLibrary] = depName
1174 }
1175 })
1176
1177 // Decide which library should be used between original and API imported library
1178 ctx.VisitDirectDeps(func(dep android.Module) {
1179 depName := ctx.OtherModuleName(dep)
1180 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1181 if cc.ShouldUseStubForApex(ctx, dep) {
1182 skipModuleList[depName] = true
1183 } else {
1184 skipModuleList[apiLibrary] = true
1185 }
1186 }
1187 })
1188 }
1189
Cole Faustb6e6f992023-08-17 17:42:26 -07001190 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1191 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001192
Ivan Lozanoffee3342019-08-27 12:03:00 -07001193 ctx.VisitDirectDeps(func(dep android.Module) {
1194 depName := ctx.OtherModuleName(dep)
1195 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001196
Spandan Das604f3762023-03-16 22:51:40 +00001197 if _, exists := skipModuleList[depName]; exists {
1198 return
1199 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001200
1201 if depTag == android.DarwinUniversalVariantTag {
1202 return
1203 }
1204
Ivan Lozano0a468a42024-05-13 21:03:34 -04001205 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001206 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001207 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001208
Ivan Lozanoffee3342019-08-27 12:03:00 -07001209 switch depTag {
1210 case dylibDepTag:
1211 dylib, ok := rustDep.compiler.(libraryInterface)
1212 if !ok || !dylib.dylib() {
1213 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1214 return
1215 }
1216 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001217 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001218 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1219
Ivan Lozanoffee3342019-08-27 12:03:00 -07001220 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001221
Ivan Lozanoffee3342019-08-27 12:03:00 -07001222 rlib, ok := rustDep.compiler.(libraryInterface)
1223 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001224 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001225 return
1226 }
1227 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001228 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001229 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1230
Ivan Lozano0a468a42024-05-13 21:03:34 -04001231 // rust_ffi rlibs may export include dirs, so collect those here.
1232 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1233 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1234 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1235
Ivan Lozanoffee3342019-08-27 12:03:00 -07001236 case procMacroDepTag:
1237 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001238 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001239 // proc_macro link dirs need to be exported, so collect those here.
1240 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001241
1242 case sourceDepTag:
1243 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1244 collectIncludedProtos(mod, rustDep)
1245 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001246 }
1247
Cole Faustb6e6f992023-08-17 17:42:26 -07001248 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1249
Paul Duffind5cf92e2021-07-09 17:38:55 +01001250 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001251 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1252 // OS/Arch variant is used.
1253 var helper string
1254 if ctx.Host() {
1255 helper = "missing 'host_supported'?"
1256 } else {
1257 helper = "device module defined?"
1258 }
1259
1260 if dep.Target().Os != ctx.Os() {
1261 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1262 return
1263 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1264 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1265 return
1266 }
1267 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001268 }
1269
Ivan Lozano0a468a42024-05-13 21:03:34 -04001270 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001271 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001272 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001273 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1274 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001275 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001276 }
1277
Ivan Lozanoffee3342019-08-27 12:03:00 -07001278 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001279 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001280 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001281 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001282 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 }
1284 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001285
Ivan Lozanod106efe2023-09-21 23:30:26 -04001286 if depTag == sourceDepTag {
1287 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1288 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001289 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001290 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1291 }
1292 }
1293 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001294 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001295 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001296 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001297 if _, ok := ccDep.(*Module); !ok {
1298 if ccDep.Module().Target().Os != ctx.Os() {
1299 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1300 return
1301 }
1302 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1303 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1304 return
1305 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001306 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001307 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001308 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001309 if !ctx.Config().AllowMissingDependencies() {
1310 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1311 } else {
1312 ctx.AddMissingDependencies([]string{depName})
1313 }
1314 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001315 }
1316
Wen-yi Chu41326c12023-09-22 03:58:59 +00001317 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001318
Ivan Lozanoffee3342019-08-27 12:03:00 -07001319 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001320 switch {
1321 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001322 if cc.IsWholeStaticLib(depTag) {
1323 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1324 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001325 if mod.Binary() {
1326 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1327 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1328 // final linkage, pass the args directly to the linker to handle these cases.
1329 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1330 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001331 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001332 } else {
1333 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 -05001334 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001335 }
1336
1337 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1338 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001339 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001340 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1341
Colin Cross313aa542023-12-13 13:47:44 -08001342 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001343 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1344 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1345 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1346 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001347 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001348
1349 // Record baseLibName for snapshots.
1350 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1351
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001352 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001353 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001354 // For the shared lib dependencies, we may link to the stub variant
1355 // of the dependency depending on the context (e.g. if this
1356 // dependency crosses the APEX boundaries).
1357 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1358
1359 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1360 // object (either from stub or non-stub) to use.
1361 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001362 if !linkObject.Valid() {
1363 if !ctx.Config().AllowMissingDependencies() {
1364 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1365 } else {
1366 ctx.AddMissingDependencies([]string{depName})
1367 }
1368 return
1369 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001370 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001371
Ivan Lozanoffee3342019-08-27 12:03:00 -07001372 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001373 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001374 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1375 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1376 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1377 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001378 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001379
1380 // Record baseLibName for snapshots.
1381 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1382
Cole Faustb6e6f992023-08-17 17:42:26 -07001383 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001384 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001385 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001386 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001387 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1388 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1389 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001390 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001391 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001392 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001393 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001394 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001395 }
1396
1397 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001398 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1399 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001400 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001401 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001402 } else {
1403 switch {
1404 case depTag == cc.CrtBeginDepTag:
1405 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1406 case depTag == cc.CrtEndDepTag:
1407 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1408 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001409 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001410
1411 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001412 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001413 // These are usually genrules which don't have per-target variants.
1414 directSrcDeps = append(directSrcDeps, srcDep)
1415 }
1416 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001417 })
1418
Wen-yi Chu41326c12023-09-22 03:58:59 +00001419 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1420
1421 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001422 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001423 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001424 crateName := dep.CrateName()
1425 if alias, aliased := aliases[crateName]; aliased {
1426 crateName = alias
1427 }
1428 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001429 }
1430 var dylibDepFiles RustLibraries
1431 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001432 crateName := dep.CrateName()
1433 if alias, aliased := aliases[crateName]; aliased {
1434 crateName = alias
1435 }
1436 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001437 }
1438 var procMacroDepFiles RustLibraries
1439 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001440 crateName := dep.CrateName()
1441 if alias, aliased := aliases[crateName]; aliased {
1442 crateName = alias
1443 }
1444 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001445 }
1446
Colin Cross004bd3f2023-10-02 11:39:17 -07001447 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001448 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001449 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001450 }
1451
Colin Cross004bd3f2023-10-02 11:39:17 -07001452 var sharedLibFiles android.Paths
1453 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001454 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001455 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001456 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001457 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001458 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001459 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001460 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001461 }
1462
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001463 var srcProviderDepFiles android.Paths
1464 for _, dep := range directSrcProvidersDeps {
mrziwang0cbd3b02024-06-20 16:39:25 -07001465 srcs := android.OutputFilesForModule(ctx, dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001466 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1467 }
1468 for _, dep := range directSrcDeps {
1469 srcs := dep.Srcs()
1470 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1471 }
1472
Wen-yi Chu41326c12023-09-22 03:58:59 +00001473 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1474 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001475 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1476 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1477 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001478 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001479 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480
1481 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001482 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001483 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001484 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001485 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1486 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1487 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001488
1489 return depPaths
1490}
1491
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001492func (mod *Module) InstallInData() bool {
1493 if mod.compiler == nil {
1494 return false
1495 }
1496 return mod.compiler.inData()
1497}
1498
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001499func (mod *Module) InstallInRamdisk() bool {
1500 return mod.InRamdisk()
1501}
1502
1503func (mod *Module) InstallInVendorRamdisk() bool {
1504 return mod.InVendorRamdisk()
1505}
1506
1507func (mod *Module) InstallInRecovery() bool {
1508 return mod.InRecovery()
1509}
1510
Wen-yi Chu41326c12023-09-22 03:58:59 +00001511func linkPathFromFilePath(filepath android.Path) string {
1512 return strings.Split(filepath.String(), filepath.Base())[0]
1513}
1514
Spandan Das604f3762023-03-16 22:51:40 +00001515// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1516func (r *Module) usePublicApi() bool {
1517 return r.Device() && r.UseSdk()
1518}
1519
1520// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1521func (r *Module) useVendorApi() bool {
1522 return r.Device() && (r.InVendor() || r.InProduct())
1523}
1524
Ivan Lozanoffee3342019-08-27 12:03:00 -07001525func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1526 ctx := &depsContext{
1527 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001528 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001529
1530 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001531 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001532
Kiyoung Kim487689e2022-07-26 09:48:22 +09001533 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001534 if mod.usePublicApi() || mod.useVendorApi() {
1535 for idx, lib := range deps.SharedLibs {
1536 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1537 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001538 }
1539
Ivan Lozano1921e802021-05-20 13:39:16 -04001540 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001541 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001542 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001543
Ivan Lozano2b081132020-09-08 12:46:52 -04001544 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001545 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001546 stdLinkage = "rlib-std"
1547 }
1548
1549 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001550 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001551
Ivan Lozano2b081132020-09-08 12:46:52 -04001552 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1553 rlibDepVariations = append(rlibDepVariations,
1554 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1555 }
1556
Ivan Lozano1921e802021-05-20 13:39:16 -04001557 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001558 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001559 for _, lib := range deps.Rlibs {
1560 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001561 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001562 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001563
1564 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001565 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001566 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1567
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001568 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001569 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001570 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001571
Ivan Lozano1921e802021-05-20 13:39:16 -04001572 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001573 if deps.Rustlibs != nil {
1574 if !mod.compiler.Disabled() {
1575 for _, lib := range deps.Rustlibs {
1576 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1577 if autoDep.depTag == rlibDepTag {
1578 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001579 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1580
Ivan Lozanod106efe2023-09-21 23:30:26 -04001581 } else {
1582 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1583 // Check for the existence of the dylib deptag variant. Select it if available,
1584 // otherwise select the rlib variant.
1585 autoDepVariations := append(commonDepVariations,
1586 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001587 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001588 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1589 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001590
Ivan Lozanod106efe2023-09-21 23:30:26 -04001591 } else {
1592 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001593 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1594
Ivan Lozanod106efe2023-09-21 23:30:26 -04001595 }
1596 }
1597 }
1598 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1599 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001600 srcProviderVariations := append(commonDepVariations,
1601 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001602 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001603
Ivan Lozano0a468a42024-05-13 21:03:34 -04001604 // Only add rustlib dependencies if they're source providers themselves.
1605 // This is used to track which crate names need to be added to the source generated
1606 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001607 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001608 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001609 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001610 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001611 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001612 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001613
Ivan Lozano1921e802021-05-20 13:39:16 -04001614 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001615 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001616 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001617 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001618 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001619 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001620 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001621 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001622 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001623 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1624
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001625 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001626 }
1627 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001628
1629 for _, lib := range deps.SharedLibs {
1630 depTag := cc.SharedDepTag()
1631 name, version := cc.StubsLibNameAndVersion(lib)
1632
1633 variations := []blueprint.Variation{
1634 {Mutator: "link", Variation: "shared"},
1635 }
Spandan Das604f3762023-03-16 22:51:40 +00001636 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1637 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1638 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1639 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1640 }
1641
1642 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1643 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1644 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001645 }
1646
1647 for _, lib := range deps.WholeStaticLibs {
1648 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001649
1650 actx.AddVariationDependencies([]blueprint.Variation{
1651 {Mutator: "link", Variation: "static"},
1652 }, depTag, lib)
1653 }
1654
1655 for _, lib := range deps.StaticLibs {
1656 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001657
1658 actx.AddVariationDependencies([]blueprint.Variation{
1659 {Mutator: "link", Variation: "static"},
1660 }, depTag, lib)
1661 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001662
Zach Johnson3df4e632020-11-06 11:56:27 -08001663 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1664
Colin Cross565cafd2020-09-25 18:47:38 -07001665 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001666 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001667 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001668 }
Colin Crossfe605e12022-01-23 20:46:16 -08001669 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001670 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001671 }
1672
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001673 if mod.sourceProvider != nil {
1674 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1675 bindgen.Properties.Custom_bindgen != "" {
1676 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1677 bindgen.Properties.Custom_bindgen)
1678 }
1679 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001680
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001681 actx.AddVariationDependencies([]blueprint.Variation{
1682 {Mutator: "link", Variation: "shared"},
1683 }, dataLibDepTag, deps.DataLibs...)
1684
1685 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1686
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001687 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001688 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001689
1690 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001691}
1692
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001693func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001694 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001695 mod.beginMutator(ctx)
1696 }
1697}
1698
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001699func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1700 ctx := &baseModuleContext{
1701 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001702 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001703
1704 mod.begin(ctx)
1705}
1706
Ivan Lozanoffee3342019-08-27 12:03:00 -07001707func (mod *Module) Name() string {
1708 name := mod.ModuleBase.Name()
1709 if p, ok := mod.compiler.(interface {
1710 Name(string) string
1711 }); ok {
1712 name = p.Name(name)
1713 }
1714 return name
1715}
1716
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001717func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001718 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001719 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001720 }
1721}
1722
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001723var _ android.HostToolProvider = (*Module)(nil)
1724
1725func (mod *Module) HostToolPath() android.OptionalPath {
1726 if !mod.Host() {
1727 return android.OptionalPath{}
1728 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001729 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1730 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001731 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1732 // Even though proc-macros aren't strictly "tools", since they target the compiler
1733 // and act as compiler plugins, we treat them similarly.
1734 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001735 }
1736 return android.OptionalPath{}
1737}
1738
Jiyong Park99644e92020-11-17 22:21:02 +09001739var _ android.ApexModule = (*Module)(nil)
1740
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001741func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001742 return String(mod.Properties.Min_sdk_version)
1743}
1744
Jiyong Park45bf82e2020-12-15 22:29:02 +09001745// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001746func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001747 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001748 if minSdkVersion == "apex_inherit" {
1749 return nil
1750 }
1751 if minSdkVersion == "" {
1752 return fmt.Errorf("min_sdk_version is not specificed")
1753 }
1754
1755 // Not using nativeApiLevelFromUser because the context here is not
1756 // necessarily a native context.
1757 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1758 if err != nil {
1759 return err
1760 }
1761
1762 if ver.GreaterThan(sdkVersion) {
1763 return fmt.Errorf("newer SDK(%v)", ver)
1764 }
Jiyong Park99644e92020-11-17 22:21:02 +09001765 return nil
1766}
1767
Jiyong Park45bf82e2020-12-15 22:29:02 +09001768// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001769func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1770 depTag := ctx.OtherModuleDependencyTag(dep)
1771
1772 if ccm, ok := dep.(*cc.Module); ok {
1773 if ccm.HasStubsVariants() {
1774 if cc.IsSharedDepTag(depTag) {
1775 // dynamic dep to a stubs lib crosses APEX boundary
1776 return false
1777 }
1778 if cc.IsRuntimeDepTag(depTag) {
1779 // runtime dep to a stubs lib also crosses APEX boundary
1780 return false
1781 }
1782
1783 if cc.IsHeaderDepTag(depTag) {
1784 return false
1785 }
1786 }
1787 if mod.Static() && cc.IsSharedDepTag(depTag) {
1788 // shared_lib dependency from a static lib is considered as crossing
1789 // the APEX boundary because the dependency doesn't actually is
1790 // linked; the dependency is used only during the compilation phase.
1791 return false
1792 }
1793 }
1794
Matthew Maurer581b6d82022-09-29 16:46:25 -07001795 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001796 return false
1797 }
1798
1799 return true
1800}
1801
1802// Overrides ApexModule.IsInstallabeToApex()
1803func (mod *Module) IsInstallableToApex() bool {
1804 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001805 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001806 return true
1807 }
1808 if _, ok := mod.compiler.(*binaryDecorator); ok {
1809 return true
1810 }
1811 }
1812 return false
1813}
1814
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001815// If a library file has a "lib" prefix, extract the library name without the prefix.
1816func libNameFromFilePath(filepath android.Path) (string, bool) {
1817 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1818 if strings.HasPrefix(libName, "lib") {
1819 libName = libName[3:]
1820 return libName, true
1821 }
1822 return "", false
1823}
1824
Sasha Smundaka76acba2022-04-18 20:12:56 -07001825func kytheExtractRustFactory() android.Singleton {
1826 return &kytheExtractRustSingleton{}
1827}
1828
1829type kytheExtractRustSingleton struct {
1830}
1831
1832func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1833 var xrefTargets android.Paths
1834 ctx.VisitAllModules(func(module android.Module) {
1835 if rustModule, ok := module.(xref); ok {
1836 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1837 }
1838 })
1839 if len(xrefTargets) > 0 {
1840 ctx.Phony("xref_rust", xrefTargets...)
1841 }
1842}
1843
Jihoon Kangf78a8902022-09-01 22:47:07 +00001844func (c *Module) Partition() string {
1845 return ""
1846}
1847
Ivan Lozanoffee3342019-08-27 12:03:00 -07001848var Bool = proptools.Bool
1849var BoolDefault = proptools.BoolDefault
1850var String = proptools.String
1851var StringPtr = proptools.StringPtr