blob: 15144acadd406ae3321b51fb4694cfe4c1715158 [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
29 "android/soong/android"
30 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020031 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070032 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000033 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070034 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040035 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070036)
37
38var pctx = android.NewPackageContext("android/soong/rust")
39
40func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070041 android.RegisterModuleType("rust_defaults", defaultsFactory)
42 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
43 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040044 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040045 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050046 })
47 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
48 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 })
Inseob Kim3b244062023-07-11 13:31:36 +090050 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070051 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020052 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000053 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070054}
55
56type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040057 GlobalRustFlags []string // Flags that apply globally to rust
58 GlobalLinkFlags []string // Flags that apply globally to linker
59 RustFlags []string // Flags that apply to rust
60 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070062 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070063 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040064 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020065 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070066 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070067}
68
69type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050070 AndroidMkRlibs []string `blueprint:"mutated"`
71 AndroidMkDylibs []string `blueprint:"mutated"`
72 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050073 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040074
Ivan Lozano6a884432020-12-02 09:15:16 -050075 ImageVariationPrefix string `blueprint:"mutated"`
76 VndkVersion string `blueprint:"mutated"`
77 SubName string `blueprint:"mutated"`
78
Ivan Lozanoc08897c2021-04-02 12:41:32 -040079 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
80 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
81 // appended before SubName.
82 RustSubName string `blueprint:"mutated"`
83
Ivan Lozano6a884432020-12-02 09:15:16 -050084 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 CoreVariantNeeded bool `blueprint:"mutated"`
86 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070087 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080088 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050089 ExtraVariants []string `blueprint:"mutated"`
90
Ivan Lozanoa2268632021-07-22 10:52:06 -040091 // Allows this module to use non-APEX version of libraries. Useful
92 // for building binaries that are started before APEXes are activated.
93 Bootstrap *bool
94
Ivan Lozano1921e802021-05-20 13:39:16 -040095 // Used by vendor snapshot to record dependencies from snapshot modules.
96 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090097 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040098 SnapshotRlibs []string `blueprint:"mutated"`
99 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400100
Matthew Maurerc6868382021-07-13 14:12:37 -0700101 // Make this module available when building for ramdisk.
102 // On device without a dedicated recovery partition, the module is only
103 // available after switching root into
104 // /first_stage_ramdisk. To expose the module before switching root, install
105 // the recovery variant instead.
106 Ramdisk_available *bool
107
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500108 // Make this module available when building for vendor ramdisk.
109 // On device without a dedicated recovery partition, the module is only
110 // available after switching root into
111 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800112 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500113 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400114
Ivan Lozano1921e802021-05-20 13:39:16 -0400115 // Normally Soong uses the directory structure to decide which modules
116 // should be included (framework) or excluded (non-framework) from the
117 // different snapshots (vendor, recovery, etc.), but this property
118 // allows a partner to exclude a module normally thought of as a
119 // framework module from the vendor snapshot.
120 Exclude_from_vendor_snapshot *bool
121
122 // Normally Soong uses the directory structure to decide which modules
123 // should be included (framework) or excluded (non-framework) from the
124 // different snapshots (vendor, recovery, etc.), but this property
125 // allows a partner to exclude a module normally thought of as a
126 // framework module from the recovery snapshot.
127 Exclude_from_recovery_snapshot *bool
128
Matthew Maurer460ee942021-02-11 12:31:46 -0800129 // Make this module available when building for recovery
130 Recovery_available *bool
131
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500132 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
133 Min_sdk_version *string
134
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900135 HideFromMake bool `blueprint:"mutated"`
136 PreventInstall bool `blueprint:"mutated"`
137
138 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139}
140
141type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700142 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143
Ivan Lozano6a884432020-12-02 09:15:16 -0500144 VendorProperties cc.VendorProperties
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 Properties BaseProperties
147
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000148 hod android.HostOrDeviceSupported
149 multilib android.Multilib
150 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151
Ivan Lozano6a884432020-12-02 09:15:16 -0500152 makeLinkType string
153
Yi Kong46c6e592022-01-20 22:55:00 +0800154 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400156 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200157 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500158 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400160 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700161 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400162
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400163 // Output file to be installed, may be stripped or unstripped.
164 outputFile android.OptionalPath
165
Sasha Smundaka76acba2022-04-18 20:12:56 -0700166 // Cross-reference input file
167 kytheFiles android.Paths
168
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400169 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900170
171 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500172
173 // For apex variants, this is set as apex.min_sdk_version
174 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700175
176 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400177
178 android.BazelModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179}
180
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500181func (mod *Module) Header() bool {
182 //TODO: If Rust libraries provide header variants, this needs to be updated.
183 return false
184}
185
186func (mod *Module) SetPreventInstall() {
187 mod.Properties.PreventInstall = true
188}
189
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500190func (mod *Module) SetHideFromMake() {
191 mod.Properties.HideFromMake = true
192}
193
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900194func (mod *Module) HiddenFromMake() bool {
195 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400196}
197
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500198func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500199 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
200 // nil since we need compiler to actually sanitize.
201 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202}
203
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500204func (mod *Module) IsPrebuilt() bool {
205 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
206 return true
207 }
208 return false
209}
210
Ivan Lozano43845682020-07-09 21:03:28 -0400211func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
212 switch tag {
213 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700214 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400215 return mod.sourceProvider.Srcs(), nil
216 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900217 if mod.OutputFile().Valid() {
218 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400219 }
220 return android.Paths{}, nil
221 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500222 case "unstripped":
223 if mod.compiler != nil {
224 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
225 }
226 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400227 default:
228 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
229 }
230}
231
Ivan Lozano52767be2019-10-18 14:49:46 -0700232func (mod *Module) SelectedStl() string {
233 return ""
234}
235
Ivan Lozano2b262972019-11-21 12:30:50 -0800236func (mod *Module) NonCcVariants() bool {
237 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400238 if _, ok := mod.compiler.(libraryInterface); ok {
239 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800240 }
241 }
242 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
243}
244
Ivan Lozano52767be2019-10-18 14:49:46 -0700245func (mod *Module) Static() bool {
246 if mod.compiler != nil {
247 if library, ok := mod.compiler.(libraryInterface); ok {
248 return library.static()
249 }
250 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400251 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700252}
253
254func (mod *Module) Shared() bool {
255 if mod.compiler != nil {
256 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400257 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700258 }
259 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400260 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700261}
262
Ivan Lozanod7586b62021-04-01 09:49:36 -0400263func (mod *Module) Dylib() bool {
264 if mod.compiler != nil {
265 if library, ok := mod.compiler.(libraryInterface); ok {
266 return library.dylib()
267 }
268 }
269 return false
270}
271
Ivan Lozanod106efe2023-09-21 23:30:26 -0400272func (mod *Module) Source() bool {
273 if mod.compiler != nil {
274 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
275 return library.source()
276 }
277 }
278 return false
279}
280
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400281func (mod *Module) RlibStd() bool {
282 if mod.compiler != nil {
283 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
284 return library.rlibStd()
285 }
286 }
287 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
288}
289
Ivan Lozanod7586b62021-04-01 09:49:36 -0400290func (mod *Module) Rlib() bool {
291 if mod.compiler != nil {
292 if library, ok := mod.compiler.(libraryInterface); ok {
293 return library.rlib()
294 }
295 }
296 return false
297}
298
299func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400300 if binary, ok := mod.compiler.(binaryInterface); ok {
301 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302 }
303 return false
304}
305
Justin Yun5e035862021-06-29 20:50:37 +0900306func (mod *Module) StaticExecutable() bool {
307 if !mod.Binary() {
308 return false
309 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400310 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900311}
312
Ivan Lozanod7586b62021-04-01 09:49:36 -0400313func (mod *Module) Object() bool {
314 // Rust has no modules which produce only object files.
315 return false
316}
317
Ivan Lozano52767be2019-10-18 14:49:46 -0700318func (mod *Module) Toc() android.OptionalPath {
319 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400320 if lib, ok := mod.compiler.(libraryInterface); ok {
321 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 }
323 }
324 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
325}
326
Colin Crossc511bc52020-04-07 16:50:32 +0000327func (mod *Module) UseSdk() bool {
328 return false
329}
330
Ivan Lozanod7586b62021-04-01 09:49:36 -0400331func (mod *Module) RelativeInstallPath() string {
332 if mod.compiler != nil {
333 return mod.compiler.relativeInstallPath()
334 }
335 return ""
336}
337
Ivan Lozano52767be2019-10-18 14:49:46 -0700338func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500339 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700340}
341
Jiyong Park7d55b612021-06-11 17:22:09 +0900342func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400343 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900344}
345
Ivan Lozano52767be2019-10-18 14:49:46 -0700346func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400347 return true
348}
349
350func (mod *Module) SubName() string {
351 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700352}
353
354func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500355 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700356 return false
357}
358
Ivan Lozanof9e21722020-12-02 09:00:51 -0500359func (mod *Module) IsVndkExt() bool {
360 return false
361}
362
Ivan Lozanod7586b62021-04-01 09:49:36 -0400363func (mod *Module) IsVndkSp() bool {
364 return false
365}
366
Ivan Lozanof1868af2022-04-12 13:08:36 -0400367func (mod *Module) IsVndkPrebuiltLibrary() bool {
368 // Rust modules do not provide VNDK prebuilts
369 return false
370}
371
372func (mod *Module) IsVendorPublicLibrary() bool {
373 return mod.VendorProperties.IsVendorPublicLibrary
374}
375
376func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
377 // Rust modules to not provide Sdk variants
378 return false
379}
380
Colin Cross127bb8b2020-12-16 16:46:01 -0800381func (c *Module) IsVndkPrivate() bool {
382 return false
383}
384
385func (c *Module) IsLlndk() bool {
386 return false
387}
388
389func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500390 return false
391}
392
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400393func (mod *Module) KernelHeadersDecorator() bool {
394 return false
395}
396
Colin Cross1f3f1302021-04-26 18:37:44 -0700397func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400398 return false
399}
400
Colin Cross5271fea2021-04-27 13:06:04 -0700401func (m *Module) NeedsVendorPublicLibraryVariants() bool {
402 return false
403}
404
Ivan Lozanod7586b62021-04-01 09:49:36 -0400405func (mod *Module) HasLlndkStubs() bool {
406 return false
407}
408
409func (mod *Module) StubsVersion() string {
410 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
411}
412
Ivan Lozano52767be2019-10-18 14:49:46 -0700413func (mod *Module) SdkVersion() string {
414 return ""
415}
416
Colin Crossc511bc52020-04-07 16:50:32 +0000417func (mod *Module) AlwaysSdk() bool {
418 return false
419}
420
Jiyong Park2286afd2020-06-16 21:58:53 +0900421func (mod *Module) IsSdkVariant() bool {
422 return false
423}
424
Colin Cross1348ce32020-10-01 13:37:16 -0700425func (mod *Module) SplitPerApiLevel() bool {
426 return false
427}
428
Sasha Smundaka76acba2022-04-18 20:12:56 -0700429func (mod *Module) XrefRustFiles() android.Paths {
430 return mod.kytheFiles
431}
432
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400434 Dylibs []string
435 Rlibs []string
436 Rustlibs []string
437 Stdlibs []string
438 ProcMacros []string
439 SharedLibs []string
440 StaticLibs []string
441 WholeStaticLibs []string
442 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400444 // Used for data dependencies adjacent to tests
445 DataLibs []string
446 DataBins []string
447
Colin Crossfe605e12022-01-23 20:46:16 -0800448 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449}
450
451type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700452 DyLibs RustLibraries
453 RLibs RustLibraries
454 SharedLibs android.Paths
455 SharedLibDeps android.Paths
456 StaticLibs android.Paths
457 ProcMacros RustLibraries
458 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500459
460 // depFlags and depLinkFlags are rustc and linker (clang) flags.
461 depFlags []string
462 depLinkFlags []string
463
464 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
465 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000466 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700467 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700468
Ivan Lozano45901ed2020-07-24 16:05:01 -0400469 // Used by bindgen modules which call clang
470 depClangFlags []string
471 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400472 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400473 depSystemIncludePaths android.Paths
474
Colin Crossfe605e12022-01-23 20:46:16 -0800475 CrtBegin android.Paths
476 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700477
478 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500479 SrcDeps android.Paths
480 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481}
482
483type RustLibraries []RustLibrary
484
485type RustLibrary struct {
486 Path android.Path
487 CrateName string
488}
489
490type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100491 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700492 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400493 cfgFlags(ctx ModuleContext, flags Flags) Flags
494 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700496 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700497 compilerDeps(ctx DepsContext, deps Deps) Deps
498 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700499 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700500
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100501 // Output directory in which source-generated code from dependencies is
502 // copied. This is equivalent to Cargo's OUT_DIR variable.
503 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700504
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400505 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
506 CargoPkgVersion() string
507
508 // CargoEnvCompat returns whether Cargo environment variables should be used.
509 CargoEnvCompat() bool
510
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800511 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200512 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700513 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400514 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400515
516 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400517
518 Disabled() bool
519 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400520
Ivan Lozanodd055472020-09-28 13:22:45 -0400521 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500522 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900523
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400524 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900525 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400526}
527
Matthew Maurerbb3add12020-06-25 09:34:12 -0700528type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000529 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700530 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700531}
532
Sasha Smundaka76acba2022-04-18 20:12:56 -0700533type xref interface {
534 XrefRustFiles() android.Paths
535}
536
Matthew Maurerbb3add12020-06-25 09:34:12 -0700537type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000538 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700539 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700540}
541
Wen-yi Chu41326c12023-09-22 03:58:59 +0000542func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
543 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700544}
545
Colin Cross004bd3f2023-10-02 11:39:17 -0700546func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
547 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400548}
549
Colin Cross0de8a1e2020-09-18 14:15:30 -0700550func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
551 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700552 LinkDirs: flagExporter.linkDirs,
553 LinkObjects: flagExporter.linkObjects,
554 })
555}
556
Matthew Maurerbb3add12020-06-25 09:34:12 -0700557var _ exportedFlagsProducer = (*flagExporter)(nil)
558
559func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700560 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700561}
562
Colin Cross0de8a1e2020-09-18 14:15:30 -0700563type FlagExporterInfo struct {
564 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000565 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700566 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700567}
568
569var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
570
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400571func (mod *Module) isCoverageVariant() bool {
572 return mod.coverage.Properties.IsCoverageVariant
573}
574
575var _ cc.Coverage = (*Module)(nil)
576
577func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
578 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
579}
580
Ivan Lozanod7586b62021-04-01 09:49:36 -0400581func (mod *Module) VndkVersion() string {
582 return mod.Properties.VndkVersion
583}
584
585func (mod *Module) PreventInstall() bool {
586 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400587}
588
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400589func (mod *Module) MarkAsCoverageVariant(coverage bool) {
590 mod.coverage.Properties.IsCoverageVariant = coverage
591}
592
593func (mod *Module) EnableCoverageIfNeeded() {
594 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700595}
596
597func defaultsFactory() android.Module {
598 return DefaultsFactory()
599}
600
601type Defaults struct {
602 android.ModuleBase
603 android.DefaultsModuleBase
604}
605
606func DefaultsFactory(props ...interface{}) android.Module {
607 module := &Defaults{}
608
609 module.AddProperties(props...)
610 module.AddProperties(
611 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800612 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500613 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100614 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400615 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700616 &BaseCompilerProperties{},
617 &BinaryCompilerProperties{},
618 &LibraryCompilerProperties{},
619 &ProcMacroCompilerProperties{},
620 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400621 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700622 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400623 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400624 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200625 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500626 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000627 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700628 )
629
630 android.InitDefaultsModule(module)
631 return module
632}
633
634func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700635 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700636}
637
Ivan Lozano183a3212019-10-18 14:18:45 -0700638func (mod *Module) CcLibrary() bool {
639 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500640 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700641 return true
642 }
643 }
644 return false
645}
646
647func (mod *Module) CcLibraryInterface() bool {
648 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400649 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
650 // VariantIs{Static,Shared} is set.
651 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700652 return true
653 }
654 }
655 return false
656}
657
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400658func (mod *Module) RustLibraryInterface() bool {
659 if mod.compiler != nil {
660 if _, ok := mod.compiler.(libraryInterface); ok {
661 return true
662 }
663 }
664 return false
665}
666
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500667func (mod *Module) IsFuzzModule() bool {
668 if _, ok := mod.compiler.(*fuzzDecorator); ok {
669 return true
670 }
671 return false
672}
673
674func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
675 return mod.FuzzModule
676}
677
678func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
679 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
680 return fuzzer.fuzzPackagedModule
681 }
682 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
683}
684
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000685func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500686 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
687 return fuzzer.sharedLibraries
688 }
689 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
690}
691
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400692func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400693 if mod.compiler != nil {
694 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400695 }
696 return nil
697}
698
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800699func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700700 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700701 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800702 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700703 }
704 }
705 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
706}
707
708func (mod *Module) SetStatic() {
709 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700710 if library, ok := mod.compiler.(libraryInterface); ok {
711 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700712 return
713 }
714 }
715 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
716}
717
718func (mod *Module) SetShared() {
719 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700720 if library, ok := mod.compiler.(libraryInterface); ok {
721 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700722 return
723 }
724 }
725 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
726}
727
Ivan Lozano183a3212019-10-18 14:18:45 -0700728func (mod *Module) BuildStaticVariant() bool {
729 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700730 if library, ok := mod.compiler.(libraryInterface); ok {
731 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700732 }
733 }
734 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
735}
736
737func (mod *Module) BuildSharedVariant() bool {
738 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700739 if library, ok := mod.compiler.(libraryInterface); ok {
740 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700741 }
742 }
743 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
744}
745
Ivan Lozano183a3212019-10-18 14:18:45 -0700746func (mod *Module) Module() android.Module {
747 return mod
748}
749
Ivan Lozano183a3212019-10-18 14:18:45 -0700750func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400751 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700752}
753
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400754func (mod *Module) CoverageFiles() android.Paths {
755 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800756 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400757 }
758 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
759}
760
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400761// Rust does not produce gcno files, and therefore does not produce a coverage archive.
762func (mod *Module) CoverageOutputFile() android.OptionalPath {
763 return android.OptionalPath{}
764}
765
766func (mod *Module) IsNdk(config android.Config) bool {
767 return false
768}
769
770func (mod *Module) IsStubs() bool {
771 return false
772}
773
Jiyong Park459feca2020-12-15 11:02:21 +0900774func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900775 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900776 return false
777 }
778
Jiyong Park459feca2020-12-15 11:02:21 +0900779 // The apex variant is not installable because it is included in the APEX and won't appear
780 // in the system partition as a standalone file.
781 if !apexInfo.IsForPlatform() {
782 return false
783 }
784
Jiyong Parke54f07e2021-04-07 15:08:04 +0900785 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900786}
787
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500788func (ctx moduleContext) apexVariationName() string {
789 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
790}
791
Ivan Lozano183a3212019-10-18 14:18:45 -0700792var _ cc.LinkableInterface = (*Module)(nil)
793
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794func (mod *Module) Init() android.Module {
795 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500796 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797
Yi Kong46c6e592022-01-20 22:55:00 +0800798 if mod.afdo != nil {
799 mod.AddProperties(mod.afdo.props()...)
800 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801 if mod.compiler != nil {
802 mod.AddProperties(mod.compiler.compilerProps()...)
803 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400804 if mod.coverage != nil {
805 mod.AddProperties(mod.coverage.props()...)
806 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200807 if mod.clippy != nil {
808 mod.AddProperties(mod.clippy.props()...)
809 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400810 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700811 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400812 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500813 if mod.sanitize != nil {
814 mod.AddProperties(mod.sanitize.props()...)
815 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400816
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900818 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819
820 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 return mod
822}
823
824func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
825 return &Module{
826 hod: hod,
827 multilib: multilib,
828 }
829}
830func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
831 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800832 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400833 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200834 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500835 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 return module
837}
838
839type ModuleContext interface {
840 android.ModuleContext
841 ModuleContextIntf
842}
843
844type BaseModuleContext interface {
845 android.BaseModuleContext
846 ModuleContextIntf
847}
848
849type DepsContext interface {
850 android.BottomUpMutatorContext
851 ModuleContextIntf
852}
853
854type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200855 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700856 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857}
858
859type depsContext struct {
860 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861}
862
863type moduleContext struct {
864 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865}
866
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200867type baseModuleContext struct {
868 android.BaseModuleContext
869}
870
871func (ctx *moduleContext) RustModule() *Module {
872 return ctx.Module().(*Module)
873}
874
875func (ctx *moduleContext) toolchain() config.Toolchain {
876 return ctx.RustModule().toolchain(ctx)
877}
878
879func (ctx *depsContext) RustModule() *Module {
880 return ctx.Module().(*Module)
881}
882
883func (ctx *depsContext) toolchain() config.Toolchain {
884 return ctx.RustModule().toolchain(ctx)
885}
886
887func (ctx *baseModuleContext) RustModule() *Module {
888 return ctx.Module().(*Module)
889}
890
891func (ctx *baseModuleContext) toolchain() config.Toolchain {
892 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400893}
894
895func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700896 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
897 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
898 return false
899 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400900 return mod.compiler != nil && mod.compiler.nativeCoverage()
901}
902
Ivan Lozanod7586b62021-04-01 09:49:36 -0400903func (mod *Module) EverInstallable() bool {
904 return mod.compiler != nil &&
905 // Check to see whether the module is actually ever installable.
906 mod.compiler.everInstallable()
907}
908
909func (mod *Module) Installable() *bool {
910 return mod.Properties.Installable
911}
912
Ivan Lozano872d5792022-03-23 17:31:39 -0400913func (mod *Module) ProcMacro() bool {
914 if pm, ok := mod.compiler.(procMacroInterface); ok {
915 return pm.ProcMacro()
916 }
917 return false
918}
919
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
921 if mod.cachedToolchain == nil {
922 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
923 }
924 return mod.cachedToolchain
925}
926
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200927func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
928 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
929}
930
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
932}
933
934func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
935 ctx := &moduleContext{
936 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938
Jiyong Park99644e92020-11-17 22:21:02 +0900939 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
940 if !apexInfo.IsForPlatform() {
941 mod.hideApexVariantFromMake = true
942 }
943
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500945 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
946
Ivan Lozanof1868af2022-04-12 13:08:36 -0400947 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700948
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949 if !toolchain.Supported() {
950 // This toolchain's unsupported, there's nothing to do for this mod.
951 return
952 }
953
954 deps := mod.depsToPaths(ctx)
955 flags := Flags{
956 Toolchain: toolchain,
957 }
958
Ivan Lozano67eada32021-09-23 11:50:33 -0400959 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800960 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500961 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800962 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963 if mod.compiler != nil {
964 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400965 flags = mod.compiler.cfgFlags(ctx, flags)
966 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400967 }
968 if mod.coverage != nil {
969 flags, deps = mod.coverage.flags(ctx, flags, deps)
970 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200971 if mod.clippy != nil {
972 flags, deps = mod.clippy.flags(ctx, flags, deps)
973 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500974 if mod.sanitize != nil {
975 flags, deps = mod.sanitize.flags(ctx, flags, deps)
976 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400977
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200978 // SourceProvider needs to call GenerateSource() before compiler calls
979 // compile() so it can provide the source. A SourceProvider has
980 // multiple variants (e.g. source, rlib, dylib). Only the "source"
981 // variant is responsible for effectively generating the source. The
982 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400983 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200984 if mod.compiler.(libraryInterface).source() {
985 mod.sourceProvider.GenerateSource(ctx, deps)
986 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
987 } else {
988 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
989 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700990 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200991 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400992 }
993
994 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100995 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700996 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400997 if ctx.Failed() {
998 return
999 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001000 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
1001 if buildOutput.kytheFile != nil {
1002 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1003 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001004 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001005
Dan Albert06feee92021-03-19 15:06:02 -07001006 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001007 if mod.docTimestampFile.Valid() {
1008 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1009 }
Dan Albert06feee92021-03-19 15:06:02 -07001010
Ivan Lozano1921e802021-05-20 13:39:16 -04001011 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1012 // RECOVERY_SNAPSHOT_VERSION is current.
1013 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1014 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1015 lib.collectHeadersForSnapshot(ctx, deps)
1016 }
1017 }
1018
Jiyong Park459feca2020-12-15 11:02:21 +09001019 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001020 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001021 // If the module has been specifically configure to not be installed then
1022 // hide from make as otherwise it will break when running inside make as the
1023 // output path to install will not be specified. Not all uninstallable
1024 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001025 // side dependencies. In particular, proc-macros need to be captured in the
1026 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001027 mod.HideFromMake()
1028 } else if !mod.installable(apexInfo) {
1029 mod.SkipInstall()
1030 }
1031
1032 // Still call install though, the installs will be stored as PackageSpecs to allow
1033 // using the outputs in a genrule.
1034 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001035 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001036 if ctx.Failed() {
1037 return
1038 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001039 }
Chris Wailes74be7642021-07-22 16:20:28 -07001040
1041 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001043 if mod.testModule {
1044 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
1045 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046}
1047
1048func (mod *Module) deps(ctx DepsContext) Deps {
1049 deps := Deps{}
1050
1051 if mod.compiler != nil {
1052 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001053 }
1054 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001055 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 }
1057
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001058 if mod.coverage != nil {
1059 deps = mod.coverage.deps(ctx, deps)
1060 }
1061
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001062 if mod.sanitize != nil {
1063 deps = mod.sanitize.deps(ctx, deps)
1064 }
1065
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1067 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001068 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1070 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1071 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001072 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001073 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074 return deps
1075
1076}
1077
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078type dependencyTag struct {
1079 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001080 name string
1081 library bool
1082 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001083 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001084}
1085
Jiyong Park65b62242020-11-25 12:44:59 +09001086// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1087// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1088func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001089 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001090}
1091
1092var _ android.InstallNeededDependencyTag = dependencyTag{}
1093
Colin Cross65cb3142021-12-10 23:05:02 +00001094func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1095 if d.library && d.dynamic {
1096 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1097 }
1098 return nil
1099}
1100
1101var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1102
Ivan Lozanoffee3342019-08-27 12:03:00 -07001103var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001104 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1105 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001106 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001107 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001108 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001109 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001110 dataLibDepTag = dependencyTag{name: "data lib"}
1111 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001112)
1113
Jiyong Park99644e92020-11-17 22:21:02 +09001114func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1115 tag, ok := depTag.(dependencyTag)
1116 return ok && tag == dylibDepTag
1117}
1118
Jiyong Park94e22fd2021-04-08 18:19:15 +09001119func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1120 tag, ok := depTag.(dependencyTag)
1121 return ok && tag == rlibDepTag
1122}
1123
Matthew Maurer0f003b12020-06-29 14:34:06 -07001124type autoDep struct {
1125 variation string
1126 depTag dependencyTag
1127}
1128
1129var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001130 rlibVariation = "rlib"
1131 dylibVariation = "dylib"
1132 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1133 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001134)
1135
1136type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001137 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001138}
1139
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001140func (mod *Module) begin(ctx BaseModuleContext) {
1141 if mod.coverage != nil {
1142 mod.coverage.begin(ctx)
1143 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001144 if mod.sanitize != nil {
1145 mod.sanitize.begin(ctx)
1146 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001147}
1148
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001149func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001150 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001151 return p.prebuilt()
1152 }
1153 return nil
1154}
1155
Justin Yun24b246a2023-03-16 10:36:16 +09001156func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1157 if rustDep, ok := dep.(*Module); ok {
1158 // Use base module name for snapshots when exporting to Makefile.
1159 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1160 baseName := rustDep.BaseModuleName()
1161 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1162 }
1163 }
1164 return cc.MakeLibName(ctx, c, dep, depName)
1165}
1166
Ivan Lozanod106efe2023-09-21 23:30:26 -04001167func collectIncludedProtos(mod *Module, dep *Module) {
1168 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1169 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1170 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1171 }
1172 }
1173}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001174func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1175 var depPaths PathDeps
1176
1177 directRlibDeps := []*Module{}
1178 directDylibDeps := []*Module{}
1179 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001180 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001181 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001182 directSrcProvidersDeps := []*Module{}
1183 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001184
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001185 // For the dependency from platform to apex, use the latest stubs
1186 mod.apexSdkVersion = android.FutureApiLevel
1187 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1188 if !apexInfo.IsForPlatform() {
1189 mod.apexSdkVersion = apexInfo.MinSdkVersion
1190 }
1191
1192 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1193 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1194 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1195 // (b/144430859)
1196 mod.apexSdkVersion = android.FutureApiLevel
1197 }
1198
Spandan Das604f3762023-03-16 22:51:40 +00001199 skipModuleList := map[string]bool{}
1200
1201 var apiImportInfo multitree.ApiImportInfo
1202 hasApiImportInfo := false
1203
1204 ctx.VisitDirectDeps(func(dep android.Module) {
1205 if dep.Name() == "api_imports" {
1206 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1207 hasApiImportInfo = true
1208 }
1209 })
1210
1211 if hasApiImportInfo {
1212 targetStubModuleList := map[string]string{}
1213 targetOrigModuleList := map[string]string{}
1214
1215 // Search for dependency which both original module and API imported library with APEX stub exists
1216 ctx.VisitDirectDeps(func(dep android.Module) {
1217 depName := ctx.OtherModuleName(dep)
1218 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1219 targetStubModuleList[apiLibrary] = depName
1220 }
1221 })
1222 ctx.VisitDirectDeps(func(dep android.Module) {
1223 depName := ctx.OtherModuleName(dep)
1224 if origLibrary, ok := targetStubModuleList[depName]; ok {
1225 targetOrigModuleList[origLibrary] = depName
1226 }
1227 })
1228
1229 // Decide which library should be used between original and API imported library
1230 ctx.VisitDirectDeps(func(dep android.Module) {
1231 depName := ctx.OtherModuleName(dep)
1232 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1233 if cc.ShouldUseStubForApex(ctx, dep) {
1234 skipModuleList[depName] = true
1235 } else {
1236 skipModuleList[apiLibrary] = true
1237 }
1238 }
1239 })
1240 }
1241
Cole Faustb6e6f992023-08-17 17:42:26 -07001242 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1243 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001244
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 ctx.VisitDirectDeps(func(dep android.Module) {
1246 depName := ctx.OtherModuleName(dep)
1247 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001248
Spandan Das604f3762023-03-16 22:51:40 +00001249 if _, exists := skipModuleList[depName]; exists {
1250 return
1251 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001252
1253 if depTag == android.DarwinUniversalVariantTag {
1254 return
1255 }
1256
Ivan Lozano89435d12020-07-31 11:01:18 -04001257 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001258 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001259 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001260
Ivan Lozanoffee3342019-08-27 12:03:00 -07001261 switch depTag {
1262 case dylibDepTag:
1263 dylib, ok := rustDep.compiler.(libraryInterface)
1264 if !ok || !dylib.dylib() {
1265 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1266 return
1267 }
1268 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001269 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001270 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1271
Ivan Lozanoffee3342019-08-27 12:03:00 -07001272 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001273
Ivan Lozanoffee3342019-08-27 12:03:00 -07001274 rlib, ok := rustDep.compiler.(libraryInterface)
1275 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001276 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001277 return
1278 }
1279 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001280 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001281 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1282
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 case procMacroDepTag:
1284 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001285 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001286
1287 case sourceDepTag:
1288 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1289 collectIncludedProtos(mod, rustDep)
1290 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001291 }
1292
Cole Faustb6e6f992023-08-17 17:42:26 -07001293 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1294
Paul Duffind5cf92e2021-07-09 17:38:55 +01001295 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001296 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1297 // OS/Arch variant is used.
1298 var helper string
1299 if ctx.Host() {
1300 helper = "missing 'host_supported'?"
1301 } else {
1302 helper = "device module defined?"
1303 }
1304
1305 if dep.Target().Os != ctx.Os() {
1306 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1307 return
1308 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1309 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1310 return
1311 }
1312 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001313 }
1314
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001315 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001316 if depTag != procMacroDepTag {
1317 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1318 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1319 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1320 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001321 }
1322
Ivan Lozanoffee3342019-08-27 12:03:00 -07001323 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001324 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001325 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001326 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001327 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001328 }
1329 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001330 if depTag == sourceDepTag {
1331 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1332 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
1333 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1334 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1335 }
1336 }
1337 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001338 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001339 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001340 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001341 if _, ok := ccDep.(*Module); !ok {
1342 if ccDep.Module().Target().Os != ctx.Os() {
1343 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1344 return
1345 }
1346 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1347 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1348 return
1349 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001350 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001351 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001352 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001353 if !ctx.Config().AllowMissingDependencies() {
1354 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1355 } else {
1356 ctx.AddMissingDependencies([]string{depName})
1357 }
1358 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001359 }
1360
Wen-yi Chu41326c12023-09-22 03:58:59 +00001361 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001362
Ivan Lozanoffee3342019-08-27 12:03:00 -07001363 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001364 switch {
1365 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001366 if cc.IsWholeStaticLib(depTag) {
1367 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1368 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001369 if mod.Binary() {
1370 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1371 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1372 // final linkage, pass the args directly to the linker to handle these cases.
1373 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1374 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001375 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001376 } else {
1377 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 -05001378 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001379 }
1380
1381 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1382 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001383 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001384 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1385
Colin Cross0de8a1e2020-09-18 14:15:30 -07001386 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1387 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...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001391 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001392
1393 // Record baseLibName for snapshots.
1394 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1395
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001396 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001397 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001398 // For the shared lib dependencies, we may link to the stub variant
1399 // of the dependency depending on the context (e.g. if this
1400 // dependency crosses the APEX boundaries).
1401 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1402
1403 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1404 // object (either from stub or non-stub) to use.
1405 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001406 if !linkObject.Valid() {
1407 if !ctx.Config().AllowMissingDependencies() {
1408 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1409 } else {
1410 ctx.AddMissingDependencies([]string{depName})
1411 }
1412 return
1413 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001414 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001415
Ivan Lozanoffee3342019-08-27 12:03:00 -07001416 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001417 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001418 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1419 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1420 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1421 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001422 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001423
1424 // Record baseLibName for snapshots.
1425 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1426
Cole Faustb6e6f992023-08-17 17:42:26 -07001427 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001428 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001429 case cc.IsHeaderDepTag(depTag):
1430 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1431 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1432 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1433 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001434 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001435 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001436 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001437 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001438 }
1439
1440 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001441 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1442 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001443 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001444 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001445 } else {
1446 switch {
1447 case depTag == cc.CrtBeginDepTag:
1448 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1449 case depTag == cc.CrtEndDepTag:
1450 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1451 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001452 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001453
1454 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001455 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001456 // These are usually genrules which don't have per-target variants.
1457 directSrcDeps = append(directSrcDeps, srcDep)
1458 }
1459 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001460 })
1461
Wen-yi Chu41326c12023-09-22 03:58:59 +00001462 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1463
1464 var rlibDepFiles RustLibraries
1465 for _, dep := range directRlibDeps {
1466 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1467 }
1468 var dylibDepFiles RustLibraries
1469 for _, dep := range directDylibDeps {
1470 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1471 }
1472 var procMacroDepFiles RustLibraries
1473 for _, dep := range directProcMacroDeps {
1474 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1475 }
1476
Colin Cross004bd3f2023-10-02 11:39:17 -07001477 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001478 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001479 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480 }
1481
Colin Cross004bd3f2023-10-02 11:39:17 -07001482 var sharedLibFiles android.Paths
1483 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001484 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001485 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001486 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001487 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001488 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001489 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001490 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001491 }
1492
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001493 var srcProviderDepFiles android.Paths
1494 for _, dep := range directSrcProvidersDeps {
1495 srcs, _ := dep.OutputFiles("")
1496 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1497 }
1498 for _, dep := range directSrcDeps {
1499 srcs := dep.Srcs()
1500 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1501 }
1502
Wen-yi Chu41326c12023-09-22 03:58:59 +00001503 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1504 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001505 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1506 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1507 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001508 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001509 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001510
1511 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001512 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001513 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001514 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001515 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1516 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1517 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001518
1519 return depPaths
1520}
1521
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001522func (mod *Module) InstallInData() bool {
1523 if mod.compiler == nil {
1524 return false
1525 }
1526 return mod.compiler.inData()
1527}
1528
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001529func (mod *Module) InstallInRamdisk() bool {
1530 return mod.InRamdisk()
1531}
1532
1533func (mod *Module) InstallInVendorRamdisk() bool {
1534 return mod.InVendorRamdisk()
1535}
1536
1537func (mod *Module) InstallInRecovery() bool {
1538 return mod.InRecovery()
1539}
1540
Wen-yi Chu41326c12023-09-22 03:58:59 +00001541func linkPathFromFilePath(filepath android.Path) string {
1542 return strings.Split(filepath.String(), filepath.Base())[0]
1543}
1544
Spandan Das604f3762023-03-16 22:51:40 +00001545// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1546func (r *Module) usePublicApi() bool {
1547 return r.Device() && r.UseSdk()
1548}
1549
1550// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1551func (r *Module) useVendorApi() bool {
1552 return r.Device() && (r.InVendor() || r.InProduct())
1553}
1554
Ivan Lozanoffee3342019-08-27 12:03:00 -07001555func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1556 ctx := &depsContext{
1557 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001558 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001559
1560 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001561 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001562 var snapshotInfo *cc.SnapshotInfo
1563
Kiyoung Kim487689e2022-07-26 09:48:22 +09001564 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001565 if mod.usePublicApi() || mod.useVendorApi() {
1566 for idx, lib := range deps.SharedLibs {
1567 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1568 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001569 }
1570
Ivan Lozano1921e802021-05-20 13:39:16 -04001571 if ctx.Os() == android.Android {
1572 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1573 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001574
Ivan Lozano2b081132020-09-08 12:46:52 -04001575 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001576 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001577 stdLinkage = "rlib-std"
1578 }
1579
1580 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001581
Ivan Lozano2b081132020-09-08 12:46:52 -04001582 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1583 rlibDepVariations = append(rlibDepVariations,
1584 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1585 }
1586
Ivan Lozano1921e802021-05-20 13:39:16 -04001587 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001588 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001589 for _, lib := range deps.Rlibs {
1590 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001591 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001592
Ivan Lozano2d407632022-04-07 12:59:11 -04001593 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001594 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001595
1596 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001597 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1598 for _, lib := range deps.Dylibs {
1599 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1600 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001601
Ivan Lozano1921e802021-05-20 13:39:16 -04001602 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001603 if deps.Rustlibs != nil {
1604 if !mod.compiler.Disabled() {
1605 for _, lib := range deps.Rustlibs {
1606 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1607 if autoDep.depTag == rlibDepTag {
1608 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001609 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001610 } else {
1611 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1612 // Check for the existence of the dylib deptag variant. Select it if available,
1613 // otherwise select the rlib variant.
1614 autoDepVariations := append(commonDepVariations,
1615 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1616
1617 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1618
1619 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1620 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1621 } else {
1622 // If there's no dylib dependency available, try to add the rlib dependency instead.
1623 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1624 }
1625 }
1626 }
1627 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1628 for _, lib := range deps.Rustlibs {
1629 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1630 srcProviderVariations := append(commonDepVariations,
1631 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1632
1633 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1634 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001635 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001636 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001637 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001638 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001639
Ivan Lozano1921e802021-05-20 13:39:16 -04001640 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001641 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001642 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001643 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001644 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001645 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001646 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001647 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001648 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001649 for _, lib := range deps.Stdlibs {
1650 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1651 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001652 }
1653 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001654
1655 for _, lib := range deps.SharedLibs {
1656 depTag := cc.SharedDepTag()
1657 name, version := cc.StubsLibNameAndVersion(lib)
1658
1659 variations := []blueprint.Variation{
1660 {Mutator: "link", Variation: "shared"},
1661 }
Spandan Das604f3762023-03-16 22:51:40 +00001662 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1663 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1664 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1665 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1666 }
1667
1668 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1669 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1670 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001671 }
1672
1673 for _, lib := range deps.WholeStaticLibs {
1674 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001675 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001676
1677 actx.AddVariationDependencies([]blueprint.Variation{
1678 {Mutator: "link", Variation: "static"},
1679 }, depTag, lib)
1680 }
1681
1682 for _, lib := range deps.StaticLibs {
1683 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001684 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001685
1686 actx.AddVariationDependencies([]blueprint.Variation{
1687 {Mutator: "link", Variation: "static"},
1688 }, depTag, lib)
1689 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001690
Zach Johnson3df4e632020-11-06 11:56:27 -08001691 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1692
Colin Cross565cafd2020-09-25 18:47:38 -07001693 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001694 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001695 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001696 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001697 }
Colin Crossfe605e12022-01-23 20:46:16 -08001698 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001699 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001700 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001701 }
1702
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001703 if mod.sourceProvider != nil {
1704 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1705 bindgen.Properties.Custom_bindgen != "" {
1706 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1707 bindgen.Properties.Custom_bindgen)
1708 }
1709 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001710
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001711 actx.AddVariationDependencies([]blueprint.Variation{
1712 {Mutator: "link", Variation: "shared"},
1713 }, dataLibDepTag, deps.DataLibs...)
1714
1715 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1716
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001717 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001718 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001719
1720 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001721}
1722
Ivan Lozano2d407632022-04-07 12:59:11 -04001723// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001724func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1725 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001726 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1727}
1728
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001729func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1730 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1731 actx.AddVariationDependencies(variations, depTag, lib)
1732}
1733
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001734func BeginMutator(ctx android.BottomUpMutatorContext) {
1735 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1736 mod.beginMutator(ctx)
1737 }
1738}
1739
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001740func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1741 ctx := &baseModuleContext{
1742 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001743 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001744
1745 mod.begin(ctx)
1746}
1747
Ivan Lozanoffee3342019-08-27 12:03:00 -07001748func (mod *Module) Name() string {
1749 name := mod.ModuleBase.Name()
1750 if p, ok := mod.compiler.(interface {
1751 Name(string) string
1752 }); ok {
1753 name = p.Name(name)
1754 }
1755 return name
1756}
1757
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001758func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001759 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001760 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001761 }
1762}
1763
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001764var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001765var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001766
1767func (mod *Module) HostToolPath() android.OptionalPath {
1768 if !mod.Host() {
1769 return android.OptionalPath{}
1770 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001771 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1772 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001773 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1774 // Even though proc-macros aren't strictly "tools", since they target the compiler
1775 // and act as compiler plugins, we treat them similarly.
1776 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001777 }
1778 return android.OptionalPath{}
1779}
1780
Jiyong Park99644e92020-11-17 22:21:02 +09001781var _ android.ApexModule = (*Module)(nil)
1782
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001783func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001784 return String(mod.Properties.Min_sdk_version)
1785}
1786
Jiyong Park45bf82e2020-12-15 22:29:02 +09001787// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001788func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001789 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001790 if minSdkVersion == "apex_inherit" {
1791 return nil
1792 }
1793 if minSdkVersion == "" {
1794 return fmt.Errorf("min_sdk_version is not specificed")
1795 }
1796
1797 // Not using nativeApiLevelFromUser because the context here is not
1798 // necessarily a native context.
1799 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1800 if err != nil {
1801 return err
1802 }
1803
1804 if ver.GreaterThan(sdkVersion) {
1805 return fmt.Errorf("newer SDK(%v)", ver)
1806 }
Jiyong Park99644e92020-11-17 22:21:02 +09001807 return nil
1808}
1809
Jiyong Park45bf82e2020-12-15 22:29:02 +09001810// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001811func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1812 depTag := ctx.OtherModuleDependencyTag(dep)
1813
1814 if ccm, ok := dep.(*cc.Module); ok {
1815 if ccm.HasStubsVariants() {
1816 if cc.IsSharedDepTag(depTag) {
1817 // dynamic dep to a stubs lib crosses APEX boundary
1818 return false
1819 }
1820 if cc.IsRuntimeDepTag(depTag) {
1821 // runtime dep to a stubs lib also crosses APEX boundary
1822 return false
1823 }
1824
1825 if cc.IsHeaderDepTag(depTag) {
1826 return false
1827 }
1828 }
1829 if mod.Static() && cc.IsSharedDepTag(depTag) {
1830 // shared_lib dependency from a static lib is considered as crossing
1831 // the APEX boundary because the dependency doesn't actually is
1832 // linked; the dependency is used only during the compilation phase.
1833 return false
1834 }
1835 }
1836
Matthew Maurer581b6d82022-09-29 16:46:25 -07001837 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001838 return false
1839 }
1840
1841 return true
1842}
1843
1844// Overrides ApexModule.IsInstallabeToApex()
1845func (mod *Module) IsInstallableToApex() bool {
1846 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001847 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001848 return true
1849 }
1850 if _, ok := mod.compiler.(*binaryDecorator); ok {
1851 return true
1852 }
1853 }
1854 return false
1855}
1856
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001857// If a library file has a "lib" prefix, extract the library name without the prefix.
1858func libNameFromFilePath(filepath android.Path) (string, bool) {
1859 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1860 if strings.HasPrefix(libName, "lib") {
1861 libName = libName[3:]
1862 return libName, true
1863 }
1864 return "", false
1865}
1866
Sasha Smundaka76acba2022-04-18 20:12:56 -07001867func kytheExtractRustFactory() android.Singleton {
1868 return &kytheExtractRustSingleton{}
1869}
1870
1871type kytheExtractRustSingleton struct {
1872}
1873
1874func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1875 var xrefTargets android.Paths
1876 ctx.VisitAllModules(func(module android.Module) {
1877 if rustModule, ok := module.(xref); ok {
1878 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1879 }
1880 })
1881 if len(xrefTargets) > 0 {
1882 ctx.Phony("xref_rust", xrefTargets...)
1883 }
1884}
1885
Jihoon Kangf78a8902022-09-01 22:47:07 +00001886func (c *Module) Partition() string {
1887 return ""
1888}
1889
Chris Parsons637458d2023-09-19 20:09:00 +00001890func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001891 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1892 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001893 } else if ctx.ModuleType() == "rust_proc_macro" {
1894 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001895 } else if ctx.ModuleType() == "rust_binary_host" {
1896 binaryBp2build(ctx, m)
Vinh Tran47faaad2023-09-25 14:47:19 -04001897 } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" {
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001898 protoLibraryBp2build(ctx, m)
Vinh Tran75159942023-10-17 16:28:04 -04001899 } else if ctx.ModuleType() == "rust_ffi_static" {
1900 ffiStaticBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001901 } else {
1902 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1903 }
1904}
1905
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001906// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001907// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001908// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1909// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1910// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001911func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001912 var srcs bazel.LabelList
1913 var compileData bazel.LabelList
1914
1915 if c.Properties.Srcs[0] == "src/lib.rs" {
1916 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1917 compileData = android.BazelLabelForModuleSrc(
1918 ctx,
1919 []string{
1920 "src/**/*.proto",
1921 "examples/**/*.rs",
1922 "**/*.md",
1923 },
1924 )
1925 } else {
1926 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1927 }
1928
1929 return srcs, compileData
1930}
1931
Ivan Lozanoffee3342019-08-27 12:03:00 -07001932var Bool = proptools.Bool
1933var BoolDefault = proptools.BoolDefault
1934var String = proptools.String
1935var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001936
1937var _ android.OutputFileProducer = (*Module)(nil)