blob: 250e39857b9c20965ab21082f78b130c08852a0f [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"
Vinh Tranbcb5f572023-08-24 11:10:01 -040023 "android/soong/ui/metrics/bp2build_metrics_proto"
Ivan Lozanoffee3342019-08-27 12:03:00 -070024
25 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020030 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070031 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000032 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040034 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070035)
36
37var pctx = android.NewPackageContext("android/soong/rust")
38
39func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070040 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050045 })
46 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
47 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070048 })
Inseob Kim3b244062023-07-11 13:31:36 +090049 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070050 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020051 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000052 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
55type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040056 GlobalRustFlags []string // Flags that apply globally to rust
57 GlobalLinkFlags []string // Flags that apply globally to linker
58 RustFlags []string // Flags that apply to rust
59 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070061 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070062 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020064 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070065 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050069 AndroidMkRlibs []string `blueprint:"mutated"`
70 AndroidMkDylibs []string `blueprint:"mutated"`
71 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050072 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040073
Ivan Lozano6a884432020-12-02 09:15:16 -050074 ImageVariationPrefix string `blueprint:"mutated"`
75 VndkVersion string `blueprint:"mutated"`
76 SubName string `blueprint:"mutated"`
77
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
147 hod android.HostOrDeviceSupported
148 multilib android.Multilib
149
Ivan Lozano6a884432020-12-02 09:15:16 -0500150 makeLinkType string
151
Yi Kong46c6e592022-01-20 22:55:00 +0800152 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400154 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200155 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500156 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700159 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400160
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400161 // Output file to be installed, may be stripped or unstripped.
162 outputFile android.OptionalPath
163
Sasha Smundaka76acba2022-04-18 20:12:56 -0700164 // Cross-reference input file
165 kytheFiles android.Paths
166
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400167 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900168
169 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500170
171 // For apex variants, this is set as apex.min_sdk_version
172 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700173
174 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400175
176 android.BazelModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177}
178
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179func (mod *Module) Header() bool {
180 //TODO: If Rust libraries provide header variants, this needs to be updated.
181 return false
182}
183
184func (mod *Module) SetPreventInstall() {
185 mod.Properties.PreventInstall = true
186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SetHideFromMake() {
189 mod.Properties.HideFromMake = true
190}
191
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900192func (mod *Module) HiddenFromMake() bool {
193 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400194}
195
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500196func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500197 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
198 // nil since we need compiler to actually sanitize.
199 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500200}
201
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (mod *Module) IsPrebuilt() bool {
203 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
204 return true
205 }
206 return false
207}
208
Ivan Lozano43845682020-07-09 21:03:28 -0400209func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
210 switch tag {
211 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700212 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400213 return mod.sourceProvider.Srcs(), nil
214 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900215 if mod.OutputFile().Valid() {
216 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400217 }
218 return android.Paths{}, nil
219 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500220 case "unstripped":
221 if mod.compiler != nil {
222 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
223 }
224 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400225 default:
226 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
227 }
228}
229
Ivan Lozano52767be2019-10-18 14:49:46 -0700230func (mod *Module) SelectedStl() string {
231 return ""
232}
233
Ivan Lozano2b262972019-11-21 12:30:50 -0800234func (mod *Module) NonCcVariants() bool {
235 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400236 if _, ok := mod.compiler.(libraryInterface); ok {
237 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800238 }
239 }
240 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
241}
242
Ivan Lozano52767be2019-10-18 14:49:46 -0700243func (mod *Module) Static() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
246 return library.static()
247 }
248 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700250}
251
252func (mod *Module) Shared() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400255 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700256 }
257 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400258 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700259}
260
Ivan Lozanod7586b62021-04-01 09:49:36 -0400261func (mod *Module) Dylib() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok {
264 return library.dylib()
265 }
266 }
267 return false
268}
269
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400270func (mod *Module) RlibStd() bool {
271 if mod.compiler != nil {
272 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
273 return library.rlibStd()
274 }
275 }
276 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
277}
278
Ivan Lozanod7586b62021-04-01 09:49:36 -0400279func (mod *Module) Rlib() bool {
280 if mod.compiler != nil {
281 if library, ok := mod.compiler.(libraryInterface); ok {
282 return library.rlib()
283 }
284 }
285 return false
286}
287
288func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400289 if binary, ok := mod.compiler.(binaryInterface); ok {
290 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400291 }
292 return false
293}
294
Justin Yun5e035862021-06-29 20:50:37 +0900295func (mod *Module) StaticExecutable() bool {
296 if !mod.Binary() {
297 return false
298 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400299 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900300}
301
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302func (mod *Module) Object() bool {
303 // Rust has no modules which produce only object files.
304 return false
305}
306
Ivan Lozano52767be2019-10-18 14:49:46 -0700307func (mod *Module) Toc() android.OptionalPath {
308 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400309 if lib, ok := mod.compiler.(libraryInterface); ok {
310 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700311 }
312 }
313 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
314}
315
Colin Crossc511bc52020-04-07 16:50:32 +0000316func (mod *Module) UseSdk() bool {
317 return false
318}
319
Ivan Lozanod7586b62021-04-01 09:49:36 -0400320func (mod *Module) RelativeInstallPath() string {
321 if mod.compiler != nil {
322 return mod.compiler.relativeInstallPath()
323 }
324 return ""
325}
326
Ivan Lozano52767be2019-10-18 14:49:46 -0700327func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500328 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700329}
330
Jiyong Park7d55b612021-06-11 17:22:09 +0900331func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400332 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900333}
334
Ivan Lozano52767be2019-10-18 14:49:46 -0700335func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400336 return true
337}
338
339func (mod *Module) SubName() string {
340 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700341}
342
343func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500344 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700345 return false
346}
347
Ivan Lozanof9e21722020-12-02 09:00:51 -0500348func (mod *Module) IsVndkExt() bool {
349 return false
350}
351
Ivan Lozanod7586b62021-04-01 09:49:36 -0400352func (mod *Module) IsVndkSp() bool {
353 return false
354}
355
Ivan Lozanof1868af2022-04-12 13:08:36 -0400356func (mod *Module) IsVndkPrebuiltLibrary() bool {
357 // Rust modules do not provide VNDK prebuilts
358 return false
359}
360
361func (mod *Module) IsVendorPublicLibrary() bool {
362 return mod.VendorProperties.IsVendorPublicLibrary
363}
364
365func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
366 // Rust modules to not provide Sdk variants
367 return false
368}
369
Colin Cross127bb8b2020-12-16 16:46:01 -0800370func (c *Module) IsVndkPrivate() bool {
371 return false
372}
373
374func (c *Module) IsLlndk() bool {
375 return false
376}
377
378func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500379 return false
380}
381
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400382func (mod *Module) KernelHeadersDecorator() bool {
383 return false
384}
385
Colin Cross1f3f1302021-04-26 18:37:44 -0700386func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400387 return false
388}
389
Colin Cross5271fea2021-04-27 13:06:04 -0700390func (m *Module) NeedsVendorPublicLibraryVariants() bool {
391 return false
392}
393
Ivan Lozanod7586b62021-04-01 09:49:36 -0400394func (mod *Module) HasLlndkStubs() bool {
395 return false
396}
397
398func (mod *Module) StubsVersion() string {
399 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
400}
401
Ivan Lozano52767be2019-10-18 14:49:46 -0700402func (mod *Module) SdkVersion() string {
403 return ""
404}
405
Colin Crossc511bc52020-04-07 16:50:32 +0000406func (mod *Module) AlwaysSdk() bool {
407 return false
408}
409
Jiyong Park2286afd2020-06-16 21:58:53 +0900410func (mod *Module) IsSdkVariant() bool {
411 return false
412}
413
Colin Cross1348ce32020-10-01 13:37:16 -0700414func (mod *Module) SplitPerApiLevel() bool {
415 return false
416}
417
Sasha Smundaka76acba2022-04-18 20:12:56 -0700418func (mod *Module) XrefRustFiles() android.Paths {
419 return mod.kytheFiles
420}
421
Ivan Lozanoffee3342019-08-27 12:03:00 -0700422type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400423 Dylibs []string
424 Rlibs []string
425 Rustlibs []string
426 Stdlibs []string
427 ProcMacros []string
428 SharedLibs []string
429 StaticLibs []string
430 WholeStaticLibs []string
431 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400433 // Used for data dependencies adjacent to tests
434 DataLibs []string
435 DataBins []string
436
Colin Crossfe605e12022-01-23 20:46:16 -0800437 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438}
439
440type PathDeps struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000441 DyLibs RustLibraries
442 RLibs RustLibraries
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700443 LibDeps android.Paths
444 WholeStaticLibs android.Paths
Wen-yi Chu41326c12023-09-22 03:58:59 +0000445 ProcMacros RustLibraries
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700446 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500447
448 // depFlags and depLinkFlags are rustc and linker (clang) flags.
449 depFlags []string
450 depLinkFlags []string
451
452 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
453 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000454 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700455 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700456
Ivan Lozano45901ed2020-07-24 16:05:01 -0400457 // Used by bindgen modules which call clang
458 depClangFlags []string
459 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400460 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400461 depSystemIncludePaths android.Paths
462
Colin Crossfe605e12022-01-23 20:46:16 -0800463 CrtBegin android.Paths
464 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700465
466 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500467 SrcDeps android.Paths
468 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700469}
470
471type RustLibraries []RustLibrary
472
473type RustLibrary struct {
474 Path android.Path
475 CrateName string
476}
477
478type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100479 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700480 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400481 cfgFlags(ctx ModuleContext, flags Flags) Flags
482 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700483 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700484 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485 compilerDeps(ctx DepsContext, deps Deps) Deps
486 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700487 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700488
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100489 // Output directory in which source-generated code from dependencies is
490 // copied. This is equivalent to Cargo's OUT_DIR variable.
491 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700492
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400493 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
494 CargoPkgVersion() string
495
496 // CargoEnvCompat returns whether Cargo environment variables should be used.
497 CargoEnvCompat() bool
498
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800499 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200500 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700501 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400502 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400503
504 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400505
506 Disabled() bool
507 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400508
Ivan Lozanodd055472020-09-28 13:22:45 -0400509 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500510 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900511
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400512 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900513 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400514}
515
Matthew Maurerbb3add12020-06-25 09:34:12 -0700516type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000517 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700518 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700519}
520
Sasha Smundaka76acba2022-04-18 20:12:56 -0700521type xref interface {
522 XrefRustFiles() android.Paths
523}
524
Matthew Maurerbb3add12020-06-25 09:34:12 -0700525type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000526 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700527 linkObjects android.Paths
528 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700529}
530
Wen-yi Chu41326c12023-09-22 03:58:59 +0000531func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
532 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700533}
534
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700535func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
536 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
537}
538
539func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
540 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400541}
542
Colin Cross0de8a1e2020-09-18 14:15:30 -0700543func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
544 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700545 LinkDirs: flagExporter.linkDirs,
546 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700547 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700548 })
549}
550
Matthew Maurerbb3add12020-06-25 09:34:12 -0700551var _ exportedFlagsProducer = (*flagExporter)(nil)
552
553func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700554 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700555}
556
Colin Cross0de8a1e2020-09-18 14:15:30 -0700557type FlagExporterInfo struct {
558 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000559 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700560 LinkObjects android.Paths
561 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700562}
563
564var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
565
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400566func (mod *Module) isCoverageVariant() bool {
567 return mod.coverage.Properties.IsCoverageVariant
568}
569
570var _ cc.Coverage = (*Module)(nil)
571
572func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
573 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
574}
575
Ivan Lozanod7586b62021-04-01 09:49:36 -0400576func (mod *Module) VndkVersion() string {
577 return mod.Properties.VndkVersion
578}
579
580func (mod *Module) PreventInstall() bool {
581 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400582}
583
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400584func (mod *Module) MarkAsCoverageVariant(coverage bool) {
585 mod.coverage.Properties.IsCoverageVariant = coverage
586}
587
588func (mod *Module) EnableCoverageIfNeeded() {
589 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590}
591
592func defaultsFactory() android.Module {
593 return DefaultsFactory()
594}
595
596type Defaults struct {
597 android.ModuleBase
598 android.DefaultsModuleBase
599}
600
601func DefaultsFactory(props ...interface{}) android.Module {
602 module := &Defaults{}
603
604 module.AddProperties(props...)
605 module.AddProperties(
606 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800607 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500608 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100609 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400610 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611 &BaseCompilerProperties{},
612 &BinaryCompilerProperties{},
613 &LibraryCompilerProperties{},
614 &ProcMacroCompilerProperties{},
615 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400616 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700617 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400618 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400619 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200620 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500621 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000622 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700623 )
624
625 android.InitDefaultsModule(module)
626 return module
627}
628
629func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700630 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700631}
632
Ivan Lozano183a3212019-10-18 14:18:45 -0700633func (mod *Module) CcLibrary() bool {
634 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500635 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700636 return true
637 }
638 }
639 return false
640}
641
642func (mod *Module) CcLibraryInterface() bool {
643 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400644 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
645 // VariantIs{Static,Shared} is set.
646 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700647 return true
648 }
649 }
650 return false
651}
652
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400653func (mod *Module) RustLibraryInterface() bool {
654 if mod.compiler != nil {
655 if _, ok := mod.compiler.(libraryInterface); ok {
656 return true
657 }
658 }
659 return false
660}
661
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500662func (mod *Module) IsFuzzModule() bool {
663 if _, ok := mod.compiler.(*fuzzDecorator); ok {
664 return true
665 }
666 return false
667}
668
669func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
670 return mod.FuzzModule
671}
672
673func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
674 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
675 return fuzzer.fuzzPackagedModule
676 }
677 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
678}
679
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000680func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500681 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
682 return fuzzer.sharedLibraries
683 }
684 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
685}
686
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400687func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400688 if mod.compiler != nil {
689 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400690 }
691 return nil
692}
693
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800694func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700695 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700696 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800697 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700698 }
699 }
700 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
701}
702
703func (mod *Module) SetStatic() {
704 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700705 if library, ok := mod.compiler.(libraryInterface); ok {
706 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700707 return
708 }
709 }
710 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
711}
712
713func (mod *Module) SetShared() {
714 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700715 if library, ok := mod.compiler.(libraryInterface); ok {
716 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700717 return
718 }
719 }
720 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
721}
722
Ivan Lozano183a3212019-10-18 14:18:45 -0700723func (mod *Module) BuildStaticVariant() bool {
724 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700725 if library, ok := mod.compiler.(libraryInterface); ok {
726 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700727 }
728 }
729 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
730}
731
732func (mod *Module) BuildSharedVariant() bool {
733 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700734 if library, ok := mod.compiler.(libraryInterface); ok {
735 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700736 }
737 }
738 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
739}
740
Ivan Lozano183a3212019-10-18 14:18:45 -0700741func (mod *Module) Module() android.Module {
742 return mod
743}
744
Ivan Lozano183a3212019-10-18 14:18:45 -0700745func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400746 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700747}
748
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400749func (mod *Module) CoverageFiles() android.Paths {
750 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800751 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400752 }
753 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
754}
755
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400756// Rust does not produce gcno files, and therefore does not produce a coverage archive.
757func (mod *Module) CoverageOutputFile() android.OptionalPath {
758 return android.OptionalPath{}
759}
760
761func (mod *Module) IsNdk(config android.Config) bool {
762 return false
763}
764
765func (mod *Module) IsStubs() bool {
766 return false
767}
768
Jiyong Park459feca2020-12-15 11:02:21 +0900769func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900770 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900771 return false
772 }
773
Jiyong Park459feca2020-12-15 11:02:21 +0900774 // The apex variant is not installable because it is included in the APEX and won't appear
775 // in the system partition as a standalone file.
776 if !apexInfo.IsForPlatform() {
777 return false
778 }
779
Jiyong Parke54f07e2021-04-07 15:08:04 +0900780 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900781}
782
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500783func (ctx moduleContext) apexVariationName() string {
784 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
785}
786
Ivan Lozano183a3212019-10-18 14:18:45 -0700787var _ cc.LinkableInterface = (*Module)(nil)
788
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789func (mod *Module) Init() android.Module {
790 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500791 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792
Yi Kong46c6e592022-01-20 22:55:00 +0800793 if mod.afdo != nil {
794 mod.AddProperties(mod.afdo.props()...)
795 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 if mod.compiler != nil {
797 mod.AddProperties(mod.compiler.compilerProps()...)
798 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400799 if mod.coverage != nil {
800 mod.AddProperties(mod.coverage.props()...)
801 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200802 if mod.clippy != nil {
803 mod.AddProperties(mod.clippy.props()...)
804 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400805 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700806 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400807 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500808 if mod.sanitize != nil {
809 mod.AddProperties(mod.sanitize.props()...)
810 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400811
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900813 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814
815 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816 return mod
817}
818
819func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
820 return &Module{
821 hod: hod,
822 multilib: multilib,
823 }
824}
825func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
826 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800827 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400828 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200829 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500830 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831 return module
832}
833
834type ModuleContext interface {
835 android.ModuleContext
836 ModuleContextIntf
837}
838
839type BaseModuleContext interface {
840 android.BaseModuleContext
841 ModuleContextIntf
842}
843
844type DepsContext interface {
845 android.BottomUpMutatorContext
846 ModuleContextIntf
847}
848
849type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200850 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852}
853
854type depsContext struct {
855 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700856}
857
858type moduleContext struct {
859 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860}
861
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200862type baseModuleContext struct {
863 android.BaseModuleContext
864}
865
866func (ctx *moduleContext) RustModule() *Module {
867 return ctx.Module().(*Module)
868}
869
870func (ctx *moduleContext) toolchain() config.Toolchain {
871 return ctx.RustModule().toolchain(ctx)
872}
873
874func (ctx *depsContext) RustModule() *Module {
875 return ctx.Module().(*Module)
876}
877
878func (ctx *depsContext) toolchain() config.Toolchain {
879 return ctx.RustModule().toolchain(ctx)
880}
881
882func (ctx *baseModuleContext) RustModule() *Module {
883 return ctx.Module().(*Module)
884}
885
886func (ctx *baseModuleContext) toolchain() config.Toolchain {
887 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400888}
889
890func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700891 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
892 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
893 return false
894 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400895 return mod.compiler != nil && mod.compiler.nativeCoverage()
896}
897
Ivan Lozanod7586b62021-04-01 09:49:36 -0400898func (mod *Module) EverInstallable() bool {
899 return mod.compiler != nil &&
900 // Check to see whether the module is actually ever installable.
901 mod.compiler.everInstallable()
902}
903
904func (mod *Module) Installable() *bool {
905 return mod.Properties.Installable
906}
907
Ivan Lozano872d5792022-03-23 17:31:39 -0400908func (mod *Module) ProcMacro() bool {
909 if pm, ok := mod.compiler.(procMacroInterface); ok {
910 return pm.ProcMacro()
911 }
912 return false
913}
914
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
916 if mod.cachedToolchain == nil {
917 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
918 }
919 return mod.cachedToolchain
920}
921
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200922func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
923 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
924}
925
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
927}
928
929func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
930 ctx := &moduleContext{
931 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700932 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700933
Jiyong Park99644e92020-11-17 22:21:02 +0900934 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
935 if !apexInfo.IsForPlatform() {
936 mod.hideApexVariantFromMake = true
937 }
938
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500940 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
941
Ivan Lozanof1868af2022-04-12 13:08:36 -0400942 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700943
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944 if !toolchain.Supported() {
945 // This toolchain's unsupported, there's nothing to do for this mod.
946 return
947 }
948
949 deps := mod.depsToPaths(ctx)
950 flags := Flags{
951 Toolchain: toolchain,
952 }
953
Ivan Lozano67eada32021-09-23 11:50:33 -0400954 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800955 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500956 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800957 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700958 if mod.compiler != nil {
959 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400960 flags = mod.compiler.cfgFlags(ctx, flags)
961 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400962 }
963 if mod.coverage != nil {
964 flags, deps = mod.coverage.flags(ctx, flags, deps)
965 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200966 if mod.clippy != nil {
967 flags, deps = mod.clippy.flags(ctx, flags, deps)
968 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500969 if mod.sanitize != nil {
970 flags, deps = mod.sanitize.flags(ctx, flags, deps)
971 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400972
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200973 // SourceProvider needs to call GenerateSource() before compiler calls
974 // compile() so it can provide the source. A SourceProvider has
975 // multiple variants (e.g. source, rlib, dylib). Only the "source"
976 // variant is responsible for effectively generating the source. The
977 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400978 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200979 if mod.compiler.(libraryInterface).source() {
980 mod.sourceProvider.GenerateSource(ctx, deps)
981 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
982 } else {
983 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
984 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700985 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200986 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400987 }
988
989 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100990 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700991 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400992 if ctx.Failed() {
993 return
994 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700995 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
996 if buildOutput.kytheFile != nil {
997 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
998 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400999 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001000
Dan Albert06feee92021-03-19 15:06:02 -07001001 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001002 if mod.docTimestampFile.Valid() {
1003 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1004 }
Dan Albert06feee92021-03-19 15:06:02 -07001005
Ivan Lozano1921e802021-05-20 13:39:16 -04001006 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1007 // RECOVERY_SNAPSHOT_VERSION is current.
1008 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1009 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1010 lib.collectHeadersForSnapshot(ctx, deps)
1011 }
1012 }
1013
Jiyong Park459feca2020-12-15 11:02:21 +09001014 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001015 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001016 // If the module has been specifically configure to not be installed then
1017 // hide from make as otherwise it will break when running inside make as the
1018 // output path to install will not be specified. Not all uninstallable
1019 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001020 // side dependencies. In particular, proc-macros need to be captured in the
1021 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001022 mod.HideFromMake()
1023 } else if !mod.installable(apexInfo) {
1024 mod.SkipInstall()
1025 }
1026
1027 // Still call install though, the installs will be stored as PackageSpecs to allow
1028 // using the outputs in a genrule.
1029 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001030 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001031 if ctx.Failed() {
1032 return
1033 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001034 }
Chris Wailes74be7642021-07-22 16:20:28 -07001035
1036 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001037 }
1038}
1039
1040func (mod *Module) deps(ctx DepsContext) Deps {
1041 deps := Deps{}
1042
1043 if mod.compiler != nil {
1044 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001045 }
1046 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001047 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048 }
1049
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001050 if mod.coverage != nil {
1051 deps = mod.coverage.deps(ctx, deps)
1052 }
1053
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001054 if mod.sanitize != nil {
1055 deps = mod.sanitize.deps(ctx, deps)
1056 }
1057
Ivan Lozanoffee3342019-08-27 12:03:00 -07001058 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1059 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001060 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1062 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1063 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001064 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001065 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066 return deps
1067
1068}
1069
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070type dependencyTag struct {
1071 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001072 name string
1073 library bool
1074 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001075 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001076}
1077
Jiyong Park65b62242020-11-25 12:44:59 +09001078// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1079// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1080func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001081 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001082}
1083
1084var _ android.InstallNeededDependencyTag = dependencyTag{}
1085
Colin Cross65cb3142021-12-10 23:05:02 +00001086func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1087 if d.library && d.dynamic {
1088 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1089 }
1090 return nil
1091}
1092
1093var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1094
Ivan Lozanoffee3342019-08-27 12:03:00 -07001095var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001096 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1097 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001098 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001099 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001100 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001101 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001102 dataLibDepTag = dependencyTag{name: "data lib"}
1103 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001104)
1105
Jiyong Park99644e92020-11-17 22:21:02 +09001106func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1107 tag, ok := depTag.(dependencyTag)
1108 return ok && tag == dylibDepTag
1109}
1110
Jiyong Park94e22fd2021-04-08 18:19:15 +09001111func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1112 tag, ok := depTag.(dependencyTag)
1113 return ok && tag == rlibDepTag
1114}
1115
Matthew Maurer0f003b12020-06-29 14:34:06 -07001116type autoDep struct {
1117 variation string
1118 depTag dependencyTag
1119}
1120
1121var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001122 rlibVariation = "rlib"
1123 dylibVariation = "dylib"
1124 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1125 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001126)
1127
1128type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001129 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001130}
1131
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001132func (mod *Module) begin(ctx BaseModuleContext) {
1133 if mod.coverage != nil {
1134 mod.coverage.begin(ctx)
1135 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001136 if mod.sanitize != nil {
1137 mod.sanitize.begin(ctx)
1138 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001139}
1140
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001141func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001142 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001143 return p.prebuilt()
1144 }
1145 return nil
1146}
1147
Justin Yun24b246a2023-03-16 10:36:16 +09001148func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1149 if rustDep, ok := dep.(*Module); ok {
1150 // Use base module name for snapshots when exporting to Makefile.
1151 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1152 baseName := rustDep.BaseModuleName()
1153 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1154 }
1155 }
1156 return cc.MakeLibName(ctx, c, dep, depName)
1157}
1158
Ivan Lozanoffee3342019-08-27 12:03:00 -07001159func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1160 var depPaths PathDeps
1161
1162 directRlibDeps := []*Module{}
1163 directDylibDeps := []*Module{}
1164 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001165 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001166 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001167 directSrcProvidersDeps := []*Module{}
1168 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001169
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001170 // For the dependency from platform to apex, use the latest stubs
1171 mod.apexSdkVersion = android.FutureApiLevel
1172 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1173 if !apexInfo.IsForPlatform() {
1174 mod.apexSdkVersion = apexInfo.MinSdkVersion
1175 }
1176
1177 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1178 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1179 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1180 // (b/144430859)
1181 mod.apexSdkVersion = android.FutureApiLevel
1182 }
1183
Spandan Das604f3762023-03-16 22:51:40 +00001184 skipModuleList := map[string]bool{}
1185
1186 var apiImportInfo multitree.ApiImportInfo
1187 hasApiImportInfo := false
1188
1189 ctx.VisitDirectDeps(func(dep android.Module) {
1190 if dep.Name() == "api_imports" {
1191 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1192 hasApiImportInfo = true
1193 }
1194 })
1195
1196 if hasApiImportInfo {
1197 targetStubModuleList := map[string]string{}
1198 targetOrigModuleList := map[string]string{}
1199
1200 // Search for dependency which both original module and API imported library with APEX stub exists
1201 ctx.VisitDirectDeps(func(dep android.Module) {
1202 depName := ctx.OtherModuleName(dep)
1203 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1204 targetStubModuleList[apiLibrary] = depName
1205 }
1206 })
1207 ctx.VisitDirectDeps(func(dep android.Module) {
1208 depName := ctx.OtherModuleName(dep)
1209 if origLibrary, ok := targetStubModuleList[depName]; ok {
1210 targetOrigModuleList[origLibrary] = depName
1211 }
1212 })
1213
1214 // Decide which library should be used between original and API imported library
1215 ctx.VisitDirectDeps(func(dep android.Module) {
1216 depName := ctx.OtherModuleName(dep)
1217 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1218 if cc.ShouldUseStubForApex(ctx, dep) {
1219 skipModuleList[depName] = true
1220 } else {
1221 skipModuleList[apiLibrary] = true
1222 }
1223 }
1224 })
1225 }
1226
Cole Faustb6e6f992023-08-17 17:42:26 -07001227 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1228 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001229
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230 ctx.VisitDirectDeps(func(dep android.Module) {
1231 depName := ctx.OtherModuleName(dep)
1232 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001233
Spandan Das604f3762023-03-16 22:51:40 +00001234 if _, exists := skipModuleList[depName]; exists {
1235 return
1236 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001237
1238 if depTag == android.DarwinUniversalVariantTag {
1239 return
1240 }
1241
Ivan Lozano89435d12020-07-31 11:01:18 -04001242 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001243 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001244 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001245
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 switch depTag {
1247 case dylibDepTag:
1248 dylib, ok := rustDep.compiler.(libraryInterface)
1249 if !ok || !dylib.dylib() {
1250 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1251 return
1252 }
1253 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001254 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001255 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1256
Ivan Lozanoffee3342019-08-27 12:03:00 -07001257 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001258
Ivan Lozanoffee3342019-08-27 12:03:00 -07001259 rlib, ok := rustDep.compiler.(libraryInterface)
1260 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001261 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001262 return
1263 }
1264 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001265 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001266 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1267
Ivan Lozanoffee3342019-08-27 12:03:00 -07001268 case procMacroDepTag:
1269 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001270 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001271 }
1272
Cole Faustb6e6f992023-08-17 17:42:26 -07001273 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1274
Paul Duffind5cf92e2021-07-09 17:38:55 +01001275 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001276 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1277 // OS/Arch variant is used.
1278 var helper string
1279 if ctx.Host() {
1280 helper = "missing 'host_supported'?"
1281 } else {
1282 helper = "device module defined?"
1283 }
1284
1285 if dep.Target().Os != ctx.Os() {
1286 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1287 return
1288 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1289 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1290 return
1291 }
1292 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001293 }
1294
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001295 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001296 if depTag != procMacroDepTag {
1297 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1298 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1299 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1300 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001301 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001302 }
1303
Ivan Lozanoffee3342019-08-27 12:03:00 -07001304 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001305 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001306 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001307 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001308 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001309 }
1310 }
1311
Ivan Lozano89435d12020-07-31 11:01:18 -04001312 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001313 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001314 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001315 if _, ok := ccDep.(*Module); !ok {
1316 if ccDep.Module().Target().Os != ctx.Os() {
1317 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1318 return
1319 }
1320 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1321 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1322 return
1323 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001324 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001325 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001326 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001327 if !ctx.Config().AllowMissingDependencies() {
1328 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1329 } else {
1330 ctx.AddMissingDependencies([]string{depName})
1331 }
1332 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001333 }
1334
Wen-yi Chu41326c12023-09-22 03:58:59 +00001335 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001336
Ivan Lozanoffee3342019-08-27 12:03:00 -07001337 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001338 switch {
1339 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001340 if cc.IsWholeStaticLib(depTag) {
1341 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1342 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001343 if mod.Binary() {
1344 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1345 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1346 // final linkage, pass the args directly to the linker to handle these cases.
1347 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1348 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001349 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001350 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001351 } else {
1352 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 -05001353 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001354 }
1355
1356 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1357 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001358 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001359 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1360
Colin Cross0de8a1e2020-09-18 14:15:30 -07001361 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1362 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1363 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1364 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1365 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001366 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001367
1368 // Record baseLibName for snapshots.
1369 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1370
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001371 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001372 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001373 // For the shared lib dependencies, we may link to the stub variant
1374 // of the dependency depending on the context (e.g. if this
1375 // dependency crosses the APEX boundaries).
1376 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1377
1378 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1379 // object (either from stub or non-stub) to use.
1380 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001381 if !linkObject.Valid() {
1382 if !ctx.Config().AllowMissingDependencies() {
1383 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1384 } else {
1385 ctx.AddMissingDependencies([]string{depName})
1386 }
1387 return
1388 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001389 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001390
Ivan Lozanoffee3342019-08-27 12:03:00 -07001391 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001392 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001393 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1394 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1395 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1396 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001397 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001398
1399 // Record baseLibName for snapshots.
1400 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1401
Cole Faustb6e6f992023-08-17 17:42:26 -07001402 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001403 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001404 case cc.IsHeaderDepTag(depTag):
1405 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1406 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1407 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1408 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001409 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001410 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001411 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001412 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001413 }
1414
1415 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001416 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1417 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001418 if linkObject.Valid() {
1419 lib.exportLinkObjects(linkObject.Path())
1420 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001421 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001422 } else {
1423 switch {
1424 case depTag == cc.CrtBeginDepTag:
1425 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1426 case depTag == cc.CrtEndDepTag:
1427 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1428 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001429 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001430
1431 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001432 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001433 // These are usually genrules which don't have per-target variants.
1434 directSrcDeps = append(directSrcDeps, srcDep)
1435 }
1436 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001437 })
1438
Wen-yi Chu41326c12023-09-22 03:58:59 +00001439 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1440
1441 var rlibDepFiles RustLibraries
1442 for _, dep := range directRlibDeps {
1443 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1444 }
1445 var dylibDepFiles RustLibraries
1446 for _, dep := range directDylibDeps {
1447 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1448 }
1449 var procMacroDepFiles RustLibraries
1450 for _, dep := range directProcMacroDeps {
1451 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1452 }
1453
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001454 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001456 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001457 }
1458
Ivan Lozanoffee3342019-08-27 12:03:00 -07001459 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001460 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001461 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001462 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001463 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001464 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001465 }
1466
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001467 var srcProviderDepFiles android.Paths
1468 for _, dep := range directSrcProvidersDeps {
1469 srcs, _ := dep.OutputFiles("")
1470 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1471 }
1472 for _, dep := range directSrcDeps {
1473 srcs := dep.Srcs()
1474 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1475 }
1476
Wen-yi Chu41326c12023-09-22 03:58:59 +00001477 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1478 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001479 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001480 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001481 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001482
1483 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001484 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001485 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001486 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001487 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1488 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1489 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001490
1491 return depPaths
1492}
1493
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001494func (mod *Module) InstallInData() bool {
1495 if mod.compiler == nil {
1496 return false
1497 }
1498 return mod.compiler.inData()
1499}
1500
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001501func (mod *Module) InstallInRamdisk() bool {
1502 return mod.InRamdisk()
1503}
1504
1505func (mod *Module) InstallInVendorRamdisk() bool {
1506 return mod.InVendorRamdisk()
1507}
1508
1509func (mod *Module) InstallInRecovery() bool {
1510 return mod.InRecovery()
1511}
1512
Wen-yi Chu41326c12023-09-22 03:58:59 +00001513func linkPathFromFilePath(filepath android.Path) string {
1514 return strings.Split(filepath.String(), filepath.Base())[0]
1515}
1516
Spandan Das604f3762023-03-16 22:51:40 +00001517// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1518func (r *Module) usePublicApi() bool {
1519 return r.Device() && r.UseSdk()
1520}
1521
1522// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1523func (r *Module) useVendorApi() bool {
1524 return r.Device() && (r.InVendor() || r.InProduct())
1525}
1526
Ivan Lozanoffee3342019-08-27 12:03:00 -07001527func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1528 ctx := &depsContext{
1529 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001530 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001531
1532 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001533 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001534 var snapshotInfo *cc.SnapshotInfo
1535
Kiyoung Kim487689e2022-07-26 09:48:22 +09001536 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001537 if mod.usePublicApi() || mod.useVendorApi() {
1538 for idx, lib := range deps.SharedLibs {
1539 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1540 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001541 }
1542
Ivan Lozano1921e802021-05-20 13:39:16 -04001543 if ctx.Os() == android.Android {
1544 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1545 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001546
Ivan Lozano2b081132020-09-08 12:46:52 -04001547 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001548 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001549 stdLinkage = "rlib-std"
1550 }
1551
1552 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001553
Ivan Lozano2b081132020-09-08 12:46:52 -04001554 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1555 rlibDepVariations = append(rlibDepVariations,
1556 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1557 }
1558
Ivan Lozano1921e802021-05-20 13:39:16 -04001559 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001560 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001561 for _, lib := range deps.Rlibs {
1562 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001563 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001564
Ivan Lozano2d407632022-04-07 12:59:11 -04001565 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001566 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001567
1568 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001569 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1570 for _, lib := range deps.Dylibs {
1571 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1572 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001573
Ivan Lozano1921e802021-05-20 13:39:16 -04001574 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001575 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1576 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001577 for _, lib := range deps.Rustlibs {
1578 if autoDep.depTag == rlibDepTag {
1579 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001580 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001581 } else {
1582 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1583 // Check for the existence of the dylib deptag variant. Select it if available,
1584 // otherwise select the rlib variant.
1585 autoDepVariations := append(commonDepVariations,
1586 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001587
1588 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1589
1590 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1591 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001592 } else {
1593 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001594 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001595 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001596 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001597 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001598 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001599 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001600 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001601 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001602 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001603 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001604 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001605 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001606 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001607 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001608 for _, lib := range deps.Stdlibs {
1609 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1610 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001611 }
1612 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001613
1614 for _, lib := range deps.SharedLibs {
1615 depTag := cc.SharedDepTag()
1616 name, version := cc.StubsLibNameAndVersion(lib)
1617
1618 variations := []blueprint.Variation{
1619 {Mutator: "link", Variation: "shared"},
1620 }
Spandan Das604f3762023-03-16 22:51:40 +00001621 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1622 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1623 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1624 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1625 }
1626
1627 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1628 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1629 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001630 }
1631
1632 for _, lib := range deps.WholeStaticLibs {
1633 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001634 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001635
1636 actx.AddVariationDependencies([]blueprint.Variation{
1637 {Mutator: "link", Variation: "static"},
1638 }, depTag, lib)
1639 }
1640
1641 for _, lib := range deps.StaticLibs {
1642 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001643 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001644
1645 actx.AddVariationDependencies([]blueprint.Variation{
1646 {Mutator: "link", Variation: "static"},
1647 }, depTag, lib)
1648 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001649
Zach Johnson3df4e632020-11-06 11:56:27 -08001650 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1651
Colin Cross565cafd2020-09-25 18:47:38 -07001652 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001653 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001654 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001655 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001656 }
Colin Crossfe605e12022-01-23 20:46:16 -08001657 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001658 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001659 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001660 }
1661
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001662 if mod.sourceProvider != nil {
1663 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1664 bindgen.Properties.Custom_bindgen != "" {
1665 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1666 bindgen.Properties.Custom_bindgen)
1667 }
1668 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001669
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001670 actx.AddVariationDependencies([]blueprint.Variation{
1671 {Mutator: "link", Variation: "shared"},
1672 }, dataLibDepTag, deps.DataLibs...)
1673
1674 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1675
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001676 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001677 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001678
1679 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001680}
1681
Ivan Lozano2d407632022-04-07 12:59:11 -04001682// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001683func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1684 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001685 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1686}
1687
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001688func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1689 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1690 actx.AddVariationDependencies(variations, depTag, lib)
1691}
1692
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001693func BeginMutator(ctx android.BottomUpMutatorContext) {
1694 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1695 mod.beginMutator(ctx)
1696 }
1697}
1698
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001699func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1700 ctx := &baseModuleContext{
1701 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001702 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001703
1704 mod.begin(ctx)
1705}
1706
Ivan Lozanoffee3342019-08-27 12:03:00 -07001707func (mod *Module) Name() string {
1708 name := mod.ModuleBase.Name()
1709 if p, ok := mod.compiler.(interface {
1710 Name(string) string
1711 }); ok {
1712 name = p.Name(name)
1713 }
1714 return name
1715}
1716
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001717func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001718 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001719 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001720 }
1721}
1722
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001723var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001724var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001725
1726func (mod *Module) HostToolPath() android.OptionalPath {
1727 if !mod.Host() {
1728 return android.OptionalPath{}
1729 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001730 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1731 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001732 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1733 // Even though proc-macros aren't strictly "tools", since they target the compiler
1734 // and act as compiler plugins, we treat them similarly.
1735 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001736 }
1737 return android.OptionalPath{}
1738}
1739
Jiyong Park99644e92020-11-17 22:21:02 +09001740var _ android.ApexModule = (*Module)(nil)
1741
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001742func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001743 return String(mod.Properties.Min_sdk_version)
1744}
1745
Jiyong Park45bf82e2020-12-15 22:29:02 +09001746// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001747func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001748 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001749 if minSdkVersion == "apex_inherit" {
1750 return nil
1751 }
1752 if minSdkVersion == "" {
1753 return fmt.Errorf("min_sdk_version is not specificed")
1754 }
1755
1756 // Not using nativeApiLevelFromUser because the context here is not
1757 // necessarily a native context.
1758 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1759 if err != nil {
1760 return err
1761 }
1762
1763 if ver.GreaterThan(sdkVersion) {
1764 return fmt.Errorf("newer SDK(%v)", ver)
1765 }
Jiyong Park99644e92020-11-17 22:21:02 +09001766 return nil
1767}
1768
Jiyong Park45bf82e2020-12-15 22:29:02 +09001769// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001770func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1771 depTag := ctx.OtherModuleDependencyTag(dep)
1772
1773 if ccm, ok := dep.(*cc.Module); ok {
1774 if ccm.HasStubsVariants() {
1775 if cc.IsSharedDepTag(depTag) {
1776 // dynamic dep to a stubs lib crosses APEX boundary
1777 return false
1778 }
1779 if cc.IsRuntimeDepTag(depTag) {
1780 // runtime dep to a stubs lib also crosses APEX boundary
1781 return false
1782 }
1783
1784 if cc.IsHeaderDepTag(depTag) {
1785 return false
1786 }
1787 }
1788 if mod.Static() && cc.IsSharedDepTag(depTag) {
1789 // shared_lib dependency from a static lib is considered as crossing
1790 // the APEX boundary because the dependency doesn't actually is
1791 // linked; the dependency is used only during the compilation phase.
1792 return false
1793 }
1794 }
1795
Matthew Maurer581b6d82022-09-29 16:46:25 -07001796 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001797 return false
1798 }
1799
1800 return true
1801}
1802
1803// Overrides ApexModule.IsInstallabeToApex()
1804func (mod *Module) IsInstallableToApex() bool {
1805 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001806 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001807 return true
1808 }
1809 if _, ok := mod.compiler.(*binaryDecorator); ok {
1810 return true
1811 }
1812 }
1813 return false
1814}
1815
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001816// If a library file has a "lib" prefix, extract the library name without the prefix.
1817func libNameFromFilePath(filepath android.Path) (string, bool) {
1818 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1819 if strings.HasPrefix(libName, "lib") {
1820 libName = libName[3:]
1821 return libName, true
1822 }
1823 return "", false
1824}
1825
Sasha Smundaka76acba2022-04-18 20:12:56 -07001826func kytheExtractRustFactory() android.Singleton {
1827 return &kytheExtractRustSingleton{}
1828}
1829
1830type kytheExtractRustSingleton struct {
1831}
1832
1833func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1834 var xrefTargets android.Paths
1835 ctx.VisitAllModules(func(module android.Module) {
1836 if rustModule, ok := module.(xref); ok {
1837 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1838 }
1839 })
1840 if len(xrefTargets) > 0 {
1841 ctx.Phony("xref_rust", xrefTargets...)
1842 }
1843}
1844
Jihoon Kangf78a8902022-09-01 22:47:07 +00001845func (c *Module) Partition() string {
1846 return ""
1847}
1848
Chris Parsons637458d2023-09-19 20:09:00 +00001849func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001850 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1851 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001852 } else if ctx.ModuleType() == "rust_proc_macro" {
1853 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001854 } else if ctx.ModuleType() == "rust_binary_host" {
1855 binaryBp2build(ctx, m)
Vinh Tran47faaad2023-09-25 14:47:19 -04001856 } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" {
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001857 protoLibraryBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001858 } else {
1859 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1860 }
1861}
1862
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001863// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001864// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001865// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1866// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1867// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001868func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001869 var srcs bazel.LabelList
1870 var compileData bazel.LabelList
1871
1872 if c.Properties.Srcs[0] == "src/lib.rs" {
1873 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1874 compileData = android.BazelLabelForModuleSrc(
1875 ctx,
1876 []string{
1877 "src/**/*.proto",
1878 "examples/**/*.rs",
1879 "**/*.md",
1880 },
1881 )
1882 } else {
1883 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1884 }
1885
1886 return srcs, compileData
1887}
1888
Ivan Lozanoffee3342019-08-27 12:03:00 -07001889var Bool = proptools.Bool
1890var BoolDefault = proptools.BoolDefault
1891var String = proptools.String
1892var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001893
1894var _ android.OutputFileProducer = (*Module)(nil)