blob: d4d33c71d9f24a733f966b2e1369b025f8357e81 [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
Matthew Maurerbb3add12020-06-25 09:34:12 -0700490type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000491 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700492 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700493}
494
Sasha Smundaka76acba2022-04-18 20:12:56 -0700495type xref interface {
496 XrefRustFiles() android.Paths
497}
498
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000500 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700501 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700502}
503
Wen-yi Chu41326c12023-09-22 03:58:59 +0000504func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
505 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700506}
507
Colin Cross004bd3f2023-10-02 11:39:17 -0700508func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
509 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400510}
511
Colin Cross0de8a1e2020-09-18 14:15:30 -0700512func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
513 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700514 LinkDirs: flagExporter.linkDirs,
515 LinkObjects: flagExporter.linkObjects,
516 })
517}
518
Matthew Maurerbb3add12020-06-25 09:34:12 -0700519var _ exportedFlagsProducer = (*flagExporter)(nil)
520
521func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700522 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700523}
524
Colin Cross0de8a1e2020-09-18 14:15:30 -0700525type FlagExporterInfo struct {
526 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000527 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700528 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700529}
530
531var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
532
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533func (mod *Module) isCoverageVariant() bool {
534 return mod.coverage.Properties.IsCoverageVariant
535}
536
537var _ cc.Coverage = (*Module)(nil)
538
539func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
540 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
541}
542
Ivan Lozanod7586b62021-04-01 09:49:36 -0400543func (mod *Module) VndkVersion() string {
544 return mod.Properties.VndkVersion
545}
546
547func (mod *Module) PreventInstall() bool {
548 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400549}
550
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400551func (mod *Module) MarkAsCoverageVariant(coverage bool) {
552 mod.coverage.Properties.IsCoverageVariant = coverage
553}
554
555func (mod *Module) EnableCoverageIfNeeded() {
556 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557}
558
559func defaultsFactory() android.Module {
560 return DefaultsFactory()
561}
562
563type Defaults struct {
564 android.ModuleBase
565 android.DefaultsModuleBase
566}
567
568func DefaultsFactory(props ...interface{}) android.Module {
569 module := &Defaults{}
570
571 module.AddProperties(props...)
572 module.AddProperties(
573 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800574 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500575 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100576 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400577 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578 &BaseCompilerProperties{},
579 &BinaryCompilerProperties{},
580 &LibraryCompilerProperties{},
581 &ProcMacroCompilerProperties{},
582 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400583 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700584 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400585 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400586 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200587 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500588 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000589 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590 )
591
592 android.InitDefaultsModule(module)
593 return module
594}
595
596func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700597 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598}
599
Ivan Lozano183a3212019-10-18 14:18:45 -0700600func (mod *Module) CcLibrary() bool {
601 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500602 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700603 return true
604 }
605 }
606 return false
607}
608
609func (mod *Module) CcLibraryInterface() bool {
610 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400611 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
612 // VariantIs{Static,Shared} is set.
613 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700614 return true
615 }
616 }
617 return false
618}
619
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400620func (mod *Module) RustLibraryInterface() bool {
621 if mod.compiler != nil {
622 if _, ok := mod.compiler.(libraryInterface); ok {
623 return true
624 }
625 }
626 return false
627}
628
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500629func (mod *Module) IsFuzzModule() bool {
630 if _, ok := mod.compiler.(*fuzzDecorator); ok {
631 return true
632 }
633 return false
634}
635
636func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
637 return mod.FuzzModule
638}
639
640func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
641 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
642 return fuzzer.fuzzPackagedModule
643 }
644 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
645}
646
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000647func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500648 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
649 return fuzzer.sharedLibraries
650 }
651 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
652}
653
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400654func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400655 if mod.compiler != nil {
656 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400657 }
658 return nil
659}
660
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800661func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700662 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700663 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800664 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700665 }
666 }
667 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
668}
669
670func (mod *Module) SetStatic() {
671 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700672 if library, ok := mod.compiler.(libraryInterface); ok {
673 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700674 return
675 }
676 }
677 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
678}
679
680func (mod *Module) SetShared() {
681 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700682 if library, ok := mod.compiler.(libraryInterface); ok {
683 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700684 return
685 }
686 }
687 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
688}
689
Ivan Lozano183a3212019-10-18 14:18:45 -0700690func (mod *Module) BuildStaticVariant() bool {
691 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700692 if library, ok := mod.compiler.(libraryInterface); ok {
693 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700694 }
695 }
696 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
697}
698
699func (mod *Module) BuildSharedVariant() bool {
700 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700701 if library, ok := mod.compiler.(libraryInterface); ok {
702 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700703 }
704 }
705 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
706}
707
Ivan Lozano183a3212019-10-18 14:18:45 -0700708func (mod *Module) Module() android.Module {
709 return mod
710}
711
Ivan Lozano183a3212019-10-18 14:18:45 -0700712func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400713 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700714}
715
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400716func (mod *Module) CoverageFiles() android.Paths {
717 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800718 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400719 }
720 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
721}
722
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400723// Rust does not produce gcno files, and therefore does not produce a coverage archive.
724func (mod *Module) CoverageOutputFile() android.OptionalPath {
725 return android.OptionalPath{}
726}
727
728func (mod *Module) IsNdk(config android.Config) bool {
729 return false
730}
731
732func (mod *Module) IsStubs() bool {
733 return false
734}
735
Jiyong Park459feca2020-12-15 11:02:21 +0900736func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900737 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900738 return false
739 }
740
Jiyong Park459feca2020-12-15 11:02:21 +0900741 // The apex variant is not installable because it is included in the APEX and won't appear
742 // in the system partition as a standalone file.
743 if !apexInfo.IsForPlatform() {
744 return false
745 }
746
Jiyong Parke54f07e2021-04-07 15:08:04 +0900747 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900748}
749
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500750func (ctx moduleContext) apexVariationName() string {
751 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
752}
753
Ivan Lozano183a3212019-10-18 14:18:45 -0700754var _ cc.LinkableInterface = (*Module)(nil)
755
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756func (mod *Module) Init() android.Module {
757 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500758 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759
Yi Kong46c6e592022-01-20 22:55:00 +0800760 if mod.afdo != nil {
761 mod.AddProperties(mod.afdo.props()...)
762 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763 if mod.compiler != nil {
764 mod.AddProperties(mod.compiler.compilerProps()...)
765 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766 if mod.coverage != nil {
767 mod.AddProperties(mod.coverage.props()...)
768 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200769 if mod.clippy != nil {
770 mod.AddProperties(mod.clippy.props()...)
771 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400772 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700773 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400774 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500775 if mod.sanitize != nil {
776 mod.AddProperties(mod.sanitize.props()...)
777 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900780 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781
782 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 return mod
784}
785
786func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
787 return &Module{
788 hod: hod,
789 multilib: multilib,
790 }
791}
792func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
793 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800794 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400795 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200796 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500797 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 return module
799}
800
801type ModuleContext interface {
802 android.ModuleContext
803 ModuleContextIntf
804}
805
806type BaseModuleContext interface {
807 android.BaseModuleContext
808 ModuleContextIntf
809}
810
811type DepsContext interface {
812 android.BottomUpMutatorContext
813 ModuleContextIntf
814}
815
816type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200817 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819}
820
821type depsContext struct {
822 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823}
824
825type moduleContext struct {
826 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827}
828
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200829type baseModuleContext struct {
830 android.BaseModuleContext
831}
832
833func (ctx *moduleContext) RustModule() *Module {
834 return ctx.Module().(*Module)
835}
836
837func (ctx *moduleContext) toolchain() config.Toolchain {
838 return ctx.RustModule().toolchain(ctx)
839}
840
841func (ctx *depsContext) RustModule() *Module {
842 return ctx.Module().(*Module)
843}
844
845func (ctx *depsContext) toolchain() config.Toolchain {
846 return ctx.RustModule().toolchain(ctx)
847}
848
849func (ctx *baseModuleContext) RustModule() *Module {
850 return ctx.Module().(*Module)
851}
852
853func (ctx *baseModuleContext) toolchain() config.Toolchain {
854 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400855}
856
857func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700858 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
859 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
860 return false
861 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400862 return mod.compiler != nil && mod.compiler.nativeCoverage()
863}
864
Ivan Lozanod7586b62021-04-01 09:49:36 -0400865func (mod *Module) EverInstallable() bool {
866 return mod.compiler != nil &&
867 // Check to see whether the module is actually ever installable.
868 mod.compiler.everInstallable()
869}
870
871func (mod *Module) Installable() *bool {
872 return mod.Properties.Installable
873}
874
Ivan Lozano872d5792022-03-23 17:31:39 -0400875func (mod *Module) ProcMacro() bool {
876 if pm, ok := mod.compiler.(procMacroInterface); ok {
877 return pm.ProcMacro()
878 }
879 return false
880}
881
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
883 if mod.cachedToolchain == nil {
884 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
885 }
886 return mod.cachedToolchain
887}
888
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200889func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
890 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
891}
892
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
894}
895
896func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
897 ctx := &moduleContext{
898 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900
Jiyong Park99644e92020-11-17 22:21:02 +0900901 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
902 if !apexInfo.IsForPlatform() {
903 mod.hideApexVariantFromMake = true
904 }
905
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500907 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
908
Ivan Lozanof1868af2022-04-12 13:08:36 -0400909 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 if !toolchain.Supported() {
912 // This toolchain's unsupported, there's nothing to do for this mod.
913 return
914 }
915
916 deps := mod.depsToPaths(ctx)
917 flags := Flags{
918 Toolchain: toolchain,
919 }
920
Ivan Lozano67eada32021-09-23 11:50:33 -0400921 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800922 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500923 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800924 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 if mod.compiler != nil {
926 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400927 flags = mod.compiler.cfgFlags(ctx, flags)
928 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400929 }
930 if mod.coverage != nil {
931 flags, deps = mod.coverage.flags(ctx, flags, deps)
932 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200933 if mod.clippy != nil {
934 flags, deps = mod.clippy.flags(ctx, flags, deps)
935 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500936 if mod.sanitize != nil {
937 flags, deps = mod.sanitize.flags(ctx, flags, deps)
938 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400939
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200940 // SourceProvider needs to call GenerateSource() before compiler calls
941 // compile() so it can provide the source. A SourceProvider has
942 // multiple variants (e.g. source, rlib, dylib). Only the "source"
943 // variant is responsible for effectively generating the source. The
944 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400945 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200946 if mod.compiler.(libraryInterface).source() {
947 mod.sourceProvider.GenerateSource(ctx, deps)
948 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
949 } else {
950 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
951 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700952 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200953 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400954 }
955
956 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100957 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700958 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400959 if ctx.Failed() {
960 return
961 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700962 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
963 if buildOutput.kytheFile != nil {
964 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
965 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400966 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900967
Dan Albert06feee92021-03-19 15:06:02 -0700968 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000969 if mod.docTimestampFile.Valid() {
970 ctx.CheckbuildFile(mod.docTimestampFile.Path())
971 }
Dan Albert06feee92021-03-19 15:06:02 -0700972
Ivan Lozano1921e802021-05-20 13:39:16 -0400973 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
974 // RECOVERY_SNAPSHOT_VERSION is current.
975 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
976 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
977 lib.collectHeadersForSnapshot(ctx, deps)
978 }
979 }
980
Jiyong Park459feca2020-12-15 11:02:21 +0900981 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400982 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900983 // If the module has been specifically configure to not be installed then
984 // hide from make as otherwise it will break when running inside make as the
985 // output path to install will not be specified. Not all uninstallable
986 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400987 // side dependencies. In particular, proc-macros need to be captured in the
988 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900989 mod.HideFromMake()
990 } else if !mod.installable(apexInfo) {
991 mod.SkipInstall()
992 }
993
994 // Still call install though, the installs will be stored as PackageSpecs to allow
995 // using the outputs in a genrule.
996 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200997 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900998 if ctx.Failed() {
999 return
1000 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001001 }
Chris Wailes74be7642021-07-22 16:20:28 -07001002
1003 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001004 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001005 if mod.testModule {
1006 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
1007 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001008}
1009
1010func (mod *Module) deps(ctx DepsContext) Deps {
1011 deps := Deps{}
1012
1013 if mod.compiler != nil {
1014 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001015 }
1016 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001017 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018 }
1019
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001020 if mod.coverage != nil {
1021 deps = mod.coverage.deps(ctx, deps)
1022 }
1023
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001024 if mod.sanitize != nil {
1025 deps = mod.sanitize.deps(ctx, deps)
1026 }
1027
Ivan Lozanoffee3342019-08-27 12:03:00 -07001028 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1029 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001030 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1032 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1033 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001034 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001035 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001036 return deps
1037
1038}
1039
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040type dependencyTag struct {
1041 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001042 name string
1043 library bool
1044 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001045 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046}
1047
Jiyong Park65b62242020-11-25 12:44:59 +09001048// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1049// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1050func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001051 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001052}
1053
1054var _ android.InstallNeededDependencyTag = dependencyTag{}
1055
Colin Cross65cb3142021-12-10 23:05:02 +00001056func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1057 if d.library && d.dynamic {
1058 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1059 }
1060 return nil
1061}
1062
1063var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1064
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001066 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1067 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001068 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001069 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001070 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001071 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001072 dataLibDepTag = dependencyTag{name: "data lib"}
1073 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074)
1075
Jiyong Park99644e92020-11-17 22:21:02 +09001076func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1077 tag, ok := depTag.(dependencyTag)
1078 return ok && tag == dylibDepTag
1079}
1080
Jiyong Park94e22fd2021-04-08 18:19:15 +09001081func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1082 tag, ok := depTag.(dependencyTag)
1083 return ok && tag == rlibDepTag
1084}
1085
Matthew Maurer0f003b12020-06-29 14:34:06 -07001086type autoDep struct {
1087 variation string
1088 depTag dependencyTag
1089}
1090
1091var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001092 rlibVariation = "rlib"
1093 dylibVariation = "dylib"
1094 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1095 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001096)
1097
1098type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001099 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001100}
1101
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001102func (mod *Module) begin(ctx BaseModuleContext) {
1103 if mod.coverage != nil {
1104 mod.coverage.begin(ctx)
1105 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001106 if mod.sanitize != nil {
1107 mod.sanitize.begin(ctx)
1108 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001109}
1110
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001111func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001112 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001113 return p.prebuilt()
1114 }
1115 return nil
1116}
1117
Justin Yun24b246a2023-03-16 10:36:16 +09001118func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1119 if rustDep, ok := dep.(*Module); ok {
1120 // Use base module name for snapshots when exporting to Makefile.
1121 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1122 baseName := rustDep.BaseModuleName()
1123 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1124 }
1125 }
1126 return cc.MakeLibName(ctx, c, dep, depName)
1127}
1128
Ivan Lozanod106efe2023-09-21 23:30:26 -04001129func collectIncludedProtos(mod *Module, dep *Module) {
1130 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1131 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1132 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1133 }
1134 }
1135}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1137 var depPaths PathDeps
1138
1139 directRlibDeps := []*Module{}
1140 directDylibDeps := []*Module{}
1141 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001142 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001143 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001144 directSrcProvidersDeps := []*Module{}
1145 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001146
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001147 // For the dependency from platform to apex, use the latest stubs
1148 mod.apexSdkVersion = android.FutureApiLevel
1149 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1150 if !apexInfo.IsForPlatform() {
1151 mod.apexSdkVersion = apexInfo.MinSdkVersion
1152 }
1153
1154 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1155 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1156 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1157 // (b/144430859)
1158 mod.apexSdkVersion = android.FutureApiLevel
1159 }
1160
Spandan Das604f3762023-03-16 22:51:40 +00001161 skipModuleList := map[string]bool{}
1162
1163 var apiImportInfo multitree.ApiImportInfo
1164 hasApiImportInfo := false
1165
1166 ctx.VisitDirectDeps(func(dep android.Module) {
1167 if dep.Name() == "api_imports" {
1168 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1169 hasApiImportInfo = true
1170 }
1171 })
1172
1173 if hasApiImportInfo {
1174 targetStubModuleList := map[string]string{}
1175 targetOrigModuleList := map[string]string{}
1176
1177 // Search for dependency which both original module and API imported library with APEX stub exists
1178 ctx.VisitDirectDeps(func(dep android.Module) {
1179 depName := ctx.OtherModuleName(dep)
1180 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1181 targetStubModuleList[apiLibrary] = depName
1182 }
1183 })
1184 ctx.VisitDirectDeps(func(dep android.Module) {
1185 depName := ctx.OtherModuleName(dep)
1186 if origLibrary, ok := targetStubModuleList[depName]; ok {
1187 targetOrigModuleList[origLibrary] = depName
1188 }
1189 })
1190
1191 // Decide which library should be used between original and API imported library
1192 ctx.VisitDirectDeps(func(dep android.Module) {
1193 depName := ctx.OtherModuleName(dep)
1194 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1195 if cc.ShouldUseStubForApex(ctx, dep) {
1196 skipModuleList[depName] = true
1197 } else {
1198 skipModuleList[apiLibrary] = true
1199 }
1200 }
1201 })
1202 }
1203
Cole Faustb6e6f992023-08-17 17:42:26 -07001204 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1205 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001206
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207 ctx.VisitDirectDeps(func(dep android.Module) {
1208 depName := ctx.OtherModuleName(dep)
1209 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001210
Spandan Das604f3762023-03-16 22:51:40 +00001211 if _, exists := skipModuleList[depName]; exists {
1212 return
1213 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001214
1215 if depTag == android.DarwinUniversalVariantTag {
1216 return
1217 }
1218
Ivan Lozano89435d12020-07-31 11:01:18 -04001219 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001220 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001221 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001222
Ivan Lozanoffee3342019-08-27 12:03:00 -07001223 switch depTag {
1224 case dylibDepTag:
1225 dylib, ok := rustDep.compiler.(libraryInterface)
1226 if !ok || !dylib.dylib() {
1227 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1228 return
1229 }
1230 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001231 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001232 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1233
Ivan Lozanoffee3342019-08-27 12:03:00 -07001234 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001235
Ivan Lozanoffee3342019-08-27 12:03:00 -07001236 rlib, ok := rustDep.compiler.(libraryInterface)
1237 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001238 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001239 return
1240 }
1241 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001242 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001243 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1244
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 case procMacroDepTag:
1246 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001247 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001248
1249 case sourceDepTag:
1250 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1251 collectIncludedProtos(mod, rustDep)
1252 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001253 }
1254
Cole Faustb6e6f992023-08-17 17:42:26 -07001255 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1256
Paul Duffind5cf92e2021-07-09 17:38:55 +01001257 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001258 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1259 // OS/Arch variant is used.
1260 var helper string
1261 if ctx.Host() {
1262 helper = "missing 'host_supported'?"
1263 } else {
1264 helper = "device module defined?"
1265 }
1266
1267 if dep.Target().Os != ctx.Os() {
1268 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1269 return
1270 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1271 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1272 return
1273 }
1274 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275 }
1276
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001277 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001278 if depTag != procMacroDepTag {
1279 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1280 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1281 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1282 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 }
1284
Ivan Lozanoffee3342019-08-27 12:03:00 -07001285 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001286 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001287 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001288 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001289 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001290 }
1291 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001292 if depTag == sourceDepTag {
1293 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1294 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
1295 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1296 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1297 }
1298 }
1299 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001300 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001301 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001302 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001303 if _, ok := ccDep.(*Module); !ok {
1304 if ccDep.Module().Target().Os != ctx.Os() {
1305 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1306 return
1307 }
1308 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1309 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1310 return
1311 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001312 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001313 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001314 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001315 if !ctx.Config().AllowMissingDependencies() {
1316 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1317 } else {
1318 ctx.AddMissingDependencies([]string{depName})
1319 }
1320 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001321 }
1322
Wen-yi Chu41326c12023-09-22 03:58:59 +00001323 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001324
Ivan Lozanoffee3342019-08-27 12:03:00 -07001325 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001326 switch {
1327 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001328 if cc.IsWholeStaticLib(depTag) {
1329 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1330 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001331 if mod.Binary() {
1332 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1333 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1334 // final linkage, pass the args directly to the linker to handle these cases.
1335 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1336 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001337 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001338 } else {
1339 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 -05001340 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001341 }
1342
1343 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1344 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001345 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001346 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1347
Colin Cross0de8a1e2020-09-18 14:15:30 -07001348 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1349 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1350 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1351 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1352 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001353 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001354
1355 // Record baseLibName for snapshots.
1356 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1357
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001358 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001359 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001360 // For the shared lib dependencies, we may link to the stub variant
1361 // of the dependency depending on the context (e.g. if this
1362 // dependency crosses the APEX boundaries).
1363 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1364
1365 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1366 // object (either from stub or non-stub) to use.
1367 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001368 if !linkObject.Valid() {
1369 if !ctx.Config().AllowMissingDependencies() {
1370 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1371 } else {
1372 ctx.AddMissingDependencies([]string{depName})
1373 }
1374 return
1375 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001376 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001377
Ivan Lozanoffee3342019-08-27 12:03:00 -07001378 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001379 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001380 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1381 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1382 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1383 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001384 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001385
1386 // Record baseLibName for snapshots.
1387 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1388
Cole Faustb6e6f992023-08-17 17:42:26 -07001389 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001390 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001391 case cc.IsHeaderDepTag(depTag):
1392 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1393 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1394 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1395 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001396 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001397 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001398 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001399 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001400 }
1401
1402 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001403 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1404 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001405 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001406 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001407 } else {
1408 switch {
1409 case depTag == cc.CrtBeginDepTag:
1410 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1411 case depTag == cc.CrtEndDepTag:
1412 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1413 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001414 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001415
1416 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001417 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001418 // These are usually genrules which don't have per-target variants.
1419 directSrcDeps = append(directSrcDeps, srcDep)
1420 }
1421 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001422 })
1423
Wen-yi Chu41326c12023-09-22 03:58:59 +00001424 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1425
1426 var rlibDepFiles RustLibraries
1427 for _, dep := range directRlibDeps {
1428 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1429 }
1430 var dylibDepFiles RustLibraries
1431 for _, dep := range directDylibDeps {
1432 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1433 }
1434 var procMacroDepFiles RustLibraries
1435 for _, dep := range directProcMacroDeps {
1436 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1437 }
1438
Colin Cross004bd3f2023-10-02 11:39:17 -07001439 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001440 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001441 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001442 }
1443
Colin Cross004bd3f2023-10-02 11:39:17 -07001444 var sharedLibFiles android.Paths
1445 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001446 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001447 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001448 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001449 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001450 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001451 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001452 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001453 }
1454
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001455 var srcProviderDepFiles android.Paths
1456 for _, dep := range directSrcProvidersDeps {
1457 srcs, _ := dep.OutputFiles("")
1458 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1459 }
1460 for _, dep := range directSrcDeps {
1461 srcs := dep.Srcs()
1462 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1463 }
1464
Wen-yi Chu41326c12023-09-22 03:58:59 +00001465 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1466 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001467 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1468 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1469 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001470 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001471 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472
1473 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001474 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001475 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001477 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1478 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1479 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480
1481 return depPaths
1482}
1483
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001484func (mod *Module) InstallInData() bool {
1485 if mod.compiler == nil {
1486 return false
1487 }
1488 return mod.compiler.inData()
1489}
1490
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001491func (mod *Module) InstallInRamdisk() bool {
1492 return mod.InRamdisk()
1493}
1494
1495func (mod *Module) InstallInVendorRamdisk() bool {
1496 return mod.InVendorRamdisk()
1497}
1498
1499func (mod *Module) InstallInRecovery() bool {
1500 return mod.InRecovery()
1501}
1502
Wen-yi Chu41326c12023-09-22 03:58:59 +00001503func linkPathFromFilePath(filepath android.Path) string {
1504 return strings.Split(filepath.String(), filepath.Base())[0]
1505}
1506
Spandan Das604f3762023-03-16 22:51:40 +00001507// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1508func (r *Module) usePublicApi() bool {
1509 return r.Device() && r.UseSdk()
1510}
1511
1512// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1513func (r *Module) useVendorApi() bool {
1514 return r.Device() && (r.InVendor() || r.InProduct())
1515}
1516
Ivan Lozanoffee3342019-08-27 12:03:00 -07001517func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1518 ctx := &depsContext{
1519 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001520 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001521
1522 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001523 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001524 var snapshotInfo *cc.SnapshotInfo
1525
Kiyoung Kim487689e2022-07-26 09:48:22 +09001526 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001527 if mod.usePublicApi() || mod.useVendorApi() {
1528 for idx, lib := range deps.SharedLibs {
1529 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1530 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001531 }
1532
Ivan Lozano1921e802021-05-20 13:39:16 -04001533 if ctx.Os() == android.Android {
1534 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1535 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001536
Ivan Lozano2b081132020-09-08 12:46:52 -04001537 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001538 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001539 stdLinkage = "rlib-std"
1540 }
1541
1542 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001543
Ivan Lozano2b081132020-09-08 12:46:52 -04001544 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1545 rlibDepVariations = append(rlibDepVariations,
1546 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1547 }
1548
Ivan Lozano1921e802021-05-20 13:39:16 -04001549 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001550 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001551 for _, lib := range deps.Rlibs {
1552 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001553 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001554
Ivan Lozano2d407632022-04-07 12:59:11 -04001555 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001556 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001557
1558 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001559 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1560 for _, lib := range deps.Dylibs {
1561 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1562 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001563
Ivan Lozano1921e802021-05-20 13:39:16 -04001564 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001565 if deps.Rustlibs != nil {
1566 if !mod.compiler.Disabled() {
1567 for _, lib := range deps.Rustlibs {
1568 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1569 if autoDep.depTag == rlibDepTag {
1570 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001571 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001572 } else {
1573 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1574 // Check for the existence of the dylib deptag variant. Select it if available,
1575 // otherwise select the rlib variant.
1576 autoDepVariations := append(commonDepVariations,
1577 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1578
1579 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1580
1581 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1582 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1583 } else {
1584 // If there's no dylib dependency available, try to add the rlib dependency instead.
1585 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1586 }
1587 }
1588 }
1589 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1590 for _, lib := range deps.Rustlibs {
1591 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1592 srcProviderVariations := append(commonDepVariations,
1593 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1594
1595 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1596 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001597 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001598 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001599 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001600 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001601
Ivan Lozano1921e802021-05-20 13:39:16 -04001602 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001603 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001604 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001605 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001606 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001607 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001608 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001609 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001610 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001611 for _, lib := range deps.Stdlibs {
1612 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1613 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001614 }
1615 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001616
1617 for _, lib := range deps.SharedLibs {
1618 depTag := cc.SharedDepTag()
1619 name, version := cc.StubsLibNameAndVersion(lib)
1620
1621 variations := []blueprint.Variation{
1622 {Mutator: "link", Variation: "shared"},
1623 }
Spandan Das604f3762023-03-16 22:51:40 +00001624 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1625 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1626 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1627 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1628 }
1629
1630 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1631 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1632 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001633 }
1634
1635 for _, lib := range deps.WholeStaticLibs {
1636 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001637 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001638
1639 actx.AddVariationDependencies([]blueprint.Variation{
1640 {Mutator: "link", Variation: "static"},
1641 }, depTag, lib)
1642 }
1643
1644 for _, lib := range deps.StaticLibs {
1645 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001646 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001647
1648 actx.AddVariationDependencies([]blueprint.Variation{
1649 {Mutator: "link", Variation: "static"},
1650 }, depTag, lib)
1651 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001652
Zach Johnson3df4e632020-11-06 11:56:27 -08001653 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1654
Colin Cross565cafd2020-09-25 18:47:38 -07001655 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001656 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001657 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001658 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001659 }
Colin Crossfe605e12022-01-23 20:46:16 -08001660 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001661 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001662 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001663 }
1664
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001665 if mod.sourceProvider != nil {
1666 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1667 bindgen.Properties.Custom_bindgen != "" {
1668 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1669 bindgen.Properties.Custom_bindgen)
1670 }
1671 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001672
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001673 actx.AddVariationDependencies([]blueprint.Variation{
1674 {Mutator: "link", Variation: "shared"},
1675 }, dataLibDepTag, deps.DataLibs...)
1676
1677 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1678
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001679 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001680 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001681
1682 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001683}
1684
Ivan Lozano2d407632022-04-07 12:59:11 -04001685// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001686func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1687 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001688 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1689}
1690
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001691func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1692 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1693 actx.AddVariationDependencies(variations, depTag, lib)
1694}
1695
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001696func BeginMutator(ctx android.BottomUpMutatorContext) {
1697 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1698 mod.beginMutator(ctx)
1699 }
1700}
1701
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001702func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1703 ctx := &baseModuleContext{
1704 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001705 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001706
1707 mod.begin(ctx)
1708}
1709
Ivan Lozanoffee3342019-08-27 12:03:00 -07001710func (mod *Module) Name() string {
1711 name := mod.ModuleBase.Name()
1712 if p, ok := mod.compiler.(interface {
1713 Name(string) string
1714 }); ok {
1715 name = p.Name(name)
1716 }
1717 return name
1718}
1719
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001720func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001721 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001722 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001723 }
1724}
1725
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001726var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001727var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001728
1729func (mod *Module) HostToolPath() android.OptionalPath {
1730 if !mod.Host() {
1731 return android.OptionalPath{}
1732 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001733 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1734 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001735 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1736 // Even though proc-macros aren't strictly "tools", since they target the compiler
1737 // and act as compiler plugins, we treat them similarly.
1738 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001739 }
1740 return android.OptionalPath{}
1741}
1742
Jiyong Park99644e92020-11-17 22:21:02 +09001743var _ android.ApexModule = (*Module)(nil)
1744
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001745func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001746 return String(mod.Properties.Min_sdk_version)
1747}
1748
Jiyong Park45bf82e2020-12-15 22:29:02 +09001749// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001750func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001751 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001752 if minSdkVersion == "apex_inherit" {
1753 return nil
1754 }
1755 if minSdkVersion == "" {
1756 return fmt.Errorf("min_sdk_version is not specificed")
1757 }
1758
1759 // Not using nativeApiLevelFromUser because the context here is not
1760 // necessarily a native context.
1761 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1762 if err != nil {
1763 return err
1764 }
1765
1766 if ver.GreaterThan(sdkVersion) {
1767 return fmt.Errorf("newer SDK(%v)", ver)
1768 }
Jiyong Park99644e92020-11-17 22:21:02 +09001769 return nil
1770}
1771
Jiyong Park45bf82e2020-12-15 22:29:02 +09001772// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001773func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1774 depTag := ctx.OtherModuleDependencyTag(dep)
1775
1776 if ccm, ok := dep.(*cc.Module); ok {
1777 if ccm.HasStubsVariants() {
1778 if cc.IsSharedDepTag(depTag) {
1779 // dynamic dep to a stubs lib crosses APEX boundary
1780 return false
1781 }
1782 if cc.IsRuntimeDepTag(depTag) {
1783 // runtime dep to a stubs lib also crosses APEX boundary
1784 return false
1785 }
1786
1787 if cc.IsHeaderDepTag(depTag) {
1788 return false
1789 }
1790 }
1791 if mod.Static() && cc.IsSharedDepTag(depTag) {
1792 // shared_lib dependency from a static lib is considered as crossing
1793 // the APEX boundary because the dependency doesn't actually is
1794 // linked; the dependency is used only during the compilation phase.
1795 return false
1796 }
1797 }
1798
Matthew Maurer581b6d82022-09-29 16:46:25 -07001799 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001800 return false
1801 }
1802
1803 return true
1804}
1805
1806// Overrides ApexModule.IsInstallabeToApex()
1807func (mod *Module) IsInstallableToApex() bool {
1808 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001809 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001810 return true
1811 }
1812 if _, ok := mod.compiler.(*binaryDecorator); ok {
1813 return true
1814 }
1815 }
1816 return false
1817}
1818
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001819// If a library file has a "lib" prefix, extract the library name without the prefix.
1820func libNameFromFilePath(filepath android.Path) (string, bool) {
1821 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1822 if strings.HasPrefix(libName, "lib") {
1823 libName = libName[3:]
1824 return libName, true
1825 }
1826 return "", false
1827}
1828
Sasha Smundaka76acba2022-04-18 20:12:56 -07001829func kytheExtractRustFactory() android.Singleton {
1830 return &kytheExtractRustSingleton{}
1831}
1832
1833type kytheExtractRustSingleton struct {
1834}
1835
1836func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1837 var xrefTargets android.Paths
1838 ctx.VisitAllModules(func(module android.Module) {
1839 if rustModule, ok := module.(xref); ok {
1840 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1841 }
1842 })
1843 if len(xrefTargets) > 0 {
1844 ctx.Phony("xref_rust", xrefTargets...)
1845 }
1846}
1847
Jihoon Kangf78a8902022-09-01 22:47:07 +00001848func (c *Module) Partition() string {
1849 return ""
1850}
1851
Chris Parsons637458d2023-09-19 20:09:00 +00001852func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001853 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1854 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001855 } else if ctx.ModuleType() == "rust_proc_macro" {
1856 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001857 } else if ctx.ModuleType() == "rust_binary_host" {
1858 binaryBp2build(ctx, m)
Vinh Tran47faaad2023-09-25 14:47:19 -04001859 } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" {
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001860 protoLibraryBp2build(ctx, m)
Vinh Tran75159942023-10-17 16:28:04 -04001861 } else if ctx.ModuleType() == "rust_ffi_static" {
1862 ffiStaticBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001863 } else {
1864 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1865 }
1866}
1867
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001868// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001869// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001870// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1871// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1872// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001873func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001874 var srcs bazel.LabelList
1875 var compileData bazel.LabelList
1876
1877 if c.Properties.Srcs[0] == "src/lib.rs" {
1878 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1879 compileData = android.BazelLabelForModuleSrc(
1880 ctx,
1881 []string{
1882 "src/**/*.proto",
1883 "examples/**/*.rs",
1884 "**/*.md",
1885 },
1886 )
1887 } else {
1888 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1889 }
1890
1891 return srcs, compileData
1892}
1893
Ivan Lozanoffee3342019-08-27 12:03:00 -07001894var Bool = proptools.Bool
1895var BoolDefault = proptools.BoolDefault
1896var String = proptools.String
1897var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001898
1899var _ android.OutputFileProducer = (*Module)(nil)