blob: ddebb7599cabb25e809a1a3b510598fadc916661 [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
Vinh Tranb4bb20f2023-08-24 11:10:01 -040021 "android/soong/bazel"
Sasha Smundaka76acba2022-04-18 20:12:56 -070022 "android/soong/bloaty"
Aditya Choudhary87b2ab22023-11-17 15:27:06 +000023 "android/soong/testing"
Vinh Tranbcb5f572023-08-24 11:10:01 -040024 "android/soong/ui/metrics/bp2build_metrics_proto"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025
26 "github.com/google/blueprint"
27 "github.com/google/blueprint/proptools"
28
LaMont Jones0c971852023-12-07 21:56:59 +000029 "android/soong/aconfig"
Ivan Lozanoffee3342019-08-27 12:03:00 -070030 "android/soong/android"
31 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020032 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070033 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000034 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070035 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040036 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070037)
38
39var pctx = android.NewPackageContext("android/soong/rust")
40
41func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070042 android.RegisterModuleType("rust_defaults", defaultsFactory)
43 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040045 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040046 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050047 })
48 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
49 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070050 })
Inseob Kim3b244062023-07-11 13:31:36 +090051 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070052 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020053 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000054 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070055}
56
57type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040058 GlobalRustFlags []string // Flags that apply globally to rust
59 GlobalLinkFlags []string // Flags that apply globally to linker
60 RustFlags []string // Flags that apply to rust
61 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070063 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070064 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040065 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020066 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070067 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070068}
69
70type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050071 AndroidMkRlibs []string `blueprint:"mutated"`
72 AndroidMkDylibs []string `blueprint:"mutated"`
73 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050074 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040075
Ivan Lozano6a884432020-12-02 09:15:16 -050076 ImageVariationPrefix string `blueprint:"mutated"`
77 VndkVersion string `blueprint:"mutated"`
78 SubName string `blueprint:"mutated"`
79
Ivan Lozanoc08897c2021-04-02 12:41:32 -040080 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
81 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
82 // appended before SubName.
83 RustSubName string `blueprint:"mutated"`
84
Ivan Lozano6a884432020-12-02 09:15:16 -050085 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050086 CoreVariantNeeded bool `blueprint:"mutated"`
87 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070088 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080089 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050090 ExtraVariants []string `blueprint:"mutated"`
91
Ivan Lozanoa2268632021-07-22 10:52:06 -040092 // Allows this module to use non-APEX version of libraries. Useful
93 // for building binaries that are started before APEXes are activated.
94 Bootstrap *bool
95
Ivan Lozano1921e802021-05-20 13:39:16 -040096 // Used by vendor snapshot to record dependencies from snapshot modules.
97 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090098 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040099 SnapshotRlibs []string `blueprint:"mutated"`
100 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400101
Matthew Maurerc6868382021-07-13 14:12:37 -0700102 // Make this module available when building for ramdisk.
103 // On device without a dedicated recovery partition, the module is only
104 // available after switching root into
105 // /first_stage_ramdisk. To expose the module before switching root, install
106 // the recovery variant instead.
107 Ramdisk_available *bool
108
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500109 // Make this module available when building for vendor ramdisk.
110 // On device without a dedicated recovery partition, the module is only
111 // available after switching root into
112 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800113 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500114 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400115
Ivan Lozano1921e802021-05-20 13:39:16 -0400116 // Normally Soong uses the directory structure to decide which modules
117 // should be included (framework) or excluded (non-framework) from the
118 // different snapshots (vendor, recovery, etc.), but this property
119 // allows a partner to exclude a module normally thought of as a
120 // framework module from the vendor snapshot.
121 Exclude_from_vendor_snapshot *bool
122
123 // Normally Soong uses the directory structure to decide which modules
124 // should be included (framework) or excluded (non-framework) from the
125 // different snapshots (vendor, recovery, etc.), but this property
126 // allows a partner to exclude a module normally thought of as a
127 // framework module from the recovery snapshot.
128 Exclude_from_recovery_snapshot *bool
129
Matthew Maurer460ee942021-02-11 12:31:46 -0800130 // Make this module available when building for recovery
131 Recovery_available *bool
132
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500133 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
134 Min_sdk_version *string
135
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900136 HideFromMake bool `blueprint:"mutated"`
137 PreventInstall bool `blueprint:"mutated"`
138
139 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140}
141
142type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700143 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144
Ivan Lozano6a884432020-12-02 09:15:16 -0500145 VendorProperties cc.VendorProperties
146
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147 Properties BaseProperties
148
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000149 hod android.HostOrDeviceSupported
150 multilib android.Multilib
151 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152
Ivan Lozano6a884432020-12-02 09:15:16 -0500153 makeLinkType string
154
Yi Kong46c6e592022-01-20 22:55:00 +0800155 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400157 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200158 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500159 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700160 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400161 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700162 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400163
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400164 // Output file to be installed, may be stripped or unstripped.
165 outputFile android.OptionalPath
166
Sasha Smundaka76acba2022-04-18 20:12:56 -0700167 // Cross-reference input file
168 kytheFiles android.Paths
169
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400170 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900171
172 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500173
174 // For apex variants, this is set as apex.min_sdk_version
175 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700176
177 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400178
179 android.BazelModuleBase
LaMont Jones0c971852023-12-07 21:56:59 +0000180
181 // Aconfig files for all transitive deps. Also exposed via TransitiveDeclarationsInfo
182 mergedAconfigFiles map[string]android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700183}
184
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500185func (mod *Module) Header() bool {
186 //TODO: If Rust libraries provide header variants, this needs to be updated.
187 return false
188}
189
190func (mod *Module) SetPreventInstall() {
191 mod.Properties.PreventInstall = true
192}
193
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194func (mod *Module) SetHideFromMake() {
195 mod.Properties.HideFromMake = true
196}
197
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900198func (mod *Module) HiddenFromMake() bool {
199 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400200}
201
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500203 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
204 // nil since we need compiler to actually sanitize.
205 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500206}
207
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500208func (mod *Module) IsPrebuilt() bool {
209 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
210 return true
211 }
212 return false
213}
214
Ivan Lozano43845682020-07-09 21:03:28 -0400215func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
216 switch tag {
217 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700218 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400219 return mod.sourceProvider.Srcs(), nil
220 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900221 if mod.OutputFile().Valid() {
222 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400223 }
224 return android.Paths{}, nil
225 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500226 case "unstripped":
227 if mod.compiler != nil {
228 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
229 }
230 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400231 default:
232 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
233 }
234}
235
Ivan Lozano52767be2019-10-18 14:49:46 -0700236func (mod *Module) SelectedStl() string {
237 return ""
238}
239
Ivan Lozano2b262972019-11-21 12:30:50 -0800240func (mod *Module) NonCcVariants() bool {
241 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400242 if _, ok := mod.compiler.(libraryInterface); ok {
243 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800244 }
245 }
246 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
247}
248
Ivan Lozano52767be2019-10-18 14:49:46 -0700249func (mod *Module) Static() bool {
250 if mod.compiler != nil {
251 if library, ok := mod.compiler.(libraryInterface); ok {
252 return library.static()
253 }
254 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400255 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700256}
257
258func (mod *Module) Shared() bool {
259 if mod.compiler != nil {
260 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400261 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700262 }
263 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400264 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700265}
266
Ivan Lozanod7586b62021-04-01 09:49:36 -0400267func (mod *Module) Dylib() bool {
268 if mod.compiler != nil {
269 if library, ok := mod.compiler.(libraryInterface); ok {
270 return library.dylib()
271 }
272 }
273 return false
274}
275
Ivan Lozanod106efe2023-09-21 23:30:26 -0400276func (mod *Module) Source() bool {
277 if mod.compiler != nil {
278 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
279 return library.source()
280 }
281 }
282 return false
283}
284
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400285func (mod *Module) RlibStd() bool {
286 if mod.compiler != nil {
287 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
288 return library.rlibStd()
289 }
290 }
291 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
292}
293
Ivan Lozanod7586b62021-04-01 09:49:36 -0400294func (mod *Module) Rlib() bool {
295 if mod.compiler != nil {
296 if library, ok := mod.compiler.(libraryInterface); ok {
297 return library.rlib()
298 }
299 }
300 return false
301}
302
303func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400304 if binary, ok := mod.compiler.(binaryInterface); ok {
305 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400306 }
307 return false
308}
309
Justin Yun5e035862021-06-29 20:50:37 +0900310func (mod *Module) StaticExecutable() bool {
311 if !mod.Binary() {
312 return false
313 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400314 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900315}
316
Ivan Lozanod7586b62021-04-01 09:49:36 -0400317func (mod *Module) Object() bool {
318 // Rust has no modules which produce only object files.
319 return false
320}
321
Ivan Lozano52767be2019-10-18 14:49:46 -0700322func (mod *Module) Toc() android.OptionalPath {
323 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400324 if lib, ok := mod.compiler.(libraryInterface); ok {
325 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700326 }
327 }
328 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
329}
330
Colin Crossc511bc52020-04-07 16:50:32 +0000331func (mod *Module) UseSdk() bool {
332 return false
333}
334
Ivan Lozanod7586b62021-04-01 09:49:36 -0400335func (mod *Module) RelativeInstallPath() string {
336 if mod.compiler != nil {
337 return mod.compiler.relativeInstallPath()
338 }
339 return ""
340}
341
Ivan Lozano52767be2019-10-18 14:49:46 -0700342func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500343 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700344}
345
Jiyong Park7d55b612021-06-11 17:22:09 +0900346func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400347 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900348}
349
Ivan Lozano52767be2019-10-18 14:49:46 -0700350func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400351 return true
352}
353
354func (mod *Module) SubName() string {
355 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700356}
357
358func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500359 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700360 return false
361}
362
Ivan Lozanof9e21722020-12-02 09:00:51 -0500363func (mod *Module) IsVndkExt() bool {
364 return false
365}
366
Ivan Lozanod7586b62021-04-01 09:49:36 -0400367func (mod *Module) IsVndkSp() bool {
368 return false
369}
370
Ivan Lozanof1868af2022-04-12 13:08:36 -0400371func (mod *Module) IsVndkPrebuiltLibrary() bool {
372 // Rust modules do not provide VNDK prebuilts
373 return false
374}
375
376func (mod *Module) IsVendorPublicLibrary() bool {
377 return mod.VendorProperties.IsVendorPublicLibrary
378}
379
380func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
381 // Rust modules to not provide Sdk variants
382 return false
383}
384
Colin Cross127bb8b2020-12-16 16:46:01 -0800385func (c *Module) IsVndkPrivate() bool {
386 return false
387}
388
389func (c *Module) IsLlndk() bool {
390 return false
391}
392
393func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500394 return false
395}
396
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400397func (mod *Module) KernelHeadersDecorator() bool {
398 return false
399}
400
Colin Cross1f3f1302021-04-26 18:37:44 -0700401func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400402 return false
403}
404
Colin Cross5271fea2021-04-27 13:06:04 -0700405func (m *Module) NeedsVendorPublicLibraryVariants() bool {
406 return false
407}
408
Ivan Lozanod7586b62021-04-01 09:49:36 -0400409func (mod *Module) HasLlndkStubs() bool {
410 return false
411}
412
413func (mod *Module) StubsVersion() string {
414 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
415}
416
Ivan Lozano52767be2019-10-18 14:49:46 -0700417func (mod *Module) SdkVersion() string {
418 return ""
419}
420
Colin Crossc511bc52020-04-07 16:50:32 +0000421func (mod *Module) AlwaysSdk() bool {
422 return false
423}
424
Jiyong Park2286afd2020-06-16 21:58:53 +0900425func (mod *Module) IsSdkVariant() bool {
426 return false
427}
428
Colin Cross1348ce32020-10-01 13:37:16 -0700429func (mod *Module) SplitPerApiLevel() bool {
430 return false
431}
432
Sasha Smundaka76acba2022-04-18 20:12:56 -0700433func (mod *Module) XrefRustFiles() android.Paths {
434 return mod.kytheFiles
435}
436
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400438 Dylibs []string
439 Rlibs []string
440 Rustlibs []string
441 Stdlibs []string
442 ProcMacros []string
443 SharedLibs []string
444 StaticLibs []string
445 WholeStaticLibs []string
446 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400448 // Used for data dependencies adjacent to tests
449 DataLibs []string
450 DataBins []string
451
Colin Crossfe605e12022-01-23 20:46:16 -0800452 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453}
454
455type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700456 DyLibs RustLibraries
457 RLibs RustLibraries
458 SharedLibs android.Paths
459 SharedLibDeps android.Paths
460 StaticLibs android.Paths
461 ProcMacros RustLibraries
462 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500463
464 // depFlags and depLinkFlags are rustc and linker (clang) flags.
465 depFlags []string
466 depLinkFlags []string
467
468 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
469 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000470 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700471 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700472
Ivan Lozano45901ed2020-07-24 16:05:01 -0400473 // Used by bindgen modules which call clang
474 depClangFlags []string
475 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400476 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400477 depSystemIncludePaths android.Paths
478
Colin Crossfe605e12022-01-23 20:46:16 -0800479 CrtBegin android.Paths
480 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700481
482 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500483 SrcDeps android.Paths
484 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485}
486
487type RustLibraries []RustLibrary
488
489type RustLibrary struct {
490 Path android.Path
491 CrateName string
492}
493
Matthew Maurerbb3add12020-06-25 09:34:12 -0700494type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000495 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700496 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700497}
498
Sasha Smundaka76acba2022-04-18 20:12:56 -0700499type xref interface {
500 XrefRustFiles() android.Paths
501}
502
Matthew Maurerbb3add12020-06-25 09:34:12 -0700503type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000504 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700505 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700506}
507
Wen-yi Chu41326c12023-09-22 03:58:59 +0000508func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
509 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700510}
511
Colin Cross004bd3f2023-10-02 11:39:17 -0700512func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
513 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400514}
515
Colin Cross0de8a1e2020-09-18 14:15:30 -0700516func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
517 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700518 LinkDirs: flagExporter.linkDirs,
519 LinkObjects: flagExporter.linkObjects,
520 })
521}
522
Matthew Maurerbb3add12020-06-25 09:34:12 -0700523var _ exportedFlagsProducer = (*flagExporter)(nil)
524
525func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700526 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700527}
528
Colin Cross0de8a1e2020-09-18 14:15:30 -0700529type FlagExporterInfo struct {
530 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000531 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700532 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700533}
534
535var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
536
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400537func (mod *Module) isCoverageVariant() bool {
538 return mod.coverage.Properties.IsCoverageVariant
539}
540
541var _ cc.Coverage = (*Module)(nil)
542
543func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
544 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
545}
546
Ivan Lozanod7586b62021-04-01 09:49:36 -0400547func (mod *Module) VndkVersion() string {
548 return mod.Properties.VndkVersion
549}
550
551func (mod *Module) PreventInstall() bool {
552 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400553}
554
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400555func (mod *Module) MarkAsCoverageVariant(coverage bool) {
556 mod.coverage.Properties.IsCoverageVariant = coverage
557}
558
559func (mod *Module) EnableCoverageIfNeeded() {
560 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700561}
562
563func defaultsFactory() android.Module {
564 return DefaultsFactory()
565}
566
567type Defaults struct {
568 android.ModuleBase
569 android.DefaultsModuleBase
570}
571
572func DefaultsFactory(props ...interface{}) android.Module {
573 module := &Defaults{}
574
575 module.AddProperties(props...)
576 module.AddProperties(
577 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800578 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500579 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100580 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400581 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700582 &BaseCompilerProperties{},
583 &BinaryCompilerProperties{},
584 &LibraryCompilerProperties{},
585 &ProcMacroCompilerProperties{},
586 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400587 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700588 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400589 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400590 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200591 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500592 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000593 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594 )
595
596 android.InitDefaultsModule(module)
597 return module
598}
599
600func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700601 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602}
603
Ivan Lozano183a3212019-10-18 14:18:45 -0700604func (mod *Module) CcLibrary() bool {
605 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500606 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700607 return true
608 }
609 }
610 return false
611}
612
613func (mod *Module) CcLibraryInterface() bool {
614 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400615 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
616 // VariantIs{Static,Shared} is set.
617 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700618 return true
619 }
620 }
621 return false
622}
623
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400624func (mod *Module) RustLibraryInterface() bool {
625 if mod.compiler != nil {
626 if _, ok := mod.compiler.(libraryInterface); ok {
627 return true
628 }
629 }
630 return false
631}
632
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500633func (mod *Module) IsFuzzModule() bool {
634 if _, ok := mod.compiler.(*fuzzDecorator); ok {
635 return true
636 }
637 return false
638}
639
640func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
641 return mod.FuzzModule
642}
643
644func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
645 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
646 return fuzzer.fuzzPackagedModule
647 }
648 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
649}
650
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000651func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500652 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
653 return fuzzer.sharedLibraries
654 }
655 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
656}
657
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400658func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400659 if mod.compiler != nil {
660 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400661 }
662 return nil
663}
664
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800665func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700666 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700667 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800668 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700669 }
670 }
671 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
672}
673
674func (mod *Module) SetStatic() {
675 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700676 if library, ok := mod.compiler.(libraryInterface); ok {
677 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700678 return
679 }
680 }
681 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
682}
683
684func (mod *Module) SetShared() {
685 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700686 if library, ok := mod.compiler.(libraryInterface); ok {
687 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700688 return
689 }
690 }
691 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
692}
693
Ivan Lozano183a3212019-10-18 14:18:45 -0700694func (mod *Module) BuildStaticVariant() bool {
695 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700696 if library, ok := mod.compiler.(libraryInterface); ok {
697 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700698 }
699 }
700 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
701}
702
703func (mod *Module) BuildSharedVariant() bool {
704 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700705 if library, ok := mod.compiler.(libraryInterface); ok {
706 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700707 }
708 }
709 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
710}
711
Ivan Lozano183a3212019-10-18 14:18:45 -0700712func (mod *Module) Module() android.Module {
713 return mod
714}
715
Ivan Lozano183a3212019-10-18 14:18:45 -0700716func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400717 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700718}
719
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400720func (mod *Module) CoverageFiles() android.Paths {
721 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800722 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400723 }
724 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
725}
726
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400727// Rust does not produce gcno files, and therefore does not produce a coverage archive.
728func (mod *Module) CoverageOutputFile() android.OptionalPath {
729 return android.OptionalPath{}
730}
731
732func (mod *Module) IsNdk(config android.Config) bool {
733 return false
734}
735
736func (mod *Module) IsStubs() bool {
737 return false
738}
739
Jiyong Park459feca2020-12-15 11:02:21 +0900740func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900741 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900742 return false
743 }
744
Jiyong Park459feca2020-12-15 11:02:21 +0900745 // The apex variant is not installable because it is included in the APEX and won't appear
746 // in the system partition as a standalone file.
747 if !apexInfo.IsForPlatform() {
748 return false
749 }
750
Jiyong Parke54f07e2021-04-07 15:08:04 +0900751 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900752}
753
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500754func (ctx moduleContext) apexVariationName() string {
755 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
756}
757
Ivan Lozano183a3212019-10-18 14:18:45 -0700758var _ cc.LinkableInterface = (*Module)(nil)
759
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760func (mod *Module) Init() android.Module {
761 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500762 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763
Yi Kong46c6e592022-01-20 22:55:00 +0800764 if mod.afdo != nil {
765 mod.AddProperties(mod.afdo.props()...)
766 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767 if mod.compiler != nil {
768 mod.AddProperties(mod.compiler.compilerProps()...)
769 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400770 if mod.coverage != nil {
771 mod.AddProperties(mod.coverage.props()...)
772 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200773 if mod.clippy != nil {
774 mod.AddProperties(mod.clippy.props()...)
775 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400776 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700777 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400778 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500779 if mod.sanitize != nil {
780 mod.AddProperties(mod.sanitize.props()...)
781 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400782
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900784 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785
786 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787 return mod
788}
789
790func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
791 return &Module{
792 hod: hod,
793 multilib: multilib,
794 }
795}
796func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
797 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800798 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400799 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200800 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500801 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802 return module
803}
804
805type ModuleContext interface {
806 android.ModuleContext
807 ModuleContextIntf
808}
809
810type BaseModuleContext interface {
811 android.BaseModuleContext
812 ModuleContextIntf
813}
814
815type DepsContext interface {
816 android.BottomUpMutatorContext
817 ModuleContextIntf
818}
819
820type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200821 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823}
824
825type depsContext struct {
826 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827}
828
829type moduleContext struct {
830 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831}
832
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200833type baseModuleContext struct {
834 android.BaseModuleContext
835}
836
837func (ctx *moduleContext) RustModule() *Module {
838 return ctx.Module().(*Module)
839}
840
841func (ctx *moduleContext) toolchain() config.Toolchain {
842 return ctx.RustModule().toolchain(ctx)
843}
844
845func (ctx *depsContext) RustModule() *Module {
846 return ctx.Module().(*Module)
847}
848
849func (ctx *depsContext) toolchain() config.Toolchain {
850 return ctx.RustModule().toolchain(ctx)
851}
852
853func (ctx *baseModuleContext) RustModule() *Module {
854 return ctx.Module().(*Module)
855}
856
857func (ctx *baseModuleContext) toolchain() config.Toolchain {
858 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400859}
860
861func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700862 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
863 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
864 return false
865 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400866 return mod.compiler != nil && mod.compiler.nativeCoverage()
867}
868
Ivan Lozanod7586b62021-04-01 09:49:36 -0400869func (mod *Module) EverInstallable() bool {
870 return mod.compiler != nil &&
871 // Check to see whether the module is actually ever installable.
872 mod.compiler.everInstallable()
873}
874
875func (mod *Module) Installable() *bool {
876 return mod.Properties.Installable
877}
878
Ivan Lozano872d5792022-03-23 17:31:39 -0400879func (mod *Module) ProcMacro() bool {
880 if pm, ok := mod.compiler.(procMacroInterface); ok {
881 return pm.ProcMacro()
882 }
883 return false
884}
885
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
887 if mod.cachedToolchain == nil {
888 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
889 }
890 return mod.cachedToolchain
891}
892
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200893func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
894 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
895}
896
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
898}
899
900func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
901 ctx := &moduleContext{
902 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700903 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904
Jiyong Park99644e92020-11-17 22:21:02 +0900905 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
906 if !apexInfo.IsForPlatform() {
907 mod.hideApexVariantFromMake = true
908 }
909
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500911 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
912
Ivan Lozanof1868af2022-04-12 13:08:36 -0400913 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700914
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915 if !toolchain.Supported() {
916 // This toolchain's unsupported, there's nothing to do for this mod.
917 return
918 }
919
920 deps := mod.depsToPaths(ctx)
921 flags := Flags{
922 Toolchain: toolchain,
923 }
924
Ivan Lozano67eada32021-09-23 11:50:33 -0400925 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800926 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500927 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800928 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929 if mod.compiler != nil {
930 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400931 flags = mod.compiler.cfgFlags(ctx, flags)
932 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400933 }
934 if mod.coverage != nil {
935 flags, deps = mod.coverage.flags(ctx, flags, deps)
936 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200937 if mod.clippy != nil {
938 flags, deps = mod.clippy.flags(ctx, flags, deps)
939 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500940 if mod.sanitize != nil {
941 flags, deps = mod.sanitize.flags(ctx, flags, deps)
942 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400943
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200944 // SourceProvider needs to call GenerateSource() before compiler calls
945 // compile() so it can provide the source. A SourceProvider has
946 // multiple variants (e.g. source, rlib, dylib). Only the "source"
947 // variant is responsible for effectively generating the source. The
948 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400949 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200950 if mod.compiler.(libraryInterface).source() {
951 mod.sourceProvider.GenerateSource(ctx, deps)
952 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
953 } else {
954 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
955 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700956 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200957 }
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000958 ctx.SetProvider(blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400959 }
960
961 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100962 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700963 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400964 if ctx.Failed() {
965 return
966 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700967 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
968 if buildOutput.kytheFile != nil {
969 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
970 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400971 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900972
Dan Albert06feee92021-03-19 15:06:02 -0700973 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000974 if mod.docTimestampFile.Valid() {
975 ctx.CheckbuildFile(mod.docTimestampFile.Path())
976 }
Dan Albert06feee92021-03-19 15:06:02 -0700977
Ivan Lozano1921e802021-05-20 13:39:16 -0400978 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
979 // RECOVERY_SNAPSHOT_VERSION is current.
980 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
981 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
982 lib.collectHeadersForSnapshot(ctx, deps)
983 }
984 }
985
Jiyong Park459feca2020-12-15 11:02:21 +0900986 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400987 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900988 // If the module has been specifically configure to not be installed then
989 // hide from make as otherwise it will break when running inside make as the
990 // output path to install will not be specified. Not all uninstallable
991 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400992 // side dependencies. In particular, proc-macros need to be captured in the
993 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900994 mod.HideFromMake()
995 } else if !mod.installable(apexInfo) {
996 mod.SkipInstall()
997 }
998
999 // Still call install though, the installs will be stored as PackageSpecs to allow
1000 // using the outputs in a genrule.
1001 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001002 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001003 if ctx.Failed() {
1004 return
1005 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001006 }
Chris Wailes74be7642021-07-22 16:20:28 -07001007
1008 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001009 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001010 if mod.testModule {
1011 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
1012 }
LaMont Jones0c971852023-12-07 21:56:59 +00001013
1014 aconfig.CollectDependencyAconfigFiles(ctx, &mod.mergedAconfigFiles)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015}
1016
1017func (mod *Module) deps(ctx DepsContext) Deps {
1018 deps := Deps{}
1019
1020 if mod.compiler != nil {
1021 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001022 }
1023 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001024 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 }
1026
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001027 if mod.coverage != nil {
1028 deps = mod.coverage.deps(ctx, deps)
1029 }
1030
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001031 if mod.sanitize != nil {
1032 deps = mod.sanitize.deps(ctx, deps)
1033 }
1034
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1036 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001037 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1039 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1040 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001041 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001042 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 return deps
1044
1045}
1046
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047type dependencyTag struct {
1048 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001049 name string
1050 library bool
1051 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001052 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053}
1054
Jiyong Park65b62242020-11-25 12:44:59 +09001055// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1056// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1057func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001058 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001059}
1060
1061var _ android.InstallNeededDependencyTag = dependencyTag{}
1062
Colin Cross65cb3142021-12-10 23:05:02 +00001063func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1064 if d.library && d.dynamic {
1065 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1066 }
1067 return nil
1068}
1069
1070var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1071
Ivan Lozanoffee3342019-08-27 12:03:00 -07001072var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001073 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1074 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001075 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001076 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001077 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001078 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001079 dataLibDepTag = dependencyTag{name: "data lib"}
1080 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001081)
1082
Jiyong Park99644e92020-11-17 22:21:02 +09001083func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1084 tag, ok := depTag.(dependencyTag)
1085 return ok && tag == dylibDepTag
1086}
1087
Jiyong Park94e22fd2021-04-08 18:19:15 +09001088func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1089 tag, ok := depTag.(dependencyTag)
1090 return ok && tag == rlibDepTag
1091}
1092
Matthew Maurer0f003b12020-06-29 14:34:06 -07001093type autoDep struct {
1094 variation string
1095 depTag dependencyTag
1096}
1097
1098var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001099 rlibVariation = "rlib"
1100 dylibVariation = "dylib"
1101 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1102 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001103)
1104
1105type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001106 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001107}
1108
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001109func (mod *Module) begin(ctx BaseModuleContext) {
1110 if mod.coverage != nil {
1111 mod.coverage.begin(ctx)
1112 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001113 if mod.sanitize != nil {
1114 mod.sanitize.begin(ctx)
1115 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001116}
1117
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001118func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001119 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001120 return p.prebuilt()
1121 }
1122 return nil
1123}
1124
Justin Yun24b246a2023-03-16 10:36:16 +09001125func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1126 if rustDep, ok := dep.(*Module); ok {
1127 // Use base module name for snapshots when exporting to Makefile.
1128 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1129 baseName := rustDep.BaseModuleName()
1130 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1131 }
1132 }
1133 return cc.MakeLibName(ctx, c, dep, depName)
1134}
1135
Ivan Lozanod106efe2023-09-21 23:30:26 -04001136func collectIncludedProtos(mod *Module, dep *Module) {
1137 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1138 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1139 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1140 }
1141 }
1142}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001143func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1144 var depPaths PathDeps
1145
1146 directRlibDeps := []*Module{}
1147 directDylibDeps := []*Module{}
1148 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001149 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001150 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001151 directSrcProvidersDeps := []*Module{}
1152 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001153
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001154 // For the dependency from platform to apex, use the latest stubs
1155 mod.apexSdkVersion = android.FutureApiLevel
1156 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1157 if !apexInfo.IsForPlatform() {
1158 mod.apexSdkVersion = apexInfo.MinSdkVersion
1159 }
1160
1161 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1162 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1163 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1164 // (b/144430859)
1165 mod.apexSdkVersion = android.FutureApiLevel
1166 }
1167
Spandan Das604f3762023-03-16 22:51:40 +00001168 skipModuleList := map[string]bool{}
1169
1170 var apiImportInfo multitree.ApiImportInfo
1171 hasApiImportInfo := false
1172
1173 ctx.VisitDirectDeps(func(dep android.Module) {
1174 if dep.Name() == "api_imports" {
1175 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1176 hasApiImportInfo = true
1177 }
1178 })
1179
1180 if hasApiImportInfo {
1181 targetStubModuleList := map[string]string{}
1182 targetOrigModuleList := map[string]string{}
1183
1184 // Search for dependency which both original module and API imported library with APEX stub exists
1185 ctx.VisitDirectDeps(func(dep android.Module) {
1186 depName := ctx.OtherModuleName(dep)
1187 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1188 targetStubModuleList[apiLibrary] = depName
1189 }
1190 })
1191 ctx.VisitDirectDeps(func(dep android.Module) {
1192 depName := ctx.OtherModuleName(dep)
1193 if origLibrary, ok := targetStubModuleList[depName]; ok {
1194 targetOrigModuleList[origLibrary] = depName
1195 }
1196 })
1197
1198 // Decide which library should be used between original and API imported library
1199 ctx.VisitDirectDeps(func(dep android.Module) {
1200 depName := ctx.OtherModuleName(dep)
1201 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1202 if cc.ShouldUseStubForApex(ctx, dep) {
1203 skipModuleList[depName] = true
1204 } else {
1205 skipModuleList[apiLibrary] = true
1206 }
1207 }
1208 })
1209 }
1210
Cole Faustb6e6f992023-08-17 17:42:26 -07001211 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1212 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001213
Ivan Lozanoffee3342019-08-27 12:03:00 -07001214 ctx.VisitDirectDeps(func(dep android.Module) {
1215 depName := ctx.OtherModuleName(dep)
1216 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001217
Spandan Das604f3762023-03-16 22:51:40 +00001218 if _, exists := skipModuleList[depName]; exists {
1219 return
1220 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001221
1222 if depTag == android.DarwinUniversalVariantTag {
1223 return
1224 }
1225
Ivan Lozano89435d12020-07-31 11:01:18 -04001226 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001227 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001228 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001229
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230 switch depTag {
1231 case dylibDepTag:
1232 dylib, ok := rustDep.compiler.(libraryInterface)
1233 if !ok || !dylib.dylib() {
1234 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1235 return
1236 }
1237 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001238 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001239 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1240
Ivan Lozanoffee3342019-08-27 12:03:00 -07001241 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001242
Ivan Lozanoffee3342019-08-27 12:03:00 -07001243 rlib, ok := rustDep.compiler.(libraryInterface)
1244 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001245 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 return
1247 }
1248 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001249 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001250 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1251
Ivan Lozanoffee3342019-08-27 12:03:00 -07001252 case procMacroDepTag:
1253 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001254 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001255
1256 case sourceDepTag:
1257 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1258 collectIncludedProtos(mod, rustDep)
1259 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001260 }
1261
Cole Faustb6e6f992023-08-17 17:42:26 -07001262 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1263
Paul Duffind5cf92e2021-07-09 17:38:55 +01001264 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001265 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1266 // OS/Arch variant is used.
1267 var helper string
1268 if ctx.Host() {
1269 helper = "missing 'host_supported'?"
1270 } else {
1271 helper = "device module defined?"
1272 }
1273
1274 if dep.Target().Os != ctx.Os() {
1275 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1276 return
1277 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1278 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1279 return
1280 }
1281 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001282 }
1283
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001284 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001285 if depTag != procMacroDepTag {
1286 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1287 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1288 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1289 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001290 }
1291
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001293 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001294 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001295 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001296 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001297 }
1298 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001299 if depTag == sourceDepTag {
1300 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1301 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
1302 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1303 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1304 }
1305 }
1306 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001307 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001308 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001309 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001310 if _, ok := ccDep.(*Module); !ok {
1311 if ccDep.Module().Target().Os != ctx.Os() {
1312 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1313 return
1314 }
1315 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1316 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1317 return
1318 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001319 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001320 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001321 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001322 if !ctx.Config().AllowMissingDependencies() {
1323 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1324 } else {
1325 ctx.AddMissingDependencies([]string{depName})
1326 }
1327 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001328 }
1329
Wen-yi Chu41326c12023-09-22 03:58:59 +00001330 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001331
Ivan Lozanoffee3342019-08-27 12:03:00 -07001332 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001333 switch {
1334 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001335 if cc.IsWholeStaticLib(depTag) {
1336 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1337 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001338 if mod.Binary() {
1339 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1340 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1341 // final linkage, pass the args directly to the linker to handle these cases.
1342 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1343 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001344 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001345 } else {
1346 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 -05001347 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001348 }
1349
1350 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1351 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001352 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001353 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1354
Colin Cross0de8a1e2020-09-18 14:15:30 -07001355 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1356 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1357 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1358 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1359 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001360 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001361
1362 // Record baseLibName for snapshots.
1363 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1364
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001365 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001366 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001367 // For the shared lib dependencies, we may link to the stub variant
1368 // of the dependency depending on the context (e.g. if this
1369 // dependency crosses the APEX boundaries).
1370 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1371
1372 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1373 // object (either from stub or non-stub) to use.
1374 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001375 if !linkObject.Valid() {
1376 if !ctx.Config().AllowMissingDependencies() {
1377 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1378 } else {
1379 ctx.AddMissingDependencies([]string{depName})
1380 }
1381 return
1382 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001383 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001384
Ivan Lozanoffee3342019-08-27 12:03:00 -07001385 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001386 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001387 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1388 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1389 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1390 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001391 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001392
1393 // Record baseLibName for snapshots.
1394 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1395
Cole Faustb6e6f992023-08-17 17:42:26 -07001396 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001397 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001398 case cc.IsHeaderDepTag(depTag):
1399 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1400 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1401 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1402 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001403 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001404 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001405 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001406 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001407 }
1408
1409 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001410 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1411 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001412 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001413 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001414 } else {
1415 switch {
1416 case depTag == cc.CrtBeginDepTag:
1417 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1418 case depTag == cc.CrtEndDepTag:
1419 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1420 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001421 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001422
1423 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001424 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001425 // These are usually genrules which don't have per-target variants.
1426 directSrcDeps = append(directSrcDeps, srcDep)
1427 }
1428 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001429 })
1430
Wen-yi Chu41326c12023-09-22 03:58:59 +00001431 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1432
1433 var rlibDepFiles RustLibraries
1434 for _, dep := range directRlibDeps {
1435 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1436 }
1437 var dylibDepFiles RustLibraries
1438 for _, dep := range directDylibDeps {
1439 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1440 }
1441 var procMacroDepFiles RustLibraries
1442 for _, dep := range directProcMacroDeps {
1443 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1444 }
1445
Colin Cross004bd3f2023-10-02 11:39:17 -07001446 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001448 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001449 }
1450
Colin Cross004bd3f2023-10-02 11:39:17 -07001451 var sharedLibFiles android.Paths
1452 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001453 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001454 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001455 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001456 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001457 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001458 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001459 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001460 }
1461
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001462 var srcProviderDepFiles android.Paths
1463 for _, dep := range directSrcProvidersDeps {
1464 srcs, _ := dep.OutputFiles("")
1465 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1466 }
1467 for _, dep := range directSrcDeps {
1468 srcs := dep.Srcs()
1469 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1470 }
1471
Wen-yi Chu41326c12023-09-22 03:58:59 +00001472 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1473 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001474 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1475 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1476 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001477 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001478 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001479
1480 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001481 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001482 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001484 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1485 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1486 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001487
1488 return depPaths
1489}
1490
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001491func (mod *Module) InstallInData() bool {
1492 if mod.compiler == nil {
1493 return false
1494 }
1495 return mod.compiler.inData()
1496}
1497
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001498func (mod *Module) InstallInRamdisk() bool {
1499 return mod.InRamdisk()
1500}
1501
1502func (mod *Module) InstallInVendorRamdisk() bool {
1503 return mod.InVendorRamdisk()
1504}
1505
1506func (mod *Module) InstallInRecovery() bool {
1507 return mod.InRecovery()
1508}
1509
Wen-yi Chu41326c12023-09-22 03:58:59 +00001510func linkPathFromFilePath(filepath android.Path) string {
1511 return strings.Split(filepath.String(), filepath.Base())[0]
1512}
1513
Spandan Das604f3762023-03-16 22:51:40 +00001514// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1515func (r *Module) usePublicApi() bool {
1516 return r.Device() && r.UseSdk()
1517}
1518
1519// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1520func (r *Module) useVendorApi() bool {
1521 return r.Device() && (r.InVendor() || r.InProduct())
1522}
1523
Ivan Lozanoffee3342019-08-27 12:03:00 -07001524func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1525 ctx := &depsContext{
1526 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001527 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001528
1529 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001530 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001531 var snapshotInfo *cc.SnapshotInfo
1532
Kiyoung Kim487689e2022-07-26 09:48:22 +09001533 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001534 if mod.usePublicApi() || mod.useVendorApi() {
1535 for idx, lib := range deps.SharedLibs {
1536 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1537 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001538 }
1539
Ivan Lozano1921e802021-05-20 13:39:16 -04001540 if ctx.Os() == android.Android {
1541 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1542 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001543
Ivan Lozano2b081132020-09-08 12:46:52 -04001544 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001545 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001546 stdLinkage = "rlib-std"
1547 }
1548
1549 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001550
Ivan Lozano2b081132020-09-08 12:46:52 -04001551 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1552 rlibDepVariations = append(rlibDepVariations,
1553 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1554 }
1555
Ivan Lozano1921e802021-05-20 13:39:16 -04001556 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001557 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001558 for _, lib := range deps.Rlibs {
1559 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001560 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001561
Ivan Lozano2d407632022-04-07 12:59:11 -04001562 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001563 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001564
1565 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001566 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1567 for _, lib := range deps.Dylibs {
1568 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1569 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001570
Ivan Lozano1921e802021-05-20 13:39:16 -04001571 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001572 if deps.Rustlibs != nil {
1573 if !mod.compiler.Disabled() {
1574 for _, lib := range deps.Rustlibs {
1575 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1576 if autoDep.depTag == rlibDepTag {
1577 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001578 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001579 } else {
1580 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1581 // Check for the existence of the dylib deptag variant. Select it if available,
1582 // otherwise select the rlib variant.
1583 autoDepVariations := append(commonDepVariations,
1584 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1585
1586 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1587
1588 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1589 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1590 } else {
1591 // If there's no dylib dependency available, try to add the rlib dependency instead.
1592 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1593 }
1594 }
1595 }
1596 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1597 for _, lib := range deps.Rustlibs {
1598 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1599 srcProviderVariations := append(commonDepVariations,
1600 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1601
1602 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1603 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001604 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001605 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001606 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001607 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001608
Ivan Lozano1921e802021-05-20 13:39:16 -04001609 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001610 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001611 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001612 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001613 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001614 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001615 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001616 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001617 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001618 for _, lib := range deps.Stdlibs {
1619 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1620 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001621 }
1622 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001623
1624 for _, lib := range deps.SharedLibs {
1625 depTag := cc.SharedDepTag()
1626 name, version := cc.StubsLibNameAndVersion(lib)
1627
1628 variations := []blueprint.Variation{
1629 {Mutator: "link", Variation: "shared"},
1630 }
Spandan Das604f3762023-03-16 22:51:40 +00001631 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1632 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1633 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1634 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1635 }
1636
1637 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1638 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1639 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001640 }
1641
1642 for _, lib := range deps.WholeStaticLibs {
1643 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001644 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001645
1646 actx.AddVariationDependencies([]blueprint.Variation{
1647 {Mutator: "link", Variation: "static"},
1648 }, depTag, lib)
1649 }
1650
1651 for _, lib := range deps.StaticLibs {
1652 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001653 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001654
1655 actx.AddVariationDependencies([]blueprint.Variation{
1656 {Mutator: "link", Variation: "static"},
1657 }, depTag, lib)
1658 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001659
Zach Johnson3df4e632020-11-06 11:56:27 -08001660 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1661
Colin Cross565cafd2020-09-25 18:47:38 -07001662 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001663 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001664 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001665 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001666 }
Colin Crossfe605e12022-01-23 20:46:16 -08001667 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001668 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001669 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001670 }
1671
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001672 if mod.sourceProvider != nil {
1673 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1674 bindgen.Properties.Custom_bindgen != "" {
1675 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1676 bindgen.Properties.Custom_bindgen)
1677 }
1678 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001679
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001680 actx.AddVariationDependencies([]blueprint.Variation{
1681 {Mutator: "link", Variation: "shared"},
1682 }, dataLibDepTag, deps.DataLibs...)
1683
1684 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1685
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001686 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001687 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001688
1689 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001690}
1691
Ivan Lozano2d407632022-04-07 12:59:11 -04001692// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001693func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1694 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001695 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1696}
1697
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001698func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1699 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1700 actx.AddVariationDependencies(variations, depTag, lib)
1701}
1702
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001703func BeginMutator(ctx android.BottomUpMutatorContext) {
1704 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1705 mod.beginMutator(ctx)
1706 }
1707}
1708
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001709func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1710 ctx := &baseModuleContext{
1711 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001712 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001713
1714 mod.begin(ctx)
1715}
1716
Ivan Lozanoffee3342019-08-27 12:03:00 -07001717func (mod *Module) Name() string {
1718 name := mod.ModuleBase.Name()
1719 if p, ok := mod.compiler.(interface {
1720 Name(string) string
1721 }); ok {
1722 name = p.Name(name)
1723 }
1724 return name
1725}
1726
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001727func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001728 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001729 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001730 }
1731}
1732
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001733var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001734var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001735
1736func (mod *Module) HostToolPath() android.OptionalPath {
1737 if !mod.Host() {
1738 return android.OptionalPath{}
1739 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001740 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1741 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001742 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1743 // Even though proc-macros aren't strictly "tools", since they target the compiler
1744 // and act as compiler plugins, we treat them similarly.
1745 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001746 }
1747 return android.OptionalPath{}
1748}
1749
Jiyong Park99644e92020-11-17 22:21:02 +09001750var _ android.ApexModule = (*Module)(nil)
1751
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001752func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001753 return String(mod.Properties.Min_sdk_version)
1754}
1755
Jiyong Park45bf82e2020-12-15 22:29:02 +09001756// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001757func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001758 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001759 if minSdkVersion == "apex_inherit" {
1760 return nil
1761 }
1762 if minSdkVersion == "" {
1763 return fmt.Errorf("min_sdk_version is not specificed")
1764 }
1765
1766 // Not using nativeApiLevelFromUser because the context here is not
1767 // necessarily a native context.
1768 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1769 if err != nil {
1770 return err
1771 }
1772
1773 if ver.GreaterThan(sdkVersion) {
1774 return fmt.Errorf("newer SDK(%v)", ver)
1775 }
Jiyong Park99644e92020-11-17 22:21:02 +09001776 return nil
1777}
1778
Jiyong Park45bf82e2020-12-15 22:29:02 +09001779// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001780func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1781 depTag := ctx.OtherModuleDependencyTag(dep)
1782
1783 if ccm, ok := dep.(*cc.Module); ok {
1784 if ccm.HasStubsVariants() {
1785 if cc.IsSharedDepTag(depTag) {
1786 // dynamic dep to a stubs lib crosses APEX boundary
1787 return false
1788 }
1789 if cc.IsRuntimeDepTag(depTag) {
1790 // runtime dep to a stubs lib also crosses APEX boundary
1791 return false
1792 }
1793
1794 if cc.IsHeaderDepTag(depTag) {
1795 return false
1796 }
1797 }
1798 if mod.Static() && cc.IsSharedDepTag(depTag) {
1799 // shared_lib dependency from a static lib is considered as crossing
1800 // the APEX boundary because the dependency doesn't actually is
1801 // linked; the dependency is used only during the compilation phase.
1802 return false
1803 }
1804 }
1805
Matthew Maurer581b6d82022-09-29 16:46:25 -07001806 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001807 return false
1808 }
1809
1810 return true
1811}
1812
1813// Overrides ApexModule.IsInstallabeToApex()
1814func (mod *Module) IsInstallableToApex() bool {
1815 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001816 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001817 return true
1818 }
1819 if _, ok := mod.compiler.(*binaryDecorator); ok {
1820 return true
1821 }
1822 }
1823 return false
1824}
1825
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001826// If a library file has a "lib" prefix, extract the library name without the prefix.
1827func libNameFromFilePath(filepath android.Path) (string, bool) {
1828 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1829 if strings.HasPrefix(libName, "lib") {
1830 libName = libName[3:]
1831 return libName, true
1832 }
1833 return "", false
1834}
1835
Sasha Smundaka76acba2022-04-18 20:12:56 -07001836func kytheExtractRustFactory() android.Singleton {
1837 return &kytheExtractRustSingleton{}
1838}
1839
1840type kytheExtractRustSingleton struct {
1841}
1842
1843func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1844 var xrefTargets android.Paths
1845 ctx.VisitAllModules(func(module android.Module) {
1846 if rustModule, ok := module.(xref); ok {
1847 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1848 }
1849 })
1850 if len(xrefTargets) > 0 {
1851 ctx.Phony("xref_rust", xrefTargets...)
1852 }
1853}
1854
Jihoon Kangf78a8902022-09-01 22:47:07 +00001855func (c *Module) Partition() string {
1856 return ""
1857}
1858
Chris Parsons637458d2023-09-19 20:09:00 +00001859func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001860 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1861 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001862 } else if ctx.ModuleType() == "rust_proc_macro" {
1863 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001864 } else if ctx.ModuleType() == "rust_binary_host" {
1865 binaryBp2build(ctx, m)
Vinh Tran47faaad2023-09-25 14:47:19 -04001866 } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" {
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001867 protoLibraryBp2build(ctx, m)
Vinh Tran75159942023-10-17 16:28:04 -04001868 } else if ctx.ModuleType() == "rust_ffi_static" {
1869 ffiStaticBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001870 } else {
1871 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1872 }
1873}
1874
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001875// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001876// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001877// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1878// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1879// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001880func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001881 var srcs bazel.LabelList
1882 var compileData bazel.LabelList
1883
1884 if c.Properties.Srcs[0] == "src/lib.rs" {
1885 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1886 compileData = android.BazelLabelForModuleSrc(
1887 ctx,
1888 []string{
1889 "src/**/*.proto",
1890 "examples/**/*.rs",
1891 "**/*.md",
1892 },
1893 )
1894 } else {
1895 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1896 }
1897
1898 return srcs, compileData
1899}
1900
Ivan Lozanoffee3342019-08-27 12:03:00 -07001901var Bool = proptools.Bool
1902var BoolDefault = proptools.BoolDefault
1903var String = proptools.String
1904var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001905
1906var _ android.OutputFileProducer = (*Module)(nil)