blob: 1ee99cdb1d1a7265cafe78b77dd8899658af6792 [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 {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700440 DyLibs RustLibraries
441 RLibs RustLibraries
442 LibDeps android.Paths
443 WholeStaticLibs android.Paths
444 ProcMacros RustLibraries
445 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500446
447 // depFlags and depLinkFlags are rustc and linker (clang) flags.
448 depFlags []string
449 depLinkFlags []string
450
451 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
452 // Both of these are exported and propagate to dependencies.
453 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700454 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700455
Ivan Lozano45901ed2020-07-24 16:05:01 -0400456 // Used by bindgen modules which call clang
457 depClangFlags []string
458 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400459 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400460 depSystemIncludePaths android.Paths
461
Colin Crossfe605e12022-01-23 20:46:16 -0800462 CrtBegin android.Paths
463 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700464
465 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500466 SrcDeps android.Paths
467 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468}
469
470type RustLibraries []RustLibrary
471
472type RustLibrary struct {
473 Path android.Path
474 CrateName string
475}
476
477type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100478 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400480 cfgFlags(ctx ModuleContext, flags Flags) Flags
481 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700482 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700483 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484 compilerDeps(ctx DepsContext, deps Deps) Deps
485 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700486 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700487
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100488 // Output directory in which source-generated code from dependencies is
489 // copied. This is equivalent to Cargo's OUT_DIR variable.
490 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700491
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400492 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
493 CargoPkgVersion() string
494
495 // CargoEnvCompat returns whether Cargo environment variables should be used.
496 CargoEnvCompat() bool
497
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800498 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200499 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700500 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400501 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400502
503 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400504
505 Disabled() bool
506 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400507
Ivan Lozanodd055472020-09-28 13:22:45 -0400508 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500509 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900510
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400511 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900512 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513}
514
Matthew Maurerbb3add12020-06-25 09:34:12 -0700515type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700516 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700517 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700518}
519
Sasha Smundaka76acba2022-04-18 20:12:56 -0700520type xref interface {
521 XrefRustFiles() android.Paths
522}
523
Matthew Maurerbb3add12020-06-25 09:34:12 -0700524type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400525 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700526 linkObjects android.Paths
527 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700528}
529
Matthew Maurerbb3add12020-06-25 09:34:12 -0700530func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
531 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
532}
533
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700534func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
535 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
536}
537
538func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
539 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400540}
541
Colin Cross0de8a1e2020-09-18 14:15:30 -0700542func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
543 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700544 LinkDirs: flagExporter.linkDirs,
545 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700546 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700547 })
548}
549
Matthew Maurerbb3add12020-06-25 09:34:12 -0700550var _ exportedFlagsProducer = (*flagExporter)(nil)
551
552func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700553 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700554}
555
Colin Cross0de8a1e2020-09-18 14:15:30 -0700556type FlagExporterInfo struct {
557 Flags []string
558 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700559 LinkObjects android.Paths
560 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700561}
562
563var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
564
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400565func (mod *Module) isCoverageVariant() bool {
566 return mod.coverage.Properties.IsCoverageVariant
567}
568
569var _ cc.Coverage = (*Module)(nil)
570
571func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
572 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
573}
574
Ivan Lozanod7586b62021-04-01 09:49:36 -0400575func (mod *Module) VndkVersion() string {
576 return mod.Properties.VndkVersion
577}
578
579func (mod *Module) PreventInstall() bool {
580 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400581}
582
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583func (mod *Module) MarkAsCoverageVariant(coverage bool) {
584 mod.coverage.Properties.IsCoverageVariant = coverage
585}
586
587func (mod *Module) EnableCoverageIfNeeded() {
588 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589}
590
591func defaultsFactory() android.Module {
592 return DefaultsFactory()
593}
594
595type Defaults struct {
596 android.ModuleBase
597 android.DefaultsModuleBase
598}
599
600func DefaultsFactory(props ...interface{}) android.Module {
601 module := &Defaults{}
602
603 module.AddProperties(props...)
604 module.AddProperties(
605 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800606 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500607 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100608 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400609 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610 &BaseCompilerProperties{},
611 &BinaryCompilerProperties{},
612 &LibraryCompilerProperties{},
613 &ProcMacroCompilerProperties{},
614 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400615 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700616 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400617 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400618 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200619 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500620 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000621 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700622 )
623
624 android.InitDefaultsModule(module)
625 return module
626}
627
628func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700629 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700630}
631
Ivan Lozano183a3212019-10-18 14:18:45 -0700632func (mod *Module) CcLibrary() bool {
633 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500634 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700635 return true
636 }
637 }
638 return false
639}
640
641func (mod *Module) CcLibraryInterface() bool {
642 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400643 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
644 // VariantIs{Static,Shared} is set.
645 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700646 return true
647 }
648 }
649 return false
650}
651
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400652func (mod *Module) RustLibraryInterface() bool {
653 if mod.compiler != nil {
654 if _, ok := mod.compiler.(libraryInterface); ok {
655 return true
656 }
657 }
658 return false
659}
660
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500661func (mod *Module) IsFuzzModule() bool {
662 if _, ok := mod.compiler.(*fuzzDecorator); ok {
663 return true
664 }
665 return false
666}
667
668func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
669 return mod.FuzzModule
670}
671
672func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
673 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
674 return fuzzer.fuzzPackagedModule
675 }
676 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
677}
678
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000679func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500680 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
681 return fuzzer.sharedLibraries
682 }
683 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
684}
685
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400686func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400687 if mod.compiler != nil {
688 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400689 }
690 return nil
691}
692
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800693func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700694 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700695 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800696 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700697 }
698 }
699 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
700}
701
702func (mod *Module) SetStatic() {
703 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700704 if library, ok := mod.compiler.(libraryInterface); ok {
705 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700706 return
707 }
708 }
709 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
710}
711
712func (mod *Module) SetShared() {
713 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700714 if library, ok := mod.compiler.(libraryInterface); ok {
715 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700716 return
717 }
718 }
719 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
720}
721
Ivan Lozano183a3212019-10-18 14:18:45 -0700722func (mod *Module) BuildStaticVariant() bool {
723 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700724 if library, ok := mod.compiler.(libraryInterface); ok {
725 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700726 }
727 }
728 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
729}
730
731func (mod *Module) BuildSharedVariant() bool {
732 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700733 if library, ok := mod.compiler.(libraryInterface); ok {
734 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700735 }
736 }
737 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
738}
739
Ivan Lozano183a3212019-10-18 14:18:45 -0700740func (mod *Module) Module() android.Module {
741 return mod
742}
743
Ivan Lozano183a3212019-10-18 14:18:45 -0700744func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400745 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700746}
747
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400748func (mod *Module) CoverageFiles() android.Paths {
749 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800750 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400751 }
752 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
753}
754
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400755// Rust does not produce gcno files, and therefore does not produce a coverage archive.
756func (mod *Module) CoverageOutputFile() android.OptionalPath {
757 return android.OptionalPath{}
758}
759
760func (mod *Module) IsNdk(config android.Config) bool {
761 return false
762}
763
764func (mod *Module) IsStubs() bool {
765 return false
766}
767
Jiyong Park459feca2020-12-15 11:02:21 +0900768func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900769 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900770 return false
771 }
772
Jiyong Park459feca2020-12-15 11:02:21 +0900773 // The apex variant is not installable because it is included in the APEX and won't appear
774 // in the system partition as a standalone file.
775 if !apexInfo.IsForPlatform() {
776 return false
777 }
778
Jiyong Parke54f07e2021-04-07 15:08:04 +0900779 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900780}
781
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500782func (ctx moduleContext) apexVariationName() string {
783 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
784}
785
Ivan Lozano183a3212019-10-18 14:18:45 -0700786var _ cc.LinkableInterface = (*Module)(nil)
787
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788func (mod *Module) Init() android.Module {
789 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500790 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700791
Yi Kong46c6e592022-01-20 22:55:00 +0800792 if mod.afdo != nil {
793 mod.AddProperties(mod.afdo.props()...)
794 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 if mod.compiler != nil {
796 mod.AddProperties(mod.compiler.compilerProps()...)
797 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400798 if mod.coverage != nil {
799 mod.AddProperties(mod.coverage.props()...)
800 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200801 if mod.clippy != nil {
802 mod.AddProperties(mod.clippy.props()...)
803 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400804 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700805 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400806 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500807 if mod.sanitize != nil {
808 mod.AddProperties(mod.sanitize.props()...)
809 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400810
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900812 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813
814 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 return mod
816}
817
818func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
819 return &Module{
820 hod: hod,
821 multilib: multilib,
822 }
823}
824func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
825 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800826 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400827 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200828 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500829 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 return module
831}
832
833type ModuleContext interface {
834 android.ModuleContext
835 ModuleContextIntf
836}
837
838type BaseModuleContext interface {
839 android.BaseModuleContext
840 ModuleContextIntf
841}
842
843type DepsContext interface {
844 android.BottomUpMutatorContext
845 ModuleContextIntf
846}
847
848type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200849 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851}
852
853type depsContext struct {
854 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855}
856
857type moduleContext struct {
858 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859}
860
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200861type baseModuleContext struct {
862 android.BaseModuleContext
863}
864
865func (ctx *moduleContext) RustModule() *Module {
866 return ctx.Module().(*Module)
867}
868
869func (ctx *moduleContext) toolchain() config.Toolchain {
870 return ctx.RustModule().toolchain(ctx)
871}
872
873func (ctx *depsContext) RustModule() *Module {
874 return ctx.Module().(*Module)
875}
876
877func (ctx *depsContext) toolchain() config.Toolchain {
878 return ctx.RustModule().toolchain(ctx)
879}
880
881func (ctx *baseModuleContext) RustModule() *Module {
882 return ctx.Module().(*Module)
883}
884
885func (ctx *baseModuleContext) toolchain() config.Toolchain {
886 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400887}
888
889func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700890 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
891 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
892 return false
893 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400894 return mod.compiler != nil && mod.compiler.nativeCoverage()
895}
896
Ivan Lozanod7586b62021-04-01 09:49:36 -0400897func (mod *Module) EverInstallable() bool {
898 return mod.compiler != nil &&
899 // Check to see whether the module is actually ever installable.
900 mod.compiler.everInstallable()
901}
902
903func (mod *Module) Installable() *bool {
904 return mod.Properties.Installable
905}
906
Ivan Lozano872d5792022-03-23 17:31:39 -0400907func (mod *Module) ProcMacro() bool {
908 if pm, ok := mod.compiler.(procMacroInterface); ok {
909 return pm.ProcMacro()
910 }
911 return false
912}
913
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
915 if mod.cachedToolchain == nil {
916 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
917 }
918 return mod.cachedToolchain
919}
920
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200921func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
922 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
923}
924
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
926}
927
928func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
929 ctx := &moduleContext{
930 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700932
Jiyong Park99644e92020-11-17 22:21:02 +0900933 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
934 if !apexInfo.IsForPlatform() {
935 mod.hideApexVariantFromMake = true
936 }
937
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500939 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
940
Ivan Lozanof1868af2022-04-12 13:08:36 -0400941 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700942
Ivan Lozanoffee3342019-08-27 12:03:00 -0700943 if !toolchain.Supported() {
944 // This toolchain's unsupported, there's nothing to do for this mod.
945 return
946 }
947
948 deps := mod.depsToPaths(ctx)
949 flags := Flags{
950 Toolchain: toolchain,
951 }
952
Ivan Lozano67eada32021-09-23 11:50:33 -0400953 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800954 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500955 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800956 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 if mod.compiler != nil {
958 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400959 flags = mod.compiler.cfgFlags(ctx, flags)
960 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400961 }
962 if mod.coverage != nil {
963 flags, deps = mod.coverage.flags(ctx, flags, deps)
964 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200965 if mod.clippy != nil {
966 flags, deps = mod.clippy.flags(ctx, flags, deps)
967 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500968 if mod.sanitize != nil {
969 flags, deps = mod.sanitize.flags(ctx, flags, deps)
970 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400971
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200972 // SourceProvider needs to call GenerateSource() before compiler calls
973 // compile() so it can provide the source. A SourceProvider has
974 // multiple variants (e.g. source, rlib, dylib). Only the "source"
975 // variant is responsible for effectively generating the source. The
976 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400977 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200978 if mod.compiler.(libraryInterface).source() {
979 mod.sourceProvider.GenerateSource(ctx, deps)
980 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
981 } else {
982 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
983 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700984 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200985 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400986 }
987
988 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100989 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700990 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400991 if ctx.Failed() {
992 return
993 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700994 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
995 if buildOutput.kytheFile != nil {
996 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
997 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400998 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900999
Dan Albert06feee92021-03-19 15:06:02 -07001000 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001001 if mod.docTimestampFile.Valid() {
1002 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1003 }
Dan Albert06feee92021-03-19 15:06:02 -07001004
Ivan Lozano1921e802021-05-20 13:39:16 -04001005 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1006 // RECOVERY_SNAPSHOT_VERSION is current.
1007 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1008 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1009 lib.collectHeadersForSnapshot(ctx, deps)
1010 }
1011 }
1012
Jiyong Park459feca2020-12-15 11:02:21 +09001013 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001014 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001015 // If the module has been specifically configure to not be installed then
1016 // hide from make as otherwise it will break when running inside make as the
1017 // output path to install will not be specified. Not all uninstallable
1018 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001019 // side dependencies. In particular, proc-macros need to be captured in the
1020 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001021 mod.HideFromMake()
1022 } else if !mod.installable(apexInfo) {
1023 mod.SkipInstall()
1024 }
1025
1026 // Still call install though, the installs will be stored as PackageSpecs to allow
1027 // using the outputs in a genrule.
1028 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001029 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001030 if ctx.Failed() {
1031 return
1032 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001033 }
Chris Wailes74be7642021-07-22 16:20:28 -07001034
1035 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001036 }
1037}
1038
1039func (mod *Module) deps(ctx DepsContext) Deps {
1040 deps := Deps{}
1041
1042 if mod.compiler != nil {
1043 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001044 }
1045 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001046 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 }
1048
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001049 if mod.coverage != nil {
1050 deps = mod.coverage.deps(ctx, deps)
1051 }
1052
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001053 if mod.sanitize != nil {
1054 deps = mod.sanitize.deps(ctx, deps)
1055 }
1056
Ivan Lozanoffee3342019-08-27 12:03:00 -07001057 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1058 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001059 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001060 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1061 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1062 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001063 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001064 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065 return deps
1066
1067}
1068
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069type dependencyTag struct {
1070 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001071 name string
1072 library bool
1073 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001074 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075}
1076
Jiyong Park65b62242020-11-25 12:44:59 +09001077// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1078// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1079func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001080 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001081}
1082
1083var _ android.InstallNeededDependencyTag = dependencyTag{}
1084
Colin Cross65cb3142021-12-10 23:05:02 +00001085func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1086 if d.library && d.dynamic {
1087 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1088 }
1089 return nil
1090}
1091
1092var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1093
Ivan Lozanoffee3342019-08-27 12:03:00 -07001094var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001095 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1096 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001097 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001098 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001099 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001100 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001101 dataLibDepTag = dependencyTag{name: "data lib"}
1102 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001103)
1104
Jiyong Park99644e92020-11-17 22:21:02 +09001105func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1106 tag, ok := depTag.(dependencyTag)
1107 return ok && tag == dylibDepTag
1108}
1109
Jiyong Park94e22fd2021-04-08 18:19:15 +09001110func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1111 tag, ok := depTag.(dependencyTag)
1112 return ok && tag == rlibDepTag
1113}
1114
Matthew Maurer0f003b12020-06-29 14:34:06 -07001115type autoDep struct {
1116 variation string
1117 depTag dependencyTag
1118}
1119
1120var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001121 rlibVariation = "rlib"
1122 dylibVariation = "dylib"
1123 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1124 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001125)
1126
1127type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001128 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001129}
1130
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001131func (mod *Module) begin(ctx BaseModuleContext) {
1132 if mod.coverage != nil {
1133 mod.coverage.begin(ctx)
1134 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001135 if mod.sanitize != nil {
1136 mod.sanitize.begin(ctx)
1137 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001138}
1139
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001140func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001141 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001142 return p.prebuilt()
1143 }
1144 return nil
1145}
1146
Justin Yun24b246a2023-03-16 10:36:16 +09001147func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1148 if rustDep, ok := dep.(*Module); ok {
1149 // Use base module name for snapshots when exporting to Makefile.
1150 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1151 baseName := rustDep.BaseModuleName()
1152 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1153 }
1154 }
1155 return cc.MakeLibName(ctx, c, dep, depName)
1156}
1157
Ivan Lozanoffee3342019-08-27 12:03:00 -07001158func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1159 var depPaths PathDeps
1160
1161 directRlibDeps := []*Module{}
1162 directDylibDeps := []*Module{}
1163 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001164 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001165 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001166 directSrcProvidersDeps := []*Module{}
1167 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001168
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001169 // For the dependency from platform to apex, use the latest stubs
1170 mod.apexSdkVersion = android.FutureApiLevel
1171 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1172 if !apexInfo.IsForPlatform() {
1173 mod.apexSdkVersion = apexInfo.MinSdkVersion
1174 }
1175
1176 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1177 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1178 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1179 // (b/144430859)
1180 mod.apexSdkVersion = android.FutureApiLevel
1181 }
1182
Spandan Das604f3762023-03-16 22:51:40 +00001183 skipModuleList := map[string]bool{}
1184
1185 var apiImportInfo multitree.ApiImportInfo
1186 hasApiImportInfo := false
1187
1188 ctx.VisitDirectDeps(func(dep android.Module) {
1189 if dep.Name() == "api_imports" {
1190 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1191 hasApiImportInfo = true
1192 }
1193 })
1194
1195 if hasApiImportInfo {
1196 targetStubModuleList := map[string]string{}
1197 targetOrigModuleList := map[string]string{}
1198
1199 // Search for dependency which both original module and API imported library with APEX stub exists
1200 ctx.VisitDirectDeps(func(dep android.Module) {
1201 depName := ctx.OtherModuleName(dep)
1202 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1203 targetStubModuleList[apiLibrary] = depName
1204 }
1205 })
1206 ctx.VisitDirectDeps(func(dep android.Module) {
1207 depName := ctx.OtherModuleName(dep)
1208 if origLibrary, ok := targetStubModuleList[depName]; ok {
1209 targetOrigModuleList[origLibrary] = depName
1210 }
1211 })
1212
1213 // Decide which library should be used between original and API imported library
1214 ctx.VisitDirectDeps(func(dep android.Module) {
1215 depName := ctx.OtherModuleName(dep)
1216 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1217 if cc.ShouldUseStubForApex(ctx, dep) {
1218 skipModuleList[depName] = true
1219 } else {
1220 skipModuleList[apiLibrary] = true
1221 }
1222 }
1223 })
1224 }
1225
Cole Faustb6e6f992023-08-17 17:42:26 -07001226 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1227 var directAndroidMkSharedLibs []string
1228
Ivan Lozanoffee3342019-08-27 12:03:00 -07001229 ctx.VisitDirectDeps(func(dep android.Module) {
1230 depName := ctx.OtherModuleName(dep)
1231 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001232
Spandan Das604f3762023-03-16 22:51:40 +00001233 if _, exists := skipModuleList[depName]; exists {
1234 return
1235 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001236
1237 if depTag == android.DarwinUniversalVariantTag {
1238 return
1239 }
1240
Ivan Lozano89435d12020-07-31 11:01:18 -04001241 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001242 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001243 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001244
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 switch depTag {
1246 case dylibDepTag:
1247 dylib, ok := rustDep.compiler.(libraryInterface)
1248 if !ok || !dylib.dylib() {
1249 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1250 return
1251 }
1252 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001253 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001254 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1255
Ivan Lozanoffee3342019-08-27 12:03:00 -07001256 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001257
Ivan Lozanoffee3342019-08-27 12:03:00 -07001258 rlib, ok := rustDep.compiler.(libraryInterface)
1259 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001260 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001261 return
1262 }
1263 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001264 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001265 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1266
Ivan Lozanoffee3342019-08-27 12:03:00 -07001267 case procMacroDepTag:
1268 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001269 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001270 }
1271
Cole Faustb6e6f992023-08-17 17:42:26 -07001272 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1273
Paul Duffind5cf92e2021-07-09 17:38:55 +01001274 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001275 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1276 // OS/Arch variant is used.
1277 var helper string
1278 if ctx.Host() {
1279 helper = "missing 'host_supported'?"
1280 } else {
1281 helper = "device module defined?"
1282 }
1283
1284 if dep.Target().Os != ctx.Os() {
1285 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1286 return
1287 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1288 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1289 return
1290 }
1291 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 }
1293
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001294 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001295 if depTag != procMacroDepTag {
1296 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1297 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1298 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1299 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001300 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001301 }
1302
Ivan Lozanoffee3342019-08-27 12:03:00 -07001303 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001304 linkFile := rustDep.UnstrippedOutputFile()
1305 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001306 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1307 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001308 }
1309 }
1310
Ivan Lozano89435d12020-07-31 11:01:18 -04001311 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001312 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001313 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001314 if _, ok := ccDep.(*Module); !ok {
1315 if ccDep.Module().Target().Os != ctx.Os() {
1316 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1317 return
1318 }
1319 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1320 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1321 return
1322 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001323 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001324 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001325 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001326 if !ctx.Config().AllowMissingDependencies() {
1327 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1328 } else {
1329 ctx.AddMissingDependencies([]string{depName})
1330 }
1331 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001332 }
1333
Colin Crossa86ea0e2023-08-01 09:57:22 -07001334 linkPath := linkPathFromFilePath(linkObject.Path())
1335
Ivan Lozanoffee3342019-08-27 12:03:00 -07001336 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001337 switch {
1338 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001339 if cc.IsWholeStaticLib(depTag) {
1340 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1341 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001342 if mod.Binary() {
1343 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1344 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1345 // final linkage, pass the args directly to the linker to handle these cases.
1346 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1347 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001348 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001349 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001350 } else {
1351 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 -05001352 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001353 }
1354
1355 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1356 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001357 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001358 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1359
Colin Cross0de8a1e2020-09-18 14:15:30 -07001360 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1361 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1362 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1363 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1364 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001365 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001366
1367 // Record baseLibName for snapshots.
1368 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1369
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001370 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001371 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001372 // For the shared lib dependencies, we may link to the stub variant
1373 // of the dependency depending on the context (e.g. if this
1374 // dependency crosses the APEX boundaries).
1375 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1376
1377 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1378 // object (either from stub or non-stub) to use.
1379 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001380 if !linkObject.Valid() {
1381 if !ctx.Config().AllowMissingDependencies() {
1382 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1383 } else {
1384 ctx.AddMissingDependencies([]string{depName})
1385 }
1386 return
1387 }
Jiyong Park7d55b612021-06-11 17:22:09 +09001388 linkPath = linkPathFromFilePath(linkObject.Path())
1389
Ivan Lozanoffee3342019-08-27 12:03:00 -07001390 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001391 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001392 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1393 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1394 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1395 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001396 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001397
1398 // Record baseLibName for snapshots.
1399 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1400
Cole Faustb6e6f992023-08-17 17:42:26 -07001401 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001402 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001403 case cc.IsHeaderDepTag(depTag):
1404 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1405 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1406 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1407 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001408 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001409 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001410 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001411 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001412 }
1413
1414 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001415 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1416 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001417 if linkObject.Valid() {
1418 lib.exportLinkObjects(linkObject.Path())
1419 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001420 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001421 } else {
1422 switch {
1423 case depTag == cc.CrtBeginDepTag:
1424 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1425 case depTag == cc.CrtEndDepTag:
1426 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1427 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001428 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001429
1430 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001431 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001432 // These are usually genrules which don't have per-target variants.
1433 directSrcDeps = append(directSrcDeps, srcDep)
1434 }
1435 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001436 })
1437
Cole Faustb6e6f992023-08-17 17:42:26 -07001438 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1439
Ivan Lozanoffee3342019-08-27 12:03:00 -07001440 var rlibDepFiles RustLibraries
1441 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001442 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443 }
1444 var dylibDepFiles RustLibraries
1445 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001446 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 }
1448 var procMacroDepFiles RustLibraries
1449 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001450 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001451 }
1452
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001453 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001454 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001455 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001456 }
1457
Ivan Lozanoffee3342019-08-27 12:03:00 -07001458 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001459 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001460 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001461 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001462 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001463 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001464 }
1465
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001466 var srcProviderDepFiles android.Paths
1467 for _, dep := range directSrcProvidersDeps {
1468 srcs, _ := dep.OutputFiles("")
1469 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1470 }
1471 for _, dep := range directSrcDeps {
1472 srcs := dep.Srcs()
1473 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1474 }
1475
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1477 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001478 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001479 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001480 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001481
1482 // Dedup exported flags from dependencies
1483 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001484 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001485 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001486 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1487 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1488 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001489
1490 return depPaths
1491}
1492
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001493func (mod *Module) InstallInData() bool {
1494 if mod.compiler == nil {
1495 return false
1496 }
1497 return mod.compiler.inData()
1498}
1499
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001500func (mod *Module) InstallInRamdisk() bool {
1501 return mod.InRamdisk()
1502}
1503
1504func (mod *Module) InstallInVendorRamdisk() bool {
1505 return mod.InVendorRamdisk()
1506}
1507
1508func (mod *Module) InstallInRecovery() bool {
1509 return mod.InRecovery()
1510}
1511
Ivan Lozanoffee3342019-08-27 12:03:00 -07001512func linkPathFromFilePath(filepath android.Path) string {
1513 return strings.Split(filepath.String(), filepath.Base())[0]
1514}
Ivan Lozanod648c432020-02-06 12:05:10 -05001515
Spandan Das604f3762023-03-16 22:51:40 +00001516// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1517func (r *Module) usePublicApi() bool {
1518 return r.Device() && r.UseSdk()
1519}
1520
1521// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1522func (r *Module) useVendorApi() bool {
1523 return r.Device() && (r.InVendor() || r.InProduct())
1524}
1525
Ivan Lozanoffee3342019-08-27 12:03:00 -07001526func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1527 ctx := &depsContext{
1528 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001529 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001530
1531 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001532 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001533 var snapshotInfo *cc.SnapshotInfo
1534
Kiyoung Kim487689e2022-07-26 09:48:22 +09001535 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001536 if mod.usePublicApi() || mod.useVendorApi() {
1537 for idx, lib := range deps.SharedLibs {
1538 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1539 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001540 }
1541
Ivan Lozano1921e802021-05-20 13:39:16 -04001542 if ctx.Os() == android.Android {
1543 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1544 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001545
Ivan Lozano2b081132020-09-08 12:46:52 -04001546 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001547 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001548 stdLinkage = "rlib-std"
1549 }
1550
1551 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001552
Ivan Lozano2b081132020-09-08 12:46:52 -04001553 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1554 rlibDepVariations = append(rlibDepVariations,
1555 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1556 }
1557
Ivan Lozano1921e802021-05-20 13:39:16 -04001558 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001559 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001560 for _, lib := range deps.Rlibs {
1561 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001562 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001563
Ivan Lozano2d407632022-04-07 12:59:11 -04001564 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001565 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001566
1567 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001568 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1569 for _, lib := range deps.Dylibs {
1570 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1571 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001572
Ivan Lozano1921e802021-05-20 13:39:16 -04001573 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001574 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1575 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001576 for _, lib := range deps.Rustlibs {
1577 if autoDep.depTag == rlibDepTag {
1578 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001579 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001580 } else {
1581 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1582 // Check for the existence of the dylib deptag variant. Select it if available,
1583 // otherwise select the rlib variant.
1584 autoDepVariations := append(commonDepVariations,
1585 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001586
1587 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1588
1589 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1590 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001591 } else {
1592 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001593 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001594 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001595 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001596 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001597 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001598 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001599 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001600 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001601 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001602 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001603 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001604 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001605 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001606 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001607 for _, lib := range deps.Stdlibs {
1608 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1609 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001610 }
1611 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001612
1613 for _, lib := range deps.SharedLibs {
1614 depTag := cc.SharedDepTag()
1615 name, version := cc.StubsLibNameAndVersion(lib)
1616
1617 variations := []blueprint.Variation{
1618 {Mutator: "link", Variation: "shared"},
1619 }
Spandan Das604f3762023-03-16 22:51:40 +00001620 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1621 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1622 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1623 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1624 }
1625
1626 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1627 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1628 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001629 }
1630
1631 for _, lib := range deps.WholeStaticLibs {
1632 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001633 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001634
1635 actx.AddVariationDependencies([]blueprint.Variation{
1636 {Mutator: "link", Variation: "static"},
1637 }, depTag, lib)
1638 }
1639
1640 for _, lib := range deps.StaticLibs {
1641 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001642 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001643
1644 actx.AddVariationDependencies([]blueprint.Variation{
1645 {Mutator: "link", Variation: "static"},
1646 }, depTag, lib)
1647 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001648
Zach Johnson3df4e632020-11-06 11:56:27 -08001649 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1650
Colin Cross565cafd2020-09-25 18:47:38 -07001651 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001652 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001653 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001654 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001655 }
Colin Crossfe605e12022-01-23 20:46:16 -08001656 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001657 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001658 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001659 }
1660
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001661 if mod.sourceProvider != nil {
1662 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1663 bindgen.Properties.Custom_bindgen != "" {
1664 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1665 bindgen.Properties.Custom_bindgen)
1666 }
1667 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001668
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001669 actx.AddVariationDependencies([]blueprint.Variation{
1670 {Mutator: "link", Variation: "shared"},
1671 }, dataLibDepTag, deps.DataLibs...)
1672
1673 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1674
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001675 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001676 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001677
1678 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001679}
1680
Ivan Lozano2d407632022-04-07 12:59:11 -04001681// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001682func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1683 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001684 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1685}
1686
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001687func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1688 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1689 actx.AddVariationDependencies(variations, depTag, lib)
1690}
1691
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001692func BeginMutator(ctx android.BottomUpMutatorContext) {
1693 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1694 mod.beginMutator(ctx)
1695 }
1696}
1697
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001698func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1699 ctx := &baseModuleContext{
1700 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001701 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001702
1703 mod.begin(ctx)
1704}
1705
Ivan Lozanoffee3342019-08-27 12:03:00 -07001706func (mod *Module) Name() string {
1707 name := mod.ModuleBase.Name()
1708 if p, ok := mod.compiler.(interface {
1709 Name(string) string
1710 }); ok {
1711 name = p.Name(name)
1712 }
1713 return name
1714}
1715
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001716func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001717 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001718 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001719 }
1720}
1721
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001722var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001723var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001724
1725func (mod *Module) HostToolPath() android.OptionalPath {
1726 if !mod.Host() {
1727 return android.OptionalPath{}
1728 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001729 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1730 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001731 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1732 // Even though proc-macros aren't strictly "tools", since they target the compiler
1733 // and act as compiler plugins, we treat them similarly.
1734 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001735 }
1736 return android.OptionalPath{}
1737}
1738
Jiyong Park99644e92020-11-17 22:21:02 +09001739var _ android.ApexModule = (*Module)(nil)
1740
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001741func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001742 return String(mod.Properties.Min_sdk_version)
1743}
1744
Jiyong Park45bf82e2020-12-15 22:29:02 +09001745// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001746func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001747 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001748 if minSdkVersion == "apex_inherit" {
1749 return nil
1750 }
1751 if minSdkVersion == "" {
1752 return fmt.Errorf("min_sdk_version is not specificed")
1753 }
1754
1755 // Not using nativeApiLevelFromUser because the context here is not
1756 // necessarily a native context.
1757 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1758 if err != nil {
1759 return err
1760 }
1761
1762 if ver.GreaterThan(sdkVersion) {
1763 return fmt.Errorf("newer SDK(%v)", ver)
1764 }
Jiyong Park99644e92020-11-17 22:21:02 +09001765 return nil
1766}
1767
Jiyong Park45bf82e2020-12-15 22:29:02 +09001768// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001769func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1770 depTag := ctx.OtherModuleDependencyTag(dep)
1771
1772 if ccm, ok := dep.(*cc.Module); ok {
1773 if ccm.HasStubsVariants() {
1774 if cc.IsSharedDepTag(depTag) {
1775 // dynamic dep to a stubs lib crosses APEX boundary
1776 return false
1777 }
1778 if cc.IsRuntimeDepTag(depTag) {
1779 // runtime dep to a stubs lib also crosses APEX boundary
1780 return false
1781 }
1782
1783 if cc.IsHeaderDepTag(depTag) {
1784 return false
1785 }
1786 }
1787 if mod.Static() && cc.IsSharedDepTag(depTag) {
1788 // shared_lib dependency from a static lib is considered as crossing
1789 // the APEX boundary because the dependency doesn't actually is
1790 // linked; the dependency is used only during the compilation phase.
1791 return false
1792 }
1793 }
1794
Matthew Maurer581b6d82022-09-29 16:46:25 -07001795 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001796 return false
1797 }
1798
1799 return true
1800}
1801
1802// Overrides ApexModule.IsInstallabeToApex()
1803func (mod *Module) IsInstallableToApex() bool {
1804 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001805 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001806 return true
1807 }
1808 if _, ok := mod.compiler.(*binaryDecorator); ok {
1809 return true
1810 }
1811 }
1812 return false
1813}
1814
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001815// If a library file has a "lib" prefix, extract the library name without the prefix.
1816func libNameFromFilePath(filepath android.Path) (string, bool) {
1817 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1818 if strings.HasPrefix(libName, "lib") {
1819 libName = libName[3:]
1820 return libName, true
1821 }
1822 return "", false
1823}
1824
Sasha Smundaka76acba2022-04-18 20:12:56 -07001825func kytheExtractRustFactory() android.Singleton {
1826 return &kytheExtractRustSingleton{}
1827}
1828
1829type kytheExtractRustSingleton struct {
1830}
1831
1832func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1833 var xrefTargets android.Paths
1834 ctx.VisitAllModules(func(module android.Module) {
1835 if rustModule, ok := module.(xref); ok {
1836 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1837 }
1838 })
1839 if len(xrefTargets) > 0 {
1840 ctx.Phony("xref_rust", xrefTargets...)
1841 }
1842}
1843
Jihoon Kangf78a8902022-09-01 22:47:07 +00001844func (c *Module) Partition() string {
1845 return ""
1846}
1847
Vinh Tranbcb5f572023-08-24 11:10:01 -04001848func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
1849 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1850 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001851 } else if ctx.ModuleType() == "rust_proc_macro" {
1852 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001853 } else if ctx.ModuleType() == "rust_binary_host" {
1854 binaryBp2build(ctx, m)
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001855 } else if ctx.ModuleType() == "rust_protobuf_host" {
1856 protoLibraryBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001857 } else {
1858 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1859 }
1860}
1861
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001862// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001863// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001864// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1865// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1866// instead of using globs.
1867func srcsAndCompileDataAttrs(ctx android.TopDownMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
1868 var srcs bazel.LabelList
1869 var compileData bazel.LabelList
1870
1871 if c.Properties.Srcs[0] == "src/lib.rs" {
1872 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1873 compileData = android.BazelLabelForModuleSrc(
1874 ctx,
1875 []string{
1876 "src/**/*.proto",
1877 "examples/**/*.rs",
1878 "**/*.md",
1879 },
1880 )
1881 } else {
1882 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1883 }
1884
1885 return srcs, compileData
1886}
1887
Ivan Lozanoffee3342019-08-27 12:03:00 -07001888var Bool = proptools.Bool
1889var BoolDefault = proptools.BoolDefault
1890var String = proptools.String
1891var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001892
1893var _ android.OutputFileProducer = (*Module)(nil)