blob: 6d6b55efeac2128cbba9aad5a5ae7e95453c053d [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
19 "strings"
20
Vinh Tranb4bb20f2023-08-24 11:10:01 -040021 "android/soong/bazel"
Sasha Smundaka76acba2022-04-18 20:12:56 -070022 "android/soong/bloaty"
Vinh Tranbcb5f572023-08-24 11:10:01 -040023 "android/soong/ui/metrics/bp2build_metrics_proto"
Ivan Lozanoffee3342019-08-27 12:03:00 -070024
25 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020030 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070031 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000032 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040034 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070035)
36
37var pctx = android.NewPackageContext("android/soong/rust")
38
39func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070040 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050045 })
46 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
47 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070048 })
Inseob Kim3b244062023-07-11 13:31:36 +090049 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070050 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020051 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000052 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
55type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040056 GlobalRustFlags []string // Flags that apply globally to rust
57 GlobalLinkFlags []string // Flags that apply globally to linker
58 RustFlags []string // Flags that apply to rust
59 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070061 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070062 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020064 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070065 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050069 AndroidMkRlibs []string `blueprint:"mutated"`
70 AndroidMkDylibs []string `blueprint:"mutated"`
71 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050072 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040073
Ivan Lozano6a884432020-12-02 09:15:16 -050074 ImageVariationPrefix string `blueprint:"mutated"`
75 VndkVersion string `blueprint:"mutated"`
76 SubName string `blueprint:"mutated"`
77
Ivan Lozanoc08897c2021-04-02 12:41:32 -040078 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
79 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
80 // appended before SubName.
81 RustSubName string `blueprint:"mutated"`
82
Ivan Lozano6a884432020-12-02 09:15:16 -050083 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050084 CoreVariantNeeded bool `blueprint:"mutated"`
85 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070086 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080087 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050088 ExtraVariants []string `blueprint:"mutated"`
89
Ivan Lozanoa2268632021-07-22 10:52:06 -040090 // Allows this module to use non-APEX version of libraries. Useful
91 // for building binaries that are started before APEXes are activated.
92 Bootstrap *bool
93
Ivan Lozano1921e802021-05-20 13:39:16 -040094 // Used by vendor snapshot to record dependencies from snapshot modules.
95 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090096 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040097 SnapshotRlibs []string `blueprint:"mutated"`
98 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040099
Matthew Maurerc6868382021-07-13 14:12:37 -0700100 // Make this module available when building for ramdisk.
101 // On device without a dedicated recovery partition, the module is only
102 // available after switching root into
103 // /first_stage_ramdisk. To expose the module before switching root, install
104 // the recovery variant instead.
105 Ramdisk_available *bool
106
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500107 // Make this module available when building for vendor ramdisk.
108 // On device without a dedicated recovery partition, the module is only
109 // available after switching root into
110 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800111 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500112 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400113
Ivan Lozano1921e802021-05-20 13:39:16 -0400114 // Normally Soong uses the directory structure to decide which modules
115 // should be included (framework) or excluded (non-framework) from the
116 // different snapshots (vendor, recovery, etc.), but this property
117 // allows a partner to exclude a module normally thought of as a
118 // framework module from the vendor snapshot.
119 Exclude_from_vendor_snapshot *bool
120
121 // Normally Soong uses the directory structure to decide which modules
122 // should be included (framework) or excluded (non-framework) from the
123 // different snapshots (vendor, recovery, etc.), but this property
124 // allows a partner to exclude a module normally thought of as a
125 // framework module from the recovery snapshot.
126 Exclude_from_recovery_snapshot *bool
127
Matthew Maurer460ee942021-02-11 12:31:46 -0800128 // Make this module available when building for recovery
129 Recovery_available *bool
130
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500131 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
132 Min_sdk_version *string
133
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900134 HideFromMake bool `blueprint:"mutated"`
135 PreventInstall bool `blueprint:"mutated"`
136
137 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138}
139
140type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700141 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142
Ivan Lozano6a884432020-12-02 09:15:16 -0500143 VendorProperties cc.VendorProperties
144
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 Properties BaseProperties
146
147 hod android.HostOrDeviceSupported
148 multilib android.Multilib
149
Ivan Lozano6a884432020-12-02 09:15:16 -0500150 makeLinkType string
151
Yi Kong46c6e592022-01-20 22:55:00 +0800152 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400154 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200155 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500156 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700159 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400160
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400161 // Output file to be installed, may be stripped or unstripped.
162 outputFile android.OptionalPath
163
Sasha Smundaka76acba2022-04-18 20:12:56 -0700164 // Cross-reference input file
165 kytheFiles android.Paths
166
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400167 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900168
169 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500170
171 // For apex variants, this is set as apex.min_sdk_version
172 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700173
174 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400175
176 android.BazelModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177}
178
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179func (mod *Module) Header() bool {
180 //TODO: If Rust libraries provide header variants, this needs to be updated.
181 return false
182}
183
184func (mod *Module) SetPreventInstall() {
185 mod.Properties.PreventInstall = true
186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SetHideFromMake() {
189 mod.Properties.HideFromMake = true
190}
191
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900192func (mod *Module) HiddenFromMake() bool {
193 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400194}
195
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500196func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500197 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
198 // nil since we need compiler to actually sanitize.
199 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500200}
201
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (mod *Module) IsPrebuilt() bool {
203 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
204 return true
205 }
206 return false
207}
208
Ivan Lozano43845682020-07-09 21:03:28 -0400209func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
210 switch tag {
211 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700212 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400213 return mod.sourceProvider.Srcs(), nil
214 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900215 if mod.OutputFile().Valid() {
216 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400217 }
218 return android.Paths{}, nil
219 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500220 case "unstripped":
221 if mod.compiler != nil {
222 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
223 }
224 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400225 default:
226 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
227 }
228}
229
Ivan Lozano52767be2019-10-18 14:49:46 -0700230func (mod *Module) SelectedStl() string {
231 return ""
232}
233
Ivan Lozano2b262972019-11-21 12:30:50 -0800234func (mod *Module) NonCcVariants() bool {
235 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400236 if _, ok := mod.compiler.(libraryInterface); ok {
237 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800238 }
239 }
240 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
241}
242
Ivan Lozano52767be2019-10-18 14:49:46 -0700243func (mod *Module) Static() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
246 return library.static()
247 }
248 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700250}
251
252func (mod *Module) Shared() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400255 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700256 }
257 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400258 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700259}
260
Ivan Lozanod7586b62021-04-01 09:49:36 -0400261func (mod *Module) Dylib() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok {
264 return library.dylib()
265 }
266 }
267 return false
268}
269
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400270func (mod *Module) RlibStd() bool {
271 if mod.compiler != nil {
272 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
273 return library.rlibStd()
274 }
275 }
276 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
277}
278
Ivan Lozanod7586b62021-04-01 09:49:36 -0400279func (mod *Module) Rlib() bool {
280 if mod.compiler != nil {
281 if library, ok := mod.compiler.(libraryInterface); ok {
282 return library.rlib()
283 }
284 }
285 return false
286}
287
288func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400289 if binary, ok := mod.compiler.(binaryInterface); ok {
290 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400291 }
292 return false
293}
294
Justin Yun5e035862021-06-29 20:50:37 +0900295func (mod *Module) StaticExecutable() bool {
296 if !mod.Binary() {
297 return false
298 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400299 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900300}
301
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302func (mod *Module) Object() bool {
303 // Rust has no modules which produce only object files.
304 return false
305}
306
Ivan Lozano52767be2019-10-18 14:49:46 -0700307func (mod *Module) Toc() android.OptionalPath {
308 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400309 if lib, ok := mod.compiler.(libraryInterface); ok {
310 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700311 }
312 }
313 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
314}
315
Colin Crossc511bc52020-04-07 16:50:32 +0000316func (mod *Module) UseSdk() bool {
317 return false
318}
319
Ivan Lozanod7586b62021-04-01 09:49:36 -0400320func (mod *Module) RelativeInstallPath() string {
321 if mod.compiler != nil {
322 return mod.compiler.relativeInstallPath()
323 }
324 return ""
325}
326
Ivan Lozano52767be2019-10-18 14:49:46 -0700327func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500328 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700329}
330
Jiyong Park7d55b612021-06-11 17:22:09 +0900331func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400332 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900333}
334
Ivan Lozano52767be2019-10-18 14:49:46 -0700335func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400336 return true
337}
338
339func (mod *Module) SubName() string {
340 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700341}
342
343func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500344 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700345 return false
346}
347
Ivan Lozanof9e21722020-12-02 09:00:51 -0500348func (mod *Module) IsVndkExt() bool {
349 return false
350}
351
Ivan Lozanod7586b62021-04-01 09:49:36 -0400352func (mod *Module) IsVndkSp() bool {
353 return false
354}
355
Ivan Lozanof1868af2022-04-12 13:08:36 -0400356func (mod *Module) IsVndkPrebuiltLibrary() bool {
357 // Rust modules do not provide VNDK prebuilts
358 return false
359}
360
361func (mod *Module) IsVendorPublicLibrary() bool {
362 return mod.VendorProperties.IsVendorPublicLibrary
363}
364
365func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
366 // Rust modules to not provide Sdk variants
367 return false
368}
369
Colin Cross127bb8b2020-12-16 16:46:01 -0800370func (c *Module) IsVndkPrivate() bool {
371 return false
372}
373
374func (c *Module) IsLlndk() bool {
375 return false
376}
377
378func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500379 return false
380}
381
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400382func (mod *Module) KernelHeadersDecorator() bool {
383 return false
384}
385
Colin Cross1f3f1302021-04-26 18:37:44 -0700386func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400387 return false
388}
389
Colin Cross5271fea2021-04-27 13:06:04 -0700390func (m *Module) NeedsVendorPublicLibraryVariants() bool {
391 return false
392}
393
Ivan Lozanod7586b62021-04-01 09:49:36 -0400394func (mod *Module) HasLlndkStubs() bool {
395 return false
396}
397
398func (mod *Module) StubsVersion() string {
399 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
400}
401
Ivan Lozano52767be2019-10-18 14:49:46 -0700402func (mod *Module) SdkVersion() string {
403 return ""
404}
405
Colin Crossc511bc52020-04-07 16:50:32 +0000406func (mod *Module) AlwaysSdk() bool {
407 return false
408}
409
Jiyong Park2286afd2020-06-16 21:58:53 +0900410func (mod *Module) IsSdkVariant() bool {
411 return false
412}
413
Colin Cross1348ce32020-10-01 13:37:16 -0700414func (mod *Module) SplitPerApiLevel() bool {
415 return false
416}
417
Sasha Smundaka76acba2022-04-18 20:12:56 -0700418func (mod *Module) XrefRustFiles() android.Paths {
419 return mod.kytheFiles
420}
421
Ivan Lozanoffee3342019-08-27 12:03:00 -0700422type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400423 Dylibs []string
424 Rlibs []string
425 Rustlibs []string
426 Stdlibs []string
427 ProcMacros []string
428 SharedLibs []string
429 StaticLibs []string
430 WholeStaticLibs []string
431 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400433 // Used for data dependencies adjacent to tests
434 DataLibs []string
435 DataBins []string
436
Colin Crossfe605e12022-01-23 20:46:16 -0800437 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438}
439
440type PathDeps struct {
Sam Delmericoa588d152023-06-16 10:28:04 -0400441 Dylibs *android.DepSet[RustLibrary]
442 Rlibs *android.DepSet[RustLibrary]
443 ProcMacros *android.DepSet[RustLibrary]
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700444 LibDeps android.Paths
445 WholeStaticLibs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700446 AfdoProfiles android.Paths
Sam Delmericoa588d152023-06-16 10:28:04 -0400447 // These paths are files needed to run the build tools and will be located under
448 // __SBOX_SANDBOX_DIR__/tools/...
449 BuildToolDeps android.Paths
450 // These paths are files needed to run the build tools and will be located under
451 // __SBOX_SANDBOX_DIR__/...
452 BuildToolSrcDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500453
454 // depFlags and depLinkFlags are rustc and linker (clang) flags.
455 depFlags []string
456 depLinkFlags []string
457
458 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
459 // Both of these are exported and propagate to dependencies.
Sam Delmericoa588d152023-06-16 10:28:04 -0400460 linkDirs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700461 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700462
Ivan Lozano45901ed2020-07-24 16:05:01 -0400463 // Used by bindgen modules which call clang
464 depClangFlags []string
465 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400466 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400467 depSystemIncludePaths android.Paths
468
Colin Crossfe605e12022-01-23 20:46:16 -0800469 CrtBegin android.Paths
470 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700471
472 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500473 SrcDeps android.Paths
474 srcProviderFiles android.Paths
Colin Cross05cbcb32023-09-22 22:02:28 +0000475
476 // Paths to specific build tools
477 Rustc android.Path
478 Clang android.Path
479 Llvm_ar android.Path
480 Clippy_driver android.Path
481 Rustdoc android.Path
Ivan Lozanoffee3342019-08-27 12:03:00 -0700482}
483
484type RustLibraries []RustLibrary
485
486type RustLibrary struct {
487 Path android.Path
488 CrateName string
489}
490
491type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100492 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700493 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400494 cfgFlags(ctx ModuleContext, flags Flags) Flags
495 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700496 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700497 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498 compilerDeps(ctx DepsContext, deps Deps) Deps
499 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700500 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Sam Delmericoa588d152023-06-16 10:28:04 -0400501 crateRoot(ctx ModuleContext) android.Path
502 compilationSourcesAndData(ctx ModuleContext) android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700503
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100504 // Output directory in which source-generated code from dependencies is
505 // copied. This is equivalent to Cargo's OUT_DIR variable.
506 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700507
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400508 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
509 CargoPkgVersion() string
510
511 // CargoEnvCompat returns whether Cargo environment variables should be used.
512 CargoEnvCompat() bool
513
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800514 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200515 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700516 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400517 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400518
519 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400520
521 Disabled() bool
522 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400523
Ivan Lozanodd055472020-09-28 13:22:45 -0400524 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500525 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900526
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400527 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900528 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400529}
530
Matthew Maurerbb3add12020-06-25 09:34:12 -0700531type exportedFlagsProducer interface {
Sam Delmericoa588d152023-06-16 10:28:04 -0400532 exportLinkDirs(...android.Path)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700533 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700534}
535
Sasha Smundaka76acba2022-04-18 20:12:56 -0700536type xref interface {
537 XrefRustFiles() android.Paths
538}
539
Matthew Maurerbb3add12020-06-25 09:34:12 -0700540type flagExporter struct {
Sam Delmericoa588d152023-06-16 10:28:04 -0400541 linkDirs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700542 linkObjects android.Paths
543 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700544}
545
Sam Delmericoa588d152023-06-16 10:28:04 -0400546func (flagExporter *flagExporter) exportLinkDirs(dirs ...android.Path) {
547 flagExporter.linkDirs = android.FirstUniquePaths(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700548}
549
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700550func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
551 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
552}
553
554func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
555 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400556}
557
Colin Cross0de8a1e2020-09-18 14:15:30 -0700558func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
559 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700560 LinkDirs: flagExporter.linkDirs,
561 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700562 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700563 })
564}
565
Matthew Maurerbb3add12020-06-25 09:34:12 -0700566var _ exportedFlagsProducer = (*flagExporter)(nil)
567
568func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700569 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700570}
571
Colin Cross0de8a1e2020-09-18 14:15:30 -0700572type FlagExporterInfo struct {
573 Flags []string
Sam Delmericoa588d152023-06-16 10:28:04 -0400574 LinkDirs android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700575 LinkObjects android.Paths
576 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700577}
578
579var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
580
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400581func (mod *Module) isCoverageVariant() bool {
582 return mod.coverage.Properties.IsCoverageVariant
583}
584
585var _ cc.Coverage = (*Module)(nil)
586
587func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
588 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
589}
590
Ivan Lozanod7586b62021-04-01 09:49:36 -0400591func (mod *Module) VndkVersion() string {
592 return mod.Properties.VndkVersion
593}
594
595func (mod *Module) PreventInstall() bool {
596 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400597}
598
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400599func (mod *Module) MarkAsCoverageVariant(coverage bool) {
600 mod.coverage.Properties.IsCoverageVariant = coverage
601}
602
603func (mod *Module) EnableCoverageIfNeeded() {
604 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605}
606
607func defaultsFactory() android.Module {
608 return DefaultsFactory()
609}
610
611type Defaults struct {
612 android.ModuleBase
613 android.DefaultsModuleBase
614}
615
616func DefaultsFactory(props ...interface{}) android.Module {
617 module := &Defaults{}
618
619 module.AddProperties(props...)
620 module.AddProperties(
621 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800622 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500623 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100624 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400625 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700626 &BaseCompilerProperties{},
627 &BinaryCompilerProperties{},
628 &LibraryCompilerProperties{},
629 &ProcMacroCompilerProperties{},
630 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400631 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700632 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400633 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400634 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200635 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500636 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000637 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700638 )
639
640 android.InitDefaultsModule(module)
641 return module
642}
643
644func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700645 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700646}
647
Ivan Lozano183a3212019-10-18 14:18:45 -0700648func (mod *Module) CcLibrary() bool {
649 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500650 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700651 return true
652 }
653 }
654 return false
655}
656
657func (mod *Module) CcLibraryInterface() bool {
658 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400659 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
660 // VariantIs{Static,Shared} is set.
661 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700662 return true
663 }
664 }
665 return false
666}
667
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400668func (mod *Module) RustLibraryInterface() bool {
669 if mod.compiler != nil {
670 if _, ok := mod.compiler.(libraryInterface); ok {
671 return true
672 }
673 }
674 return false
675}
676
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500677func (mod *Module) IsFuzzModule() bool {
678 if _, ok := mod.compiler.(*fuzzDecorator); ok {
679 return true
680 }
681 return false
682}
683
684func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
685 return mod.FuzzModule
686}
687
688func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
689 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
690 return fuzzer.fuzzPackagedModule
691 }
692 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
693}
694
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000695func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500696 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
697 return fuzzer.sharedLibraries
698 }
699 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
700}
701
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400702func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400703 if mod.compiler != nil {
704 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400705 }
706 return nil
707}
708
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800709func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700710 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700711 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800712 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700713 }
714 }
715 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
716}
717
718func (mod *Module) SetStatic() {
719 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700720 if library, ok := mod.compiler.(libraryInterface); ok {
721 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700722 return
723 }
724 }
725 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
726}
727
728func (mod *Module) SetShared() {
729 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700730 if library, ok := mod.compiler.(libraryInterface); ok {
731 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700732 return
733 }
734 }
735 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
736}
737
Ivan Lozano183a3212019-10-18 14:18:45 -0700738func (mod *Module) BuildStaticVariant() bool {
739 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700740 if library, ok := mod.compiler.(libraryInterface); ok {
741 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700742 }
743 }
744 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
745}
746
747func (mod *Module) BuildSharedVariant() bool {
748 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700749 if library, ok := mod.compiler.(libraryInterface); ok {
750 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700751 }
752 }
753 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
754}
755
Ivan Lozano183a3212019-10-18 14:18:45 -0700756func (mod *Module) Module() android.Module {
757 return mod
758}
759
Ivan Lozano183a3212019-10-18 14:18:45 -0700760func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400761 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700762}
763
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400764func (mod *Module) CoverageFiles() android.Paths {
765 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800766 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400767 }
768 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
769}
770
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400771// Rust does not produce gcno files, and therefore does not produce a coverage archive.
772func (mod *Module) CoverageOutputFile() android.OptionalPath {
773 return android.OptionalPath{}
774}
775
776func (mod *Module) IsNdk(config android.Config) bool {
777 return false
778}
779
780func (mod *Module) IsStubs() bool {
781 return false
782}
783
Jiyong Park459feca2020-12-15 11:02:21 +0900784func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900785 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900786 return false
787 }
788
Jiyong Park459feca2020-12-15 11:02:21 +0900789 // The apex variant is not installable because it is included in the APEX and won't appear
790 // in the system partition as a standalone file.
791 if !apexInfo.IsForPlatform() {
792 return false
793 }
794
Jiyong Parke54f07e2021-04-07 15:08:04 +0900795 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900796}
797
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500798func (ctx moduleContext) apexVariationName() string {
799 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
800}
801
Ivan Lozano183a3212019-10-18 14:18:45 -0700802var _ cc.LinkableInterface = (*Module)(nil)
803
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804func (mod *Module) Init() android.Module {
805 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500806 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807
Yi Kong46c6e592022-01-20 22:55:00 +0800808 if mod.afdo != nil {
809 mod.AddProperties(mod.afdo.props()...)
810 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 if mod.compiler != nil {
812 mod.AddProperties(mod.compiler.compilerProps()...)
813 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400814 if mod.coverage != nil {
815 mod.AddProperties(mod.coverage.props()...)
816 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200817 if mod.clippy != nil {
818 mod.AddProperties(mod.clippy.props()...)
819 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400820 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700821 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400822 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500823 if mod.sanitize != nil {
824 mod.AddProperties(mod.sanitize.props()...)
825 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400826
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900828 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829
830 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831 return mod
832}
833
834func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
835 return &Module{
836 hod: hod,
837 multilib: multilib,
838 }
839}
840func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
841 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800842 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400843 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200844 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500845 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 return module
847}
848
849type ModuleContext interface {
850 android.ModuleContext
851 ModuleContextIntf
852}
853
854type BaseModuleContext interface {
855 android.BaseModuleContext
856 ModuleContextIntf
857}
858
859type DepsContext interface {
860 android.BottomUpMutatorContext
861 ModuleContextIntf
862}
863
864type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200865 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700867}
868
869type depsContext struct {
870 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700871}
872
873type moduleContext struct {
874 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700875}
876
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200877type baseModuleContext struct {
878 android.BaseModuleContext
879}
880
881func (ctx *moduleContext) RustModule() *Module {
882 return ctx.Module().(*Module)
883}
884
885func (ctx *moduleContext) toolchain() config.Toolchain {
886 return ctx.RustModule().toolchain(ctx)
887}
888
889func (ctx *depsContext) RustModule() *Module {
890 return ctx.Module().(*Module)
891}
892
893func (ctx *depsContext) toolchain() config.Toolchain {
894 return ctx.RustModule().toolchain(ctx)
895}
896
897func (ctx *baseModuleContext) RustModule() *Module {
898 return ctx.Module().(*Module)
899}
900
901func (ctx *baseModuleContext) toolchain() config.Toolchain {
902 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400903}
904
905func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700906 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
907 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
908 return false
909 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400910 return mod.compiler != nil && mod.compiler.nativeCoverage()
911}
912
Ivan Lozanod7586b62021-04-01 09:49:36 -0400913func (mod *Module) EverInstallable() bool {
914 return mod.compiler != nil &&
915 // Check to see whether the module is actually ever installable.
916 mod.compiler.everInstallable()
917}
918
919func (mod *Module) Installable() *bool {
920 return mod.Properties.Installable
921}
922
Ivan Lozano872d5792022-03-23 17:31:39 -0400923func (mod *Module) ProcMacro() bool {
924 if pm, ok := mod.compiler.(procMacroInterface); ok {
925 return pm.ProcMacro()
926 }
927 return false
928}
929
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
931 if mod.cachedToolchain == nil {
932 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
933 }
934 return mod.cachedToolchain
935}
936
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200937func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
938 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
939}
940
Ivan Lozanoffee3342019-08-27 12:03:00 -0700941func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
942}
943
Sam Delmericoa588d152023-06-16 10:28:04 -0400944type RustInfo struct {
945 TransitiveRlibs *android.DepSet[RustLibrary]
946 TransitiveDylibs *android.DepSet[RustLibrary]
947 TransitiveProcMacros *android.DepSet[RustLibrary]
948}
949
950var RustInfoProvider = blueprint.NewProvider(RustInfo{})
951
Ivan Lozanoffee3342019-08-27 12:03:00 -0700952func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
953 ctx := &moduleContext{
954 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956
Jiyong Park99644e92020-11-17 22:21:02 +0900957 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
958 if !apexInfo.IsForPlatform() {
959 mod.hideApexVariantFromMake = true
960 }
961
Ivan Lozanoffee3342019-08-27 12:03:00 -0700962 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500963 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
964
Ivan Lozanof1868af2022-04-12 13:08:36 -0400965 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700966
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967 if !toolchain.Supported() {
968 // This toolchain's unsupported, there's nothing to do for this mod.
969 return
970 }
971
972 deps := mod.depsToPaths(ctx)
973 flags := Flags{
974 Toolchain: toolchain,
975 }
976
Ivan Lozano67eada32021-09-23 11:50:33 -0400977 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800978 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500979 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800980 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700981 if mod.compiler != nil {
982 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400983 flags = mod.compiler.cfgFlags(ctx, flags)
984 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400985 }
986 if mod.coverage != nil {
987 flags, deps = mod.coverage.flags(ctx, flags, deps)
988 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200989 if mod.clippy != nil {
990 flags, deps = mod.clippy.flags(ctx, flags, deps)
991 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500992 if mod.sanitize != nil {
993 flags, deps = mod.sanitize.flags(ctx, flags, deps)
994 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400995
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200996 // SourceProvider needs to call GenerateSource() before compiler calls
997 // compile() so it can provide the source. A SourceProvider has
998 // multiple variants (e.g. source, rlib, dylib). Only the "source"
999 // variant is responsible for effectively generating the source. The
1000 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001001 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001002 if mod.compiler.(libraryInterface).source() {
1003 mod.sourceProvider.GenerateSource(ctx, deps)
1004 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1005 } else {
1006 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
1007 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -07001008 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001009 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001010 }
1011
1012 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001013 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001014 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001015 if ctx.Failed() {
1016 return
1017 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001018 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
1019 if buildOutput.kytheFile != nil {
1020 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1021 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001022 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001023
Dan Albert06feee92021-03-19 15:06:02 -07001024 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001025 if mod.docTimestampFile.Valid() {
1026 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1027 }
Dan Albert06feee92021-03-19 15:06:02 -07001028
Ivan Lozano1921e802021-05-20 13:39:16 -04001029 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1030 // RECOVERY_SNAPSHOT_VERSION is current.
1031 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1032 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1033 lib.collectHeadersForSnapshot(ctx, deps)
1034 }
1035 }
1036
Jiyong Park459feca2020-12-15 11:02:21 +09001037 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001038 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001039 // If the module has been specifically configure to not be installed then
1040 // hide from make as otherwise it will break when running inside make as the
1041 // output path to install will not be specified. Not all uninstallable
1042 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001043 // side dependencies. In particular, proc-macros need to be captured in the
1044 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001045 mod.HideFromMake()
1046 } else if !mod.installable(apexInfo) {
1047 mod.SkipInstall()
1048 }
1049
1050 // Still call install though, the installs will be stored as PackageSpecs to allow
1051 // using the outputs in a genrule.
1052 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001053 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001054 if ctx.Failed() {
1055 return
1056 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001057 }
Chris Wailes74be7642021-07-22 16:20:28 -07001058
1059 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001060 }
Sam Delmericoa588d152023-06-16 10:28:04 -04001061
1062 ctx.SetProvider(RustInfoProvider, RustInfo{
1063 TransitiveRlibs: deps.Rlibs,
1064 TransitiveDylibs: deps.Dylibs,
1065 TransitiveProcMacros: deps.ProcMacros,
1066 })
Ivan Lozanoffee3342019-08-27 12:03:00 -07001067}
1068
1069func (mod *Module) deps(ctx DepsContext) Deps {
1070 deps := Deps{}
1071
1072 if mod.compiler != nil {
1073 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001074 }
1075 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001076 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001077 }
1078
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001079 if mod.coverage != nil {
1080 deps = mod.coverage.deps(ctx, deps)
1081 }
1082
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001083 if mod.sanitize != nil {
1084 deps = mod.sanitize.deps(ctx, deps)
1085 }
1086
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1088 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001089 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001090 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1091 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1092 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001093 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001094 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001095 return deps
1096
1097}
1098
Ivan Lozanoffee3342019-08-27 12:03:00 -07001099type dependencyTag struct {
1100 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001101 name string
1102 library bool
1103 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001104 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001105}
1106
Jiyong Park65b62242020-11-25 12:44:59 +09001107// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1108// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1109func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001110 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001111}
1112
1113var _ android.InstallNeededDependencyTag = dependencyTag{}
1114
Colin Cross65cb3142021-12-10 23:05:02 +00001115func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1116 if d.library && d.dynamic {
1117 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1118 }
1119 return nil
1120}
1121
1122var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1123
Ivan Lozanoffee3342019-08-27 12:03:00 -07001124var (
Sam Delmericoa588d152023-06-16 10:28:04 -04001125 buildToolDepTag = dependencyTag{name: "buildToolTag"}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001126 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1127 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001128 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001129 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001130 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001131 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001132 dataLibDepTag = dependencyTag{name: "data lib"}
1133 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134)
1135
Jiyong Park99644e92020-11-17 22:21:02 +09001136func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1137 tag, ok := depTag.(dependencyTag)
1138 return ok && tag == dylibDepTag
1139}
1140
Jiyong Park94e22fd2021-04-08 18:19:15 +09001141func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1142 tag, ok := depTag.(dependencyTag)
1143 return ok && tag == rlibDepTag
1144}
1145
Matthew Maurer0f003b12020-06-29 14:34:06 -07001146type autoDep struct {
1147 variation string
1148 depTag dependencyTag
1149}
1150
1151var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001152 rlibVariation = "rlib"
1153 dylibVariation = "dylib"
1154 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1155 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001156)
1157
1158type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001159 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001160}
1161
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001162func (mod *Module) begin(ctx BaseModuleContext) {
1163 if mod.coverage != nil {
1164 mod.coverage.begin(ctx)
1165 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001166 if mod.sanitize != nil {
1167 mod.sanitize.begin(ctx)
1168 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001169}
1170
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001171func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001172 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001173 return p.prebuilt()
1174 }
1175 return nil
1176}
1177
Justin Yun24b246a2023-03-16 10:36:16 +09001178func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1179 if rustDep, ok := dep.(*Module); ok {
1180 // Use base module name for snapshots when exporting to Makefile.
1181 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1182 baseName := rustDep.BaseModuleName()
1183 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1184 }
1185 }
1186 return cc.MakeLibName(ctx, c, dep, depName)
1187}
1188
Ivan Lozanoffee3342019-08-27 12:03:00 -07001189func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1190 var depPaths PathDeps
1191
1192 directRlibDeps := []*Module{}
1193 directDylibDeps := []*Module{}
1194 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001195 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001196 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001197 directSrcProvidersDeps := []*Module{}
1198 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001199
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001200 // For the dependency from platform to apex, use the latest stubs
1201 mod.apexSdkVersion = android.FutureApiLevel
1202 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1203 if !apexInfo.IsForPlatform() {
1204 mod.apexSdkVersion = apexInfo.MinSdkVersion
1205 }
1206
1207 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1208 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1209 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1210 // (b/144430859)
1211 mod.apexSdkVersion = android.FutureApiLevel
1212 }
1213
Spandan Das604f3762023-03-16 22:51:40 +00001214 skipModuleList := map[string]bool{}
1215
1216 var apiImportInfo multitree.ApiImportInfo
1217 hasApiImportInfo := false
1218
1219 ctx.VisitDirectDeps(func(dep android.Module) {
1220 if dep.Name() == "api_imports" {
1221 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1222 hasApiImportInfo = true
1223 }
1224 })
1225
1226 if hasApiImportInfo {
1227 targetStubModuleList := map[string]string{}
1228 targetOrigModuleList := map[string]string{}
1229
1230 // Search for dependency which both original module and API imported library with APEX stub exists
1231 ctx.VisitDirectDeps(func(dep android.Module) {
1232 depName := ctx.OtherModuleName(dep)
1233 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1234 targetStubModuleList[apiLibrary] = depName
1235 }
1236 })
1237 ctx.VisitDirectDeps(func(dep android.Module) {
1238 depName := ctx.OtherModuleName(dep)
1239 if origLibrary, ok := targetStubModuleList[depName]; ok {
1240 targetOrigModuleList[origLibrary] = depName
1241 }
1242 })
1243
1244 // Decide which library should be used between original and API imported library
1245 ctx.VisitDirectDeps(func(dep android.Module) {
1246 depName := ctx.OtherModuleName(dep)
1247 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1248 if cc.ShouldUseStubForApex(ctx, dep) {
1249 skipModuleList[depName] = true
1250 } else {
1251 skipModuleList[apiLibrary] = true
1252 }
1253 }
1254 })
1255 }
1256
Cole Faustb6e6f992023-08-17 17:42:26 -07001257 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1258 var directAndroidMkSharedLibs []string
Sam Delmericoa588d152023-06-16 10:28:04 -04001259 transitiveRlibs := android.NewDepSetBuilder[RustLibrary](android.PREORDER)
1260 transitiveDylibs := android.NewDepSetBuilder[RustLibrary](android.PREORDER)
1261 transitiveProcMacros := android.NewDepSetBuilder[RustLibrary](android.PREORDER)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001262 ctx.VisitDirectDeps(func(dep android.Module) {
1263 depName := ctx.OtherModuleName(dep)
1264 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001265
Spandan Das604f3762023-03-16 22:51:40 +00001266 if _, exists := skipModuleList[depName]; exists {
1267 return
1268 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001269
1270 if depTag == android.DarwinUniversalVariantTag {
1271 return
1272 }
1273
Sam Delmericoa588d152023-06-16 10:28:04 -04001274 rustInfo := ctx.OtherModuleProvider(dep, RustInfoProvider).(RustInfo)
1275 if rustInfo.TransitiveDylibs != nil {
1276 transitiveDylibs.Transitive(rustInfo.TransitiveDylibs)
1277 }
1278 if rustInfo.TransitiveRlibs != nil {
1279 transitiveRlibs.Transitive(rustInfo.TransitiveRlibs)
1280 }
1281 if rustInfo.TransitiveProcMacros != nil {
1282 transitiveProcMacros.Transitive(rustInfo.TransitiveProcMacros)
1283 }
1284
Ivan Lozano89435d12020-07-31 11:01:18 -04001285 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001286 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001287 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001288
Ivan Lozanoffee3342019-08-27 12:03:00 -07001289 switch depTag {
1290 case dylibDepTag:
1291 dylib, ok := rustDep.compiler.(libraryInterface)
1292 if !ok || !dylib.dylib() {
1293 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1294 return
1295 }
1296 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001297 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001298 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
Sam Delmericoa588d152023-06-16 10:28:04 -04001299 transitiveDylibs.Direct(RustLibrary{
1300 Path: rustDep.UnstrippedOutputFile(),
1301 CrateName: rustDep.CrateName(),
1302 })
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001303
Ivan Lozanoffee3342019-08-27 12:03:00 -07001304 case rlibDepTag:
1305 rlib, ok := rustDep.compiler.(libraryInterface)
1306 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001307 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001308 return
1309 }
1310 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001311 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001312 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
Sam Delmericoa588d152023-06-16 10:28:04 -04001313 transitiveRlibs.Direct(RustLibrary{
1314 Path: rustDep.UnstrippedOutputFile(),
1315 CrateName: rustDep.CrateName(),
1316 })
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001317
Ivan Lozanoffee3342019-08-27 12:03:00 -07001318 case procMacroDepTag:
1319 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001320 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Sam Delmericoa588d152023-06-16 10:28:04 -04001321 transitiveProcMacros.Direct(RustLibrary{
1322 Path: rustDep.UnstrippedOutputFile(),
1323 CrateName: rustDep.CrateName(),
1324 })
Paul Duffind5cf92e2021-07-09 17:38:55 +01001325 }
1326
Cole Faustb6e6f992023-08-17 17:42:26 -07001327 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1328
Paul Duffind5cf92e2021-07-09 17:38:55 +01001329 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001330 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1331 // OS/Arch variant is used.
1332 var helper string
1333 if ctx.Host() {
1334 helper = "missing 'host_supported'?"
1335 } else {
1336 helper = "device module defined?"
1337 }
1338
1339 if dep.Target().Os != ctx.Os() {
1340 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1341 return
1342 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1343 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1344 return
1345 }
1346 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001347 }
1348
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001349 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001350 if depTag != procMacroDepTag {
1351 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1352 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1353 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1354 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001355 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001356 }
1357
Ivan Lozanoffee3342019-08-27 12:03:00 -07001358 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001359 linkFile := rustDep.UnstrippedOutputFile()
Matthew Maurerbb3add12020-06-25 09:34:12 -07001360 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Sam Delmericoa588d152023-06-16 10:28:04 -04001361 lib.exportLinkDirs(linkFile.Dir())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001362 }
1363 }
1364
Ivan Lozano89435d12020-07-31 11:01:18 -04001365 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001366 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001367 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001368 if _, ok := ccDep.(*Module); !ok {
1369 if ccDep.Module().Target().Os != ctx.Os() {
1370 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1371 return
1372 }
1373 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1374 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1375 return
1376 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001377 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001378 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001379 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001380 if !ctx.Config().AllowMissingDependencies() {
1381 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1382 } else {
1383 ctx.AddMissingDependencies([]string{depName})
1384 }
1385 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001386 }
1387
Sam Delmericoa588d152023-06-16 10:28:04 -04001388 linkPath := linkObject.Path().Dir()
Colin Crossa86ea0e2023-08-01 09:57:22 -07001389
Ivan Lozanoffee3342019-08-27 12:03:00 -07001390 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001391 switch {
1392 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001393 if cc.IsWholeStaticLib(depTag) {
1394 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1395 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001396 if mod.Binary() {
1397 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1398 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1399 // final linkage, pass the args directly to the linker to handle these cases.
1400 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1401 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001402 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001403 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001404 } else {
1405 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 -05001406 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001407 }
1408
1409 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1410 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001411 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001412 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1413
Colin Cross0de8a1e2020-09-18 14:15:30 -07001414 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1415 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1416 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1417 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1418 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001419 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001420
1421 // Record baseLibName for snapshots.
1422 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1423
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001424 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001425 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001426 // For the shared lib dependencies, we may link to the stub variant
1427 // of the dependency depending on the context (e.g. if this
1428 // dependency crosses the APEX boundaries).
1429 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1430
1431 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1432 // object (either from stub or non-stub) to use.
1433 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001434 if !linkObject.Valid() {
1435 if !ctx.Config().AllowMissingDependencies() {
1436 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1437 } else {
1438 ctx.AddMissingDependencies([]string{depName})
1439 }
1440 return
1441 }
Sam Delmericoa588d152023-06-16 10:28:04 -04001442 linkPath = linkObject.Path().Dir()
Jiyong Park7d55b612021-06-11 17:22:09 +09001443
Ivan Lozanoffee3342019-08-27 12:03:00 -07001444 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001445 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001446 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1447 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1448 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1449 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001450 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001451
1452 // Record baseLibName for snapshots.
1453 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1454
Cole Faustb6e6f992023-08-17 17:42:26 -07001455 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001456 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001457 case cc.IsHeaderDepTag(depTag):
1458 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1459 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1460 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1461 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001462 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001463 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001464 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001465 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001466 }
1467
1468 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001469 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1470 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001471 if linkObject.Valid() {
1472 lib.exportLinkObjects(linkObject.Path())
1473 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001474 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001475 } else {
1476 switch {
Sam Delmericoa588d152023-06-16 10:28:04 -04001477 case depTag == buildToolDepTag:
1478 buildTool := ctx.OtherModuleProvider(dep, android.PrebuiltBuildToolInfoProvider).(android.PrebuiltBuildToolInfo)
1479 depPaths.BuildToolDeps = append(depPaths.BuildToolDeps, buildTool.Src)
1480 depPaths.BuildToolDeps = append(depPaths.BuildToolDeps, buildTool.Deps...)
1481 switch android.RemoveOptionalPrebuiltPrefix(dep.Name()) {
1482 case "rustc":
Colin Cross05cbcb32023-09-22 22:02:28 +00001483 depPaths.Rustc = buildTool.Src
Sam Delmericoa588d152023-06-16 10:28:04 -04001484 // rustc expects the standard cc toolchain libraries (libdl, libm, libc, etc.)
1485 // not to be under the __SBOX_SANDBOX_DIR__/ directory
1486 depPaths.BuildToolSrcDeps = append(depPaths.BuildToolSrcDeps, buildTool.Deps...)
Colin Cross05cbcb32023-09-22 22:02:28 +00001487 case "clang++":
1488 depPaths.Clang = buildTool.Src
1489 case "llvm-ar":
1490 depPaths.Llvm_ar = buildTool.Src
1491 case "clippy-driver":
1492 depPaths.Clippy_driver = buildTool.Src
1493 case "rustdoc":
1494 depPaths.Rustdoc = buildTool.Src
Sam Delmericoa588d152023-06-16 10:28:04 -04001495 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001496 case depTag == cc.CrtBeginDepTag:
1497 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1498 case depTag == cc.CrtEndDepTag:
1499 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1500 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001501 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001502
1503 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001504 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001505 // These are usually genrules which don't have per-target variants.
1506 directSrcDeps = append(directSrcDeps, srcDep)
1507 }
1508 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001509 })
1510
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001511 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001512 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001513 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001514 }
1515
Ivan Lozanoffee3342019-08-27 12:03:00 -07001516 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001517 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001518 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001519 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001520 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001521 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001522 }
1523
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001524 var srcProviderDepFiles android.Paths
1525 for _, dep := range directSrcProvidersDeps {
1526 srcs, _ := dep.OutputFiles("")
1527 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1528 }
1529 for _, dep := range directSrcDeps {
1530 srcs := dep.Srcs()
1531 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1532 }
1533
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001534 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001535 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001536
1537 // Dedup exported flags from dependencies
Sam Delmericoa588d152023-06-16 10:28:04 -04001538 depPaths.linkDirs = android.FirstUniquePaths(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001539 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001540 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001541 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1542 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1543 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001544
Sam Delmericoa588d152023-06-16 10:28:04 -04001545 depPaths.Rlibs = transitiveRlibs.Build()
1546 depPaths.Dylibs = transitiveDylibs.Build()
1547 depPaths.ProcMacros = transitiveProcMacros.Build()
1548 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1549
Ivan Lozanoffee3342019-08-27 12:03:00 -07001550 return depPaths
1551}
1552
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001553func (mod *Module) InstallInData() bool {
1554 if mod.compiler == nil {
1555 return false
1556 }
1557 return mod.compiler.inData()
1558}
1559
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001560func (mod *Module) InstallInRamdisk() bool {
1561 return mod.InRamdisk()
1562}
1563
1564func (mod *Module) InstallInVendorRamdisk() bool {
1565 return mod.InVendorRamdisk()
1566}
1567
1568func (mod *Module) InstallInRecovery() bool {
1569 return mod.InRecovery()
1570}
1571
Spandan Das604f3762023-03-16 22:51:40 +00001572// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1573func (r *Module) usePublicApi() bool {
1574 return r.Device() && r.UseSdk()
1575}
1576
1577// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1578func (r *Module) useVendorApi() bool {
1579 return r.Device() && (r.InVendor() || r.InProduct())
1580}
1581
Ivan Lozanoffee3342019-08-27 12:03:00 -07001582func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1583 ctx := &depsContext{
1584 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001585 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001586
1587 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001588 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001589 var snapshotInfo *cc.SnapshotInfo
1590
Kiyoung Kim487689e2022-07-26 09:48:22 +09001591 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001592 if mod.usePublicApi() || mod.useVendorApi() {
1593 for idx, lib := range deps.SharedLibs {
1594 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1595 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001596 }
1597
Ivan Lozano1921e802021-05-20 13:39:16 -04001598 if ctx.Os() == android.Android {
1599 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1600 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001601
Ivan Lozano2b081132020-09-08 12:46:52 -04001602 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001603 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001604 stdLinkage = "rlib-std"
1605 }
1606
1607 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001608
Ivan Lozano2b081132020-09-08 12:46:52 -04001609 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1610 rlibDepVariations = append(rlibDepVariations,
1611 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1612 }
1613
Sam Delmericoa588d152023-06-16 10:28:04 -04001614 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "rustc")
Colin Cross05cbcb32023-09-22 22:02:28 +00001615 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "clippy-driver")
1616 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "rustdoc")
1617 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "clang++")
1618 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "clang++.real")
1619 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "lld")
1620 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "ld.lld")
1621 ctx.AddFarVariationDependencies([]blueprint.Variation{}, buildToolDepTag, "llvm-ar")
Sam Delmericoa588d152023-06-16 10:28:04 -04001622
Ivan Lozano1921e802021-05-20 13:39:16 -04001623 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001624 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001625 for _, lib := range deps.Rlibs {
1626 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001627 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001628
Ivan Lozano2d407632022-04-07 12:59:11 -04001629 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001630 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001631
1632 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001633 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1634 for _, lib := range deps.Dylibs {
1635 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1636 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001637
Ivan Lozano1921e802021-05-20 13:39:16 -04001638 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001639 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1640 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001641 for _, lib := range deps.Rustlibs {
1642 if autoDep.depTag == rlibDepTag {
1643 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001644 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001645 } else {
1646 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1647 // Check for the existence of the dylib deptag variant. Select it if available,
1648 // otherwise select the rlib variant.
1649 autoDepVariations := append(commonDepVariations,
1650 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001651
1652 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1653
1654 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1655 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001656 } else {
1657 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001658 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001659 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001660 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001661 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001662 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001663 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001664 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001665 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001666 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001667 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001668 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001669 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001670 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001671 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001672 for _, lib := range deps.Stdlibs {
1673 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1674 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001675 }
1676 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001677
1678 for _, lib := range deps.SharedLibs {
1679 depTag := cc.SharedDepTag()
1680 name, version := cc.StubsLibNameAndVersion(lib)
1681
1682 variations := []blueprint.Variation{
1683 {Mutator: "link", Variation: "shared"},
1684 }
Spandan Das604f3762023-03-16 22:51:40 +00001685 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1686 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1687 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1688 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1689 }
1690
1691 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1692 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1693 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001694 }
1695
1696 for _, lib := range deps.WholeStaticLibs {
1697 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001698 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001699
1700 actx.AddVariationDependencies([]blueprint.Variation{
1701 {Mutator: "link", Variation: "static"},
1702 }, depTag, lib)
1703 }
1704
1705 for _, lib := range deps.StaticLibs {
1706 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001707 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001708
1709 actx.AddVariationDependencies([]blueprint.Variation{
1710 {Mutator: "link", Variation: "static"},
1711 }, depTag, lib)
1712 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001713
Zach Johnson3df4e632020-11-06 11:56:27 -08001714 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1715
Colin Cross565cafd2020-09-25 18:47:38 -07001716 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001717 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001718 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001719 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001720 }
Colin Crossfe605e12022-01-23 20:46:16 -08001721 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001722 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001723 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001724 }
1725
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001726 if mod.sourceProvider != nil {
1727 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1728 bindgen.Properties.Custom_bindgen != "" {
1729 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1730 bindgen.Properties.Custom_bindgen)
1731 }
1732 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001733
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001734 actx.AddVariationDependencies([]blueprint.Variation{
1735 {Mutator: "link", Variation: "shared"},
1736 }, dataLibDepTag, deps.DataLibs...)
1737
1738 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1739
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001740 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001741 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001742
1743 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001744}
1745
Ivan Lozano2d407632022-04-07 12:59:11 -04001746// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001747func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1748 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001749 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1750}
1751
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001752func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1753 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1754 actx.AddVariationDependencies(variations, depTag, lib)
1755}
1756
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001757func BeginMutator(ctx android.BottomUpMutatorContext) {
1758 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1759 mod.beginMutator(ctx)
1760 }
1761}
1762
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001763func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1764 ctx := &baseModuleContext{
1765 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001766 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001767
1768 mod.begin(ctx)
1769}
1770
Ivan Lozanoffee3342019-08-27 12:03:00 -07001771func (mod *Module) Name() string {
1772 name := mod.ModuleBase.Name()
1773 if p, ok := mod.compiler.(interface {
1774 Name(string) string
1775 }); ok {
1776 name = p.Name(name)
1777 }
1778 return name
1779}
1780
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001781func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001782 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001783 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001784 }
1785}
1786
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001787var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001788var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001789
1790func (mod *Module) HostToolPath() android.OptionalPath {
1791 if !mod.Host() {
1792 return android.OptionalPath{}
1793 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001794 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1795 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001796 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1797 // Even though proc-macros aren't strictly "tools", since they target the compiler
1798 // and act as compiler plugins, we treat them similarly.
1799 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001800 }
1801 return android.OptionalPath{}
1802}
1803
Jiyong Park99644e92020-11-17 22:21:02 +09001804var _ android.ApexModule = (*Module)(nil)
1805
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001806func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001807 return String(mod.Properties.Min_sdk_version)
1808}
1809
Jiyong Park45bf82e2020-12-15 22:29:02 +09001810// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001811func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001812 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001813 if minSdkVersion == "apex_inherit" {
1814 return nil
1815 }
1816 if minSdkVersion == "" {
1817 return fmt.Errorf("min_sdk_version is not specificed")
1818 }
1819
1820 // Not using nativeApiLevelFromUser because the context here is not
1821 // necessarily a native context.
1822 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1823 if err != nil {
1824 return err
1825 }
1826
1827 if ver.GreaterThan(sdkVersion) {
1828 return fmt.Errorf("newer SDK(%v)", ver)
1829 }
Jiyong Park99644e92020-11-17 22:21:02 +09001830 return nil
1831}
1832
Jiyong Park45bf82e2020-12-15 22:29:02 +09001833// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001834func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1835 depTag := ctx.OtherModuleDependencyTag(dep)
1836
1837 if ccm, ok := dep.(*cc.Module); ok {
1838 if ccm.HasStubsVariants() {
1839 if cc.IsSharedDepTag(depTag) {
1840 // dynamic dep to a stubs lib crosses APEX boundary
1841 return false
1842 }
1843 if cc.IsRuntimeDepTag(depTag) {
1844 // runtime dep to a stubs lib also crosses APEX boundary
1845 return false
1846 }
1847
1848 if cc.IsHeaderDepTag(depTag) {
1849 return false
1850 }
1851 }
1852 if mod.Static() && cc.IsSharedDepTag(depTag) {
1853 // shared_lib dependency from a static lib is considered as crossing
1854 // the APEX boundary because the dependency doesn't actually is
1855 // linked; the dependency is used only during the compilation phase.
1856 return false
1857 }
1858 }
1859
Matthew Maurer581b6d82022-09-29 16:46:25 -07001860 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001861 return false
1862 }
1863
1864 return true
1865}
1866
1867// Overrides ApexModule.IsInstallabeToApex()
1868func (mod *Module) IsInstallableToApex() bool {
1869 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001870 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001871 return true
1872 }
1873 if _, ok := mod.compiler.(*binaryDecorator); ok {
1874 return true
1875 }
1876 }
1877 return false
1878}
1879
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001880// If a library file has a "lib" prefix, extract the library name without the prefix.
1881func libNameFromFilePath(filepath android.Path) (string, bool) {
1882 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1883 if strings.HasPrefix(libName, "lib") {
1884 libName = libName[3:]
1885 return libName, true
1886 }
1887 return "", false
1888}
1889
Sasha Smundaka76acba2022-04-18 20:12:56 -07001890func kytheExtractRustFactory() android.Singleton {
1891 return &kytheExtractRustSingleton{}
1892}
1893
1894type kytheExtractRustSingleton struct {
1895}
1896
1897func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1898 var xrefTargets android.Paths
1899 ctx.VisitAllModules(func(module android.Module) {
1900 if rustModule, ok := module.(xref); ok {
1901 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1902 }
1903 })
1904 if len(xrefTargets) > 0 {
1905 ctx.Phony("xref_rust", xrefTargets...)
1906 }
1907}
1908
Jihoon Kangf78a8902022-09-01 22:47:07 +00001909func (c *Module) Partition() string {
1910 return ""
1911}
1912
Chris Parsons637458d2023-09-19 20:09:00 +00001913func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001914 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1915 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001916 } else if ctx.ModuleType() == "rust_proc_macro" {
1917 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001918 } else if ctx.ModuleType() == "rust_binary_host" {
1919 binaryBp2build(ctx, m)
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001920 } else if ctx.ModuleType() == "rust_protobuf_host" {
1921 protoLibraryBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001922 } else {
1923 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1924 }
1925}
1926
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001927// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001928// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001929// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1930// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1931// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001932func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001933 var srcs bazel.LabelList
1934 var compileData bazel.LabelList
1935
1936 if c.Properties.Srcs[0] == "src/lib.rs" {
1937 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1938 compileData = android.BazelLabelForModuleSrc(
1939 ctx,
1940 []string{
1941 "src/**/*.proto",
1942 "examples/**/*.rs",
1943 "**/*.md",
1944 },
1945 )
1946 } else {
1947 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1948 }
1949
1950 return srcs, compileData
1951}
1952
Ivan Lozanoffee3342019-08-27 12:03:00 -07001953var Bool = proptools.Bool
1954var BoolDefault = proptools.BoolDefault
1955var String = proptools.String
1956var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001957
1958var _ android.OutputFileProducer = (*Module)(nil)