blob: 5790dd6e404a60a3fec412b6c80baad990d349ca [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
348func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500349 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700350 return false
351}
352
Ivan Lozanof9e21722020-12-02 09:00:51 -0500353func (mod *Module) IsVndkExt() bool {
354 return false
355}
356
Ivan Lozanod7586b62021-04-01 09:49:36 -0400357func (mod *Module) IsVndkSp() bool {
358 return false
359}
360
Ivan Lozanof1868af2022-04-12 13:08:36 -0400361func (mod *Module) IsVndkPrebuiltLibrary() bool {
362 // Rust modules do not provide VNDK prebuilts
363 return false
364}
365
366func (mod *Module) IsVendorPublicLibrary() bool {
367 return mod.VendorProperties.IsVendorPublicLibrary
368}
369
370func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
371 // Rust modules to not provide Sdk variants
372 return false
373}
374
Colin Cross127bb8b2020-12-16 16:46:01 -0800375func (c *Module) IsVndkPrivate() bool {
376 return false
377}
378
379func (c *Module) IsLlndk() bool {
380 return false
381}
382
383func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500384 return false
385}
386
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400387func (mod *Module) KernelHeadersDecorator() bool {
388 return false
389}
390
Colin Cross1f3f1302021-04-26 18:37:44 -0700391func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400392 return false
393}
394
Colin Cross5271fea2021-04-27 13:06:04 -0700395func (m *Module) NeedsVendorPublicLibraryVariants() bool {
396 return false
397}
398
Ivan Lozanod7586b62021-04-01 09:49:36 -0400399func (mod *Module) HasLlndkStubs() bool {
400 return false
401}
402
403func (mod *Module) StubsVersion() string {
404 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
405}
406
Ivan Lozano52767be2019-10-18 14:49:46 -0700407func (mod *Module) SdkVersion() string {
408 return ""
409}
410
Colin Crossc511bc52020-04-07 16:50:32 +0000411func (mod *Module) AlwaysSdk() bool {
412 return false
413}
414
Jiyong Park2286afd2020-06-16 21:58:53 +0900415func (mod *Module) IsSdkVariant() bool {
416 return false
417}
418
Colin Cross1348ce32020-10-01 13:37:16 -0700419func (mod *Module) SplitPerApiLevel() bool {
420 return false
421}
422
Sasha Smundaka76acba2022-04-18 20:12:56 -0700423func (mod *Module) XrefRustFiles() android.Paths {
424 return mod.kytheFiles
425}
426
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400428 Dylibs []string
429 Rlibs []string
430 Rustlibs []string
431 Stdlibs []string
432 ProcMacros []string
433 SharedLibs []string
434 StaticLibs []string
435 WholeStaticLibs []string
436 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400438 // Used for data dependencies adjacent to tests
439 DataLibs []string
440 DataBins []string
441
Colin Crossfe605e12022-01-23 20:46:16 -0800442 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443}
444
445type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700446 DyLibs RustLibraries
447 RLibs RustLibraries
448 SharedLibs android.Paths
449 SharedLibDeps android.Paths
450 StaticLibs android.Paths
451 ProcMacros RustLibraries
452 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500453
454 // depFlags and depLinkFlags are rustc and linker (clang) flags.
455 depFlags []string
456 depLinkFlags []string
457
458 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
459 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000460 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700461 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700462
Ivan Lozano0a468a42024-05-13 21:03:34 -0400463 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
464 // cc_library_static dependants of rlibs.
465 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
466 exportedLinkDirs []string
467
Ivan Lozano45901ed2020-07-24 16:05:01 -0400468 // Used by bindgen modules which call clang
469 depClangFlags []string
470 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400471 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400472 depSystemIncludePaths android.Paths
473
Colin Crossfe605e12022-01-23 20:46:16 -0800474 CrtBegin android.Paths
475 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700476
477 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500478 SrcDeps android.Paths
479 srcProviderFiles android.Paths
Ivan Lozano0a468a42024-05-13 21:03:34 -0400480
481 // Used by Generated Libraries
482 depExportedRlibs []cc.RustRlibDep
Ivan Lozanoffee3342019-08-27 12:03:00 -0700483}
484
485type RustLibraries []RustLibrary
486
487type RustLibrary struct {
488 Path android.Path
489 CrateName string
490}
491
Matthew Maurerbb3add12020-06-25 09:34:12 -0700492type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000493 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700494 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700495}
496
Sasha Smundaka76acba2022-04-18 20:12:56 -0700497type xref interface {
498 XrefRustFiles() android.Paths
499}
500
Matthew Maurerbb3add12020-06-25 09:34:12 -0700501type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000502 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700503 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700504}
505
Wen-yi Chu41326c12023-09-22 03:58:59 +0000506func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
507 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700508}
509
Colin Cross004bd3f2023-10-02 11:39:17 -0700510func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
511 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400512}
513
Colin Cross0de8a1e2020-09-18 14:15:30 -0700514func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800515 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700516 LinkDirs: flagExporter.linkDirs,
517 LinkObjects: flagExporter.linkObjects,
518 })
519}
520
Matthew Maurerbb3add12020-06-25 09:34:12 -0700521var _ exportedFlagsProducer = (*flagExporter)(nil)
522
523func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700524 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700525}
526
Colin Cross0de8a1e2020-09-18 14:15:30 -0700527type FlagExporterInfo struct {
528 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000529 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700530 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700531}
532
Colin Crossbc7d76c2023-12-12 16:39:03 -0800533var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700534
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400535func (mod *Module) isCoverageVariant() bool {
536 return mod.coverage.Properties.IsCoverageVariant
537}
538
539var _ cc.Coverage = (*Module)(nil)
540
Colin Crossf5f4ad32024-01-19 15:41:48 -0800541func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400542 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
543}
544
Ivan Lozanod7586b62021-04-01 09:49:36 -0400545func (mod *Module) VndkVersion() string {
546 return mod.Properties.VndkVersion
547}
548
Ivan Lozano0a468a42024-05-13 21:03:34 -0400549func (mod *Module) ExportedCrateLinkDirs() []string {
550 return mod.exportedLinkDirs
551}
552
Ivan Lozanod7586b62021-04-01 09:49:36 -0400553func (mod *Module) PreventInstall() bool {
554 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400555}
556
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400557func (mod *Module) MarkAsCoverageVariant(coverage bool) {
558 mod.coverage.Properties.IsCoverageVariant = coverage
559}
560
561func (mod *Module) EnableCoverageIfNeeded() {
562 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563}
564
565func defaultsFactory() android.Module {
566 return DefaultsFactory()
567}
568
569type Defaults struct {
570 android.ModuleBase
571 android.DefaultsModuleBase
572}
573
574func DefaultsFactory(props ...interface{}) android.Module {
575 module := &Defaults{}
576
577 module.AddProperties(props...)
578 module.AddProperties(
579 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800580 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500581 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100582 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400583 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700584 &BaseCompilerProperties{},
585 &BinaryCompilerProperties{},
586 &LibraryCompilerProperties{},
587 &ProcMacroCompilerProperties{},
588 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400589 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700590 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400591 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400592 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200593 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500594 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000595 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596 )
597
598 android.InitDefaultsModule(module)
599 return module
600}
601
602func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700603 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604}
605
Ivan Lozano183a3212019-10-18 14:18:45 -0700606func (mod *Module) CcLibrary() bool {
607 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500608 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700609 return true
610 }
611 }
612 return false
613}
614
615func (mod *Module) CcLibraryInterface() bool {
616 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400617 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
618 // VariantIs{Static,Shared} is set.
619 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700620 return true
621 }
622 }
623 return false
624}
625
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400626func (mod *Module) RustLibraryInterface() bool {
627 if mod.compiler != nil {
628 if _, ok := mod.compiler.(libraryInterface); ok {
629 return true
630 }
631 }
632 return false
633}
634
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500635func (mod *Module) IsFuzzModule() bool {
636 if _, ok := mod.compiler.(*fuzzDecorator); ok {
637 return true
638 }
639 return false
640}
641
642func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
643 return mod.FuzzModule
644}
645
646func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
647 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
648 return fuzzer.fuzzPackagedModule
649 }
650 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
651}
652
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000653func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500654 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
655 return fuzzer.sharedLibraries
656 }
657 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
658}
659
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400660func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400661 if mod.compiler != nil {
662 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400663 }
664 return nil
665}
666
Ivan Lozano183a3212019-10-18 14:18:45 -0700667func (mod *Module) SetStatic() {
668 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700669 if library, ok := mod.compiler.(libraryInterface); ok {
670 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700671 return
672 }
673 }
674 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
675}
676
677func (mod *Module) SetShared() {
678 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700679 if library, ok := mod.compiler.(libraryInterface); ok {
680 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700681 return
682 }
683 }
684 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
685}
686
Ivan Lozano183a3212019-10-18 14:18:45 -0700687func (mod *Module) BuildStaticVariant() bool {
688 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700689 if library, ok := mod.compiler.(libraryInterface); ok {
690 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700691 }
692 }
693 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
694}
695
696func (mod *Module) BuildSharedVariant() bool {
697 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700698 if library, ok := mod.compiler.(libraryInterface); ok {
699 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700700 }
701 }
702 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
703}
704
Ivan Lozano183a3212019-10-18 14:18:45 -0700705func (mod *Module) Module() android.Module {
706 return mod
707}
708
Ivan Lozano183a3212019-10-18 14:18:45 -0700709func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400710 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700711}
712
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400713func (mod *Module) CoverageFiles() android.Paths {
714 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800715 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400716 }
717 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
718}
719
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400720// Rust does not produce gcno files, and therefore does not produce a coverage archive.
721func (mod *Module) CoverageOutputFile() android.OptionalPath {
722 return android.OptionalPath{}
723}
724
725func (mod *Module) IsNdk(config android.Config) bool {
726 return false
727}
728
729func (mod *Module) IsStubs() bool {
730 return false
731}
732
Jiyong Park459feca2020-12-15 11:02:21 +0900733func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900734 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900735 return false
736 }
737
Jiyong Park459feca2020-12-15 11:02:21 +0900738 // The apex variant is not installable because it is included in the APEX and won't appear
739 // in the system partition as a standalone file.
740 if !apexInfo.IsForPlatform() {
741 return false
742 }
743
Jiyong Parke54f07e2021-04-07 15:08:04 +0900744 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900745}
746
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500747func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800748 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
749 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500750}
751
Ivan Lozano183a3212019-10-18 14:18:45 -0700752var _ cc.LinkableInterface = (*Module)(nil)
753
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754func (mod *Module) Init() android.Module {
755 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500756 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757
Yi Kong46c6e592022-01-20 22:55:00 +0800758 if mod.afdo != nil {
759 mod.AddProperties(mod.afdo.props()...)
760 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 if mod.compiler != nil {
762 mod.AddProperties(mod.compiler.compilerProps()...)
763 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400764 if mod.coverage != nil {
765 mod.AddProperties(mod.coverage.props()...)
766 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200767 if mod.clippy != nil {
768 mod.AddProperties(mod.clippy.props()...)
769 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400770 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700771 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400772 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500773 if mod.sanitize != nil {
774 mod.AddProperties(mod.sanitize.props()...)
775 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400776
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900778 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779
780 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781 return mod
782}
783
784func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
785 return &Module{
786 hod: hod,
787 multilib: multilib,
788 }
789}
790func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
791 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800792 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400793 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200794 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500795 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 return module
797}
798
799type ModuleContext interface {
800 android.ModuleContext
801 ModuleContextIntf
802}
803
804type BaseModuleContext interface {
805 android.BaseModuleContext
806 ModuleContextIntf
807}
808
809type DepsContext interface {
810 android.BottomUpMutatorContext
811 ModuleContextIntf
812}
813
814type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200815 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817}
818
819type depsContext struct {
820 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821}
822
823type moduleContext struct {
824 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825}
826
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200827type baseModuleContext struct {
828 android.BaseModuleContext
829}
830
831func (ctx *moduleContext) RustModule() *Module {
832 return ctx.Module().(*Module)
833}
834
835func (ctx *moduleContext) toolchain() config.Toolchain {
836 return ctx.RustModule().toolchain(ctx)
837}
838
839func (ctx *depsContext) RustModule() *Module {
840 return ctx.Module().(*Module)
841}
842
843func (ctx *depsContext) toolchain() config.Toolchain {
844 return ctx.RustModule().toolchain(ctx)
845}
846
847func (ctx *baseModuleContext) RustModule() *Module {
848 return ctx.Module().(*Module)
849}
850
851func (ctx *baseModuleContext) toolchain() config.Toolchain {
852 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400853}
854
855func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700856 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
857 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
858 return false
859 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400860 return mod.compiler != nil && mod.compiler.nativeCoverage()
861}
862
Ivan Lozanod7586b62021-04-01 09:49:36 -0400863func (mod *Module) EverInstallable() bool {
864 return mod.compiler != nil &&
865 // Check to see whether the module is actually ever installable.
866 mod.compiler.everInstallable()
867}
868
869func (mod *Module) Installable() *bool {
870 return mod.Properties.Installable
871}
872
Ivan Lozano872d5792022-03-23 17:31:39 -0400873func (mod *Module) ProcMacro() bool {
874 if pm, ok := mod.compiler.(procMacroInterface); ok {
875 return pm.ProcMacro()
876 }
877 return false
878}
879
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
881 if mod.cachedToolchain == nil {
882 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
883 }
884 return mod.cachedToolchain
885}
886
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200887func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
888 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
889}
890
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
892}
893
894func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
895 ctx := &moduleContext{
896 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898
Colin Crossff694a82023-12-13 15:54:49 -0800899 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900900 if !apexInfo.IsForPlatform() {
901 mod.hideApexVariantFromMake = true
902 }
903
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500905 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
906
Ivan Lozanof1868af2022-04-12 13:08:36 -0400907 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700908
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909 if !toolchain.Supported() {
910 // This toolchain's unsupported, there's nothing to do for this mod.
911 return
912 }
913
914 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -0400915 // Export linkDirs for CC rust generatedlibs
916 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
917 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
918
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919 flags := Flags{
920 Toolchain: toolchain,
921 }
922
Ivan Lozano67eada32021-09-23 11:50:33 -0400923 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800924 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500925 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800926 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927 if mod.compiler != nil {
928 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400929 flags = mod.compiler.cfgFlags(ctx, flags)
930 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400931 }
932 if mod.coverage != nil {
933 flags, deps = mod.coverage.flags(ctx, flags, deps)
934 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200935 if mod.clippy != nil {
936 flags, deps = mod.clippy.flags(ctx, flags, deps)
937 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500938 if mod.sanitize != nil {
939 flags, deps = mod.sanitize.flags(ctx, flags, deps)
940 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400941
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200942 // SourceProvider needs to call GenerateSource() before compiler calls
943 // compile() so it can provide the source. A SourceProvider has
944 // multiple variants (e.g. source, rlib, dylib). Only the "source"
945 // variant is responsible for effectively generating the source. The
946 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400947 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200948 if mod.compiler.(libraryInterface).source() {
949 mod.sourceProvider.GenerateSource(ctx, deps)
950 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
951 } else {
952 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
953 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700954 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200955 }
Colin Cross40213022023-12-13 15:19:49 -0800956 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400957 }
958
959 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100960 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700961 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400962 if ctx.Failed() {
963 return
964 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700965 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
966 if buildOutput.kytheFile != nil {
967 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
968 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400969 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900970
Dan Albert06feee92021-03-19 15:06:02 -0700971 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000972 if mod.docTimestampFile.Valid() {
973 ctx.CheckbuildFile(mod.docTimestampFile.Path())
974 }
Dan Albert06feee92021-03-19 15:06:02 -0700975
Colin Crossff694a82023-12-13 15:54:49 -0800976 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400977 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900978 // If the module has been specifically configure to not be installed then
979 // hide from make as otherwise it will break when running inside make as the
980 // output path to install will not be specified. Not all uninstallable
981 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400982 // side dependencies. In particular, proc-macros need to be captured in the
983 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900984 mod.HideFromMake()
985 } else if !mod.installable(apexInfo) {
986 mod.SkipInstall()
987 }
988
989 // Still call install though, the installs will be stored as PackageSpecs to allow
990 // using the outputs in a genrule.
991 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200992 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900993 if ctx.Failed() {
994 return
995 }
Ivan Lozano0a468a42024-05-13 21:03:34 -0400996 // Export your own directory as a linkDir
997 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
998
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400999 }
Chris Wailes74be7642021-07-22 16:20:28 -07001000
1001 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001003 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -08001004 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001005 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001006}
1007
1008func (mod *Module) deps(ctx DepsContext) Deps {
1009 deps := Deps{}
1010
1011 if mod.compiler != nil {
1012 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001013 }
1014 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001015 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001016 }
1017
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001018 if mod.coverage != nil {
1019 deps = mod.coverage.deps(ctx, deps)
1020 }
1021
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001022 if mod.sanitize != nil {
1023 deps = mod.sanitize.deps(ctx, deps)
1024 }
1025
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1027 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001028 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1030 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1031 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001032 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001033 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034 return deps
1035
1036}
1037
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038type dependencyTag struct {
1039 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001040 name string
1041 library bool
1042 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001043 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044}
1045
Jiyong Park65b62242020-11-25 12:44:59 +09001046// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1047// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1048func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001049 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001050}
1051
1052var _ android.InstallNeededDependencyTag = dependencyTag{}
1053
Colin Cross65cb3142021-12-10 23:05:02 +00001054func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1055 if d.library && d.dynamic {
1056 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1057 }
1058 return nil
1059}
1060
Yu Liuc8884602024-03-15 18:48:38 +00001061func (d dependencyTag) PropagateAconfigValidation() bool {
1062 return d == rlibDepTag || d == sourceDepTag
1063}
1064
1065var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1066
Colin Cross65cb3142021-12-10 23:05:02 +00001067var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1068
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001070 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1071 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001072 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001073 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001074 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001075 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001076 dataLibDepTag = dependencyTag{name: "data lib"}
1077 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078)
1079
Jiyong Park99644e92020-11-17 22:21:02 +09001080func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1081 tag, ok := depTag.(dependencyTag)
1082 return ok && tag == dylibDepTag
1083}
1084
Jiyong Park94e22fd2021-04-08 18:19:15 +09001085func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1086 tag, ok := depTag.(dependencyTag)
1087 return ok && tag == rlibDepTag
1088}
1089
Matthew Maurer0f003b12020-06-29 14:34:06 -07001090type autoDep struct {
1091 variation string
1092 depTag dependencyTag
1093}
1094
1095var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001096 rlibVariation = "rlib"
1097 dylibVariation = "dylib"
1098 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1099 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001100)
1101
1102type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001103 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001104}
1105
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001106func (mod *Module) begin(ctx BaseModuleContext) {
1107 if mod.coverage != nil {
1108 mod.coverage.begin(ctx)
1109 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001110 if mod.sanitize != nil {
1111 mod.sanitize.begin(ctx)
1112 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001113}
1114
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001115func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001116 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001117 return p.prebuilt()
1118 }
1119 return nil
1120}
1121
Kiyoung Kim37693d02024-04-04 09:56:15 +09001122func (mod *Module) Symlinks() []string {
1123 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1124 return nil
1125}
1126
Justin Yun24b246a2023-03-16 10:36:16 +09001127func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1128 if rustDep, ok := dep.(*Module); ok {
1129 // Use base module name for snapshots when exporting to Makefile.
1130 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1131 baseName := rustDep.BaseModuleName()
1132 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1133 }
1134 }
1135 return cc.MakeLibName(ctx, c, dep, depName)
1136}
1137
Ivan Lozanod106efe2023-09-21 23:30:26 -04001138func collectIncludedProtos(mod *Module, dep *Module) {
1139 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1140 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1141 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1142 }
1143 }
1144}
Andrew Walbran52533232024-03-19 11:36:04 +00001145
Ivan Lozanoffee3342019-08-27 12:03:00 -07001146func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1147 var depPaths PathDeps
1148
1149 directRlibDeps := []*Module{}
1150 directDylibDeps := []*Module{}
1151 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001152 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001153 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001154 directSrcProvidersDeps := []*Module{}
1155 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001156
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001157 // For the dependency from platform to apex, use the latest stubs
1158 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001159 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001160 if !apexInfo.IsForPlatform() {
1161 mod.apexSdkVersion = apexInfo.MinSdkVersion
1162 }
1163
1164 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1165 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1166 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1167 // (b/144430859)
1168 mod.apexSdkVersion = android.FutureApiLevel
1169 }
1170
Spandan Das604f3762023-03-16 22:51:40 +00001171 skipModuleList := map[string]bool{}
1172
1173 var apiImportInfo multitree.ApiImportInfo
1174 hasApiImportInfo := false
1175
1176 ctx.VisitDirectDeps(func(dep android.Module) {
1177 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001178 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001179 hasApiImportInfo = true
1180 }
1181 })
1182
1183 if hasApiImportInfo {
1184 targetStubModuleList := map[string]string{}
1185 targetOrigModuleList := map[string]string{}
1186
1187 // Search for dependency which both original module and API imported library with APEX stub exists
1188 ctx.VisitDirectDeps(func(dep android.Module) {
1189 depName := ctx.OtherModuleName(dep)
1190 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1191 targetStubModuleList[apiLibrary] = depName
1192 }
1193 })
1194 ctx.VisitDirectDeps(func(dep android.Module) {
1195 depName := ctx.OtherModuleName(dep)
1196 if origLibrary, ok := targetStubModuleList[depName]; ok {
1197 targetOrigModuleList[origLibrary] = depName
1198 }
1199 })
1200
1201 // Decide which library should be used between original and API imported library
1202 ctx.VisitDirectDeps(func(dep android.Module) {
1203 depName := ctx.OtherModuleName(dep)
1204 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1205 if cc.ShouldUseStubForApex(ctx, dep) {
1206 skipModuleList[depName] = true
1207 } else {
1208 skipModuleList[apiLibrary] = true
1209 }
1210 }
1211 })
1212 }
1213
Cole Faustb6e6f992023-08-17 17:42:26 -07001214 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1215 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001216
Ivan Lozanoffee3342019-08-27 12:03:00 -07001217 ctx.VisitDirectDeps(func(dep android.Module) {
1218 depName := ctx.OtherModuleName(dep)
1219 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001220
Spandan Das604f3762023-03-16 22:51:40 +00001221 if _, exists := skipModuleList[depName]; exists {
1222 return
1223 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001224
1225 if depTag == android.DarwinUniversalVariantTag {
1226 return
1227 }
1228
Ivan Lozano0a468a42024-05-13 21:03:34 -04001229 if rustDep, ok := dep.(*Module); ok && !rustDep.Static() && !rustDep.Shared() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001231 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001232
Ivan Lozanoffee3342019-08-27 12:03:00 -07001233 switch depTag {
1234 case dylibDepTag:
1235 dylib, ok := rustDep.compiler.(libraryInterface)
1236 if !ok || !dylib.dylib() {
1237 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1238 return
1239 }
1240 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001241 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001242 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1243
Ivan Lozanoffee3342019-08-27 12:03:00 -07001244 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001245
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 rlib, ok := rustDep.compiler.(libraryInterface)
1247 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001248 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001249 return
1250 }
1251 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001252 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001253 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1254
Ivan Lozano0a468a42024-05-13 21:03:34 -04001255 // rust_ffi rlibs may export include dirs, so collect those here.
1256 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1257 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1258 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
1259
Ivan Lozanoffee3342019-08-27 12:03:00 -07001260 case procMacroDepTag:
1261 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001262 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001263 // proc_macro link dirs need to be exported, so collect those here.
1264 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(rustDep.OutputFile().Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001265
1266 case sourceDepTag:
1267 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1268 collectIncludedProtos(mod, rustDep)
1269 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001270 }
1271
Cole Faustb6e6f992023-08-17 17:42:26 -07001272 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1273
Paul Duffind5cf92e2021-07-09 17:38:55 +01001274 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001275 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1276 // OS/Arch variant is used.
1277 var helper string
1278 if ctx.Host() {
1279 helper = "missing 'host_supported'?"
1280 } else {
1281 helper = "device module defined?"
1282 }
1283
1284 if dep.Target().Os != ctx.Os() {
1285 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1286 return
1287 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1288 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1289 return
1290 }
1291 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 }
1293
Ivan Lozano0a468a42024-05-13 21:03:34 -04001294 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001295 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001296 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001297 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1298 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001299 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001300 }
1301
Ivan Lozanoffee3342019-08-27 12:03:00 -07001302 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001303 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001304 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001305 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001306 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001307 }
1308 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001309
Ivan Lozanod106efe2023-09-21 23:30:26 -04001310 if depTag == sourceDepTag {
1311 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1312 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001313 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001314 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1315 }
1316 }
1317 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001318 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001319 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001320 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001321 if _, ok := ccDep.(*Module); !ok {
1322 if ccDep.Module().Target().Os != ctx.Os() {
1323 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1324 return
1325 }
1326 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1327 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1328 return
1329 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001330 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001331 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001332 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001333 if !ctx.Config().AllowMissingDependencies() {
1334 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1335 } else {
1336 ctx.AddMissingDependencies([]string{depName})
1337 }
1338 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001339 }
1340
Wen-yi Chu41326c12023-09-22 03:58:59 +00001341 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001342
Ivan Lozanoffee3342019-08-27 12:03:00 -07001343 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001344 switch {
1345 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001346 if cc.IsWholeStaticLib(depTag) {
1347 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1348 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001349 if mod.Binary() {
1350 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1351 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1352 // final linkage, pass the args directly to the linker to handle these cases.
1353 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1354 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001355 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001356 } else {
1357 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 -05001358 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001359 }
1360
1361 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1362 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001363 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001364 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1365
Colin Cross313aa542023-12-13 13:47:44 -08001366 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001367 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1368 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1369 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1370 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001371 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001372
1373 // Record baseLibName for snapshots.
1374 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1375
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001376 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001377 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001378 // For the shared lib dependencies, we may link to the stub variant
1379 // of the dependency depending on the context (e.g. if this
1380 // dependency crosses the APEX boundaries).
1381 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1382
1383 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1384 // object (either from stub or non-stub) to use.
1385 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001386 if !linkObject.Valid() {
1387 if !ctx.Config().AllowMissingDependencies() {
1388 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1389 } else {
1390 ctx.AddMissingDependencies([]string{depName})
1391 }
1392 return
1393 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001394 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001395
Ivan Lozanoffee3342019-08-27 12:03:00 -07001396 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001397 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001398 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1399 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1400 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1401 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001402 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001403
1404 // Record baseLibName for snapshots.
1405 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1406
Cole Faustb6e6f992023-08-17 17:42:26 -07001407 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001408 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001409 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001410 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001411 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1412 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1413 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001414 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001415 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001416 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001417 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001418 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001419 }
1420
1421 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001422 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1423 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001424 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001425 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001426 } else {
1427 switch {
1428 case depTag == cc.CrtBeginDepTag:
1429 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1430 case depTag == cc.CrtEndDepTag:
1431 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1432 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001433 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001434
1435 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001436 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001437 // These are usually genrules which don't have per-target variants.
1438 directSrcDeps = append(directSrcDeps, srcDep)
1439 }
1440 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001441 })
1442
Wen-yi Chu41326c12023-09-22 03:58:59 +00001443 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1444
1445 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001446 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001447 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001448 crateName := dep.CrateName()
1449 if alias, aliased := aliases[crateName]; aliased {
1450 crateName = alias
1451 }
1452 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001453 }
1454 var dylibDepFiles RustLibraries
1455 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001456 crateName := dep.CrateName()
1457 if alias, aliased := aliases[crateName]; aliased {
1458 crateName = alias
1459 }
1460 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001461 }
1462 var procMacroDepFiles RustLibraries
1463 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001464 crateName := dep.CrateName()
1465 if alias, aliased := aliases[crateName]; aliased {
1466 crateName = alias
1467 }
1468 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001469 }
1470
Colin Cross004bd3f2023-10-02 11:39:17 -07001471 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001473 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001474 }
1475
Colin Cross004bd3f2023-10-02 11:39:17 -07001476 var sharedLibFiles android.Paths
1477 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001478 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001479 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001480 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001481 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001482 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001483 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001484 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001485 }
1486
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001487 var srcProviderDepFiles android.Paths
1488 for _, dep := range directSrcProvidersDeps {
1489 srcs, _ := dep.OutputFiles("")
1490 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1491 }
1492 for _, dep := range directSrcDeps {
1493 srcs := dep.Srcs()
1494 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1495 }
1496
Wen-yi Chu41326c12023-09-22 03:58:59 +00001497 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1498 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001499 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1500 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1501 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001502 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001503 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001504
1505 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001506 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001507 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001508 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001509 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1510 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1511 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001512
1513 return depPaths
1514}
1515
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001516func (mod *Module) InstallInData() bool {
1517 if mod.compiler == nil {
1518 return false
1519 }
1520 return mod.compiler.inData()
1521}
1522
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001523func (mod *Module) InstallInRamdisk() bool {
1524 return mod.InRamdisk()
1525}
1526
1527func (mod *Module) InstallInVendorRamdisk() bool {
1528 return mod.InVendorRamdisk()
1529}
1530
1531func (mod *Module) InstallInRecovery() bool {
1532 return mod.InRecovery()
1533}
1534
Wen-yi Chu41326c12023-09-22 03:58:59 +00001535func linkPathFromFilePath(filepath android.Path) string {
1536 return strings.Split(filepath.String(), filepath.Base())[0]
1537}
1538
Spandan Das604f3762023-03-16 22:51:40 +00001539// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1540func (r *Module) usePublicApi() bool {
1541 return r.Device() && r.UseSdk()
1542}
1543
1544// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1545func (r *Module) useVendorApi() bool {
1546 return r.Device() && (r.InVendor() || r.InProduct())
1547}
1548
Ivan Lozanoffee3342019-08-27 12:03:00 -07001549func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1550 ctx := &depsContext{
1551 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001552 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001553
1554 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001555 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001556
Kiyoung Kim487689e2022-07-26 09:48:22 +09001557 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001558 if mod.usePublicApi() || mod.useVendorApi() {
1559 for idx, lib := range deps.SharedLibs {
1560 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1561 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001562 }
1563
Ivan Lozano1921e802021-05-20 13:39:16 -04001564 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001565 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001566 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001567
Ivan Lozano2b081132020-09-08 12:46:52 -04001568 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001569 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001570 stdLinkage = "rlib-std"
1571 }
1572
1573 rlibDepVariations := commonDepVariations
Ivan Lozano0a468a42024-05-13 21:03:34 -04001574 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozano1921e802021-05-20 13:39:16 -04001575
Ivan Lozano2b081132020-09-08 12:46:52 -04001576 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1577 rlibDepVariations = append(rlibDepVariations,
1578 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1579 }
1580
Ivan Lozano1921e802021-05-20 13:39:16 -04001581 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001582 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001583 for _, lib := range deps.Rlibs {
1584 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001585 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001586 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001587
1588 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001589 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001590 dylibDepVariations = append(dylibDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
1591
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001592 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001593 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001594 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001595
Ivan Lozano1921e802021-05-20 13:39:16 -04001596 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001597 if deps.Rustlibs != nil {
1598 if !mod.compiler.Disabled() {
1599 for _, lib := range deps.Rustlibs {
1600 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1601 if autoDep.depTag == rlibDepTag {
1602 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001603 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1604
Ivan Lozanod106efe2023-09-21 23:30:26 -04001605 } else {
1606 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1607 // Check for the existence of the dylib deptag variant. Select it if available,
1608 // otherwise select the rlib variant.
1609 autoDepVariations := append(commonDepVariations,
1610 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001611 autoDepVariations = append(autoDepVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001612 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1613 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001614
Ivan Lozanod106efe2023-09-21 23:30:26 -04001615 } else {
1616 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001617 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1618
Ivan Lozanod106efe2023-09-21 23:30:26 -04001619 }
1620 }
1621 }
1622 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1623 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001624 srcProviderVariations := append(commonDepVariations,
1625 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001626 srcProviderVariations = append(srcProviderVariations, blueprint.Variation{Mutator: "link", Variation: ""})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001627
Ivan Lozano0a468a42024-05-13 21:03:34 -04001628 // Only add rustlib dependencies if they're source providers themselves.
1629 // This is used to track which crate names need to be added to the source generated
1630 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001631 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001632 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001633 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001634 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001635 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001636 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001637
Ivan Lozano1921e802021-05-20 13:39:16 -04001638 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001639 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001640 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001641 for _, lib := range deps.Stdlibs {
Ivan Lozano0a468a42024-05-13 21:03:34 -04001642 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}, {Mutator: "link", Variation: ""}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001643 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001644 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001645 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001646 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001647 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1648
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001649 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001650 }
1651 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001652
1653 for _, lib := range deps.SharedLibs {
1654 depTag := cc.SharedDepTag()
1655 name, version := cc.StubsLibNameAndVersion(lib)
1656
1657 variations := []blueprint.Variation{
1658 {Mutator: "link", Variation: "shared"},
1659 }
Spandan Das604f3762023-03-16 22:51:40 +00001660 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1661 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1662 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1663 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1664 }
1665
1666 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1667 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1668 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001669 }
1670
1671 for _, lib := range deps.WholeStaticLibs {
1672 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001673
1674 actx.AddVariationDependencies([]blueprint.Variation{
1675 {Mutator: "link", Variation: "static"},
1676 }, depTag, lib)
1677 }
1678
1679 for _, lib := range deps.StaticLibs {
1680 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001681
1682 actx.AddVariationDependencies([]blueprint.Variation{
1683 {Mutator: "link", Variation: "static"},
1684 }, depTag, lib)
1685 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001686
Zach Johnson3df4e632020-11-06 11:56:27 -08001687 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1688
Colin Cross565cafd2020-09-25 18:47:38 -07001689 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001690 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001691 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001692 }
Colin Crossfe605e12022-01-23 20:46:16 -08001693 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001694 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001695 }
1696
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001697 if mod.sourceProvider != nil {
1698 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1699 bindgen.Properties.Custom_bindgen != "" {
1700 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1701 bindgen.Properties.Custom_bindgen)
1702 }
1703 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001704
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001705 actx.AddVariationDependencies([]blueprint.Variation{
1706 {Mutator: "link", Variation: "shared"},
1707 }, dataLibDepTag, deps.DataLibs...)
1708
1709 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1710
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001711 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001712 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001713
1714 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001715}
1716
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001717func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001718 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001719 mod.beginMutator(ctx)
1720 }
1721}
1722
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001723func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1724 ctx := &baseModuleContext{
1725 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001726 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001727
1728 mod.begin(ctx)
1729}
1730
Ivan Lozanoffee3342019-08-27 12:03:00 -07001731func (mod *Module) Name() string {
1732 name := mod.ModuleBase.Name()
1733 if p, ok := mod.compiler.(interface {
1734 Name(string) string
1735 }); ok {
1736 name = p.Name(name)
1737 }
1738 return name
1739}
1740
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001741func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001742 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001743 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001744 }
1745}
1746
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001747var _ android.HostToolProvider = (*Module)(nil)
1748
1749func (mod *Module) HostToolPath() android.OptionalPath {
1750 if !mod.Host() {
1751 return android.OptionalPath{}
1752 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001753 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1754 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001755 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1756 // Even though proc-macros aren't strictly "tools", since they target the compiler
1757 // and act as compiler plugins, we treat them similarly.
1758 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001759 }
1760 return android.OptionalPath{}
1761}
1762
Jiyong Park99644e92020-11-17 22:21:02 +09001763var _ android.ApexModule = (*Module)(nil)
1764
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001765func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001766 return String(mod.Properties.Min_sdk_version)
1767}
1768
Jiyong Park45bf82e2020-12-15 22:29:02 +09001769// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001770func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001771 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001772 if minSdkVersion == "apex_inherit" {
1773 return nil
1774 }
1775 if minSdkVersion == "" {
1776 return fmt.Errorf("min_sdk_version is not specificed")
1777 }
1778
1779 // Not using nativeApiLevelFromUser because the context here is not
1780 // necessarily a native context.
1781 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1782 if err != nil {
1783 return err
1784 }
1785
1786 if ver.GreaterThan(sdkVersion) {
1787 return fmt.Errorf("newer SDK(%v)", ver)
1788 }
Jiyong Park99644e92020-11-17 22:21:02 +09001789 return nil
1790}
1791
Jiyong Park45bf82e2020-12-15 22:29:02 +09001792// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001793func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1794 depTag := ctx.OtherModuleDependencyTag(dep)
1795
1796 if ccm, ok := dep.(*cc.Module); ok {
1797 if ccm.HasStubsVariants() {
1798 if cc.IsSharedDepTag(depTag) {
1799 // dynamic dep to a stubs lib crosses APEX boundary
1800 return false
1801 }
1802 if cc.IsRuntimeDepTag(depTag) {
1803 // runtime dep to a stubs lib also crosses APEX boundary
1804 return false
1805 }
1806
1807 if cc.IsHeaderDepTag(depTag) {
1808 return false
1809 }
1810 }
1811 if mod.Static() && cc.IsSharedDepTag(depTag) {
1812 // shared_lib dependency from a static lib is considered as crossing
1813 // the APEX boundary because the dependency doesn't actually is
1814 // linked; the dependency is used only during the compilation phase.
1815 return false
1816 }
1817 }
1818
Matthew Maurer581b6d82022-09-29 16:46:25 -07001819 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001820 return false
1821 }
1822
1823 return true
1824}
1825
1826// Overrides ApexModule.IsInstallabeToApex()
1827func (mod *Module) IsInstallableToApex() bool {
1828 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001829 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001830 return true
1831 }
1832 if _, ok := mod.compiler.(*binaryDecorator); ok {
1833 return true
1834 }
1835 }
1836 return false
1837}
1838
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001839// If a library file has a "lib" prefix, extract the library name without the prefix.
1840func libNameFromFilePath(filepath android.Path) (string, bool) {
1841 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1842 if strings.HasPrefix(libName, "lib") {
1843 libName = libName[3:]
1844 return libName, true
1845 }
1846 return "", false
1847}
1848
Sasha Smundaka76acba2022-04-18 20:12:56 -07001849func kytheExtractRustFactory() android.Singleton {
1850 return &kytheExtractRustSingleton{}
1851}
1852
1853type kytheExtractRustSingleton struct {
1854}
1855
1856func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1857 var xrefTargets android.Paths
1858 ctx.VisitAllModules(func(module android.Module) {
1859 if rustModule, ok := module.(xref); ok {
1860 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1861 }
1862 })
1863 if len(xrefTargets) > 0 {
1864 ctx.Phony("xref_rust", xrefTargets...)
1865 }
1866}
1867
Jihoon Kangf78a8902022-09-01 22:47:07 +00001868func (c *Module) Partition() string {
1869 return ""
1870}
1871
Ivan Lozanoffee3342019-08-27 12:03:00 -07001872var Bool = proptools.Bool
1873var BoolDefault = proptools.BoolDefault
1874var String = proptools.String
1875var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001876
1877var _ android.OutputFileProducer = (*Module)(nil)