blob: a3bc510f12255f698eb0154a8110891b43ddd406 [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 (
Vinh Tranb4bb20f2023-08-24 11:10:01 -040018 "android/soong/bazel"
Sasha Smundaka76acba2022-04-18 20:12:56 -070019 "android/soong/bloaty"
Vinh Tranbcb5f572023-08-24 11:10:01 -040020 "android/soong/ui/metrics/bp2build_metrics_proto"
Ivan Lozano183a3212019-10-18 14:18:45 -070021 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022 "strings"
23
24 "github.com/google/blueprint"
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020029 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070030 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000031 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040033 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070034)
35
36var pctx = android.NewPackageContext("android/soong/rust")
37
38func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070039 android.RegisterModuleType("rust_defaults", defaultsFactory)
40 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
41 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040042 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040043 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050044 })
45 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
46 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070047 })
Inseob Kim3b244062023-07-11 13:31:36 +090048 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020050 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000051 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040055 GlobalRustFlags []string // Flags that apply globally to rust
56 GlobalLinkFlags []string // Flags that apply globally to linker
57 RustFlags []string // Flags that apply to rust
58 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070060 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070061 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040062 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020063 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070064 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050068 AndroidMkRlibs []string `blueprint:"mutated"`
69 AndroidMkDylibs []string `blueprint:"mutated"`
70 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050071 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040072
Ivan Lozano6a884432020-12-02 09:15:16 -050073 ImageVariationPrefix string `blueprint:"mutated"`
74 VndkVersion string `blueprint:"mutated"`
75 SubName string `blueprint:"mutated"`
76
Ivan Lozanoc08897c2021-04-02 12:41:32 -040077 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
78 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
79 // appended before SubName.
80 RustSubName string `blueprint:"mutated"`
81
Ivan Lozano6a884432020-12-02 09:15:16 -050082 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050083 CoreVariantNeeded bool `blueprint:"mutated"`
84 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070085 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080086 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050087 ExtraVariants []string `blueprint:"mutated"`
88
Ivan Lozanoa2268632021-07-22 10:52:06 -040089 // Allows this module to use non-APEX version of libraries. Useful
90 // for building binaries that are started before APEXes are activated.
91 Bootstrap *bool
92
Ivan Lozano1921e802021-05-20 13:39:16 -040093 // Used by vendor snapshot to record dependencies from snapshot modules.
94 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090095 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040096 SnapshotRlibs []string `blueprint:"mutated"`
97 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040098
Matthew Maurerc6868382021-07-13 14:12:37 -070099 // Make this module available when building for ramdisk.
100 // On device without a dedicated recovery partition, the module is only
101 // available after switching root into
102 // /first_stage_ramdisk. To expose the module before switching root, install
103 // the recovery variant instead.
104 Ramdisk_available *bool
105
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500106 // Make this module available when building for vendor ramdisk.
107 // On device without a dedicated recovery partition, the module is only
108 // available after switching root into
109 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800110 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500111 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400112
Ivan Lozano1921e802021-05-20 13:39:16 -0400113 // Normally Soong uses the directory structure to decide which modules
114 // should be included (framework) or excluded (non-framework) from the
115 // different snapshots (vendor, recovery, etc.), but this property
116 // allows a partner to exclude a module normally thought of as a
117 // framework module from the vendor snapshot.
118 Exclude_from_vendor_snapshot *bool
119
120 // Normally Soong uses the directory structure to decide which modules
121 // should be included (framework) or excluded (non-framework) from the
122 // different snapshots (vendor, recovery, etc.), but this property
123 // allows a partner to exclude a module normally thought of as a
124 // framework module from the recovery snapshot.
125 Exclude_from_recovery_snapshot *bool
126
Matthew Maurer460ee942021-02-11 12:31:46 -0800127 // Make this module available when building for recovery
128 Recovery_available *bool
129
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500130 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
131 Min_sdk_version *string
132
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900133 HideFromMake bool `blueprint:"mutated"`
134 PreventInstall bool `blueprint:"mutated"`
135
136 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137}
138
139type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700140 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
Ivan Lozano6a884432020-12-02 09:15:16 -0500142 VendorProperties cc.VendorProperties
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144 Properties BaseProperties
145
146 hod android.HostOrDeviceSupported
147 multilib android.Multilib
148
Ivan Lozano6a884432020-12-02 09:15:16 -0500149 makeLinkType string
150
Yi Kong46c6e592022-01-20 22:55:00 +0800151 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400153 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200154 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500155 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700158 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400159
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400160 // Output file to be installed, may be stripped or unstripped.
161 outputFile android.OptionalPath
162
Sasha Smundaka76acba2022-04-18 20:12:56 -0700163 // Cross-reference input file
164 kytheFiles android.Paths
165
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400166 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900167
168 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500169
170 // For apex variants, this is set as apex.min_sdk_version
171 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700172
173 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400174
175 android.BazelModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700176}
177
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500178func (mod *Module) Header() bool {
179 //TODO: If Rust libraries provide header variants, this needs to be updated.
180 return false
181}
182
183func (mod *Module) SetPreventInstall() {
184 mod.Properties.PreventInstall = true
185}
186
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500187func (mod *Module) SetHideFromMake() {
188 mod.Properties.HideFromMake = true
189}
190
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900191func (mod *Module) HiddenFromMake() bool {
192 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400193}
194
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500195func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500196 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
197 // nil since we need compiler to actually sanitize.
198 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500199}
200
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500201func (mod *Module) IsPrebuilt() bool {
202 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
203 return true
204 }
205 return false
206}
207
Ivan Lozano43845682020-07-09 21:03:28 -0400208func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
209 switch tag {
210 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700211 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400212 return mod.sourceProvider.Srcs(), nil
213 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900214 if mod.OutputFile().Valid() {
215 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400216 }
217 return android.Paths{}, nil
218 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500219 case "unstripped":
220 if mod.compiler != nil {
221 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
222 }
223 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400224 default:
225 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
226 }
227}
228
Ivan Lozano52767be2019-10-18 14:49:46 -0700229func (mod *Module) SelectedStl() string {
230 return ""
231}
232
Ivan Lozano2b262972019-11-21 12:30:50 -0800233func (mod *Module) NonCcVariants() bool {
234 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400235 if _, ok := mod.compiler.(libraryInterface); ok {
236 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800237 }
238 }
239 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
240}
241
Ivan Lozano52767be2019-10-18 14:49:46 -0700242func (mod *Module) Static() bool {
243 if mod.compiler != nil {
244 if library, ok := mod.compiler.(libraryInterface); ok {
245 return library.static()
246 }
247 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400248 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700249}
250
251func (mod *Module) Shared() bool {
252 if mod.compiler != nil {
253 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400254 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700255 }
256 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400257 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700258}
259
Ivan Lozanod7586b62021-04-01 09:49:36 -0400260func (mod *Module) Dylib() bool {
261 if mod.compiler != nil {
262 if library, ok := mod.compiler.(libraryInterface); ok {
263 return library.dylib()
264 }
265 }
266 return false
267}
268
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400269func (mod *Module) RlibStd() bool {
270 if mod.compiler != nil {
271 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
272 return library.rlibStd()
273 }
274 }
275 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
276}
277
Ivan Lozanod7586b62021-04-01 09:49:36 -0400278func (mod *Module) Rlib() bool {
279 if mod.compiler != nil {
280 if library, ok := mod.compiler.(libraryInterface); ok {
281 return library.rlib()
282 }
283 }
284 return false
285}
286
287func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400288 if binary, ok := mod.compiler.(binaryInterface); ok {
289 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400290 }
291 return false
292}
293
Justin Yun5e035862021-06-29 20:50:37 +0900294func (mod *Module) StaticExecutable() bool {
295 if !mod.Binary() {
296 return false
297 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400298 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900299}
300
Ivan Lozanod7586b62021-04-01 09:49:36 -0400301func (mod *Module) Object() bool {
302 // Rust has no modules which produce only object files.
303 return false
304}
305
Ivan Lozano52767be2019-10-18 14:49:46 -0700306func (mod *Module) Toc() android.OptionalPath {
307 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400308 if lib, ok := mod.compiler.(libraryInterface); ok {
309 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700310 }
311 }
312 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
313}
314
Colin Crossc511bc52020-04-07 16:50:32 +0000315func (mod *Module) UseSdk() bool {
316 return false
317}
318
Ivan Lozanod7586b62021-04-01 09:49:36 -0400319func (mod *Module) RelativeInstallPath() string {
320 if mod.compiler != nil {
321 return mod.compiler.relativeInstallPath()
322 }
323 return ""
324}
325
Ivan Lozano52767be2019-10-18 14:49:46 -0700326func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500327 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700328}
329
Jiyong Park7d55b612021-06-11 17:22:09 +0900330func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400331 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900332}
333
Ivan Lozano52767be2019-10-18 14:49:46 -0700334func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400335 return true
336}
337
338func (mod *Module) SubName() string {
339 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700340}
341
342func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500343 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700344 return false
345}
346
Ivan Lozanof9e21722020-12-02 09:00:51 -0500347func (mod *Module) IsVndkExt() bool {
348 return false
349}
350
Ivan Lozanod7586b62021-04-01 09:49:36 -0400351func (mod *Module) IsVndkSp() bool {
352 return false
353}
354
Ivan Lozanof1868af2022-04-12 13:08:36 -0400355func (mod *Module) IsVndkPrebuiltLibrary() bool {
356 // Rust modules do not provide VNDK prebuilts
357 return false
358}
359
360func (mod *Module) IsVendorPublicLibrary() bool {
361 return mod.VendorProperties.IsVendorPublicLibrary
362}
363
364func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
365 // Rust modules to not provide Sdk variants
366 return false
367}
368
Colin Cross127bb8b2020-12-16 16:46:01 -0800369func (c *Module) IsVndkPrivate() bool {
370 return false
371}
372
373func (c *Module) IsLlndk() bool {
374 return false
375}
376
377func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500378 return false
379}
380
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400381func (mod *Module) KernelHeadersDecorator() bool {
382 return false
383}
384
Colin Cross1f3f1302021-04-26 18:37:44 -0700385func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400386 return false
387}
388
Colin Cross5271fea2021-04-27 13:06:04 -0700389func (m *Module) NeedsVendorPublicLibraryVariants() bool {
390 return false
391}
392
Ivan Lozanod7586b62021-04-01 09:49:36 -0400393func (mod *Module) HasLlndkStubs() bool {
394 return false
395}
396
397func (mod *Module) StubsVersion() string {
398 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
399}
400
Ivan Lozano52767be2019-10-18 14:49:46 -0700401func (mod *Module) SdkVersion() string {
402 return ""
403}
404
Colin Crossc511bc52020-04-07 16:50:32 +0000405func (mod *Module) AlwaysSdk() bool {
406 return false
407}
408
Jiyong Park2286afd2020-06-16 21:58:53 +0900409func (mod *Module) IsSdkVariant() bool {
410 return false
411}
412
Colin Cross1348ce32020-10-01 13:37:16 -0700413func (mod *Module) SplitPerApiLevel() bool {
414 return false
415}
416
Sasha Smundaka76acba2022-04-18 20:12:56 -0700417func (mod *Module) XrefRustFiles() android.Paths {
418 return mod.kytheFiles
419}
420
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400422 Dylibs []string
423 Rlibs []string
424 Rustlibs []string
425 Stdlibs []string
426 ProcMacros []string
427 SharedLibs []string
428 StaticLibs []string
429 WholeStaticLibs []string
430 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400432 // Used for data dependencies adjacent to tests
433 DataLibs []string
434 DataBins []string
435
Colin Crossfe605e12022-01-23 20:46:16 -0800436 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437}
438
439type PathDeps struct {
Sam Delmericoa588d152023-06-16 10:28:04 -0400440 Dylibs *android.DepSet[RustLibrary]
441 Rlibs *android.DepSet[RustLibrary]
442 ProcMacros *android.DepSet[RustLibrary]
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700443 LibDeps android.Paths
444 WholeStaticLibs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700445 AfdoProfiles android.Paths
Sam Delmericoa588d152023-06-16 10:28:04 -0400446 // These paths are files needed to run the build tools and will be located under
447 // __SBOX_SANDBOX_DIR__/tools/...
448 BuildToolDeps android.Paths
449 // These paths are files needed to run the build tools and will be located under
450 // __SBOX_SANDBOX_DIR__/...
451 BuildToolSrcDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500452
453 // depFlags and depLinkFlags are rustc and linker (clang) flags.
454 depFlags []string
455 depLinkFlags []string
456
457 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
458 // Both of these are exported and propagate to dependencies.
Sam Delmericoa588d152023-06-16 10:28:04 -0400459 linkDirs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700460 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700461
Ivan Lozano45901ed2020-07-24 16:05:01 -0400462 // Used by bindgen modules which call clang
463 depClangFlags []string
464 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400465 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400466 depSystemIncludePaths android.Paths
467
Colin Crossfe605e12022-01-23 20:46:16 -0800468 CrtBegin android.Paths
469 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700470
471 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500472 SrcDeps android.Paths
473 srcProviderFiles android.Paths
Sam Delmericoa588d152023-06-16 10:28:04 -0400474
475 // Paths to specific build tools
476 Rustc android.Path
477 Clang android.Path
478 Llvm_ar android.Path
479 Clippy_driver android.Path
480 Rustdoc android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481}
482
483type RustLibraries []RustLibrary
484
485type RustLibrary struct {
486 Path android.Path
487 CrateName string
488}
489
490type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100491 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700492 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400493 cfgFlags(ctx ModuleContext, flags Flags) Flags
494 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700496 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700497 compilerDeps(ctx DepsContext, deps Deps) Deps
498 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700499 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Sam Delmericoa588d152023-06-16 10:28:04 -0400500 crateRoot(ctx ModuleContext) android.Path
501 compilationSourcesAndData(ctx ModuleContext) android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700502
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100503 // Output directory in which source-generated code from dependencies is
504 // copied. This is equivalent to Cargo's OUT_DIR variable.
505 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700506
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400507 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
508 CargoPkgVersion() string
509
510 // CargoEnvCompat returns whether Cargo environment variables should be used.
511 CargoEnvCompat() bool
512
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800513 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200514 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700515 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400516 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400517
518 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400519
520 Disabled() bool
521 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400522
Ivan Lozanodd055472020-09-28 13:22:45 -0400523 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500524 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900525
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400526 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900527 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400528}
529
Matthew Maurerbb3add12020-06-25 09:34:12 -0700530type exportedFlagsProducer interface {
Sam Delmericoa588d152023-06-16 10:28:04 -0400531 exportLinkDirs(...android.Path)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700532 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700533}
534
Sasha Smundaka76acba2022-04-18 20:12:56 -0700535type xref interface {
536 XrefRustFiles() android.Paths
537}
538
Matthew Maurerbb3add12020-06-25 09:34:12 -0700539type flagExporter struct {
Sam Delmericoa588d152023-06-16 10:28:04 -0400540 linkDirs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700541 linkObjects android.Paths
542 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700543}
544
Sam Delmericoa588d152023-06-16 10:28:04 -0400545func (flagExporter *flagExporter) exportLinkDirs(dirs ...android.Path) {
546 flagExporter.linkDirs = android.FirstUniquePaths(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700547}
548
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700549func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
550 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
551}
552
553func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
554 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400555}
556
Colin Cross0de8a1e2020-09-18 14:15:30 -0700557func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
558 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700559 LinkDirs: flagExporter.linkDirs,
560 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700561 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700562 })
563}
564
Matthew Maurerbb3add12020-06-25 09:34:12 -0700565var _ exportedFlagsProducer = (*flagExporter)(nil)
566
567func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700568 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700569}
570
Colin Cross0de8a1e2020-09-18 14:15:30 -0700571type FlagExporterInfo struct {
572 Flags []string
Sam Delmericoa588d152023-06-16 10:28:04 -0400573 LinkDirs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700574 LinkObjects android.Paths
575 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700576}
577
578var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
579
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400580func (mod *Module) isCoverageVariant() bool {
581 return mod.coverage.Properties.IsCoverageVariant
582}
583
584var _ cc.Coverage = (*Module)(nil)
585
586func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
587 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
588}
589
Ivan Lozanod7586b62021-04-01 09:49:36 -0400590func (mod *Module) VndkVersion() string {
591 return mod.Properties.VndkVersion
592}
593
594func (mod *Module) PreventInstall() bool {
595 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400596}
597
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400598func (mod *Module) MarkAsCoverageVariant(coverage bool) {
599 mod.coverage.Properties.IsCoverageVariant = coverage
600}
601
602func (mod *Module) EnableCoverageIfNeeded() {
603 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604}
605
606func defaultsFactory() android.Module {
607 return DefaultsFactory()
608}
609
610type Defaults struct {
611 android.ModuleBase
612 android.DefaultsModuleBase
613}
614
615func DefaultsFactory(props ...interface{}) android.Module {
616 module := &Defaults{}
617
618 module.AddProperties(props...)
619 module.AddProperties(
620 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800621 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500622 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100623 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400624 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700625 &BaseCompilerProperties{},
626 &BinaryCompilerProperties{},
627 &LibraryCompilerProperties{},
628 &ProcMacroCompilerProperties{},
629 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400630 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700631 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400632 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400633 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200634 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500635 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000636 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700637 )
638
639 android.InitDefaultsModule(module)
640 return module
641}
642
643func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700644 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645}
646
Ivan Lozano183a3212019-10-18 14:18:45 -0700647func (mod *Module) CcLibrary() bool {
648 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500649 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700650 return true
651 }
652 }
653 return false
654}
655
656func (mod *Module) CcLibraryInterface() bool {
657 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400658 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
659 // VariantIs{Static,Shared} is set.
660 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700661 return true
662 }
663 }
664 return false
665}
666
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400667func (mod *Module) RustLibraryInterface() bool {
668 if mod.compiler != nil {
669 if _, ok := mod.compiler.(libraryInterface); ok {
670 return true
671 }
672 }
673 return false
674}
675
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500676func (mod *Module) IsFuzzModule() bool {
677 if _, ok := mod.compiler.(*fuzzDecorator); ok {
678 return true
679 }
680 return false
681}
682
683func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
684 return mod.FuzzModule
685}
686
687func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
688 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
689 return fuzzer.fuzzPackagedModule
690 }
691 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
692}
693
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000694func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500695 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
696 return fuzzer.sharedLibraries
697 }
698 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
699}
700
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400701func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400702 if mod.compiler != nil {
703 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400704 }
705 return nil
706}
707
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800708func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700709 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700710 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800711 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700712 }
713 }
714 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
715}
716
717func (mod *Module) SetStatic() {
718 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700719 if library, ok := mod.compiler.(libraryInterface); ok {
720 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700721 return
722 }
723 }
724 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
725}
726
727func (mod *Module) SetShared() {
728 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700729 if library, ok := mod.compiler.(libraryInterface); ok {
730 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700731 return
732 }
733 }
734 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
735}
736
Ivan Lozano183a3212019-10-18 14:18:45 -0700737func (mod *Module) BuildStaticVariant() bool {
738 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700739 if library, ok := mod.compiler.(libraryInterface); ok {
740 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700741 }
742 }
743 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
744}
745
746func (mod *Module) BuildSharedVariant() bool {
747 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700748 if library, ok := mod.compiler.(libraryInterface); ok {
749 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700750 }
751 }
752 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
753}
754
Ivan Lozano183a3212019-10-18 14:18:45 -0700755func (mod *Module) Module() android.Module {
756 return mod
757}
758
Ivan Lozano183a3212019-10-18 14:18:45 -0700759func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400760 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700761}
762
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400763func (mod *Module) CoverageFiles() android.Paths {
764 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800765 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766 }
767 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
768}
769
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400770// Rust does not produce gcno files, and therefore does not produce a coverage archive.
771func (mod *Module) CoverageOutputFile() android.OptionalPath {
772 return android.OptionalPath{}
773}
774
775func (mod *Module) IsNdk(config android.Config) bool {
776 return false
777}
778
779func (mod *Module) IsStubs() bool {
780 return false
781}
782
Jiyong Park459feca2020-12-15 11:02:21 +0900783func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900784 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900785 return false
786 }
787
Jiyong Park459feca2020-12-15 11:02:21 +0900788 // The apex variant is not installable because it is included in the APEX and won't appear
789 // in the system partition as a standalone file.
790 if !apexInfo.IsForPlatform() {
791 return false
792 }
793
Jiyong Parke54f07e2021-04-07 15:08:04 +0900794 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900795}
796
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500797func (ctx moduleContext) apexVariationName() string {
798 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
799}
800
Ivan Lozano183a3212019-10-18 14:18:45 -0700801var _ cc.LinkableInterface = (*Module)(nil)
802
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803func (mod *Module) Init() android.Module {
804 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500805 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806
Yi Kong46c6e592022-01-20 22:55:00 +0800807 if mod.afdo != nil {
808 mod.AddProperties(mod.afdo.props()...)
809 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 if mod.compiler != nil {
811 mod.AddProperties(mod.compiler.compilerProps()...)
812 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400813 if mod.coverage != nil {
814 mod.AddProperties(mod.coverage.props()...)
815 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200816 if mod.clippy != nil {
817 mod.AddProperties(mod.clippy.props()...)
818 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400819 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700820 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400821 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500822 if mod.sanitize != nil {
823 mod.AddProperties(mod.sanitize.props()...)
824 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400825
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900827 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828
829 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 return mod
831}
832
833func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
834 return &Module{
835 hod: hod,
836 multilib: multilib,
837 }
838}
839func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
840 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800841 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400842 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200843 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500844 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 return module
846}
847
848type ModuleContext interface {
849 android.ModuleContext
850 ModuleContextIntf
851}
852
853type BaseModuleContext interface {
854 android.BaseModuleContext
855 ModuleContextIntf
856}
857
858type DepsContext interface {
859 android.BottomUpMutatorContext
860 ModuleContextIntf
861}
862
863type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200864 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866}
867
868type depsContext struct {
869 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700870}
871
872type moduleContext struct {
873 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874}
875
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200876type baseModuleContext struct {
877 android.BaseModuleContext
878}
879
880func (ctx *moduleContext) RustModule() *Module {
881 return ctx.Module().(*Module)
882}
883
884func (ctx *moduleContext) toolchain() config.Toolchain {
885 return ctx.RustModule().toolchain(ctx)
886}
887
888func (ctx *depsContext) RustModule() *Module {
889 return ctx.Module().(*Module)
890}
891
892func (ctx *depsContext) toolchain() config.Toolchain {
893 return ctx.RustModule().toolchain(ctx)
894}
895
896func (ctx *baseModuleContext) RustModule() *Module {
897 return ctx.Module().(*Module)
898}
899
900func (ctx *baseModuleContext) toolchain() config.Toolchain {
901 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400902}
903
904func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700905 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
906 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
907 return false
908 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400909 return mod.compiler != nil && mod.compiler.nativeCoverage()
910}
911
Ivan Lozanod7586b62021-04-01 09:49:36 -0400912func (mod *Module) EverInstallable() bool {
913 return mod.compiler != nil &&
914 // Check to see whether the module is actually ever installable.
915 mod.compiler.everInstallable()
916}
917
918func (mod *Module) Installable() *bool {
919 return mod.Properties.Installable
920}
921
Ivan Lozano872d5792022-03-23 17:31:39 -0400922func (mod *Module) ProcMacro() bool {
923 if pm, ok := mod.compiler.(procMacroInterface); ok {
924 return pm.ProcMacro()
925 }
926 return false
927}
928
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
930 if mod.cachedToolchain == nil {
931 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
932 }
933 return mod.cachedToolchain
934}
935
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200936func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
937 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
938}
939
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
941}
942
Sam Delmericoa588d152023-06-16 10:28:04 -0400943type RustInfo struct {
944 TransitiveRlibs *android.DepSet[RustLibrary]
945 TransitiveDylibs *android.DepSet[RustLibrary]
946 TransitiveProcMacros *android.DepSet[RustLibrary]
947}
948
949var RustInfoProvider = blueprint.NewProvider(RustInfo{})
950
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
952 ctx := &moduleContext{
953 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955
Jiyong Park99644e92020-11-17 22:21:02 +0900956 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
957 if !apexInfo.IsForPlatform() {
958 mod.hideApexVariantFromMake = true
959 }
960
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500962 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
963
Ivan Lozanof1868af2022-04-12 13:08:36 -0400964 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700965
Ivan Lozanoffee3342019-08-27 12:03:00 -0700966 if !toolchain.Supported() {
967 // This toolchain's unsupported, there's nothing to do for this mod.
968 return
969 }
970
971 deps := mod.depsToPaths(ctx)
972 flags := Flags{
973 Toolchain: toolchain,
974 }
975
Ivan Lozano67eada32021-09-23 11:50:33 -0400976 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800977 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500978 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800979 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700980 if mod.compiler != nil {
981 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400982 flags = mod.compiler.cfgFlags(ctx, flags)
983 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400984 }
985 if mod.coverage != nil {
986 flags, deps = mod.coverage.flags(ctx, flags, deps)
987 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200988 if mod.clippy != nil {
989 flags, deps = mod.clippy.flags(ctx, flags, deps)
990 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500991 if mod.sanitize != nil {
992 flags, deps = mod.sanitize.flags(ctx, flags, deps)
993 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400994
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200995 // SourceProvider needs to call GenerateSource() before compiler calls
996 // compile() so it can provide the source. A SourceProvider has
997 // multiple variants (e.g. source, rlib, dylib). Only the "source"
998 // variant is responsible for effectively generating the source. The
999 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001000 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001001 if mod.compiler.(libraryInterface).source() {
1002 mod.sourceProvider.GenerateSource(ctx, deps)
1003 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1004 } else {
1005 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
1006 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -07001007 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001008 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001009 }
1010
1011 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001012 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001013 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001014 if ctx.Failed() {
1015 return
1016 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001017 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
1018 if buildOutput.kytheFile != nil {
1019 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1020 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001021 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001022
Dan Albert06feee92021-03-19 15:06:02 -07001023 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001024 if mod.docTimestampFile.Valid() {
1025 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1026 }
Dan Albert06feee92021-03-19 15:06:02 -07001027
Ivan Lozano1921e802021-05-20 13:39:16 -04001028 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1029 // RECOVERY_SNAPSHOT_VERSION is current.
1030 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1031 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1032 lib.collectHeadersForSnapshot(ctx, deps)
1033 }
1034 }
1035
Jiyong Park459feca2020-12-15 11:02:21 +09001036 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001037 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001038 // If the module has been specifically configure to not be installed then
1039 // hide from make as otherwise it will break when running inside make as the
1040 // output path to install will not be specified. Not all uninstallable
1041 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001042 // side dependencies. In particular, proc-macros need to be captured in the
1043 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001044 mod.HideFromMake()
1045 } else if !mod.installable(apexInfo) {
1046 mod.SkipInstall()
1047 }
1048
1049 // Still call install though, the installs will be stored as PackageSpecs to allow
1050 // using the outputs in a genrule.
1051 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001052 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001053 if ctx.Failed() {
1054 return
1055 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001056 }
Chris Wailes74be7642021-07-22 16:20:28 -07001057
1058 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001059 }
Sam Delmericoa588d152023-06-16 10:28:04 -04001060
1061 ctx.SetProvider(RustInfoProvider, RustInfo{
1062 TransitiveRlibs: deps.Rlibs,
1063 TransitiveDylibs: deps.Dylibs,
1064 TransitiveProcMacros: deps.ProcMacros,
1065 })
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066}
1067
1068func (mod *Module) deps(ctx DepsContext) Deps {
1069 deps := Deps{}
1070
1071 if mod.compiler != nil {
1072 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001073 }
1074 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001075 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001076 }
1077
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001078 if mod.coverage != nil {
1079 deps = mod.coverage.deps(ctx, deps)
1080 }
1081
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001082 if mod.sanitize != nil {
1083 deps = mod.sanitize.deps(ctx, deps)
1084 }
1085
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1087 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001088 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1090 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1091 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001092 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001093 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001094 return deps
1095
1096}
1097
Ivan Lozanoffee3342019-08-27 12:03:00 -07001098type dependencyTag struct {
1099 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001100 name string
1101 library bool
1102 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001103 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001104}
1105
Jiyong Park65b62242020-11-25 12:44:59 +09001106// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1107// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1108func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001109 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001110}
1111
1112var _ android.InstallNeededDependencyTag = dependencyTag{}
1113
Colin Cross65cb3142021-12-10 23:05:02 +00001114func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1115 if d.library && d.dynamic {
1116 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1117 }
1118 return nil
1119}
1120
1121var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1122
Ivan Lozanoffee3342019-08-27 12:03:00 -07001123var (
Sam Delmericoa588d152023-06-16 10:28:04 -04001124 buildToolDepTag = dependencyTag{name: "buildToolTag"}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001125 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1126 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001127 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001128 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001129 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001130 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001131 dataLibDepTag = dependencyTag{name: "data lib"}
1132 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001133)
1134
Jiyong Park99644e92020-11-17 22:21:02 +09001135func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1136 tag, ok := depTag.(dependencyTag)
1137 return ok && tag == dylibDepTag
1138}
1139
Jiyong Park94e22fd2021-04-08 18:19:15 +09001140func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1141 tag, ok := depTag.(dependencyTag)
1142 return ok && tag == rlibDepTag
1143}
1144
Matthew Maurer0f003b12020-06-29 14:34:06 -07001145type autoDep struct {
1146 variation string
1147 depTag dependencyTag
1148}
1149
1150var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001151 rlibVariation = "rlib"
1152 dylibVariation = "dylib"
1153 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1154 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001155)
1156
1157type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001158 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001159}
1160
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001161func (mod *Module) begin(ctx BaseModuleContext) {
1162 if mod.coverage != nil {
1163 mod.coverage.begin(ctx)
1164 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001165 if mod.sanitize != nil {
1166 mod.sanitize.begin(ctx)
1167 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001168}
1169
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001170func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001171 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001172 return p.prebuilt()
1173 }
1174 return nil
1175}
1176
Justin Yun24b246a2023-03-16 10:36:16 +09001177func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1178 if rustDep, ok := dep.(*Module); ok {
1179 // Use base module name for snapshots when exporting to Makefile.
1180 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1181 baseName := rustDep.BaseModuleName()
1182 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1183 }
1184 }
1185 return cc.MakeLibName(ctx, c, dep, depName)
1186}
1187
Ivan Lozanoffee3342019-08-27 12:03:00 -07001188func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1189 var depPaths PathDeps
1190
1191 directRlibDeps := []*Module{}
1192 directDylibDeps := []*Module{}
1193 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001194 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001195 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001196 directSrcProvidersDeps := []*Module{}
1197 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001198
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001199 // For the dependency from platform to apex, use the latest stubs
1200 mod.apexSdkVersion = android.FutureApiLevel
1201 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1202 if !apexInfo.IsForPlatform() {
1203 mod.apexSdkVersion = apexInfo.MinSdkVersion
1204 }
1205
1206 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1207 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1208 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1209 // (b/144430859)
1210 mod.apexSdkVersion = android.FutureApiLevel
1211 }
1212
Spandan Das604f3762023-03-16 22:51:40 +00001213 skipModuleList := map[string]bool{}
1214
1215 var apiImportInfo multitree.ApiImportInfo
1216 hasApiImportInfo := false
1217
1218 ctx.VisitDirectDeps(func(dep android.Module) {
1219 if dep.Name() == "api_imports" {
1220 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1221 hasApiImportInfo = true
1222 }
1223 })
1224
1225 if hasApiImportInfo {
1226 targetStubModuleList := map[string]string{}
1227 targetOrigModuleList := map[string]string{}
1228
1229 // Search for dependency which both original module and API imported library with APEX stub exists
1230 ctx.VisitDirectDeps(func(dep android.Module) {
1231 depName := ctx.OtherModuleName(dep)
1232 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1233 targetStubModuleList[apiLibrary] = depName
1234 }
1235 })
1236 ctx.VisitDirectDeps(func(dep android.Module) {
1237 depName := ctx.OtherModuleName(dep)
1238 if origLibrary, ok := targetStubModuleList[depName]; ok {
1239 targetOrigModuleList[origLibrary] = depName
1240 }
1241 })
1242
1243 // Decide which library should be used between original and API imported library
1244 ctx.VisitDirectDeps(func(dep android.Module) {
1245 depName := ctx.OtherModuleName(dep)
1246 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1247 if cc.ShouldUseStubForApex(ctx, dep) {
1248 skipModuleList[depName] = true
1249 } else {
1250 skipModuleList[apiLibrary] = true
1251 }
1252 }
1253 })
1254 }
1255
Cole Faustb6e6f992023-08-17 17:42:26 -07001256 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1257 var directAndroidMkSharedLibs []string
Sam Delmericoa588d152023-06-16 10:28:04 -04001258 transitiveRlibs := android.NewDepSetBuilder[RustLibrary](android.PREORDER)
1259 transitiveDylibs := android.NewDepSetBuilder[RustLibrary](android.PREORDER)
1260 transitiveProcMacros := android.NewDepSetBuilder[RustLibrary](android.PREORDER)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001261 ctx.VisitDirectDeps(func(dep android.Module) {
1262 depName := ctx.OtherModuleName(dep)
1263 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001264
Spandan Das604f3762023-03-16 22:51:40 +00001265 if _, exists := skipModuleList[depName]; exists {
1266 return
1267 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001268
1269 if depTag == android.DarwinUniversalVariantTag {
1270 return
1271 }
1272
Sam Delmericoa588d152023-06-16 10:28:04 -04001273 rustInfo := ctx.OtherModuleProvider(dep, RustInfoProvider).(RustInfo)
1274 if rustInfo.TransitiveDylibs != nil {
1275 transitiveDylibs.Transitive(rustInfo.TransitiveDylibs)
1276 }
1277 if rustInfo.TransitiveRlibs != nil {
1278 transitiveRlibs.Transitive(rustInfo.TransitiveRlibs)
1279 }
1280 if rustInfo.TransitiveProcMacros != nil {
1281 transitiveProcMacros.Transitive(rustInfo.TransitiveProcMacros)
1282 }
1283
Ivan Lozano89435d12020-07-31 11:01:18 -04001284 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001285 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001286 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001287
Ivan Lozanoffee3342019-08-27 12:03:00 -07001288 switch depTag {
1289 case dylibDepTag:
1290 dylib, ok := rustDep.compiler.(libraryInterface)
1291 if !ok || !dylib.dylib() {
1292 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1293 return
1294 }
1295 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001296 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001297 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
Sam Delmericoa588d152023-06-16 10:28:04 -04001298 transitiveDylibs.Direct(RustLibrary{
1299 Path: rustDep.UnstrippedOutputFile(),
1300 CrateName: rustDep.CrateName(),
1301 })
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001302
Ivan Lozanoffee3342019-08-27 12:03:00 -07001303 case rlibDepTag:
1304 rlib, ok := rustDep.compiler.(libraryInterface)
1305 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001306 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001307 return
1308 }
1309 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001310 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001311 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
Sam Delmericoa588d152023-06-16 10:28:04 -04001312 transitiveRlibs.Direct(RustLibrary{
1313 Path: rustDep.UnstrippedOutputFile(),
1314 CrateName: rustDep.CrateName(),
1315 })
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001316
Ivan Lozanoffee3342019-08-27 12:03:00 -07001317 case procMacroDepTag:
1318 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001319 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Sam Delmericoa588d152023-06-16 10:28:04 -04001320 transitiveProcMacros.Direct(RustLibrary{
1321 Path: rustDep.UnstrippedOutputFile(),
1322 CrateName: rustDep.CrateName(),
1323 })
Paul Duffind5cf92e2021-07-09 17:38:55 +01001324 }
1325
Cole Faustb6e6f992023-08-17 17:42:26 -07001326 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1327
Paul Duffind5cf92e2021-07-09 17:38:55 +01001328 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001329 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1330 // OS/Arch variant is used.
1331 var helper string
1332 if ctx.Host() {
1333 helper = "missing 'host_supported'?"
1334 } else {
1335 helper = "device module defined?"
1336 }
1337
1338 if dep.Target().Os != ctx.Os() {
1339 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1340 return
1341 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1342 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1343 return
1344 }
1345 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001346 }
1347
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001348 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001349 if depTag != procMacroDepTag {
1350 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1351 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1352 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1353 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001354 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001355 }
1356
Ivan Lozanoffee3342019-08-27 12:03:00 -07001357 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001358 linkFile := rustDep.UnstrippedOutputFile()
Matthew Maurerbb3add12020-06-25 09:34:12 -07001359 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Sam Delmericoa588d152023-06-16 10:28:04 -04001360 lib.exportLinkDirs(linkFile.Dir())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001361 }
1362 }
1363
Ivan Lozano89435d12020-07-31 11:01:18 -04001364 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001365 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001366 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001367 if _, ok := ccDep.(*Module); !ok {
1368 if ccDep.Module().Target().Os != ctx.Os() {
1369 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1370 return
1371 }
1372 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1373 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1374 return
1375 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001376 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001377 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001378 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001379 if !ctx.Config().AllowMissingDependencies() {
1380 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1381 } else {
1382 ctx.AddMissingDependencies([]string{depName})
1383 }
1384 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001385 }
1386
Sam Delmericoa588d152023-06-16 10:28:04 -04001387 linkPath := linkObject.Path().Dir()
Colin Crossa86ea0e2023-08-01 09:57:22 -07001388
Ivan Lozanoffee3342019-08-27 12:03:00 -07001389 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001390 switch {
1391 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001392 if cc.IsWholeStaticLib(depTag) {
1393 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1394 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001395 if mod.Binary() {
1396 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1397 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1398 // final linkage, pass the args directly to the linker to handle these cases.
1399 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1400 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001401 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001402 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001403 } else {
1404 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 -05001405 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001406 }
1407
1408 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1409 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001410 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001411 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1412
Colin Cross0de8a1e2020-09-18 14:15:30 -07001413 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1414 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1415 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1416 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1417 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001418 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001419
1420 // Record baseLibName for snapshots.
1421 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1422
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001423 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001424 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001425 // For the shared lib dependencies, we may link to the stub variant
1426 // of the dependency depending on the context (e.g. if this
1427 // dependency crosses the APEX boundaries).
1428 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1429
1430 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1431 // object (either from stub or non-stub) to use.
1432 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001433 if !linkObject.Valid() {
1434 if !ctx.Config().AllowMissingDependencies() {
1435 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1436 } else {
1437 ctx.AddMissingDependencies([]string{depName})
1438 }
1439 return
1440 }
Sam Delmericoa588d152023-06-16 10:28:04 -04001441 linkPath = linkObject.Path().Dir()
Jiyong Park7d55b612021-06-11 17:22:09 +09001442
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001444 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001445 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1446 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1447 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1448 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001449 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001450
1451 // Record baseLibName for snapshots.
1452 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1453
Cole Faustb6e6f992023-08-17 17:42:26 -07001454 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001456 case cc.IsHeaderDepTag(depTag):
1457 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1458 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1459 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1460 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001461 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001462 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001463 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001464 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001465 }
1466
1467 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001468 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1469 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001470 if linkObject.Valid() {
1471 lib.exportLinkObjects(linkObject.Path())
1472 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001473 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001474 } else {
1475 switch {
Sam Delmericoa588d152023-06-16 10:28:04 -04001476 case depTag == buildToolDepTag:
1477 buildTool := ctx.OtherModuleProvider(dep, android.PrebuiltBuildToolInfoProvider).(android.PrebuiltBuildToolInfo)
1478 depPaths.BuildToolDeps = append(depPaths.BuildToolDeps, buildTool.Src)
1479 depPaths.BuildToolDeps = append(depPaths.BuildToolDeps, buildTool.Deps...)
1480 switch android.RemoveOptionalPrebuiltPrefix(dep.Name()) {
1481 case "rustc":
1482 depPaths.Rustc = buildTool.Src
1483 // rustc expects the standard cc toolchain libraries (libdl, libm, libc, etc.)
1484 // not to be under the __SBOX_SANDBOX_DIR__/ directory
1485 depPaths.BuildToolSrcDeps = append(depPaths.BuildToolSrcDeps, buildTool.Deps...)
1486 case "clang++":
1487 depPaths.Clang = buildTool.Src
1488 case "llvm-ar":
1489 depPaths.Llvm_ar = buildTool.Src
1490 case "clippy-driver":
1491 depPaths.Clippy_driver = buildTool.Src
1492 case "rustdoc":
1493 depPaths.Rustdoc = buildTool.Src
1494 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001495 case depTag == cc.CrtBeginDepTag:
1496 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1497 case depTag == cc.CrtEndDepTag:
1498 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1499 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001500 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001501
1502 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001503 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001504 // These are usually genrules which don't have per-target variants.
1505 directSrcDeps = append(directSrcDeps, srcDep)
1506 }
1507 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001508 })
1509
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001510 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001511 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001512 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001513 }
1514
Ivan Lozanoffee3342019-08-27 12:03:00 -07001515 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001516 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001517 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001518 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001519 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001520 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001521 }
1522
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001523 var srcProviderDepFiles android.Paths
1524 for _, dep := range directSrcProvidersDeps {
1525 srcs, _ := dep.OutputFiles("")
1526 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1527 }
1528 for _, dep := range directSrcDeps {
1529 srcs := dep.Srcs()
1530 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1531 }
1532
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001533 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001534 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001535
1536 // Dedup exported flags from dependencies
Sam Delmericoa588d152023-06-16 10:28:04 -04001537 depPaths.linkDirs = android.FirstUniquePaths(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001538 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001539 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001540 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1541 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1542 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001543
Sam Delmericoa588d152023-06-16 10:28:04 -04001544 depPaths.Rlibs = transitiveRlibs.Build()
1545 depPaths.Dylibs = transitiveDylibs.Build()
1546 depPaths.ProcMacros = transitiveProcMacros.Build()
1547 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1548
Ivan Lozanoffee3342019-08-27 12:03:00 -07001549 return depPaths
1550}
1551
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001552func (mod *Module) InstallInData() bool {
1553 if mod.compiler == nil {
1554 return false
1555 }
1556 return mod.compiler.inData()
1557}
1558
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001559func (mod *Module) InstallInRamdisk() bool {
1560 return mod.InRamdisk()
1561}
1562
1563func (mod *Module) InstallInVendorRamdisk() bool {
1564 return mod.InVendorRamdisk()
1565}
1566
1567func (mod *Module) InstallInRecovery() bool {
1568 return mod.InRecovery()
1569}
1570
Spandan Das604f3762023-03-16 22:51:40 +00001571// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1572func (r *Module) usePublicApi() bool {
1573 return r.Device() && r.UseSdk()
1574}
1575
1576// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1577func (r *Module) useVendorApi() bool {
1578 return r.Device() && (r.InVendor() || r.InProduct())
1579}
1580
Ivan Lozanoffee3342019-08-27 12:03:00 -07001581func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1582 ctx := &depsContext{
1583 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001584 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001585
1586 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001587 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001588 var snapshotInfo *cc.SnapshotInfo
1589
Kiyoung Kim487689e2022-07-26 09:48:22 +09001590 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001591 if mod.usePublicApi() || mod.useVendorApi() {
1592 for idx, lib := range deps.SharedLibs {
1593 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1594 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001595 }
1596
Ivan Lozano1921e802021-05-20 13:39:16 -04001597 if ctx.Os() == android.Android {
1598 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1599 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001600
Ivan Lozano2b081132020-09-08 12:46:52 -04001601 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001602 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001603 stdLinkage = "rlib-std"
1604 }
1605
1606 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001607
Ivan Lozano2b081132020-09-08 12:46:52 -04001608 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1609 rlibDepVariations = append(rlibDepVariations,
1610 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1611 }
1612
Sam Delmericoa588d152023-06-16 10:28:04 -04001613 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "rustc")
1614 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "clippy-driver")
1615 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "rustdoc")
1616 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "clang++")
1617 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "clang++.real")
1618 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "lld")
1619 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "ld.lld")
1620 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "llvm-ar")
1621
Ivan Lozano1921e802021-05-20 13:39:16 -04001622 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001623 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001624 for _, lib := range deps.Rlibs {
1625 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001626 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001627
Ivan Lozano2d407632022-04-07 12:59:11 -04001628 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001629 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001630
1631 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001632 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1633 for _, lib := range deps.Dylibs {
1634 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1635 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001636
Ivan Lozano1921e802021-05-20 13:39:16 -04001637 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001638 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1639 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001640 for _, lib := range deps.Rustlibs {
1641 if autoDep.depTag == rlibDepTag {
1642 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001643 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001644 } else {
1645 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1646 // Check for the existence of the dylib deptag variant. Select it if available,
1647 // otherwise select the rlib variant.
1648 autoDepVariations := append(commonDepVariations,
1649 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001650
1651 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1652
1653 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1654 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001655 } else {
1656 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001657 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001658 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001659 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001660 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001661 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001662 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001663 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001664 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001665 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001666 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001667 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001668 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001669 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001670 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001671 for _, lib := range deps.Stdlibs {
1672 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1673 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001674 }
1675 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001676
1677 for _, lib := range deps.SharedLibs {
1678 depTag := cc.SharedDepTag()
1679 name, version := cc.StubsLibNameAndVersion(lib)
1680
1681 variations := []blueprint.Variation{
1682 {Mutator: "link", Variation: "shared"},
1683 }
Spandan Das604f3762023-03-16 22:51:40 +00001684 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1685 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1686 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1687 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1688 }
1689
1690 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1691 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1692 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001693 }
1694
1695 for _, lib := range deps.WholeStaticLibs {
1696 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001697 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001698
1699 actx.AddVariationDependencies([]blueprint.Variation{
1700 {Mutator: "link", Variation: "static"},
1701 }, depTag, lib)
1702 }
1703
1704 for _, lib := range deps.StaticLibs {
1705 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001706 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001707
1708 actx.AddVariationDependencies([]blueprint.Variation{
1709 {Mutator: "link", Variation: "static"},
1710 }, depTag, lib)
1711 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001712
Zach Johnson3df4e632020-11-06 11:56:27 -08001713 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1714
Colin Cross565cafd2020-09-25 18:47:38 -07001715 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001716 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001717 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001718 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001719 }
Colin Crossfe605e12022-01-23 20:46:16 -08001720 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001721 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001722 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001723 }
1724
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001725 if mod.sourceProvider != nil {
1726 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1727 bindgen.Properties.Custom_bindgen != "" {
1728 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1729 bindgen.Properties.Custom_bindgen)
1730 }
1731 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001732
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001733 actx.AddVariationDependencies([]blueprint.Variation{
1734 {Mutator: "link", Variation: "shared"},
1735 }, dataLibDepTag, deps.DataLibs...)
1736
1737 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1738
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001739 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001740 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001741
1742 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001743}
1744
Ivan Lozano2d407632022-04-07 12:59:11 -04001745// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001746func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1747 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001748 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1749}
1750
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001751func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1752 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1753 actx.AddVariationDependencies(variations, depTag, lib)
1754}
1755
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001756func BeginMutator(ctx android.BottomUpMutatorContext) {
1757 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1758 mod.beginMutator(ctx)
1759 }
1760}
1761
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001762func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1763 ctx := &baseModuleContext{
1764 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001765 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001766
1767 mod.begin(ctx)
1768}
1769
Ivan Lozanoffee3342019-08-27 12:03:00 -07001770func (mod *Module) Name() string {
1771 name := mod.ModuleBase.Name()
1772 if p, ok := mod.compiler.(interface {
1773 Name(string) string
1774 }); ok {
1775 name = p.Name(name)
1776 }
1777 return name
1778}
1779
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001780func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001781 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001782 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001783 }
1784}
1785
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001786var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001787var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001788
1789func (mod *Module) HostToolPath() android.OptionalPath {
1790 if !mod.Host() {
1791 return android.OptionalPath{}
1792 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001793 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1794 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001795 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1796 // Even though proc-macros aren't strictly "tools", since they target the compiler
1797 // and act as compiler plugins, we treat them similarly.
1798 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001799 }
1800 return android.OptionalPath{}
1801}
1802
Jiyong Park99644e92020-11-17 22:21:02 +09001803var _ android.ApexModule = (*Module)(nil)
1804
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001805func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001806 return String(mod.Properties.Min_sdk_version)
1807}
1808
Jiyong Park45bf82e2020-12-15 22:29:02 +09001809// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001810func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001811 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001812 if minSdkVersion == "apex_inherit" {
1813 return nil
1814 }
1815 if minSdkVersion == "" {
1816 return fmt.Errorf("min_sdk_version is not specificed")
1817 }
1818
1819 // Not using nativeApiLevelFromUser because the context here is not
1820 // necessarily a native context.
1821 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1822 if err != nil {
1823 return err
1824 }
1825
1826 if ver.GreaterThan(sdkVersion) {
1827 return fmt.Errorf("newer SDK(%v)", ver)
1828 }
Jiyong Park99644e92020-11-17 22:21:02 +09001829 return nil
1830}
1831
Jiyong Park45bf82e2020-12-15 22:29:02 +09001832// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001833func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1834 depTag := ctx.OtherModuleDependencyTag(dep)
1835
1836 if ccm, ok := dep.(*cc.Module); ok {
1837 if ccm.HasStubsVariants() {
1838 if cc.IsSharedDepTag(depTag) {
1839 // dynamic dep to a stubs lib crosses APEX boundary
1840 return false
1841 }
1842 if cc.IsRuntimeDepTag(depTag) {
1843 // runtime dep to a stubs lib also crosses APEX boundary
1844 return false
1845 }
1846
1847 if cc.IsHeaderDepTag(depTag) {
1848 return false
1849 }
1850 }
1851 if mod.Static() && cc.IsSharedDepTag(depTag) {
1852 // shared_lib dependency from a static lib is considered as crossing
1853 // the APEX boundary because the dependency doesn't actually is
1854 // linked; the dependency is used only during the compilation phase.
1855 return false
1856 }
1857 }
1858
Matthew Maurer581b6d82022-09-29 16:46:25 -07001859 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001860 return false
1861 }
1862
1863 return true
1864}
1865
1866// Overrides ApexModule.IsInstallabeToApex()
1867func (mod *Module) IsInstallableToApex() bool {
1868 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001869 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001870 return true
1871 }
1872 if _, ok := mod.compiler.(*binaryDecorator); ok {
1873 return true
1874 }
1875 }
1876 return false
1877}
1878
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001879// If a library file has a "lib" prefix, extract the library name without the prefix.
1880func libNameFromFilePath(filepath android.Path) (string, bool) {
1881 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1882 if strings.HasPrefix(libName, "lib") {
1883 libName = libName[3:]
1884 return libName, true
1885 }
1886 return "", false
1887}
1888
Sasha Smundaka76acba2022-04-18 20:12:56 -07001889func kytheExtractRustFactory() android.Singleton {
1890 return &kytheExtractRustSingleton{}
1891}
1892
1893type kytheExtractRustSingleton struct {
1894}
1895
1896func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1897 var xrefTargets android.Paths
1898 ctx.VisitAllModules(func(module android.Module) {
1899 if rustModule, ok := module.(xref); ok {
1900 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1901 }
1902 })
1903 if len(xrefTargets) > 0 {
1904 ctx.Phony("xref_rust", xrefTargets...)
1905 }
1906}
1907
Jihoon Kangf78a8902022-09-01 22:47:07 +00001908func (c *Module) Partition() string {
1909 return ""
1910}
1911
Vinh Tranbcb5f572023-08-24 11:10:01 -04001912func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
1913 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1914 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001915 } else if ctx.ModuleType() == "rust_proc_macro" {
1916 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001917 } else if ctx.ModuleType() == "rust_binary_host" {
1918 binaryBp2build(ctx, m)
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001919 } else if ctx.ModuleType() == "rust_protobuf_host" {
1920 protoLibraryBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001921 } else {
1922 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1923 }
1924}
1925
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001926// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001927// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001928// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1929// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1930// instead of using globs.
1931func srcsAndCompileDataAttrs(ctx android.TopDownMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
1932 var srcs bazel.LabelList
1933 var compileData bazel.LabelList
1934
1935 if c.Properties.Srcs[0] == "src/lib.rs" {
1936 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1937 compileData = android.BazelLabelForModuleSrc(
1938 ctx,
1939 []string{
1940 "src/**/*.proto",
1941 "examples/**/*.rs",
1942 "**/*.md",
1943 },
1944 )
1945 } else {
1946 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1947 }
1948
1949 return srcs, compileData
1950}
1951
Ivan Lozanoffee3342019-08-27 12:03:00 -07001952var Bool = proptools.Bool
1953var BoolDefault = proptools.BoolDefault
1954var String = proptools.String
1955var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001956
1957var _ android.OutputFileProducer = (*Module)(nil)