blob: 7d81c721e35e0cf64422cc75dcee55d1249109b1 [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"
Ivan Lozano872d5792022-03-23 17:31:39 -040033 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070034)
35
36var pctx = android.NewPackageContext("android/soong/rust")
37
38func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070039 android.RegisterModuleType("rust_defaults", defaultsFactory)
40 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
41 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040042 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040043 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050044 })
45 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
46 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070047 })
Inseob Kim3b244062023-07-11 13:31:36 +090048 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020050 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000051 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040055 GlobalRustFlags []string // Flags that apply globally to rust
56 GlobalLinkFlags []string // Flags that apply globally to linker
57 RustFlags []string // Flags that apply to rust
58 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070060 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070061 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040062 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020063 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070064 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050068 AndroidMkRlibs []string `blueprint:"mutated"`
69 AndroidMkDylibs []string `blueprint:"mutated"`
70 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050071 AndroidMkStaticLibs []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 Lozano8d10fc32021-11-05 16:36:47 -0400161 // Output file to be installed, may be stripped or unstripped.
162 outputFile android.OptionalPath
163
Sasha Smundaka76acba2022-04-18 20:12:56 -0700164 // Cross-reference input file
165 kytheFiles android.Paths
166
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400167 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900168
169 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500170
171 // For apex variants, this is set as apex.min_sdk_version
172 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700173
174 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400175
LaMont Jones0c971852023-12-07 21:56:59 +0000176 // Aconfig files for all transitive deps. Also exposed via TransitiveDeclarationsInfo
177 mergedAconfigFiles map[string]android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178}
179
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500180func (mod *Module) Header() bool {
181 //TODO: If Rust libraries provide header variants, this needs to be updated.
182 return false
183}
184
185func (mod *Module) SetPreventInstall() {
186 mod.Properties.PreventInstall = true
187}
188
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500189func (mod *Module) SetHideFromMake() {
190 mod.Properties.HideFromMake = true
191}
192
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900193func (mod *Module) HiddenFromMake() bool {
194 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400195}
196
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500197func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500198 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
199 // nil since we need compiler to actually sanitize.
200 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500201}
202
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500203func (mod *Module) IsPrebuilt() bool {
204 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
205 return true
206 }
207 return false
208}
209
Ivan Lozano43845682020-07-09 21:03:28 -0400210func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
211 switch tag {
212 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700213 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400214 return mod.sourceProvider.Srcs(), nil
215 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900216 if mod.OutputFile().Valid() {
217 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400218 }
219 return android.Paths{}, nil
220 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500221 case "unstripped":
222 if mod.compiler != nil {
223 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
224 }
225 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400226 default:
227 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
228 }
229}
230
Ivan Lozano52767be2019-10-18 14:49:46 -0700231func (mod *Module) SelectedStl() string {
232 return ""
233}
234
Ivan Lozano2b262972019-11-21 12:30:50 -0800235func (mod *Module) NonCcVariants() bool {
236 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400237 if _, ok := mod.compiler.(libraryInterface); ok {
238 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800239 }
240 }
241 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
242}
243
Ivan Lozano52767be2019-10-18 14:49:46 -0700244func (mod *Module) Static() bool {
245 if mod.compiler != nil {
246 if library, ok := mod.compiler.(libraryInterface); ok {
247 return library.static()
248 }
249 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400250 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700251}
252
253func (mod *Module) Shared() bool {
254 if mod.compiler != nil {
255 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400256 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700257 }
258 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400259 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700260}
261
Ivan Lozanod7586b62021-04-01 09:49:36 -0400262func (mod *Module) Dylib() bool {
263 if mod.compiler != nil {
264 if library, ok := mod.compiler.(libraryInterface); ok {
265 return library.dylib()
266 }
267 }
268 return false
269}
270
Ivan Lozanod106efe2023-09-21 23:30:26 -0400271func (mod *Module) Source() bool {
272 if mod.compiler != nil {
273 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
274 return library.source()
275 }
276 }
277 return false
278}
279
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400280func (mod *Module) RlibStd() bool {
281 if mod.compiler != nil {
282 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
283 return library.rlibStd()
284 }
285 }
286 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
287}
288
Ivan Lozanod7586b62021-04-01 09:49:36 -0400289func (mod *Module) Rlib() bool {
290 if mod.compiler != nil {
291 if library, ok := mod.compiler.(libraryInterface); ok {
292 return library.rlib()
293 }
294 }
295 return false
296}
297
298func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400299 if binary, ok := mod.compiler.(binaryInterface); ok {
300 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400301 }
302 return false
303}
304
Justin Yun5e035862021-06-29 20:50:37 +0900305func (mod *Module) StaticExecutable() bool {
306 if !mod.Binary() {
307 return false
308 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400309 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900310}
311
Ivan Lozanod7586b62021-04-01 09:49:36 -0400312func (mod *Module) Object() bool {
313 // Rust has no modules which produce only object files.
314 return false
315}
316
Ivan Lozano52767be2019-10-18 14:49:46 -0700317func (mod *Module) Toc() android.OptionalPath {
318 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400319 if lib, ok := mod.compiler.(libraryInterface); ok {
320 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700321 }
322 }
323 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
324}
325
Colin Crossc511bc52020-04-07 16:50:32 +0000326func (mod *Module) UseSdk() bool {
327 return false
328}
329
Ivan Lozanod7586b62021-04-01 09:49:36 -0400330func (mod *Module) RelativeInstallPath() string {
331 if mod.compiler != nil {
332 return mod.compiler.relativeInstallPath()
333 }
334 return ""
335}
336
Ivan Lozano52767be2019-10-18 14:49:46 -0700337func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500338 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700339}
340
Jiyong Park7d55b612021-06-11 17:22:09 +0900341func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400342 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900343}
344
Ivan Lozano52767be2019-10-18 14:49:46 -0700345func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400346 return true
347}
348
349func (mod *Module) SubName() string {
350 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700351}
352
353func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500354 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 return false
356}
357
Ivan Lozanof9e21722020-12-02 09:00:51 -0500358func (mod *Module) IsVndkExt() bool {
359 return false
360}
361
Ivan Lozanod7586b62021-04-01 09:49:36 -0400362func (mod *Module) IsVndkSp() bool {
363 return false
364}
365
Ivan Lozanof1868af2022-04-12 13:08:36 -0400366func (mod *Module) IsVndkPrebuiltLibrary() bool {
367 // Rust modules do not provide VNDK prebuilts
368 return false
369}
370
371func (mod *Module) IsVendorPublicLibrary() bool {
372 return mod.VendorProperties.IsVendorPublicLibrary
373}
374
375func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
376 // Rust modules to not provide Sdk variants
377 return false
378}
379
Colin Cross127bb8b2020-12-16 16:46:01 -0800380func (c *Module) IsVndkPrivate() bool {
381 return false
382}
383
384func (c *Module) IsLlndk() bool {
385 return false
386}
387
388func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500389 return false
390}
391
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400392func (mod *Module) KernelHeadersDecorator() bool {
393 return false
394}
395
Colin Cross1f3f1302021-04-26 18:37:44 -0700396func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400397 return false
398}
399
Colin Cross5271fea2021-04-27 13:06:04 -0700400func (m *Module) NeedsVendorPublicLibraryVariants() bool {
401 return false
402}
403
Ivan Lozanod7586b62021-04-01 09:49:36 -0400404func (mod *Module) HasLlndkStubs() bool {
405 return false
406}
407
408func (mod *Module) StubsVersion() string {
409 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
410}
411
Ivan Lozano52767be2019-10-18 14:49:46 -0700412func (mod *Module) SdkVersion() string {
413 return ""
414}
415
Colin Crossc511bc52020-04-07 16:50:32 +0000416func (mod *Module) AlwaysSdk() bool {
417 return false
418}
419
Jiyong Park2286afd2020-06-16 21:58:53 +0900420func (mod *Module) IsSdkVariant() bool {
421 return false
422}
423
Colin Cross1348ce32020-10-01 13:37:16 -0700424func (mod *Module) SplitPerApiLevel() bool {
425 return false
426}
427
Sasha Smundaka76acba2022-04-18 20:12:56 -0700428func (mod *Module) XrefRustFiles() android.Paths {
429 return mod.kytheFiles
430}
431
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400433 Dylibs []string
434 Rlibs []string
435 Rustlibs []string
436 Stdlibs []string
437 ProcMacros []string
438 SharedLibs []string
439 StaticLibs []string
440 WholeStaticLibs []string
441 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400443 // Used for data dependencies adjacent to tests
444 DataLibs []string
445 DataBins []string
446
Colin Crossfe605e12022-01-23 20:46:16 -0800447 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448}
449
450type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700451 DyLibs RustLibraries
452 RLibs RustLibraries
453 SharedLibs android.Paths
454 SharedLibDeps android.Paths
455 StaticLibs android.Paths
456 ProcMacros RustLibraries
457 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500458
459 // depFlags and depLinkFlags are rustc and linker (clang) flags.
460 depFlags []string
461 depLinkFlags []string
462
463 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
464 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000465 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700466 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700467
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 Lozanoffee3342019-08-27 12:03:00 -0700480}
481
482type RustLibraries []RustLibrary
483
484type RustLibrary struct {
485 Path android.Path
486 CrateName string
487}
488
Matthew Maurerbb3add12020-06-25 09:34:12 -0700489type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000490 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700491 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700492}
493
Sasha Smundaka76acba2022-04-18 20:12:56 -0700494type xref interface {
495 XrefRustFiles() android.Paths
496}
497
Matthew Maurerbb3add12020-06-25 09:34:12 -0700498type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000499 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700500 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700501}
502
Wen-yi Chu41326c12023-09-22 03:58:59 +0000503func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
504 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700505}
506
Colin Cross004bd3f2023-10-02 11:39:17 -0700507func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
508 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400509}
510
Colin Cross0de8a1e2020-09-18 14:15:30 -0700511func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800512 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700513 LinkDirs: flagExporter.linkDirs,
514 LinkObjects: flagExporter.linkObjects,
515 })
516}
517
Matthew Maurerbb3add12020-06-25 09:34:12 -0700518var _ exportedFlagsProducer = (*flagExporter)(nil)
519
520func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700521 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700522}
523
Colin Cross0de8a1e2020-09-18 14:15:30 -0700524type FlagExporterInfo struct {
525 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000526 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700527 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700528}
529
Colin Crossbc7d76c2023-12-12 16:39:03 -0800530var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700531
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400532func (mod *Module) isCoverageVariant() bool {
533 return mod.coverage.Properties.IsCoverageVariant
534}
535
536var _ cc.Coverage = (*Module)(nil)
537
Colin Crossf5f4ad32024-01-19 15:41:48 -0800538func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400539 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
540}
541
Ivan Lozanod7586b62021-04-01 09:49:36 -0400542func (mod *Module) VndkVersion() string {
543 return mod.Properties.VndkVersion
544}
545
546func (mod *Module) PreventInstall() bool {
547 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400548}
549
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400550func (mod *Module) MarkAsCoverageVariant(coverage bool) {
551 mod.coverage.Properties.IsCoverageVariant = coverage
552}
553
554func (mod *Module) EnableCoverageIfNeeded() {
555 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556}
557
558func defaultsFactory() android.Module {
559 return DefaultsFactory()
560}
561
562type Defaults struct {
563 android.ModuleBase
564 android.DefaultsModuleBase
565}
566
567func DefaultsFactory(props ...interface{}) android.Module {
568 module := &Defaults{}
569
570 module.AddProperties(props...)
571 module.AddProperties(
572 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800573 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500574 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100575 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400576 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700577 &BaseCompilerProperties{},
578 &BinaryCompilerProperties{},
579 &LibraryCompilerProperties{},
580 &ProcMacroCompilerProperties{},
581 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400582 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700583 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400584 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400585 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200586 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500587 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000588 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589 )
590
591 android.InitDefaultsModule(module)
592 return module
593}
594
595func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700596 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700597}
598
Ivan Lozano183a3212019-10-18 14:18:45 -0700599func (mod *Module) CcLibrary() bool {
600 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500601 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700602 return true
603 }
604 }
605 return false
606}
607
608func (mod *Module) CcLibraryInterface() bool {
609 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400610 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
611 // VariantIs{Static,Shared} is set.
612 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700613 return true
614 }
615 }
616 return false
617}
618
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400619func (mod *Module) RustLibraryInterface() bool {
620 if mod.compiler != nil {
621 if _, ok := mod.compiler.(libraryInterface); ok {
622 return true
623 }
624 }
625 return false
626}
627
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500628func (mod *Module) IsFuzzModule() bool {
629 if _, ok := mod.compiler.(*fuzzDecorator); ok {
630 return true
631 }
632 return false
633}
634
635func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
636 return mod.FuzzModule
637}
638
639func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
640 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
641 return fuzzer.fuzzPackagedModule
642 }
643 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
644}
645
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000646func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500647 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
648 return fuzzer.sharedLibraries
649 }
650 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
651}
652
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400653func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400654 if mod.compiler != nil {
655 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400656 }
657 return nil
658}
659
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800660func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700661 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700662 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800663 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700664 }
665 }
666 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
667}
668
669func (mod *Module) SetStatic() {
670 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700671 if library, ok := mod.compiler.(libraryInterface); ok {
672 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700673 return
674 }
675 }
676 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
677}
678
679func (mod *Module) SetShared() {
680 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700681 if library, ok := mod.compiler.(libraryInterface); ok {
682 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700683 return
684 }
685 }
686 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
687}
688
Ivan Lozano183a3212019-10-18 14:18:45 -0700689func (mod *Module) BuildStaticVariant() bool {
690 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700691 if library, ok := mod.compiler.(libraryInterface); ok {
692 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700693 }
694 }
695 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
696}
697
698func (mod *Module) BuildSharedVariant() bool {
699 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700700 if library, ok := mod.compiler.(libraryInterface); ok {
701 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700702 }
703 }
704 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
705}
706
Ivan Lozano183a3212019-10-18 14:18:45 -0700707func (mod *Module) Module() android.Module {
708 return mod
709}
710
Ivan Lozano183a3212019-10-18 14:18:45 -0700711func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400712 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700713}
714
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400715func (mod *Module) CoverageFiles() android.Paths {
716 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800717 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400718 }
719 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
720}
721
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400722// Rust does not produce gcno files, and therefore does not produce a coverage archive.
723func (mod *Module) CoverageOutputFile() android.OptionalPath {
724 return android.OptionalPath{}
725}
726
727func (mod *Module) IsNdk(config android.Config) bool {
728 return false
729}
730
731func (mod *Module) IsStubs() bool {
732 return false
733}
734
Jiyong Park459feca2020-12-15 11:02:21 +0900735func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900736 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900737 return false
738 }
739
Jiyong Park459feca2020-12-15 11:02:21 +0900740 // The apex variant is not installable because it is included in the APEX and won't appear
741 // in the system partition as a standalone file.
742 if !apexInfo.IsForPlatform() {
743 return false
744 }
745
Jiyong Parke54f07e2021-04-07 15:08:04 +0900746 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900747}
748
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500749func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800750 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
751 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500752}
753
Ivan Lozano183a3212019-10-18 14:18:45 -0700754var _ cc.LinkableInterface = (*Module)(nil)
755
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756func (mod *Module) Init() android.Module {
757 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500758 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759
Yi Kong46c6e592022-01-20 22:55:00 +0800760 if mod.afdo != nil {
761 mod.AddProperties(mod.afdo.props()...)
762 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763 if mod.compiler != nil {
764 mod.AddProperties(mod.compiler.compilerProps()...)
765 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766 if mod.coverage != nil {
767 mod.AddProperties(mod.coverage.props()...)
768 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200769 if mod.clippy != nil {
770 mod.AddProperties(mod.clippy.props()...)
771 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400772 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700773 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400774 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500775 if mod.sanitize != nil {
776 mod.AddProperties(mod.sanitize.props()...)
777 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900780 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781
782 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 return mod
784}
785
786func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
787 return &Module{
788 hod: hod,
789 multilib: multilib,
790 }
791}
792func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
793 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800794 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400795 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200796 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500797 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 return module
799}
800
801type ModuleContext interface {
802 android.ModuleContext
803 ModuleContextIntf
804}
805
806type BaseModuleContext interface {
807 android.BaseModuleContext
808 ModuleContextIntf
809}
810
811type DepsContext interface {
812 android.BottomUpMutatorContext
813 ModuleContextIntf
814}
815
816type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200817 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819}
820
821type depsContext struct {
822 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823}
824
825type moduleContext struct {
826 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827}
828
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200829type baseModuleContext struct {
830 android.BaseModuleContext
831}
832
833func (ctx *moduleContext) RustModule() *Module {
834 return ctx.Module().(*Module)
835}
836
837func (ctx *moduleContext) toolchain() config.Toolchain {
838 return ctx.RustModule().toolchain(ctx)
839}
840
841func (ctx *depsContext) RustModule() *Module {
842 return ctx.Module().(*Module)
843}
844
845func (ctx *depsContext) toolchain() config.Toolchain {
846 return ctx.RustModule().toolchain(ctx)
847}
848
849func (ctx *baseModuleContext) RustModule() *Module {
850 return ctx.Module().(*Module)
851}
852
853func (ctx *baseModuleContext) toolchain() config.Toolchain {
854 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400855}
856
857func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700858 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
859 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
860 return false
861 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400862 return mod.compiler != nil && mod.compiler.nativeCoverage()
863}
864
Ivan Lozanod7586b62021-04-01 09:49:36 -0400865func (mod *Module) EverInstallable() bool {
866 return mod.compiler != nil &&
867 // Check to see whether the module is actually ever installable.
868 mod.compiler.everInstallable()
869}
870
871func (mod *Module) Installable() *bool {
872 return mod.Properties.Installable
873}
874
Ivan Lozano872d5792022-03-23 17:31:39 -0400875func (mod *Module) ProcMacro() bool {
876 if pm, ok := mod.compiler.(procMacroInterface); ok {
877 return pm.ProcMacro()
878 }
879 return false
880}
881
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
883 if mod.cachedToolchain == nil {
884 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
885 }
886 return mod.cachedToolchain
887}
888
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200889func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
890 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
891}
892
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
894}
895
896func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
897 ctx := &moduleContext{
898 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900
Colin Crossff694a82023-12-13 15:54:49 -0800901 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900902 if !apexInfo.IsForPlatform() {
903 mod.hideApexVariantFromMake = true
904 }
905
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500907 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
908
Ivan Lozanof1868af2022-04-12 13:08:36 -0400909 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 if !toolchain.Supported() {
912 // This toolchain's unsupported, there's nothing to do for this mod.
913 return
914 }
915
916 deps := mod.depsToPaths(ctx)
917 flags := Flags{
918 Toolchain: toolchain,
919 }
920
Ivan Lozano67eada32021-09-23 11:50:33 -0400921 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800922 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500923 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800924 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 if mod.compiler != nil {
926 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400927 flags = mod.compiler.cfgFlags(ctx, flags)
928 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400929 }
930 if mod.coverage != nil {
931 flags, deps = mod.coverage.flags(ctx, flags, deps)
932 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200933 if mod.clippy != nil {
934 flags, deps = mod.clippy.flags(ctx, flags, deps)
935 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500936 if mod.sanitize != nil {
937 flags, deps = mod.sanitize.flags(ctx, flags, deps)
938 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400939
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200940 // SourceProvider needs to call GenerateSource() before compiler calls
941 // compile() so it can provide the source. A SourceProvider has
942 // multiple variants (e.g. source, rlib, dylib). Only the "source"
943 // variant is responsible for effectively generating the source. The
944 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400945 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200946 if mod.compiler.(libraryInterface).source() {
947 mod.sourceProvider.GenerateSource(ctx, deps)
948 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
949 } else {
950 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
951 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700952 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200953 }
Colin Cross40213022023-12-13 15:19:49 -0800954 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400955 }
956
957 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100958 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700959 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400960 if ctx.Failed() {
961 return
962 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700963 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
964 if buildOutput.kytheFile != nil {
965 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
966 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400967 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900968
Dan Albert06feee92021-03-19 15:06:02 -0700969 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000970 if mod.docTimestampFile.Valid() {
971 ctx.CheckbuildFile(mod.docTimestampFile.Path())
972 }
Dan Albert06feee92021-03-19 15:06:02 -0700973
Ivan Lozano1921e802021-05-20 13:39:16 -0400974 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
975 // RECOVERY_SNAPSHOT_VERSION is current.
976 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
977 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
978 lib.collectHeadersForSnapshot(ctx, deps)
979 }
980 }
981
Colin Crossff694a82023-12-13 15:54:49 -0800982 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400983 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900984 // If the module has been specifically configure to not be installed then
985 // hide from make as otherwise it will break when running inside make as the
986 // output path to install will not be specified. Not all uninstallable
987 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400988 // side dependencies. In particular, proc-macros need to be captured in the
989 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900990 mod.HideFromMake()
991 } else if !mod.installable(apexInfo) {
992 mod.SkipInstall()
993 }
994
995 // Still call install though, the installs will be stored as PackageSpecs to allow
996 // using the outputs in a genrule.
997 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200998 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900999 if ctx.Failed() {
1000 return
1001 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001002 }
Chris Wailes74be7642021-07-22 16:20:28 -07001003
1004 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001005 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001006 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -08001007 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001008 }
LaMont Jones0c971852023-12-07 21:56:59 +00001009
LaMont Jonesaa005ae2023-12-19 19:01:57 +00001010 android.CollectDependencyAconfigFiles(ctx, &mod.mergedAconfigFiles)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011}
1012
1013func (mod *Module) deps(ctx DepsContext) Deps {
1014 deps := Deps{}
1015
1016 if mod.compiler != nil {
1017 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001018 }
1019 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001020 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001021 }
1022
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001023 if mod.coverage != nil {
1024 deps = mod.coverage.deps(ctx, deps)
1025 }
1026
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001027 if mod.sanitize != nil {
1028 deps = mod.sanitize.deps(ctx, deps)
1029 }
1030
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1032 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001033 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1035 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1036 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001037 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001038 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001039 return deps
1040
1041}
1042
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043type dependencyTag struct {
1044 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001045 name string
1046 library bool
1047 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001048 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049}
1050
Jiyong Park65b62242020-11-25 12:44:59 +09001051// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1052// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1053func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001054 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001055}
1056
1057var _ android.InstallNeededDependencyTag = dependencyTag{}
1058
Colin Cross65cb3142021-12-10 23:05:02 +00001059func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1060 if d.library && d.dynamic {
1061 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1062 }
1063 return nil
1064}
1065
Yu Liuc8884602024-03-15 18:48:38 +00001066func (d dependencyTag) PropagateAconfigValidation() bool {
1067 return d == rlibDepTag || d == sourceDepTag
1068}
1069
1070var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1071
Colin Cross65cb3142021-12-10 23:05:02 +00001072var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1073
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001075 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1076 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001077 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001078 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001079 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001080 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001081 dataLibDepTag = dependencyTag{name: "data lib"}
1082 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001083)
1084
Jiyong Park99644e92020-11-17 22:21:02 +09001085func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1086 tag, ok := depTag.(dependencyTag)
1087 return ok && tag == dylibDepTag
1088}
1089
Jiyong Park94e22fd2021-04-08 18:19:15 +09001090func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1091 tag, ok := depTag.(dependencyTag)
1092 return ok && tag == rlibDepTag
1093}
1094
Matthew Maurer0f003b12020-06-29 14:34:06 -07001095type autoDep struct {
1096 variation string
1097 depTag dependencyTag
1098}
1099
1100var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001101 rlibVariation = "rlib"
1102 dylibVariation = "dylib"
1103 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1104 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001105)
1106
1107type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001108 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001109}
1110
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001111func (mod *Module) begin(ctx BaseModuleContext) {
1112 if mod.coverage != nil {
1113 mod.coverage.begin(ctx)
1114 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001115 if mod.sanitize != nil {
1116 mod.sanitize.begin(ctx)
1117 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001118}
1119
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001120func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001121 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001122 return p.prebuilt()
1123 }
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 Lozano89435d12020-07-31 11:01:18 -04001229 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
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 Lozanoffee3342019-08-27 12:03:00 -07001255 case procMacroDepTag:
1256 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001257 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001258
1259 case sourceDepTag:
1260 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1261 collectIncludedProtos(mod, rustDep)
1262 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001263 }
1264
Cole Faustb6e6f992023-08-17 17:42:26 -07001265 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1266
Paul Duffind5cf92e2021-07-09 17:38:55 +01001267 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001268 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1269 // OS/Arch variant is used.
1270 var helper string
1271 if ctx.Host() {
1272 helper = "missing 'host_supported'?"
1273 } else {
1274 helper = "device module defined?"
1275 }
1276
1277 if dep.Target().Os != ctx.Os() {
1278 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1279 return
1280 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1281 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1282 return
1283 }
1284 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001285 }
1286
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001287 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001288 if depTag != procMacroDepTag {
Colin Cross313aa542023-12-13 13:47:44 -08001289 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001290 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1291 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1292 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001293 }
1294
Ivan Lozanoffee3342019-08-27 12:03:00 -07001295 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001296 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001297 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001298 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001299 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001300 }
1301 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001302 if depTag == sourceDepTag {
1303 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1304 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001305 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001306 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1307 }
1308 }
1309 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001310 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001311 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001312 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001313 if _, ok := ccDep.(*Module); !ok {
1314 if ccDep.Module().Target().Os != ctx.Os() {
1315 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1316 return
1317 }
1318 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1319 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1320 return
1321 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001322 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001323 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001324 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001325 if !ctx.Config().AllowMissingDependencies() {
1326 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1327 } else {
1328 ctx.AddMissingDependencies([]string{depName})
1329 }
1330 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001331 }
1332
Wen-yi Chu41326c12023-09-22 03:58:59 +00001333 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001334
Ivan Lozanoffee3342019-08-27 12:03:00 -07001335 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001336 switch {
1337 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001338 if cc.IsWholeStaticLib(depTag) {
1339 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1340 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001341 if mod.Binary() {
1342 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1343 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1344 // final linkage, pass the args directly to the linker to handle these cases.
1345 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1346 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001347 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001348 } else {
1349 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 -05001350 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001351 }
1352
1353 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1354 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001355 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001356 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1357
Colin Cross313aa542023-12-13 13:47:44 -08001358 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001359 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1360 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1361 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1362 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001363 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001364
1365 // Record baseLibName for snapshots.
1366 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1367
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001368 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001369 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001370 // For the shared lib dependencies, we may link to the stub variant
1371 // of the dependency depending on the context (e.g. if this
1372 // dependency crosses the APEX boundaries).
1373 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1374
1375 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1376 // object (either from stub or non-stub) to use.
1377 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001378 if !linkObject.Valid() {
1379 if !ctx.Config().AllowMissingDependencies() {
1380 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1381 } else {
1382 ctx.AddMissingDependencies([]string{depName})
1383 }
1384 return
1385 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001386 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001387
Ivan Lozanoffee3342019-08-27 12:03:00 -07001388 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001389 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001390 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1391 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1392 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1393 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001394 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001395
1396 // Record baseLibName for snapshots.
1397 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1398
Cole Faustb6e6f992023-08-17 17:42:26 -07001399 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001400 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001401 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001402 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001403 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1404 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1405 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001406 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001407 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001408 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001409 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001410 }
1411
1412 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001413 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1414 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001415 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001416 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001417 } else {
1418 switch {
1419 case depTag == cc.CrtBeginDepTag:
1420 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1421 case depTag == cc.CrtEndDepTag:
1422 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1423 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001424 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001425
1426 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001427 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001428 // These are usually genrules which don't have per-target variants.
1429 directSrcDeps = append(directSrcDeps, srcDep)
1430 }
1431 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001432 })
1433
Wen-yi Chu41326c12023-09-22 03:58:59 +00001434 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1435
1436 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001437 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001438 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001439 crateName := dep.CrateName()
1440 if alias, aliased := aliases[crateName]; aliased {
1441 crateName = alias
1442 }
1443 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001444 }
1445 var dylibDepFiles RustLibraries
1446 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001447 crateName := dep.CrateName()
1448 if alias, aliased := aliases[crateName]; aliased {
1449 crateName = alias
1450 }
1451 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001452 }
1453 var procMacroDepFiles RustLibraries
1454 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001455 crateName := dep.CrateName()
1456 if alias, aliased := aliases[crateName]; aliased {
1457 crateName = alias
1458 }
1459 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001460 }
1461
Colin Cross004bd3f2023-10-02 11:39:17 -07001462 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001464 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001465 }
1466
Colin Cross004bd3f2023-10-02 11:39:17 -07001467 var sharedLibFiles android.Paths
1468 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001469 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001470 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001471 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001472 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001473 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001474 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001475 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476 }
1477
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001478 var srcProviderDepFiles android.Paths
1479 for _, dep := range directSrcProvidersDeps {
1480 srcs, _ := dep.OutputFiles("")
1481 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1482 }
1483 for _, dep := range directSrcDeps {
1484 srcs := dep.Srcs()
1485 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1486 }
1487
Wen-yi Chu41326c12023-09-22 03:58:59 +00001488 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1489 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001490 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1491 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1492 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001493 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001494 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001495
1496 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001497 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001498 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001499 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001500 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1501 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1502 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001503
1504 return depPaths
1505}
1506
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001507func (mod *Module) InstallInData() bool {
1508 if mod.compiler == nil {
1509 return false
1510 }
1511 return mod.compiler.inData()
1512}
1513
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001514func (mod *Module) InstallInRamdisk() bool {
1515 return mod.InRamdisk()
1516}
1517
1518func (mod *Module) InstallInVendorRamdisk() bool {
1519 return mod.InVendorRamdisk()
1520}
1521
1522func (mod *Module) InstallInRecovery() bool {
1523 return mod.InRecovery()
1524}
1525
Wen-yi Chu41326c12023-09-22 03:58:59 +00001526func linkPathFromFilePath(filepath android.Path) string {
1527 return strings.Split(filepath.String(), filepath.Base())[0]
1528}
1529
Spandan Das604f3762023-03-16 22:51:40 +00001530// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1531func (r *Module) usePublicApi() bool {
1532 return r.Device() && r.UseSdk()
1533}
1534
1535// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1536func (r *Module) useVendorApi() bool {
1537 return r.Device() && (r.InVendor() || r.InProduct())
1538}
1539
Ivan Lozanoffee3342019-08-27 12:03:00 -07001540func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1541 ctx := &depsContext{
1542 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001543 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001544
1545 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001546 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001547 var snapshotInfo *cc.SnapshotInfo
1548
Kiyoung Kim487689e2022-07-26 09:48:22 +09001549 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001550 if mod.usePublicApi() || mod.useVendorApi() {
1551 for idx, lib := range deps.SharedLibs {
1552 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1553 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001554 }
1555
Ivan Lozano1921e802021-05-20 13:39:16 -04001556 if ctx.Os() == android.Android {
1557 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1558 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001559
Ivan Lozano2b081132020-09-08 12:46:52 -04001560 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001561 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001562 stdLinkage = "rlib-std"
1563 }
1564
1565 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001566
Ivan Lozano2b081132020-09-08 12:46:52 -04001567 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1568 rlibDepVariations = append(rlibDepVariations,
1569 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1570 }
1571
Ivan Lozano1921e802021-05-20 13:39:16 -04001572 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001573 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001574 for _, lib := range deps.Rlibs {
1575 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001576 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001577
Ivan Lozano2d407632022-04-07 12:59:11 -04001578 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001579 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001580
1581 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001582 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1583 for _, lib := range deps.Dylibs {
1584 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1585 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001586
Ivan Lozano1921e802021-05-20 13:39:16 -04001587 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001588 if deps.Rustlibs != nil {
1589 if !mod.compiler.Disabled() {
1590 for _, lib := range deps.Rustlibs {
1591 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1592 if autoDep.depTag == rlibDepTag {
1593 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001594 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001595 } else {
1596 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1597 // Check for the existence of the dylib deptag variant. Select it if available,
1598 // otherwise select the rlib variant.
1599 autoDepVariations := append(commonDepVariations,
1600 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1601
1602 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1603
1604 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1605 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1606 } else {
1607 // If there's no dylib dependency available, try to add the rlib dependency instead.
1608 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1609 }
1610 }
1611 }
1612 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1613 for _, lib := range deps.Rustlibs {
1614 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1615 srcProviderVariations := append(commonDepVariations,
1616 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1617
1618 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1619 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001620 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001621 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001622 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001623 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001624
Ivan Lozano1921e802021-05-20 13:39:16 -04001625 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001626 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001627 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001628 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001629 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001630 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001631 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001632 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001633 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001634 for _, lib := range deps.Stdlibs {
1635 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1636 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001637 }
1638 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001639
1640 for _, lib := range deps.SharedLibs {
1641 depTag := cc.SharedDepTag()
1642 name, version := cc.StubsLibNameAndVersion(lib)
1643
1644 variations := []blueprint.Variation{
1645 {Mutator: "link", Variation: "shared"},
1646 }
Spandan Das604f3762023-03-16 22:51:40 +00001647 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1648 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1649 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1650 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1651 }
1652
1653 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1654 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1655 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001656 }
1657
1658 for _, lib := range deps.WholeStaticLibs {
1659 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001660 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001661
1662 actx.AddVariationDependencies([]blueprint.Variation{
1663 {Mutator: "link", Variation: "static"},
1664 }, depTag, lib)
1665 }
1666
1667 for _, lib := range deps.StaticLibs {
1668 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001669 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001670
1671 actx.AddVariationDependencies([]blueprint.Variation{
1672 {Mutator: "link", Variation: "static"},
1673 }, depTag, lib)
1674 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001675
Zach Johnson3df4e632020-11-06 11:56:27 -08001676 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1677
Colin Cross565cafd2020-09-25 18:47:38 -07001678 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001679 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001680 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001681 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001682 }
Colin Crossfe605e12022-01-23 20:46:16 -08001683 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001684 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001685 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001686 }
1687
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001688 if mod.sourceProvider != nil {
1689 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1690 bindgen.Properties.Custom_bindgen != "" {
1691 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1692 bindgen.Properties.Custom_bindgen)
1693 }
1694 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001695
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001696 actx.AddVariationDependencies([]blueprint.Variation{
1697 {Mutator: "link", Variation: "shared"},
1698 }, dataLibDepTag, deps.DataLibs...)
1699
1700 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1701
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001702 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001703 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001704
1705 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001706}
1707
Ivan Lozano2d407632022-04-07 12:59:11 -04001708// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001709func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1710 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001711 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1712}
1713
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001714func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1715 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1716 actx.AddVariationDependencies(variations, depTag, lib)
1717}
1718
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001719func BeginMutator(ctx android.BottomUpMutatorContext) {
1720 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1721 mod.beginMutator(ctx)
1722 }
1723}
1724
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001725func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1726 ctx := &baseModuleContext{
1727 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001728 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001729
1730 mod.begin(ctx)
1731}
1732
Ivan Lozanoffee3342019-08-27 12:03:00 -07001733func (mod *Module) Name() string {
1734 name := mod.ModuleBase.Name()
1735 if p, ok := mod.compiler.(interface {
1736 Name(string) string
1737 }); ok {
1738 name = p.Name(name)
1739 }
1740 return name
1741}
1742
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001743func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001744 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001745 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001746 }
1747}
1748
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001749var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001750var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001751
1752func (mod *Module) HostToolPath() android.OptionalPath {
1753 if !mod.Host() {
1754 return android.OptionalPath{}
1755 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001756 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1757 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001758 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1759 // Even though proc-macros aren't strictly "tools", since they target the compiler
1760 // and act as compiler plugins, we treat them similarly.
1761 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001762 }
1763 return android.OptionalPath{}
1764}
1765
Jiyong Park99644e92020-11-17 22:21:02 +09001766var _ android.ApexModule = (*Module)(nil)
1767
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001768func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001769 return String(mod.Properties.Min_sdk_version)
1770}
1771
Jiyong Park45bf82e2020-12-15 22:29:02 +09001772// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001773func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001774 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001775 if minSdkVersion == "apex_inherit" {
1776 return nil
1777 }
1778 if minSdkVersion == "" {
1779 return fmt.Errorf("min_sdk_version is not specificed")
1780 }
1781
1782 // Not using nativeApiLevelFromUser because the context here is not
1783 // necessarily a native context.
1784 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1785 if err != nil {
1786 return err
1787 }
1788
1789 if ver.GreaterThan(sdkVersion) {
1790 return fmt.Errorf("newer SDK(%v)", ver)
1791 }
Jiyong Park99644e92020-11-17 22:21:02 +09001792 return nil
1793}
1794
Jiyong Park45bf82e2020-12-15 22:29:02 +09001795// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001796func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1797 depTag := ctx.OtherModuleDependencyTag(dep)
1798
1799 if ccm, ok := dep.(*cc.Module); ok {
1800 if ccm.HasStubsVariants() {
1801 if cc.IsSharedDepTag(depTag) {
1802 // dynamic dep to a stubs lib crosses APEX boundary
1803 return false
1804 }
1805 if cc.IsRuntimeDepTag(depTag) {
1806 // runtime dep to a stubs lib also crosses APEX boundary
1807 return false
1808 }
1809
1810 if cc.IsHeaderDepTag(depTag) {
1811 return false
1812 }
1813 }
1814 if mod.Static() && cc.IsSharedDepTag(depTag) {
1815 // shared_lib dependency from a static lib is considered as crossing
1816 // the APEX boundary because the dependency doesn't actually is
1817 // linked; the dependency is used only during the compilation phase.
1818 return false
1819 }
1820 }
1821
Matthew Maurer581b6d82022-09-29 16:46:25 -07001822 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001823 return false
1824 }
1825
1826 return true
1827}
1828
1829// Overrides ApexModule.IsInstallabeToApex()
1830func (mod *Module) IsInstallableToApex() bool {
1831 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001832 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001833 return true
1834 }
1835 if _, ok := mod.compiler.(*binaryDecorator); ok {
1836 return true
1837 }
1838 }
1839 return false
1840}
1841
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001842// If a library file has a "lib" prefix, extract the library name without the prefix.
1843func libNameFromFilePath(filepath android.Path) (string, bool) {
1844 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1845 if strings.HasPrefix(libName, "lib") {
1846 libName = libName[3:]
1847 return libName, true
1848 }
1849 return "", false
1850}
1851
Sasha Smundaka76acba2022-04-18 20:12:56 -07001852func kytheExtractRustFactory() android.Singleton {
1853 return &kytheExtractRustSingleton{}
1854}
1855
1856type kytheExtractRustSingleton struct {
1857}
1858
1859func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1860 var xrefTargets android.Paths
1861 ctx.VisitAllModules(func(module android.Module) {
1862 if rustModule, ok := module.(xref); ok {
1863 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1864 }
1865 })
1866 if len(xrefTargets) > 0 {
1867 ctx.Phony("xref_rust", xrefTargets...)
1868 }
1869}
1870
Jihoon Kangf78a8902022-09-01 22:47:07 +00001871func (c *Module) Partition() string {
1872 return ""
1873}
1874
Ivan Lozanoffee3342019-08-27 12:03:00 -07001875var Bool = proptools.Bool
1876var BoolDefault = proptools.BoolDefault
1877var String = proptools.String
1878var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001879
1880var _ android.OutputFileProducer = (*Module)(nil)