blob: 49a7ff353223108f9d2f307ec9062521304435da [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 Lozanod106efe2023-09-21 23:30:26 -0400270func (mod *Module) Source() bool {
271 if mod.compiler != nil {
272 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
273 return library.source()
274 }
275 }
276 return false
277}
278
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400279func (mod *Module) RlibStd() bool {
280 if mod.compiler != nil {
281 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
282 return library.rlibStd()
283 }
284 }
285 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
286}
287
Ivan Lozanod7586b62021-04-01 09:49:36 -0400288func (mod *Module) Rlib() bool {
289 if mod.compiler != nil {
290 if library, ok := mod.compiler.(libraryInterface); ok {
291 return library.rlib()
292 }
293 }
294 return false
295}
296
297func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400298 if binary, ok := mod.compiler.(binaryInterface); ok {
299 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400300 }
301 return false
302}
303
Justin Yun5e035862021-06-29 20:50:37 +0900304func (mod *Module) StaticExecutable() bool {
305 if !mod.Binary() {
306 return false
307 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400308 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900309}
310
Ivan Lozanod7586b62021-04-01 09:49:36 -0400311func (mod *Module) Object() bool {
312 // Rust has no modules which produce only object files.
313 return false
314}
315
Ivan Lozano52767be2019-10-18 14:49:46 -0700316func (mod *Module) Toc() android.OptionalPath {
317 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400318 if lib, ok := mod.compiler.(libraryInterface); ok {
319 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700320 }
321 }
322 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
323}
324
Colin Crossc511bc52020-04-07 16:50:32 +0000325func (mod *Module) UseSdk() bool {
326 return false
327}
328
Ivan Lozanod7586b62021-04-01 09:49:36 -0400329func (mod *Module) RelativeInstallPath() string {
330 if mod.compiler != nil {
331 return mod.compiler.relativeInstallPath()
332 }
333 return ""
334}
335
Ivan Lozano52767be2019-10-18 14:49:46 -0700336func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500337 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700338}
339
Jiyong Park7d55b612021-06-11 17:22:09 +0900340func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400341 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900342}
343
Ivan Lozano52767be2019-10-18 14:49:46 -0700344func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400345 return true
346}
347
348func (mod *Module) SubName() string {
349 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700350}
351
352func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500353 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700354 return false
355}
356
Ivan Lozanof9e21722020-12-02 09:00:51 -0500357func (mod *Module) IsVndkExt() bool {
358 return false
359}
360
Ivan Lozanod7586b62021-04-01 09:49:36 -0400361func (mod *Module) IsVndkSp() bool {
362 return false
363}
364
Ivan Lozanof1868af2022-04-12 13:08:36 -0400365func (mod *Module) IsVndkPrebuiltLibrary() bool {
366 // Rust modules do not provide VNDK prebuilts
367 return false
368}
369
370func (mod *Module) IsVendorPublicLibrary() bool {
371 return mod.VendorProperties.IsVendorPublicLibrary
372}
373
374func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
375 // Rust modules to not provide Sdk variants
376 return false
377}
378
Colin Cross127bb8b2020-12-16 16:46:01 -0800379func (c *Module) IsVndkPrivate() bool {
380 return false
381}
382
383func (c *Module) IsLlndk() bool {
384 return false
385}
386
387func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500388 return false
389}
390
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400391func (mod *Module) KernelHeadersDecorator() bool {
392 return false
393}
394
Colin Cross1f3f1302021-04-26 18:37:44 -0700395func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400396 return false
397}
398
Colin Cross5271fea2021-04-27 13:06:04 -0700399func (m *Module) NeedsVendorPublicLibraryVariants() bool {
400 return false
401}
402
Ivan Lozanod7586b62021-04-01 09:49:36 -0400403func (mod *Module) HasLlndkStubs() bool {
404 return false
405}
406
407func (mod *Module) StubsVersion() string {
408 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
409}
410
Ivan Lozano52767be2019-10-18 14:49:46 -0700411func (mod *Module) SdkVersion() string {
412 return ""
413}
414
Colin Crossc511bc52020-04-07 16:50:32 +0000415func (mod *Module) AlwaysSdk() bool {
416 return false
417}
418
Jiyong Park2286afd2020-06-16 21:58:53 +0900419func (mod *Module) IsSdkVariant() bool {
420 return false
421}
422
Colin Cross1348ce32020-10-01 13:37:16 -0700423func (mod *Module) SplitPerApiLevel() bool {
424 return false
425}
426
Sasha Smundaka76acba2022-04-18 20:12:56 -0700427func (mod *Module) XrefRustFiles() android.Paths {
428 return mod.kytheFiles
429}
430
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400432 Dylibs []string
433 Rlibs []string
434 Rustlibs []string
435 Stdlibs []string
436 ProcMacros []string
437 SharedLibs []string
438 StaticLibs []string
439 WholeStaticLibs []string
440 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700441
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400442 // Used for data dependencies adjacent to tests
443 DataLibs []string
444 DataBins []string
445
Colin Crossfe605e12022-01-23 20:46:16 -0800446 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447}
448
449type PathDeps struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000450 DyLibs RustLibraries
451 RLibs RustLibraries
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700452 LibDeps android.Paths
453 WholeStaticLibs android.Paths
Wen-yi Chu41326c12023-09-22 03:58:59 +0000454 ProcMacros RustLibraries
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700455 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500456
457 // depFlags and depLinkFlags are rustc and linker (clang) flags.
458 depFlags []string
459 depLinkFlags []string
460
461 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
462 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000463 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700464 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700465
Ivan Lozano45901ed2020-07-24 16:05:01 -0400466 // Used by bindgen modules which call clang
467 depClangFlags []string
468 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400469 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400470 depSystemIncludePaths android.Paths
471
Colin Crossfe605e12022-01-23 20:46:16 -0800472 CrtBegin android.Paths
473 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700474
475 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500476 SrcDeps android.Paths
477 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478}
479
480type RustLibraries []RustLibrary
481
482type RustLibrary struct {
483 Path android.Path
484 CrateName string
485}
486
487type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100488 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700489 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400490 cfgFlags(ctx ModuleContext, flags Flags) Flags
491 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700492 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700493 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494 compilerDeps(ctx DepsContext, deps Deps) Deps
495 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700496 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700497
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100498 // Output directory in which source-generated code from dependencies is
499 // copied. This is equivalent to Cargo's OUT_DIR variable.
500 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700501
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400502 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
503 CargoPkgVersion() string
504
505 // CargoEnvCompat returns whether Cargo environment variables should be used.
506 CargoEnvCompat() bool
507
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800508 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200509 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700510 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400511 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512
513 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400514
515 Disabled() bool
516 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400517
Ivan Lozanodd055472020-09-28 13:22:45 -0400518 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500519 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900520
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400521 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900522 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400523}
524
Matthew Maurerbb3add12020-06-25 09:34:12 -0700525type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000526 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700527 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700528}
529
Sasha Smundaka76acba2022-04-18 20:12:56 -0700530type xref interface {
531 XrefRustFiles() android.Paths
532}
533
Matthew Maurerbb3add12020-06-25 09:34:12 -0700534type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000535 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700536 linkObjects android.Paths
537 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700538}
539
Wen-yi Chu41326c12023-09-22 03:58:59 +0000540func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
541 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700542}
543
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700544func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
545 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
546}
547
548func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
549 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400550}
551
Colin Cross0de8a1e2020-09-18 14:15:30 -0700552func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
553 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700554 LinkDirs: flagExporter.linkDirs,
555 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700556 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700557 })
558}
559
Matthew Maurerbb3add12020-06-25 09:34:12 -0700560var _ exportedFlagsProducer = (*flagExporter)(nil)
561
562func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700563 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700564}
565
Colin Cross0de8a1e2020-09-18 14:15:30 -0700566type FlagExporterInfo struct {
567 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000568 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700569 LinkObjects android.Paths
570 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700571}
572
573var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
574
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400575func (mod *Module) isCoverageVariant() bool {
576 return mod.coverage.Properties.IsCoverageVariant
577}
578
579var _ cc.Coverage = (*Module)(nil)
580
581func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
582 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
583}
584
Ivan Lozanod7586b62021-04-01 09:49:36 -0400585func (mod *Module) VndkVersion() string {
586 return mod.Properties.VndkVersion
587}
588
589func (mod *Module) PreventInstall() bool {
590 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400591}
592
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400593func (mod *Module) MarkAsCoverageVariant(coverage bool) {
594 mod.coverage.Properties.IsCoverageVariant = coverage
595}
596
597func (mod *Module) EnableCoverageIfNeeded() {
598 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599}
600
601func defaultsFactory() android.Module {
602 return DefaultsFactory()
603}
604
605type Defaults struct {
606 android.ModuleBase
607 android.DefaultsModuleBase
608}
609
610func DefaultsFactory(props ...interface{}) android.Module {
611 module := &Defaults{}
612
613 module.AddProperties(props...)
614 module.AddProperties(
615 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800616 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500617 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100618 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400619 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700620 &BaseCompilerProperties{},
621 &BinaryCompilerProperties{},
622 &LibraryCompilerProperties{},
623 &ProcMacroCompilerProperties{},
624 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400625 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700626 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400627 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400628 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200629 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500630 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000631 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700632 )
633
634 android.InitDefaultsModule(module)
635 return module
636}
637
638func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700639 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640}
641
Ivan Lozano183a3212019-10-18 14:18:45 -0700642func (mod *Module) CcLibrary() bool {
643 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500644 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700645 return true
646 }
647 }
648 return false
649}
650
651func (mod *Module) CcLibraryInterface() bool {
652 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400653 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
654 // VariantIs{Static,Shared} is set.
655 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700656 return true
657 }
658 }
659 return false
660}
661
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400662func (mod *Module) RustLibraryInterface() bool {
663 if mod.compiler != nil {
664 if _, ok := mod.compiler.(libraryInterface); ok {
665 return true
666 }
667 }
668 return false
669}
670
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500671func (mod *Module) IsFuzzModule() bool {
672 if _, ok := mod.compiler.(*fuzzDecorator); ok {
673 return true
674 }
675 return false
676}
677
678func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
679 return mod.FuzzModule
680}
681
682func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
683 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
684 return fuzzer.fuzzPackagedModule
685 }
686 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
687}
688
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000689func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500690 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
691 return fuzzer.sharedLibraries
692 }
693 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
694}
695
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400696func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400697 if mod.compiler != nil {
698 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400699 }
700 return nil
701}
702
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800703func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700704 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700705 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800706 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700707 }
708 }
709 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
710}
711
712func (mod *Module) SetStatic() {
713 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700714 if library, ok := mod.compiler.(libraryInterface); ok {
715 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700716 return
717 }
718 }
719 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
720}
721
722func (mod *Module) SetShared() {
723 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700724 if library, ok := mod.compiler.(libraryInterface); ok {
725 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700726 return
727 }
728 }
729 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
730}
731
Ivan Lozano183a3212019-10-18 14:18:45 -0700732func (mod *Module) BuildStaticVariant() bool {
733 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700734 if library, ok := mod.compiler.(libraryInterface); ok {
735 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700736 }
737 }
738 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
739}
740
741func (mod *Module) BuildSharedVariant() bool {
742 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700743 if library, ok := mod.compiler.(libraryInterface); ok {
744 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700745 }
746 }
747 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
748}
749
Ivan Lozano183a3212019-10-18 14:18:45 -0700750func (mod *Module) Module() android.Module {
751 return mod
752}
753
Ivan Lozano183a3212019-10-18 14:18:45 -0700754func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400755 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700756}
757
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400758func (mod *Module) CoverageFiles() android.Paths {
759 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800760 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400761 }
762 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
763}
764
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400765// Rust does not produce gcno files, and therefore does not produce a coverage archive.
766func (mod *Module) CoverageOutputFile() android.OptionalPath {
767 return android.OptionalPath{}
768}
769
770func (mod *Module) IsNdk(config android.Config) bool {
771 return false
772}
773
774func (mod *Module) IsStubs() bool {
775 return false
776}
777
Jiyong Park459feca2020-12-15 11:02:21 +0900778func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900779 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900780 return false
781 }
782
Jiyong Park459feca2020-12-15 11:02:21 +0900783 // The apex variant is not installable because it is included in the APEX and won't appear
784 // in the system partition as a standalone file.
785 if !apexInfo.IsForPlatform() {
786 return false
787 }
788
Jiyong Parke54f07e2021-04-07 15:08:04 +0900789 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900790}
791
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500792func (ctx moduleContext) apexVariationName() string {
793 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
794}
795
Ivan Lozano183a3212019-10-18 14:18:45 -0700796var _ cc.LinkableInterface = (*Module)(nil)
797
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798func (mod *Module) Init() android.Module {
799 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500800 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801
Yi Kong46c6e592022-01-20 22:55:00 +0800802 if mod.afdo != nil {
803 mod.AddProperties(mod.afdo.props()...)
804 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 if mod.compiler != nil {
806 mod.AddProperties(mod.compiler.compilerProps()...)
807 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400808 if mod.coverage != nil {
809 mod.AddProperties(mod.coverage.props()...)
810 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200811 if mod.clippy != nil {
812 mod.AddProperties(mod.clippy.props()...)
813 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400814 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700815 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400816 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500817 if mod.sanitize != nil {
818 mod.AddProperties(mod.sanitize.props()...)
819 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400820
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900822 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823
824 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825 return mod
826}
827
828func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
829 return &Module{
830 hod: hod,
831 multilib: multilib,
832 }
833}
834func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
835 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800836 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400837 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200838 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500839 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 return module
841}
842
843type ModuleContext interface {
844 android.ModuleContext
845 ModuleContextIntf
846}
847
848type BaseModuleContext interface {
849 android.BaseModuleContext
850 ModuleContextIntf
851}
852
853type DepsContext interface {
854 android.BottomUpMutatorContext
855 ModuleContextIntf
856}
857
858type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200859 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861}
862
863type depsContext struct {
864 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865}
866
867type moduleContext struct {
868 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869}
870
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200871type baseModuleContext struct {
872 android.BaseModuleContext
873}
874
875func (ctx *moduleContext) RustModule() *Module {
876 return ctx.Module().(*Module)
877}
878
879func (ctx *moduleContext) toolchain() config.Toolchain {
880 return ctx.RustModule().toolchain(ctx)
881}
882
883func (ctx *depsContext) RustModule() *Module {
884 return ctx.Module().(*Module)
885}
886
887func (ctx *depsContext) toolchain() config.Toolchain {
888 return ctx.RustModule().toolchain(ctx)
889}
890
891func (ctx *baseModuleContext) RustModule() *Module {
892 return ctx.Module().(*Module)
893}
894
895func (ctx *baseModuleContext) toolchain() config.Toolchain {
896 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400897}
898
899func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700900 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
901 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
902 return false
903 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400904 return mod.compiler != nil && mod.compiler.nativeCoverage()
905}
906
Ivan Lozanod7586b62021-04-01 09:49:36 -0400907func (mod *Module) EverInstallable() bool {
908 return mod.compiler != nil &&
909 // Check to see whether the module is actually ever installable.
910 mod.compiler.everInstallable()
911}
912
913func (mod *Module) Installable() *bool {
914 return mod.Properties.Installable
915}
916
Ivan Lozano872d5792022-03-23 17:31:39 -0400917func (mod *Module) ProcMacro() bool {
918 if pm, ok := mod.compiler.(procMacroInterface); ok {
919 return pm.ProcMacro()
920 }
921 return false
922}
923
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
925 if mod.cachedToolchain == nil {
926 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
927 }
928 return mod.cachedToolchain
929}
930
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200931func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
932 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
933}
934
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
936}
937
938func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
939 ctx := &moduleContext{
940 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942
Jiyong Park99644e92020-11-17 22:21:02 +0900943 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
944 if !apexInfo.IsForPlatform() {
945 mod.hideApexVariantFromMake = true
946 }
947
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500949 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
950
Ivan Lozanof1868af2022-04-12 13:08:36 -0400951 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700952
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953 if !toolchain.Supported() {
954 // This toolchain's unsupported, there's nothing to do for this mod.
955 return
956 }
957
958 deps := mod.depsToPaths(ctx)
959 flags := Flags{
960 Toolchain: toolchain,
961 }
962
Ivan Lozano67eada32021-09-23 11:50:33 -0400963 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800964 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500965 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800966 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967 if mod.compiler != nil {
968 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400969 flags = mod.compiler.cfgFlags(ctx, flags)
970 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400971 }
972 if mod.coverage != nil {
973 flags, deps = mod.coverage.flags(ctx, flags, deps)
974 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200975 if mod.clippy != nil {
976 flags, deps = mod.clippy.flags(ctx, flags, deps)
977 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500978 if mod.sanitize != nil {
979 flags, deps = mod.sanitize.flags(ctx, flags, deps)
980 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400981
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200982 // SourceProvider needs to call GenerateSource() before compiler calls
983 // compile() so it can provide the source. A SourceProvider has
984 // multiple variants (e.g. source, rlib, dylib). Only the "source"
985 // variant is responsible for effectively generating the source. The
986 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400987 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200988 if mod.compiler.(libraryInterface).source() {
989 mod.sourceProvider.GenerateSource(ctx, deps)
990 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
991 } else {
992 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
993 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700994 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200995 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400996 }
997
998 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100999 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001000 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001001 if ctx.Failed() {
1002 return
1003 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001004 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
1005 if buildOutput.kytheFile != nil {
1006 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1007 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001008 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001009
Dan Albert06feee92021-03-19 15:06:02 -07001010 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001011 if mod.docTimestampFile.Valid() {
1012 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1013 }
Dan Albert06feee92021-03-19 15:06:02 -07001014
Ivan Lozano1921e802021-05-20 13:39:16 -04001015 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1016 // RECOVERY_SNAPSHOT_VERSION is current.
1017 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1018 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1019 lib.collectHeadersForSnapshot(ctx, deps)
1020 }
1021 }
1022
Jiyong Park459feca2020-12-15 11:02:21 +09001023 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001024 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001025 // If the module has been specifically configure to not be installed then
1026 // hide from make as otherwise it will break when running inside make as the
1027 // output path to install will not be specified. Not all uninstallable
1028 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001029 // side dependencies. In particular, proc-macros need to be captured in the
1030 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001031 mod.HideFromMake()
1032 } else if !mod.installable(apexInfo) {
1033 mod.SkipInstall()
1034 }
1035
1036 // Still call install though, the installs will be stored as PackageSpecs to allow
1037 // using the outputs in a genrule.
1038 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001039 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001040 if ctx.Failed() {
1041 return
1042 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001043 }
Chris Wailes74be7642021-07-22 16:20:28 -07001044
1045 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046 }
1047}
1048
1049func (mod *Module) deps(ctx DepsContext) Deps {
1050 deps := Deps{}
1051
1052 if mod.compiler != nil {
1053 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001054 }
1055 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001056 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001057 }
1058
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001059 if mod.coverage != nil {
1060 deps = mod.coverage.deps(ctx, deps)
1061 }
1062
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001063 if mod.sanitize != nil {
1064 deps = mod.sanitize.deps(ctx, deps)
1065 }
1066
Ivan Lozanoffee3342019-08-27 12:03:00 -07001067 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1068 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001069 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1071 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1072 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001073 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001074 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075 return deps
1076
1077}
1078
Ivan Lozanoffee3342019-08-27 12:03:00 -07001079type dependencyTag struct {
1080 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001081 name string
1082 library bool
1083 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001084 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001085}
1086
Jiyong Park65b62242020-11-25 12:44:59 +09001087// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1088// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1089func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001090 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001091}
1092
1093var _ android.InstallNeededDependencyTag = dependencyTag{}
1094
Colin Cross65cb3142021-12-10 23:05:02 +00001095func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1096 if d.library && d.dynamic {
1097 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1098 }
1099 return nil
1100}
1101
1102var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1103
Ivan Lozanoffee3342019-08-27 12:03:00 -07001104var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001105 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1106 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001107 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001108 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001109 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001110 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001111 dataLibDepTag = dependencyTag{name: "data lib"}
1112 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001113)
1114
Jiyong Park99644e92020-11-17 22:21:02 +09001115func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1116 tag, ok := depTag.(dependencyTag)
1117 return ok && tag == dylibDepTag
1118}
1119
Jiyong Park94e22fd2021-04-08 18:19:15 +09001120func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1121 tag, ok := depTag.(dependencyTag)
1122 return ok && tag == rlibDepTag
1123}
1124
Matthew Maurer0f003b12020-06-29 14:34:06 -07001125type autoDep struct {
1126 variation string
1127 depTag dependencyTag
1128}
1129
1130var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001131 rlibVariation = "rlib"
1132 dylibVariation = "dylib"
1133 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1134 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001135)
1136
1137type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001138 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001139}
1140
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001141func (mod *Module) begin(ctx BaseModuleContext) {
1142 if mod.coverage != nil {
1143 mod.coverage.begin(ctx)
1144 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001145 if mod.sanitize != nil {
1146 mod.sanitize.begin(ctx)
1147 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001148}
1149
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001150func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001151 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001152 return p.prebuilt()
1153 }
1154 return nil
1155}
1156
Justin Yun24b246a2023-03-16 10:36:16 +09001157func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1158 if rustDep, ok := dep.(*Module); ok {
1159 // Use base module name for snapshots when exporting to Makefile.
1160 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1161 baseName := rustDep.BaseModuleName()
1162 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1163 }
1164 }
1165 return cc.MakeLibName(ctx, c, dep, depName)
1166}
1167
Ivan Lozanod106efe2023-09-21 23:30:26 -04001168func collectIncludedProtos(mod *Module, dep *Module) {
1169 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1170 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1171 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1172 }
1173 }
1174}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001175func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1176 var depPaths PathDeps
1177
1178 directRlibDeps := []*Module{}
1179 directDylibDeps := []*Module{}
1180 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001181 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001182 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001183 directSrcProvidersDeps := []*Module{}
1184 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001185
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001186 // For the dependency from platform to apex, use the latest stubs
1187 mod.apexSdkVersion = android.FutureApiLevel
1188 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1189 if !apexInfo.IsForPlatform() {
1190 mod.apexSdkVersion = apexInfo.MinSdkVersion
1191 }
1192
1193 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1194 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1195 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1196 // (b/144430859)
1197 mod.apexSdkVersion = android.FutureApiLevel
1198 }
1199
Spandan Das604f3762023-03-16 22:51:40 +00001200 skipModuleList := map[string]bool{}
1201
1202 var apiImportInfo multitree.ApiImportInfo
1203 hasApiImportInfo := false
1204
1205 ctx.VisitDirectDeps(func(dep android.Module) {
1206 if dep.Name() == "api_imports" {
1207 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1208 hasApiImportInfo = true
1209 }
1210 })
1211
1212 if hasApiImportInfo {
1213 targetStubModuleList := map[string]string{}
1214 targetOrigModuleList := map[string]string{}
1215
1216 // Search for dependency which both original module and API imported library with APEX stub exists
1217 ctx.VisitDirectDeps(func(dep android.Module) {
1218 depName := ctx.OtherModuleName(dep)
1219 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1220 targetStubModuleList[apiLibrary] = depName
1221 }
1222 })
1223 ctx.VisitDirectDeps(func(dep android.Module) {
1224 depName := ctx.OtherModuleName(dep)
1225 if origLibrary, ok := targetStubModuleList[depName]; ok {
1226 targetOrigModuleList[origLibrary] = depName
1227 }
1228 })
1229
1230 // Decide which library should be used between original and API imported library
1231 ctx.VisitDirectDeps(func(dep android.Module) {
1232 depName := ctx.OtherModuleName(dep)
1233 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1234 if cc.ShouldUseStubForApex(ctx, dep) {
1235 skipModuleList[depName] = true
1236 } else {
1237 skipModuleList[apiLibrary] = true
1238 }
1239 }
1240 })
1241 }
1242
Cole Faustb6e6f992023-08-17 17:42:26 -07001243 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1244 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001245
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 ctx.VisitDirectDeps(func(dep android.Module) {
1247 depName := ctx.OtherModuleName(dep)
1248 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001249
Spandan Das604f3762023-03-16 22:51:40 +00001250 if _, exists := skipModuleList[depName]; exists {
1251 return
1252 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001253
1254 if depTag == android.DarwinUniversalVariantTag {
1255 return
1256 }
1257
Ivan Lozano89435d12020-07-31 11:01:18 -04001258 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001259 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001260 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001261
Ivan Lozanoffee3342019-08-27 12:03:00 -07001262 switch depTag {
1263 case dylibDepTag:
1264 dylib, ok := rustDep.compiler.(libraryInterface)
1265 if !ok || !dylib.dylib() {
1266 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1267 return
1268 }
1269 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001270 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001271 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1272
Ivan Lozanoffee3342019-08-27 12:03:00 -07001273 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001274
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275 rlib, ok := rustDep.compiler.(libraryInterface)
1276 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001277 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001278 return
1279 }
1280 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001281 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001282 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1283
Ivan Lozanoffee3342019-08-27 12:03:00 -07001284 case procMacroDepTag:
1285 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001286 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001287
1288 case sourceDepTag:
1289 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1290 collectIncludedProtos(mod, rustDep)
1291 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001292 }
1293
Cole Faustb6e6f992023-08-17 17:42:26 -07001294 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1295
Paul Duffind5cf92e2021-07-09 17:38:55 +01001296 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001297 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1298 // OS/Arch variant is used.
1299 var helper string
1300 if ctx.Host() {
1301 helper = "missing 'host_supported'?"
1302 } else {
1303 helper = "device module defined?"
1304 }
1305
1306 if dep.Target().Os != ctx.Os() {
1307 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1308 return
1309 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1310 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1311 return
1312 }
1313 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001314 }
1315
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001316 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001317 if depTag != procMacroDepTag {
1318 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1319 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1320 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1321 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001322 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001323 }
1324
Ivan Lozanoffee3342019-08-27 12:03:00 -07001325 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001326 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001327 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001328 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001329 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001330 }
1331 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001332 if depTag == sourceDepTag {
1333 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1334 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
1335 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1336 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1337 }
1338 }
1339 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001340 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001341 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001342 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001343 if _, ok := ccDep.(*Module); !ok {
1344 if ccDep.Module().Target().Os != ctx.Os() {
1345 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1346 return
1347 }
1348 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1349 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1350 return
1351 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001352 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001353 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001354 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001355 if !ctx.Config().AllowMissingDependencies() {
1356 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1357 } else {
1358 ctx.AddMissingDependencies([]string{depName})
1359 }
1360 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001361 }
1362
Wen-yi Chu41326c12023-09-22 03:58:59 +00001363 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001364
Ivan Lozanoffee3342019-08-27 12:03:00 -07001365 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001366 switch {
1367 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001368 if cc.IsWholeStaticLib(depTag) {
1369 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1370 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001371 if mod.Binary() {
1372 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1373 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1374 // final linkage, pass the args directly to the linker to handle these cases.
1375 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1376 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001377 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001378 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001379 } else {
1380 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 -05001381 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001382 }
1383
1384 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1385 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001386 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001387 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1388
Colin Cross0de8a1e2020-09-18 14:15:30 -07001389 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1390 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1391 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1392 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1393 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001394 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001395
1396 // Record baseLibName for snapshots.
1397 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1398
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001399 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001400 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001401 // For the shared lib dependencies, we may link to the stub variant
1402 // of the dependency depending on the context (e.g. if this
1403 // dependency crosses the APEX boundaries).
1404 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1405
1406 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1407 // object (either from stub or non-stub) to use.
1408 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001409 if !linkObject.Valid() {
1410 if !ctx.Config().AllowMissingDependencies() {
1411 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1412 } else {
1413 ctx.AddMissingDependencies([]string{depName})
1414 }
1415 return
1416 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001417 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001418
Ivan Lozanoffee3342019-08-27 12:03:00 -07001419 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001420 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001421 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1422 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1423 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1424 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001425 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001426
1427 // Record baseLibName for snapshots.
1428 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1429
Cole Faustb6e6f992023-08-17 17:42:26 -07001430 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001431 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001432 case cc.IsHeaderDepTag(depTag):
1433 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1434 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1435 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1436 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001437 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001438 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001439 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001440 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001441 }
1442
1443 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001444 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1445 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001446 if linkObject.Valid() {
1447 lib.exportLinkObjects(linkObject.Path())
1448 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001449 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001450 } else {
1451 switch {
1452 case depTag == cc.CrtBeginDepTag:
1453 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1454 case depTag == cc.CrtEndDepTag:
1455 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1456 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001457 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001458
1459 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001460 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001461 // These are usually genrules which don't have per-target variants.
1462 directSrcDeps = append(directSrcDeps, srcDep)
1463 }
1464 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001465 })
1466
Wen-yi Chu41326c12023-09-22 03:58:59 +00001467 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1468
1469 var rlibDepFiles RustLibraries
1470 for _, dep := range directRlibDeps {
1471 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1472 }
1473 var dylibDepFiles RustLibraries
1474 for _, dep := range directDylibDeps {
1475 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1476 }
1477 var procMacroDepFiles RustLibraries
1478 for _, dep := range directProcMacroDeps {
1479 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1480 }
1481
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001482 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001484 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001485 }
1486
Ivan Lozanoffee3342019-08-27 12:03:00 -07001487 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001488 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001489 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001490 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001491 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001492 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001493 }
1494
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001495 var srcProviderDepFiles android.Paths
1496 for _, dep := range directSrcProvidersDeps {
1497 srcs, _ := dep.OutputFiles("")
1498 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1499 }
1500 for _, dep := range directSrcDeps {
1501 srcs := dep.Srcs()
1502 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1503 }
1504
Wen-yi Chu41326c12023-09-22 03:58:59 +00001505 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1506 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001507 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001508 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001509 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001510
1511 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001512 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001513 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001514 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001515 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1516 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1517 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001518
1519 return depPaths
1520}
1521
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001522func (mod *Module) InstallInData() bool {
1523 if mod.compiler == nil {
1524 return false
1525 }
1526 return mod.compiler.inData()
1527}
1528
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001529func (mod *Module) InstallInRamdisk() bool {
1530 return mod.InRamdisk()
1531}
1532
1533func (mod *Module) InstallInVendorRamdisk() bool {
1534 return mod.InVendorRamdisk()
1535}
1536
1537func (mod *Module) InstallInRecovery() bool {
1538 return mod.InRecovery()
1539}
1540
Wen-yi Chu41326c12023-09-22 03:58:59 +00001541func linkPathFromFilePath(filepath android.Path) string {
1542 return strings.Split(filepath.String(), filepath.Base())[0]
1543}
1544
Spandan Das604f3762023-03-16 22:51:40 +00001545// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1546func (r *Module) usePublicApi() bool {
1547 return r.Device() && r.UseSdk()
1548}
1549
1550// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1551func (r *Module) useVendorApi() bool {
1552 return r.Device() && (r.InVendor() || r.InProduct())
1553}
1554
Ivan Lozanoffee3342019-08-27 12:03:00 -07001555func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1556 ctx := &depsContext{
1557 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001558 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001559
1560 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001561 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001562 var snapshotInfo *cc.SnapshotInfo
1563
Kiyoung Kim487689e2022-07-26 09:48:22 +09001564 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001565 if mod.usePublicApi() || mod.useVendorApi() {
1566 for idx, lib := range deps.SharedLibs {
1567 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1568 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001569 }
1570
Ivan Lozano1921e802021-05-20 13:39:16 -04001571 if ctx.Os() == android.Android {
1572 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1573 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001574
Ivan Lozano2b081132020-09-08 12:46:52 -04001575 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001576 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001577 stdLinkage = "rlib-std"
1578 }
1579
1580 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001581
Ivan Lozano2b081132020-09-08 12:46:52 -04001582 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1583 rlibDepVariations = append(rlibDepVariations,
1584 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1585 }
1586
Ivan Lozano1921e802021-05-20 13:39:16 -04001587 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001588 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001589 for _, lib := range deps.Rlibs {
1590 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001591 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001592
Ivan Lozano2d407632022-04-07 12:59:11 -04001593 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001594 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001595
1596 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001597 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1598 for _, lib := range deps.Dylibs {
1599 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1600 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001601
Ivan Lozano1921e802021-05-20 13:39:16 -04001602 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001603 if deps.Rustlibs != nil {
1604 if !mod.compiler.Disabled() {
1605 for _, lib := range deps.Rustlibs {
1606 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1607 if autoDep.depTag == rlibDepTag {
1608 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001609 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001610 } else {
1611 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1612 // Check for the existence of the dylib deptag variant. Select it if available,
1613 // otherwise select the rlib variant.
1614 autoDepVariations := append(commonDepVariations,
1615 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1616
1617 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1618
1619 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1620 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1621 } else {
1622 // If there's no dylib dependency available, try to add the rlib dependency instead.
1623 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1624 }
1625 }
1626 }
1627 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1628 for _, lib := range deps.Rustlibs {
1629 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1630 srcProviderVariations := append(commonDepVariations,
1631 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1632
1633 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1634 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001635 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001636 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001637 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001638 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001639
Ivan Lozano1921e802021-05-20 13:39:16 -04001640 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001641 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001642 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001643 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001644 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001645 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001646 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001647 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001648 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001649 for _, lib := range deps.Stdlibs {
1650 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1651 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001652 }
1653 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001654
1655 for _, lib := range deps.SharedLibs {
1656 depTag := cc.SharedDepTag()
1657 name, version := cc.StubsLibNameAndVersion(lib)
1658
1659 variations := []blueprint.Variation{
1660 {Mutator: "link", Variation: "shared"},
1661 }
Spandan Das604f3762023-03-16 22:51:40 +00001662 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1663 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1664 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1665 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1666 }
1667
1668 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1669 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1670 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001671 }
1672
1673 for _, lib := range deps.WholeStaticLibs {
1674 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001675 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001676
1677 actx.AddVariationDependencies([]blueprint.Variation{
1678 {Mutator: "link", Variation: "static"},
1679 }, depTag, lib)
1680 }
1681
1682 for _, lib := range deps.StaticLibs {
1683 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001684 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001685
1686 actx.AddVariationDependencies([]blueprint.Variation{
1687 {Mutator: "link", Variation: "static"},
1688 }, depTag, lib)
1689 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001690
Zach Johnson3df4e632020-11-06 11:56:27 -08001691 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1692
Colin Cross565cafd2020-09-25 18:47:38 -07001693 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001694 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001695 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001696 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001697 }
Colin Crossfe605e12022-01-23 20:46:16 -08001698 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001699 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001700 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001701 }
1702
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001703 if mod.sourceProvider != nil {
1704 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1705 bindgen.Properties.Custom_bindgen != "" {
1706 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1707 bindgen.Properties.Custom_bindgen)
1708 }
1709 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001710
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001711 actx.AddVariationDependencies([]blueprint.Variation{
1712 {Mutator: "link", Variation: "shared"},
1713 }, dataLibDepTag, deps.DataLibs...)
1714
1715 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1716
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001717 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001718 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001719
1720 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001721}
1722
Ivan Lozano2d407632022-04-07 12:59:11 -04001723// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001724func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1725 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001726 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1727}
1728
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001729func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1730 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1731 actx.AddVariationDependencies(variations, depTag, lib)
1732}
1733
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001734func BeginMutator(ctx android.BottomUpMutatorContext) {
1735 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1736 mod.beginMutator(ctx)
1737 }
1738}
1739
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001740func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1741 ctx := &baseModuleContext{
1742 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001743 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001744
1745 mod.begin(ctx)
1746}
1747
Ivan Lozanoffee3342019-08-27 12:03:00 -07001748func (mod *Module) Name() string {
1749 name := mod.ModuleBase.Name()
1750 if p, ok := mod.compiler.(interface {
1751 Name(string) string
1752 }); ok {
1753 name = p.Name(name)
1754 }
1755 return name
1756}
1757
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001758func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001759 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001760 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001761 }
1762}
1763
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001764var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001765var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001766
1767func (mod *Module) HostToolPath() android.OptionalPath {
1768 if !mod.Host() {
1769 return android.OptionalPath{}
1770 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001771 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1772 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001773 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1774 // Even though proc-macros aren't strictly "tools", since they target the compiler
1775 // and act as compiler plugins, we treat them similarly.
1776 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001777 }
1778 return android.OptionalPath{}
1779}
1780
Jiyong Park99644e92020-11-17 22:21:02 +09001781var _ android.ApexModule = (*Module)(nil)
1782
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001783func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001784 return String(mod.Properties.Min_sdk_version)
1785}
1786
Jiyong Park45bf82e2020-12-15 22:29:02 +09001787// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001788func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001789 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001790 if minSdkVersion == "apex_inherit" {
1791 return nil
1792 }
1793 if minSdkVersion == "" {
1794 return fmt.Errorf("min_sdk_version is not specificed")
1795 }
1796
1797 // Not using nativeApiLevelFromUser because the context here is not
1798 // necessarily a native context.
1799 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1800 if err != nil {
1801 return err
1802 }
1803
1804 if ver.GreaterThan(sdkVersion) {
1805 return fmt.Errorf("newer SDK(%v)", ver)
1806 }
Jiyong Park99644e92020-11-17 22:21:02 +09001807 return nil
1808}
1809
Jiyong Park45bf82e2020-12-15 22:29:02 +09001810// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001811func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1812 depTag := ctx.OtherModuleDependencyTag(dep)
1813
1814 if ccm, ok := dep.(*cc.Module); ok {
1815 if ccm.HasStubsVariants() {
1816 if cc.IsSharedDepTag(depTag) {
1817 // dynamic dep to a stubs lib crosses APEX boundary
1818 return false
1819 }
1820 if cc.IsRuntimeDepTag(depTag) {
1821 // runtime dep to a stubs lib also crosses APEX boundary
1822 return false
1823 }
1824
1825 if cc.IsHeaderDepTag(depTag) {
1826 return false
1827 }
1828 }
1829 if mod.Static() && cc.IsSharedDepTag(depTag) {
1830 // shared_lib dependency from a static lib is considered as crossing
1831 // the APEX boundary because the dependency doesn't actually is
1832 // linked; the dependency is used only during the compilation phase.
1833 return false
1834 }
1835 }
1836
Matthew Maurer581b6d82022-09-29 16:46:25 -07001837 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001838 return false
1839 }
1840
1841 return true
1842}
1843
1844// Overrides ApexModule.IsInstallabeToApex()
1845func (mod *Module) IsInstallableToApex() bool {
1846 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001847 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001848 return true
1849 }
1850 if _, ok := mod.compiler.(*binaryDecorator); ok {
1851 return true
1852 }
1853 }
1854 return false
1855}
1856
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001857// If a library file has a "lib" prefix, extract the library name without the prefix.
1858func libNameFromFilePath(filepath android.Path) (string, bool) {
1859 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1860 if strings.HasPrefix(libName, "lib") {
1861 libName = libName[3:]
1862 return libName, true
1863 }
1864 return "", false
1865}
1866
Sasha Smundaka76acba2022-04-18 20:12:56 -07001867func kytheExtractRustFactory() android.Singleton {
1868 return &kytheExtractRustSingleton{}
1869}
1870
1871type kytheExtractRustSingleton struct {
1872}
1873
1874func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1875 var xrefTargets android.Paths
1876 ctx.VisitAllModules(func(module android.Module) {
1877 if rustModule, ok := module.(xref); ok {
1878 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1879 }
1880 })
1881 if len(xrefTargets) > 0 {
1882 ctx.Phony("xref_rust", xrefTargets...)
1883 }
1884}
1885
Jihoon Kangf78a8902022-09-01 22:47:07 +00001886func (c *Module) Partition() string {
1887 return ""
1888}
1889
Chris Parsons637458d2023-09-19 20:09:00 +00001890func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001891 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1892 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001893 } else if ctx.ModuleType() == "rust_proc_macro" {
1894 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001895 } else if ctx.ModuleType() == "rust_binary_host" {
1896 binaryBp2build(ctx, m)
Vinh Tran47faaad2023-09-25 14:47:19 -04001897 } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" {
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001898 protoLibraryBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001899 } else {
1900 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1901 }
1902}
1903
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001904// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001905// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001906// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1907// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1908// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001909func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001910 var srcs bazel.LabelList
1911 var compileData bazel.LabelList
1912
1913 if c.Properties.Srcs[0] == "src/lib.rs" {
1914 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1915 compileData = android.BazelLabelForModuleSrc(
1916 ctx,
1917 []string{
1918 "src/**/*.proto",
1919 "examples/**/*.rs",
1920 "**/*.md",
1921 },
1922 )
1923 } else {
1924 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1925 }
1926
1927 return srcs, compileData
1928}
1929
Ivan Lozanoffee3342019-08-27 12:03:00 -07001930var Bool = proptools.Bool
1931var BoolDefault = proptools.BoolDefault
1932var String = proptools.String
1933var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001934
1935var _ android.OutputFileProducer = (*Module)(nil)