blob: aa6493dd9d9dd715f4ea5ea3d95c7315ed71b175 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
19 "strings"
20
Sasha Smundaka76acba2022-04-18 20:12:56 -070021 "android/soong/bloaty"
Aditya Choudhary87b2ab22023-11-17 15:27:06 +000022 "android/soong/testing"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090023
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "github.com/google/blueprint"
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020029 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070030 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000031 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040033 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070034)
35
36var pctx = android.NewPackageContext("android/soong/rust")
37
38func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070039 android.RegisterModuleType("rust_defaults", defaultsFactory)
40 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
41 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040042 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040043 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050044 })
45 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
46 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070047 })
Inseob Kim3b244062023-07-11 13:31:36 +090048 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020050 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000051 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040055 GlobalRustFlags []string // Flags that apply globally to rust
56 GlobalLinkFlags []string // Flags that apply globally to linker
57 RustFlags []string // Flags that apply to rust
58 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070060 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070061 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040062 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020063 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070064 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050068 AndroidMkRlibs []string `blueprint:"mutated"`
69 AndroidMkDylibs []string `blueprint:"mutated"`
70 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050071 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +000072 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040073
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090074 ImageVariation string `blueprint:"mutated"`
75 VndkVersion string `blueprint:"mutated"`
76 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -050077
Ivan Lozanoc08897c2021-04-02 12:41:32 -040078 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
79 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
80 // appended before SubName.
81 RustSubName string `blueprint:"mutated"`
82
Ivan Lozano6a884432020-12-02 09:15:16 -050083 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050084 CoreVariantNeeded bool `blueprint:"mutated"`
85 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070086 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080087 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050088 ExtraVariants []string `blueprint:"mutated"`
89
Ivan Lozanoa2268632021-07-22 10:52:06 -040090 // Allows this module to use non-APEX version of libraries. Useful
91 // for building binaries that are started before APEXes are activated.
92 Bootstrap *bool
93
Ivan Lozano1921e802021-05-20 13:39:16 -040094 // Used by vendor snapshot to record dependencies from snapshot modules.
95 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090096 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040097 SnapshotRlibs []string `blueprint:"mutated"`
98 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040099
Matthew Maurerc6868382021-07-13 14:12:37 -0700100 // Make this module available when building for ramdisk.
101 // On device without a dedicated recovery partition, the module is only
102 // available after switching root into
103 // /first_stage_ramdisk. To expose the module before switching root, install
104 // the recovery variant instead.
105 Ramdisk_available *bool
106
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500107 // Make this module available when building for vendor ramdisk.
108 // On device without a dedicated recovery partition, the module is only
109 // available after switching root into
110 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800111 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500112 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400113
Ivan Lozano1921e802021-05-20 13:39:16 -0400114 // Normally Soong uses the directory structure to decide which modules
115 // should be included (framework) or excluded (non-framework) from the
116 // different snapshots (vendor, recovery, etc.), but this property
117 // allows a partner to exclude a module normally thought of as a
118 // framework module from the vendor snapshot.
119 Exclude_from_vendor_snapshot *bool
120
121 // Normally Soong uses the directory structure to decide which modules
122 // should be included (framework) or excluded (non-framework) from the
123 // different snapshots (vendor, recovery, etc.), but this property
124 // allows a partner to exclude a module normally thought of as a
125 // framework module from the recovery snapshot.
126 Exclude_from_recovery_snapshot *bool
127
Matthew Maurer460ee942021-02-11 12:31:46 -0800128 // Make this module available when building for recovery
129 Recovery_available *bool
130
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500131 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
132 Min_sdk_version *string
133
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900134 HideFromMake bool `blueprint:"mutated"`
135 PreventInstall bool `blueprint:"mutated"`
136
137 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138}
139
140type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700141 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142
Ivan Lozano6a884432020-12-02 09:15:16 -0500143 VendorProperties cc.VendorProperties
144
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 Properties BaseProperties
146
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000147 hod android.HostOrDeviceSupported
148 multilib android.Multilib
149 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150
Ivan Lozano6a884432020-12-02 09:15:16 -0500151 makeLinkType string
152
Yi Kong46c6e592022-01-20 22:55:00 +0800153 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400155 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200156 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500157 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400159 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700160 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400161
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400162 // Output file to be installed, may be stripped or unstripped.
163 outputFile android.OptionalPath
164
Sasha Smundaka76acba2022-04-18 20:12:56 -0700165 // Cross-reference input file
166 kytheFiles android.Paths
167
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400168 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900169
170 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500171
172 // For apex variants, this is set as apex.min_sdk_version
173 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700174
175 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400176
LaMont Jones0c971852023-12-07 21:56:59 +0000177 // Aconfig files for all transitive deps. Also exposed via TransitiveDeclarationsInfo
178 mergedAconfigFiles map[string]android.Paths
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) {
Colin Cross40213022023-12-13 15:19:49 -0800513 android.SetProvider(ctx, 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
Colin Crossbc7d76c2023-12-12 16:39:03 -0800531var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700532
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
Colin Crossf5f4ad32024-01-19 15:41:48 -0800539func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540 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 {
Colin Crossff694a82023-12-13 15:54:49 -0800751 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
752 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500753}
754
Ivan Lozano183a3212019-10-18 14:18:45 -0700755var _ cc.LinkableInterface = (*Module)(nil)
756
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757func (mod *Module) Init() android.Module {
758 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500759 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760
Yi Kong46c6e592022-01-20 22:55:00 +0800761 if mod.afdo != nil {
762 mod.AddProperties(mod.afdo.props()...)
763 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764 if mod.compiler != nil {
765 mod.AddProperties(mod.compiler.compilerProps()...)
766 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400767 if mod.coverage != nil {
768 mod.AddProperties(mod.coverage.props()...)
769 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200770 if mod.clippy != nil {
771 mod.AddProperties(mod.clippy.props()...)
772 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400773 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700774 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400775 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500776 if mod.sanitize != nil {
777 mod.AddProperties(mod.sanitize.props()...)
778 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400779
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900781 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782
783 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 return mod
785}
786
787func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
788 return &Module{
789 hod: hod,
790 multilib: multilib,
791 }
792}
793func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
794 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800795 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400796 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200797 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500798 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 return module
800}
801
802type ModuleContext interface {
803 android.ModuleContext
804 ModuleContextIntf
805}
806
807type BaseModuleContext interface {
808 android.BaseModuleContext
809 ModuleContextIntf
810}
811
812type DepsContext interface {
813 android.BottomUpMutatorContext
814 ModuleContextIntf
815}
816
817type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200818 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700820}
821
822type depsContext struct {
823 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824}
825
826type moduleContext struct {
827 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828}
829
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200830type baseModuleContext struct {
831 android.BaseModuleContext
832}
833
834func (ctx *moduleContext) RustModule() *Module {
835 return ctx.Module().(*Module)
836}
837
838func (ctx *moduleContext) toolchain() config.Toolchain {
839 return ctx.RustModule().toolchain(ctx)
840}
841
842func (ctx *depsContext) RustModule() *Module {
843 return ctx.Module().(*Module)
844}
845
846func (ctx *depsContext) toolchain() config.Toolchain {
847 return ctx.RustModule().toolchain(ctx)
848}
849
850func (ctx *baseModuleContext) RustModule() *Module {
851 return ctx.Module().(*Module)
852}
853
854func (ctx *baseModuleContext) toolchain() config.Toolchain {
855 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400856}
857
858func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700859 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
860 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
861 return false
862 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400863 return mod.compiler != nil && mod.compiler.nativeCoverage()
864}
865
Ivan Lozanod7586b62021-04-01 09:49:36 -0400866func (mod *Module) EverInstallable() bool {
867 return mod.compiler != nil &&
868 // Check to see whether the module is actually ever installable.
869 mod.compiler.everInstallable()
870}
871
872func (mod *Module) Installable() *bool {
873 return mod.Properties.Installable
874}
875
Ivan Lozano872d5792022-03-23 17:31:39 -0400876func (mod *Module) ProcMacro() bool {
877 if pm, ok := mod.compiler.(procMacroInterface); ok {
878 return pm.ProcMacro()
879 }
880 return false
881}
882
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
884 if mod.cachedToolchain == nil {
885 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
886 }
887 return mod.cachedToolchain
888}
889
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200890func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
891 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
892}
893
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
895}
896
897func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
898 ctx := &moduleContext{
899 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901
Colin Crossff694a82023-12-13 15:54:49 -0800902 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900903 if !apexInfo.IsForPlatform() {
904 mod.hideApexVariantFromMake = true
905 }
906
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500908 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
909
Ivan Lozanof1868af2022-04-12 13:08:36 -0400910 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700911
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 if !toolchain.Supported() {
913 // This toolchain's unsupported, there's nothing to do for this mod.
914 return
915 }
916
917 deps := mod.depsToPaths(ctx)
918 flags := Flags{
919 Toolchain: toolchain,
920 }
921
Ivan Lozano67eada32021-09-23 11:50:33 -0400922 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800923 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500924 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800925 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926 if mod.compiler != nil {
927 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400928 flags = mod.compiler.cfgFlags(ctx, flags)
929 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400930 }
931 if mod.coverage != nil {
932 flags, deps = mod.coverage.flags(ctx, flags, deps)
933 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200934 if mod.clippy != nil {
935 flags, deps = mod.clippy.flags(ctx, flags, deps)
936 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500937 if mod.sanitize != nil {
938 flags, deps = mod.sanitize.flags(ctx, flags, deps)
939 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400940
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200941 // SourceProvider needs to call GenerateSource() before compiler calls
942 // compile() so it can provide the source. A SourceProvider has
943 // multiple variants (e.g. source, rlib, dylib). Only the "source"
944 // variant is responsible for effectively generating the source. The
945 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400946 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200947 if mod.compiler.(libraryInterface).source() {
948 mod.sourceProvider.GenerateSource(ctx, deps)
949 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
950 } else {
951 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
952 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700953 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200954 }
Colin Cross40213022023-12-13 15:19:49 -0800955 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400956 }
957
958 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100959 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700960 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400961 if ctx.Failed() {
962 return
963 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700964 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
965 if buildOutput.kytheFile != nil {
966 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
967 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400968 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900969
Dan Albert06feee92021-03-19 15:06:02 -0700970 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000971 if mod.docTimestampFile.Valid() {
972 ctx.CheckbuildFile(mod.docTimestampFile.Path())
973 }
Dan Albert06feee92021-03-19 15:06:02 -0700974
Ivan Lozano1921e802021-05-20 13:39:16 -0400975 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
976 // RECOVERY_SNAPSHOT_VERSION is current.
977 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
978 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
979 lib.collectHeadersForSnapshot(ctx, deps)
980 }
981 }
982
Colin Crossff694a82023-12-13 15:54:49 -0800983 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400984 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900985 // If the module has been specifically configure to not be installed then
986 // hide from make as otherwise it will break when running inside make as the
987 // output path to install will not be specified. Not all uninstallable
988 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400989 // side dependencies. In particular, proc-macros need to be captured in the
990 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900991 mod.HideFromMake()
992 } else if !mod.installable(apexInfo) {
993 mod.SkipInstall()
994 }
995
996 // Still call install though, the installs will be stored as PackageSpecs to allow
997 // using the outputs in a genrule.
998 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200999 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001000 if ctx.Failed() {
1001 return
1002 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001003 }
Chris Wailes74be7642021-07-22 16:20:28 -07001004
1005 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001006 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001007 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -08001008 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001009 }
LaMont Jones0c971852023-12-07 21:56:59 +00001010
LaMont Jonesaa005ae2023-12-19 19:01:57 +00001011 android.CollectDependencyAconfigFiles(ctx, &mod.mergedAconfigFiles)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001012}
1013
1014func (mod *Module) deps(ctx DepsContext) Deps {
1015 deps := Deps{}
1016
1017 if mod.compiler != nil {
1018 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001019 }
1020 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001021 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022 }
1023
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001024 if mod.coverage != nil {
1025 deps = mod.coverage.deps(ctx, deps)
1026 }
1027
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001028 if mod.sanitize != nil {
1029 deps = mod.sanitize.deps(ctx, deps)
1030 }
1031
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1033 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001034 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1036 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1037 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001038 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001039 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 return deps
1041
1042}
1043
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044type dependencyTag struct {
1045 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001046 name string
1047 library bool
1048 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001049 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050}
1051
Jiyong Park65b62242020-11-25 12:44:59 +09001052// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1053// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1054func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001055 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001056}
1057
1058var _ android.InstallNeededDependencyTag = dependencyTag{}
1059
Colin Cross65cb3142021-12-10 23:05:02 +00001060func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1061 if d.library && d.dynamic {
1062 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1063 }
1064 return nil
1065}
1066
Yu Liuc8884602024-03-15 18:48:38 +00001067func (d dependencyTag) PropagateAconfigValidation() bool {
1068 return d == rlibDepTag || d == sourceDepTag
1069}
1070
1071var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1072
Colin Cross65cb3142021-12-10 23:05:02 +00001073var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1074
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001076 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1077 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001078 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001079 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001080 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001081 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001082 dataLibDepTag = dependencyTag{name: "data lib"}
1083 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001084)
1085
Jiyong Park99644e92020-11-17 22:21:02 +09001086func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1087 tag, ok := depTag.(dependencyTag)
1088 return ok && tag == dylibDepTag
1089}
1090
Jiyong Park94e22fd2021-04-08 18:19:15 +09001091func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1092 tag, ok := depTag.(dependencyTag)
1093 return ok && tag == rlibDepTag
1094}
1095
Matthew Maurer0f003b12020-06-29 14:34:06 -07001096type autoDep struct {
1097 variation string
1098 depTag dependencyTag
1099}
1100
1101var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001102 rlibVariation = "rlib"
1103 dylibVariation = "dylib"
1104 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1105 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001106)
1107
1108type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001109 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001110}
1111
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001112func (mod *Module) begin(ctx BaseModuleContext) {
1113 if mod.coverage != nil {
1114 mod.coverage.begin(ctx)
1115 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001116 if mod.sanitize != nil {
1117 mod.sanitize.begin(ctx)
1118 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001119}
1120
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001121func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001122 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001123 return p.prebuilt()
1124 }
1125 return nil
1126}
1127
Justin Yun24b246a2023-03-16 10:36:16 +09001128func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1129 if rustDep, ok := dep.(*Module); ok {
1130 // Use base module name for snapshots when exporting to Makefile.
1131 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1132 baseName := rustDep.BaseModuleName()
1133 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1134 }
1135 }
1136 return cc.MakeLibName(ctx, c, dep, depName)
1137}
1138
Ivan Lozanod106efe2023-09-21 23:30:26 -04001139func collectIncludedProtos(mod *Module, dep *Module) {
1140 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1141 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1142 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1143 }
1144 }
1145}
Andrew Walbran52533232024-03-19 11:36:04 +00001146
Ivan Lozanoffee3342019-08-27 12:03:00 -07001147func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1148 var depPaths PathDeps
1149
1150 directRlibDeps := []*Module{}
1151 directDylibDeps := []*Module{}
1152 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001153 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001154 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001155 directSrcProvidersDeps := []*Module{}
1156 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001157
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001158 // For the dependency from platform to apex, use the latest stubs
1159 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001160 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001161 if !apexInfo.IsForPlatform() {
1162 mod.apexSdkVersion = apexInfo.MinSdkVersion
1163 }
1164
1165 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1166 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1167 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1168 // (b/144430859)
1169 mod.apexSdkVersion = android.FutureApiLevel
1170 }
1171
Spandan Das604f3762023-03-16 22:51:40 +00001172 skipModuleList := map[string]bool{}
1173
1174 var apiImportInfo multitree.ApiImportInfo
1175 hasApiImportInfo := false
1176
1177 ctx.VisitDirectDeps(func(dep android.Module) {
1178 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001179 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001180 hasApiImportInfo = true
1181 }
1182 })
1183
1184 if hasApiImportInfo {
1185 targetStubModuleList := map[string]string{}
1186 targetOrigModuleList := map[string]string{}
1187
1188 // Search for dependency which both original module and API imported library with APEX stub exists
1189 ctx.VisitDirectDeps(func(dep android.Module) {
1190 depName := ctx.OtherModuleName(dep)
1191 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1192 targetStubModuleList[apiLibrary] = depName
1193 }
1194 })
1195 ctx.VisitDirectDeps(func(dep android.Module) {
1196 depName := ctx.OtherModuleName(dep)
1197 if origLibrary, ok := targetStubModuleList[depName]; ok {
1198 targetOrigModuleList[origLibrary] = depName
1199 }
1200 })
1201
1202 // Decide which library should be used between original and API imported library
1203 ctx.VisitDirectDeps(func(dep android.Module) {
1204 depName := ctx.OtherModuleName(dep)
1205 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1206 if cc.ShouldUseStubForApex(ctx, dep) {
1207 skipModuleList[depName] = true
1208 } else {
1209 skipModuleList[apiLibrary] = true
1210 }
1211 }
1212 })
1213 }
1214
Cole Faustb6e6f992023-08-17 17:42:26 -07001215 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1216 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001217
Ivan Lozanoffee3342019-08-27 12:03:00 -07001218 ctx.VisitDirectDeps(func(dep android.Module) {
1219 depName := ctx.OtherModuleName(dep)
1220 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001221
Spandan Das604f3762023-03-16 22:51:40 +00001222 if _, exists := skipModuleList[depName]; exists {
1223 return
1224 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001225
1226 if depTag == android.DarwinUniversalVariantTag {
1227 return
1228 }
1229
Ivan Lozano89435d12020-07-31 11:01:18 -04001230 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001231 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001232 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001233
Ivan Lozanoffee3342019-08-27 12:03:00 -07001234 switch depTag {
1235 case dylibDepTag:
1236 dylib, ok := rustDep.compiler.(libraryInterface)
1237 if !ok || !dylib.dylib() {
1238 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1239 return
1240 }
1241 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001242 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001243 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1244
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001246
Ivan Lozanoffee3342019-08-27 12:03:00 -07001247 rlib, ok := rustDep.compiler.(libraryInterface)
1248 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001249 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001250 return
1251 }
1252 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001253 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001254 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1255
Ivan Lozanoffee3342019-08-27 12:03:00 -07001256 case procMacroDepTag:
1257 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001258 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001259
1260 case sourceDepTag:
1261 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1262 collectIncludedProtos(mod, rustDep)
1263 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001264 }
1265
Cole Faustb6e6f992023-08-17 17:42:26 -07001266 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1267
Paul Duffind5cf92e2021-07-09 17:38:55 +01001268 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001269 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1270 // OS/Arch variant is used.
1271 var helper string
1272 if ctx.Host() {
1273 helper = "missing 'host_supported'?"
1274 } else {
1275 helper = "device module defined?"
1276 }
1277
1278 if dep.Target().Os != ctx.Os() {
1279 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1280 return
1281 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1282 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1283 return
1284 }
1285 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001286 }
1287
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001288 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001289 if depTag != procMacroDepTag {
Colin Cross313aa542023-12-13 13:47:44 -08001290 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001291 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1292 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1293 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001294 }
1295
Ivan Lozanoffee3342019-08-27 12:03:00 -07001296 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001297 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001298 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001299 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001300 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001301 }
1302 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001303 if depTag == sourceDepTag {
1304 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1305 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001306 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001307 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1308 }
1309 }
1310 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001311 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001312 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001313 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001314 if _, ok := ccDep.(*Module); !ok {
1315 if ccDep.Module().Target().Os != ctx.Os() {
1316 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1317 return
1318 }
1319 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1320 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1321 return
1322 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001323 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001324 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001325 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001326 if !ctx.Config().AllowMissingDependencies() {
1327 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1328 } else {
1329 ctx.AddMissingDependencies([]string{depName})
1330 }
1331 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001332 }
1333
Wen-yi Chu41326c12023-09-22 03:58:59 +00001334 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001335
Ivan Lozanoffee3342019-08-27 12:03:00 -07001336 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001337 switch {
1338 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001339 if cc.IsWholeStaticLib(depTag) {
1340 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1341 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001342 if mod.Binary() {
1343 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1344 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1345 // final linkage, pass the args directly to the linker to handle these cases.
1346 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1347 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001348 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001349 } else {
1350 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 -05001351 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001352 }
1353
1354 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1355 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001356 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001357 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1358
Colin Cross313aa542023-12-13 13:47:44 -08001359 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001360 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1361 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1362 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1363 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001364 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001365
1366 // Record baseLibName for snapshots.
1367 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1368
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001369 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001370 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001371 // For the shared lib dependencies, we may link to the stub variant
1372 // of the dependency depending on the context (e.g. if this
1373 // dependency crosses the APEX boundaries).
1374 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1375
1376 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1377 // object (either from stub or non-stub) to use.
1378 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001379 if !linkObject.Valid() {
1380 if !ctx.Config().AllowMissingDependencies() {
1381 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1382 } else {
1383 ctx.AddMissingDependencies([]string{depName})
1384 }
1385 return
1386 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001387 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001388
Ivan Lozanoffee3342019-08-27 12:03:00 -07001389 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001390 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001391 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1392 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1393 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1394 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001395 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001396
1397 // Record baseLibName for snapshots.
1398 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1399
Cole Faustb6e6f992023-08-17 17:42:26 -07001400 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001401 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001402 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001403 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001404 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1405 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1406 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001407 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001408 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001409 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001410 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001411 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001412 }
1413
1414 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001415 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1416 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001417 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001418 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001419 } else {
1420 switch {
1421 case depTag == cc.CrtBeginDepTag:
1422 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1423 case depTag == cc.CrtEndDepTag:
1424 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1425 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001426 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001427
1428 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001429 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001430 // These are usually genrules which don't have per-target variants.
1431 directSrcDeps = append(directSrcDeps, srcDep)
1432 }
1433 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001434 })
1435
Wen-yi Chu41326c12023-09-22 03:58:59 +00001436 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1437
1438 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001439 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001440 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001441 crateName := dep.CrateName()
1442 if alias, aliased := aliases[crateName]; aliased {
1443 crateName = alias
1444 }
1445 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001446 }
1447 var dylibDepFiles RustLibraries
1448 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001449 crateName := dep.CrateName()
1450 if alias, aliased := aliases[crateName]; aliased {
1451 crateName = alias
1452 }
1453 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001454 }
1455 var procMacroDepFiles RustLibraries
1456 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001457 crateName := dep.CrateName()
1458 if alias, aliased := aliases[crateName]; aliased {
1459 crateName = alias
1460 }
1461 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001462 }
1463
Colin Cross004bd3f2023-10-02 11:39:17 -07001464 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001465 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001466 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001467 }
1468
Colin Cross004bd3f2023-10-02 11:39:17 -07001469 var sharedLibFiles android.Paths
1470 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001471 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001472 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001473 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001474 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001475 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001476 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001477 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001478 }
1479
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001480 var srcProviderDepFiles android.Paths
1481 for _, dep := range directSrcProvidersDeps {
1482 srcs, _ := dep.OutputFiles("")
1483 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1484 }
1485 for _, dep := range directSrcDeps {
1486 srcs := dep.Srcs()
1487 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1488 }
1489
Wen-yi Chu41326c12023-09-22 03:58:59 +00001490 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1491 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001492 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1493 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1494 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001495 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001496 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001497
1498 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001499 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001500 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001501 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001502 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1503 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1504 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001505
1506 return depPaths
1507}
1508
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001509func (mod *Module) InstallInData() bool {
1510 if mod.compiler == nil {
1511 return false
1512 }
1513 return mod.compiler.inData()
1514}
1515
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001516func (mod *Module) InstallInRamdisk() bool {
1517 return mod.InRamdisk()
1518}
1519
1520func (mod *Module) InstallInVendorRamdisk() bool {
1521 return mod.InVendorRamdisk()
1522}
1523
1524func (mod *Module) InstallInRecovery() bool {
1525 return mod.InRecovery()
1526}
1527
Wen-yi Chu41326c12023-09-22 03:58:59 +00001528func linkPathFromFilePath(filepath android.Path) string {
1529 return strings.Split(filepath.String(), filepath.Base())[0]
1530}
1531
Spandan Das604f3762023-03-16 22:51:40 +00001532// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1533func (r *Module) usePublicApi() bool {
1534 return r.Device() && r.UseSdk()
1535}
1536
1537// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1538func (r *Module) useVendorApi() bool {
1539 return r.Device() && (r.InVendor() || r.InProduct())
1540}
1541
Ivan Lozanoffee3342019-08-27 12:03:00 -07001542func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1543 ctx := &depsContext{
1544 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001545 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001546
1547 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001548 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001549 var snapshotInfo *cc.SnapshotInfo
1550
Kiyoung Kim487689e2022-07-26 09:48:22 +09001551 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001552 if mod.usePublicApi() || mod.useVendorApi() {
1553 for idx, lib := range deps.SharedLibs {
1554 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1555 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001556 }
1557
Ivan Lozano1921e802021-05-20 13:39:16 -04001558 if ctx.Os() == android.Android {
1559 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1560 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001561
Ivan Lozano2b081132020-09-08 12:46:52 -04001562 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001563 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001564 stdLinkage = "rlib-std"
1565 }
1566
1567 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001568
Ivan Lozano2b081132020-09-08 12:46:52 -04001569 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1570 rlibDepVariations = append(rlibDepVariations,
1571 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1572 }
1573
Ivan Lozano1921e802021-05-20 13:39:16 -04001574 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001575 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001576 for _, lib := range deps.Rlibs {
1577 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001578 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001579
Ivan Lozano2d407632022-04-07 12:59:11 -04001580 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001581 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001582
1583 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001584 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1585 for _, lib := range deps.Dylibs {
1586 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1587 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001588
Ivan Lozano1921e802021-05-20 13:39:16 -04001589 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001590 if deps.Rustlibs != nil {
1591 if !mod.compiler.Disabled() {
1592 for _, lib := range deps.Rustlibs {
1593 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1594 if autoDep.depTag == rlibDepTag {
1595 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001596 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001597 } else {
1598 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1599 // Check for the existence of the dylib deptag variant. Select it if available,
1600 // otherwise select the rlib variant.
1601 autoDepVariations := append(commonDepVariations,
1602 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1603
1604 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1605
1606 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1607 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1608 } else {
1609 // If there's no dylib dependency available, try to add the rlib dependency instead.
1610 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1611 }
1612 }
1613 }
1614 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1615 for _, lib := range deps.Rustlibs {
1616 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1617 srcProviderVariations := append(commonDepVariations,
1618 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1619
1620 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1621 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001622 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001623 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001624 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001625 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001626
Ivan Lozano1921e802021-05-20 13:39:16 -04001627 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001628 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001629 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001630 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001631 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001632 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001633 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001634 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001635 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001636 for _, lib := range deps.Stdlibs {
1637 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1638 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001639 }
1640 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001641
1642 for _, lib := range deps.SharedLibs {
1643 depTag := cc.SharedDepTag()
1644 name, version := cc.StubsLibNameAndVersion(lib)
1645
1646 variations := []blueprint.Variation{
1647 {Mutator: "link", Variation: "shared"},
1648 }
Spandan Das604f3762023-03-16 22:51:40 +00001649 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1650 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1651 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1652 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1653 }
1654
1655 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1656 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1657 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001658 }
1659
1660 for _, lib := range deps.WholeStaticLibs {
1661 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001662 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001663
1664 actx.AddVariationDependencies([]blueprint.Variation{
1665 {Mutator: "link", Variation: "static"},
1666 }, depTag, lib)
1667 }
1668
1669 for _, lib := range deps.StaticLibs {
1670 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001671 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001672
1673 actx.AddVariationDependencies([]blueprint.Variation{
1674 {Mutator: "link", Variation: "static"},
1675 }, depTag, lib)
1676 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001677
Zach Johnson3df4e632020-11-06 11:56:27 -08001678 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1679
Colin Cross565cafd2020-09-25 18:47:38 -07001680 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001681 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001682 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001683 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001684 }
Colin Crossfe605e12022-01-23 20:46:16 -08001685 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001686 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001687 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001688 }
1689
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001690 if mod.sourceProvider != nil {
1691 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1692 bindgen.Properties.Custom_bindgen != "" {
1693 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1694 bindgen.Properties.Custom_bindgen)
1695 }
1696 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001697
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001698 actx.AddVariationDependencies([]blueprint.Variation{
1699 {Mutator: "link", Variation: "shared"},
1700 }, dataLibDepTag, deps.DataLibs...)
1701
1702 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1703
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001704 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001705 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001706
1707 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001708}
1709
Ivan Lozano2d407632022-04-07 12:59:11 -04001710// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001711func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1712 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001713 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1714}
1715
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001716func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1717 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1718 actx.AddVariationDependencies(variations, depTag, lib)
1719}
1720
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001721func BeginMutator(ctx android.BottomUpMutatorContext) {
1722 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1723 mod.beginMutator(ctx)
1724 }
1725}
1726
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001727func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1728 ctx := &baseModuleContext{
1729 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001730 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001731
1732 mod.begin(ctx)
1733}
1734
Ivan Lozanoffee3342019-08-27 12:03:00 -07001735func (mod *Module) Name() string {
1736 name := mod.ModuleBase.Name()
1737 if p, ok := mod.compiler.(interface {
1738 Name(string) string
1739 }); ok {
1740 name = p.Name(name)
1741 }
1742 return name
1743}
1744
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001745func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001746 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001747 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001748 }
1749}
1750
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001751var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001752var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001753
1754func (mod *Module) HostToolPath() android.OptionalPath {
1755 if !mod.Host() {
1756 return android.OptionalPath{}
1757 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001758 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1759 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001760 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1761 // Even though proc-macros aren't strictly "tools", since they target the compiler
1762 // and act as compiler plugins, we treat them similarly.
1763 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001764 }
1765 return android.OptionalPath{}
1766}
1767
Jiyong Park99644e92020-11-17 22:21:02 +09001768var _ android.ApexModule = (*Module)(nil)
1769
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001770func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001771 return String(mod.Properties.Min_sdk_version)
1772}
1773
Jiyong Park45bf82e2020-12-15 22:29:02 +09001774// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001775func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001776 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001777 if minSdkVersion == "apex_inherit" {
1778 return nil
1779 }
1780 if minSdkVersion == "" {
1781 return fmt.Errorf("min_sdk_version is not specificed")
1782 }
1783
1784 // Not using nativeApiLevelFromUser because the context here is not
1785 // necessarily a native context.
1786 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1787 if err != nil {
1788 return err
1789 }
1790
1791 if ver.GreaterThan(sdkVersion) {
1792 return fmt.Errorf("newer SDK(%v)", ver)
1793 }
Jiyong Park99644e92020-11-17 22:21:02 +09001794 return nil
1795}
1796
Jiyong Park45bf82e2020-12-15 22:29:02 +09001797// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001798func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1799 depTag := ctx.OtherModuleDependencyTag(dep)
1800
1801 if ccm, ok := dep.(*cc.Module); ok {
1802 if ccm.HasStubsVariants() {
1803 if cc.IsSharedDepTag(depTag) {
1804 // dynamic dep to a stubs lib crosses APEX boundary
1805 return false
1806 }
1807 if cc.IsRuntimeDepTag(depTag) {
1808 // runtime dep to a stubs lib also crosses APEX boundary
1809 return false
1810 }
1811
1812 if cc.IsHeaderDepTag(depTag) {
1813 return false
1814 }
1815 }
1816 if mod.Static() && cc.IsSharedDepTag(depTag) {
1817 // shared_lib dependency from a static lib is considered as crossing
1818 // the APEX boundary because the dependency doesn't actually is
1819 // linked; the dependency is used only during the compilation phase.
1820 return false
1821 }
1822 }
1823
Matthew Maurer581b6d82022-09-29 16:46:25 -07001824 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001825 return false
1826 }
1827
1828 return true
1829}
1830
1831// Overrides ApexModule.IsInstallabeToApex()
1832func (mod *Module) IsInstallableToApex() bool {
1833 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001834 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001835 return true
1836 }
1837 if _, ok := mod.compiler.(*binaryDecorator); ok {
1838 return true
1839 }
1840 }
1841 return false
1842}
1843
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001844// If a library file has a "lib" prefix, extract the library name without the prefix.
1845func libNameFromFilePath(filepath android.Path) (string, bool) {
1846 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1847 if strings.HasPrefix(libName, "lib") {
1848 libName = libName[3:]
1849 return libName, true
1850 }
1851 return "", false
1852}
1853
Sasha Smundaka76acba2022-04-18 20:12:56 -07001854func kytheExtractRustFactory() android.Singleton {
1855 return &kytheExtractRustSingleton{}
1856}
1857
1858type kytheExtractRustSingleton struct {
1859}
1860
1861func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1862 var xrefTargets android.Paths
1863 ctx.VisitAllModules(func(module android.Module) {
1864 if rustModule, ok := module.(xref); ok {
1865 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1866 }
1867 })
1868 if len(xrefTargets) > 0 {
1869 ctx.Phony("xref_rust", xrefTargets...)
1870 }
1871}
1872
Jihoon Kangf78a8902022-09-01 22:47:07 +00001873func (c *Module) Partition() string {
1874 return ""
1875}
1876
Ivan Lozanoffee3342019-08-27 12:03:00 -07001877var Bool = proptools.Bool
1878var BoolDefault = proptools.BoolDefault
1879var String = proptools.String
1880var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001881
1882var _ android.OutputFileProducer = (*Module)(nil)