blob: e2154f0cc33bdd9fd86a013a8a4e0f237f0b6957 [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 (
Sasha Smundaka76acba2022-04-18 20:12:56 -070018 "android/soong/bloaty"
Ivan Lozano183a3212019-10-18 14:18:45 -070019 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "strings"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020027 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070028 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000029 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070030 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040031 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032)
33
34var pctx = android.NewPackageContext("android/soong/rust")
35
36func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 android.RegisterModuleType("rust_defaults", defaultsFactory)
38 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
39 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040040 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040041 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050042 })
43 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
Inseob Kim3b244062023-07-11 13:31:36 +090046 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070047 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020048 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000049 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070050}
51
52type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040053 GlobalRustFlags []string // Flags that apply globally to rust
54 GlobalLinkFlags []string // Flags that apply globally to linker
55 RustFlags []string // Flags that apply to rust
56 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020057 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070058 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070059 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040060 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070062 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050066 AndroidMkRlibs []string `blueprint:"mutated"`
67 AndroidMkDylibs []string `blueprint:"mutated"`
68 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
69 AndroidMkSharedLibs []string `blueprint:"mutated"`
70 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040071
Ivan Lozano6a884432020-12-02 09:15:16 -050072 ImageVariationPrefix string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
75
Ivan Lozanoc08897c2021-04-02 12:41:32 -040076 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
77 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
78 // appended before SubName.
79 RustSubName string `blueprint:"mutated"`
80
Ivan Lozano6a884432020-12-02 09:15:16 -050081 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050082 CoreVariantNeeded bool `blueprint:"mutated"`
83 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070084 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080085 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050086 ExtraVariants []string `blueprint:"mutated"`
87
Ivan Lozanoa2268632021-07-22 10:52:06 -040088 // Allows this module to use non-APEX version of libraries. Useful
89 // for building binaries that are started before APEXes are activated.
90 Bootstrap *bool
91
Ivan Lozano1921e802021-05-20 13:39:16 -040092 // Used by vendor snapshot to record dependencies from snapshot modules.
93 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090094 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040095 SnapshotRlibs []string `blueprint:"mutated"`
96 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040097
Matthew Maurerc6868382021-07-13 14:12:37 -070098 // Make this module available when building for ramdisk.
99 // On device without a dedicated recovery partition, the module is only
100 // available after switching root into
101 // /first_stage_ramdisk. To expose the module before switching root, install
102 // the recovery variant instead.
103 Ramdisk_available *bool
104
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500105 // Make this module available when building for vendor ramdisk.
106 // On device without a dedicated recovery partition, the module is only
107 // available after switching root into
108 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800109 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500110 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400111
Ivan Lozano1921e802021-05-20 13:39:16 -0400112 // Normally Soong uses the directory structure to decide which modules
113 // should be included (framework) or excluded (non-framework) from the
114 // different snapshots (vendor, recovery, etc.), but this property
115 // allows a partner to exclude a module normally thought of as a
116 // framework module from the vendor snapshot.
117 Exclude_from_vendor_snapshot *bool
118
119 // Normally Soong uses the directory structure to decide which modules
120 // should be included (framework) or excluded (non-framework) from the
121 // different snapshots (vendor, recovery, etc.), but this property
122 // allows a partner to exclude a module normally thought of as a
123 // framework module from the recovery snapshot.
124 Exclude_from_recovery_snapshot *bool
125
Matthew Maurer460ee942021-02-11 12:31:46 -0800126 // Make this module available when building for recovery
127 Recovery_available *bool
128
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500129 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
130 Min_sdk_version *string
131
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900132 HideFromMake bool `blueprint:"mutated"`
133 PreventInstall bool `blueprint:"mutated"`
134
135 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136}
137
138type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700139 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140
Ivan Lozano6a884432020-12-02 09:15:16 -0500141 VendorProperties cc.VendorProperties
142
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143 Properties BaseProperties
144
145 hod android.HostOrDeviceSupported
146 multilib android.Multilib
147
Ivan Lozano6a884432020-12-02 09:15:16 -0500148 makeLinkType string
149
Yi Kong46c6e592022-01-20 22:55:00 +0800150 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400152 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200153 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500154 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700157 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400159 // Output file to be installed, may be stripped or unstripped.
160 outputFile android.OptionalPath
161
Sasha Smundaka76acba2022-04-18 20:12:56 -0700162 // Cross-reference input file
163 kytheFiles android.Paths
164
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400165 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900166
167 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500168
169 // For apex variants, this is set as apex.min_sdk_version
170 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700171}
172
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500173func (mod *Module) Header() bool {
174 //TODO: If Rust libraries provide header variants, this needs to be updated.
175 return false
176}
177
178func (mod *Module) SetPreventInstall() {
179 mod.Properties.PreventInstall = true
180}
181
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500182func (mod *Module) SetHideFromMake() {
183 mod.Properties.HideFromMake = true
184}
185
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900186func (mod *Module) HiddenFromMake() bool {
187 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400188}
189
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500190func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500191 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
192 // nil since we need compiler to actually sanitize.
193 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194}
195
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500196func (mod *Module) IsPrebuilt() bool {
197 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
198 return true
199 }
200 return false
201}
202
Ivan Lozano43845682020-07-09 21:03:28 -0400203func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
204 switch tag {
205 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700206 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400207 return mod.sourceProvider.Srcs(), nil
208 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900209 if mod.OutputFile().Valid() {
210 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400211 }
212 return android.Paths{}, nil
213 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500214 case "unstripped":
215 if mod.compiler != nil {
216 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
217 }
218 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400219 default:
220 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
221 }
222}
223
Ivan Lozano52767be2019-10-18 14:49:46 -0700224func (mod *Module) SelectedStl() string {
225 return ""
226}
227
Ivan Lozano2b262972019-11-21 12:30:50 -0800228func (mod *Module) NonCcVariants() bool {
229 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400230 if _, ok := mod.compiler.(libraryInterface); ok {
231 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800232 }
233 }
234 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
235}
236
Ivan Lozano52767be2019-10-18 14:49:46 -0700237func (mod *Module) Static() bool {
238 if mod.compiler != nil {
239 if library, ok := mod.compiler.(libraryInterface); ok {
240 return library.static()
241 }
242 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400243 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700244}
245
246func (mod *Module) Shared() bool {
247 if mod.compiler != nil {
248 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700250 }
251 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400252 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700253}
254
Ivan Lozanod7586b62021-04-01 09:49:36 -0400255func (mod *Module) Dylib() bool {
256 if mod.compiler != nil {
257 if library, ok := mod.compiler.(libraryInterface); ok {
258 return library.dylib()
259 }
260 }
261 return false
262}
263
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400264func (mod *Module) RlibStd() bool {
265 if mod.compiler != nil {
266 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
267 return library.rlibStd()
268 }
269 }
270 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
271}
272
Ivan Lozanod7586b62021-04-01 09:49:36 -0400273func (mod *Module) Rlib() bool {
274 if mod.compiler != nil {
275 if library, ok := mod.compiler.(libraryInterface); ok {
276 return library.rlib()
277 }
278 }
279 return false
280}
281
282func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400283 if binary, ok := mod.compiler.(binaryInterface); ok {
284 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400285 }
286 return false
287}
288
Justin Yun5e035862021-06-29 20:50:37 +0900289func (mod *Module) StaticExecutable() bool {
290 if !mod.Binary() {
291 return false
292 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400293 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900294}
295
Ivan Lozanod7586b62021-04-01 09:49:36 -0400296func (mod *Module) Object() bool {
297 // Rust has no modules which produce only object files.
298 return false
299}
300
Ivan Lozano52767be2019-10-18 14:49:46 -0700301func (mod *Module) Toc() android.OptionalPath {
302 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400303 if lib, ok := mod.compiler.(libraryInterface); ok {
304 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700305 }
306 }
307 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
308}
309
Colin Crossc511bc52020-04-07 16:50:32 +0000310func (mod *Module) UseSdk() bool {
311 return false
312}
313
Ivan Lozanod7586b62021-04-01 09:49:36 -0400314func (mod *Module) RelativeInstallPath() string {
315 if mod.compiler != nil {
316 return mod.compiler.relativeInstallPath()
317 }
318 return ""
319}
320
Ivan Lozano52767be2019-10-18 14:49:46 -0700321func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500322 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700323}
324
Jiyong Park7d55b612021-06-11 17:22:09 +0900325func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400326 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900327}
328
Ivan Lozano52767be2019-10-18 14:49:46 -0700329func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400330 return true
331}
332
333func (mod *Module) SubName() string {
334 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700335}
336
337func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500338 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700339 return false
340}
341
Ivan Lozanof9e21722020-12-02 09:00:51 -0500342func (mod *Module) IsVndkExt() bool {
343 return false
344}
345
Ivan Lozanod7586b62021-04-01 09:49:36 -0400346func (mod *Module) IsVndkSp() bool {
347 return false
348}
349
Ivan Lozanof1868af2022-04-12 13:08:36 -0400350func (mod *Module) IsVndkPrebuiltLibrary() bool {
351 // Rust modules do not provide VNDK prebuilts
352 return false
353}
354
355func (mod *Module) IsVendorPublicLibrary() bool {
356 return mod.VendorProperties.IsVendorPublicLibrary
357}
358
359func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
360 // Rust modules to not provide Sdk variants
361 return false
362}
363
Colin Cross127bb8b2020-12-16 16:46:01 -0800364func (c *Module) IsVndkPrivate() bool {
365 return false
366}
367
368func (c *Module) IsLlndk() bool {
369 return false
370}
371
372func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500373 return false
374}
375
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400376func (mod *Module) KernelHeadersDecorator() bool {
377 return false
378}
379
Colin Cross1f3f1302021-04-26 18:37:44 -0700380func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400381 return false
382}
383
Colin Cross5271fea2021-04-27 13:06:04 -0700384func (m *Module) NeedsVendorPublicLibraryVariants() bool {
385 return false
386}
387
Ivan Lozanod7586b62021-04-01 09:49:36 -0400388func (mod *Module) HasLlndkStubs() bool {
389 return false
390}
391
392func (mod *Module) StubsVersion() string {
393 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
394}
395
Ivan Lozano52767be2019-10-18 14:49:46 -0700396func (mod *Module) SdkVersion() string {
397 return ""
398}
399
Colin Crossc511bc52020-04-07 16:50:32 +0000400func (mod *Module) AlwaysSdk() bool {
401 return false
402}
403
Jiyong Park2286afd2020-06-16 21:58:53 +0900404func (mod *Module) IsSdkVariant() bool {
405 return false
406}
407
Colin Cross1348ce32020-10-01 13:37:16 -0700408func (mod *Module) SplitPerApiLevel() bool {
409 return false
410}
411
Sasha Smundaka76acba2022-04-18 20:12:56 -0700412func (mod *Module) XrefRustFiles() android.Paths {
413 return mod.kytheFiles
414}
415
Ivan Lozanoffee3342019-08-27 12:03:00 -0700416type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400417 Dylibs []string
418 Rlibs []string
419 Rustlibs []string
420 Stdlibs []string
421 ProcMacros []string
422 SharedLibs []string
423 StaticLibs []string
424 WholeStaticLibs []string
425 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700426
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400427 // Used for data dependencies adjacent to tests
428 DataLibs []string
429 DataBins []string
430
Colin Crossfe605e12022-01-23 20:46:16 -0800431 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432}
433
434type PathDeps struct {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700435 DyLibs RustLibraries
436 RLibs RustLibraries
437 LibDeps android.Paths
438 WholeStaticLibs android.Paths
439 ProcMacros RustLibraries
440 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500441
442 // depFlags and depLinkFlags are rustc and linker (clang) flags.
443 depFlags []string
444 depLinkFlags []string
445
446 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
447 // Both of these are exported and propagate to dependencies.
448 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700449 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700450
Ivan Lozano45901ed2020-07-24 16:05:01 -0400451 // Used by bindgen modules which call clang
452 depClangFlags []string
453 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400454 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400455 depSystemIncludePaths android.Paths
456
Colin Crossfe605e12022-01-23 20:46:16 -0800457 CrtBegin android.Paths
458 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700459
460 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500461 SrcDeps android.Paths
462 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700463}
464
465type RustLibraries []RustLibrary
466
467type RustLibrary struct {
468 Path android.Path
469 CrateName string
470}
471
472type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100473 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700474 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400475 cfgFlags(ctx ModuleContext, flags Flags) Flags
476 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700477 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700478 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479 compilerDeps(ctx DepsContext, deps Deps) Deps
480 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700481 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700482
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100483 // Output directory in which source-generated code from dependencies is
484 // copied. This is equivalent to Cargo's OUT_DIR variable.
485 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700486
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400487 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
488 CargoPkgVersion() string
489
490 // CargoEnvCompat returns whether Cargo environment variables should be used.
491 CargoEnvCompat() bool
492
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800493 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200494 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400496 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400497
498 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400499
500 Disabled() bool
501 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400502
Ivan Lozanodd055472020-09-28 13:22:45 -0400503 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500504 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900505
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400506 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900507 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400508}
509
Matthew Maurerbb3add12020-06-25 09:34:12 -0700510type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700511 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700512 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700513}
514
Sasha Smundaka76acba2022-04-18 20:12:56 -0700515type xref interface {
516 XrefRustFiles() android.Paths
517}
518
Matthew Maurerbb3add12020-06-25 09:34:12 -0700519type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400520 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700521 linkObjects android.Paths
522 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700523}
524
Matthew Maurerbb3add12020-06-25 09:34:12 -0700525func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
526 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
527}
528
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700529func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
530 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
531}
532
533func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
534 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400535}
536
Colin Cross0de8a1e2020-09-18 14:15:30 -0700537func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
538 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700539 LinkDirs: flagExporter.linkDirs,
540 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700541 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700542 })
543}
544
Matthew Maurerbb3add12020-06-25 09:34:12 -0700545var _ exportedFlagsProducer = (*flagExporter)(nil)
546
547func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700548 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700549}
550
Colin Cross0de8a1e2020-09-18 14:15:30 -0700551type FlagExporterInfo struct {
552 Flags []string
553 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700554 LinkObjects android.Paths
555 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700556}
557
558var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
559
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400560func (mod *Module) isCoverageVariant() bool {
561 return mod.coverage.Properties.IsCoverageVariant
562}
563
564var _ cc.Coverage = (*Module)(nil)
565
566func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
567 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
568}
569
Ivan Lozanod7586b62021-04-01 09:49:36 -0400570func (mod *Module) VndkVersion() string {
571 return mod.Properties.VndkVersion
572}
573
574func (mod *Module) PreventInstall() bool {
575 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400576}
577
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400578func (mod *Module) MarkAsCoverageVariant(coverage bool) {
579 mod.coverage.Properties.IsCoverageVariant = coverage
580}
581
582func (mod *Module) EnableCoverageIfNeeded() {
583 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700584}
585
586func defaultsFactory() android.Module {
587 return DefaultsFactory()
588}
589
590type Defaults struct {
591 android.ModuleBase
592 android.DefaultsModuleBase
593}
594
595func DefaultsFactory(props ...interface{}) android.Module {
596 module := &Defaults{}
597
598 module.AddProperties(props...)
599 module.AddProperties(
600 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800601 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500602 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100603 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400604 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 &BaseCompilerProperties{},
606 &BinaryCompilerProperties{},
607 &LibraryCompilerProperties{},
608 &ProcMacroCompilerProperties{},
609 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400610 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700611 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400612 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400613 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200614 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500615 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700616 )
617
618 android.InitDefaultsModule(module)
619 return module
620}
621
622func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700623 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700624}
625
Ivan Lozano183a3212019-10-18 14:18:45 -0700626func (mod *Module) CcLibrary() bool {
627 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500628 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700629 return true
630 }
631 }
632 return false
633}
634
635func (mod *Module) CcLibraryInterface() bool {
636 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400637 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
638 // VariantIs{Static,Shared} is set.
639 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700640 return true
641 }
642 }
643 return false
644}
645
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400646func (mod *Module) RustLibraryInterface() bool {
647 if mod.compiler != nil {
648 if _, ok := mod.compiler.(libraryInterface); ok {
649 return true
650 }
651 }
652 return false
653}
654
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500655func (mod *Module) IsFuzzModule() bool {
656 if _, ok := mod.compiler.(*fuzzDecorator); ok {
657 return true
658 }
659 return false
660}
661
662func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
663 return mod.FuzzModule
664}
665
666func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
667 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
668 return fuzzer.fuzzPackagedModule
669 }
670 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
671}
672
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000673func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500674 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
675 return fuzzer.sharedLibraries
676 }
677 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
678}
679
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400680func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400681 if mod.compiler != nil {
682 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400683 }
684 return nil
685}
686
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800687func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700688 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700689 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800690 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700691 }
692 }
693 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
694}
695
696func (mod *Module) SetStatic() {
697 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700698 if library, ok := mod.compiler.(libraryInterface); ok {
699 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700700 return
701 }
702 }
703 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
704}
705
706func (mod *Module) SetShared() {
707 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700708 if library, ok := mod.compiler.(libraryInterface); ok {
709 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700710 return
711 }
712 }
713 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
714}
715
Ivan Lozano183a3212019-10-18 14:18:45 -0700716func (mod *Module) BuildStaticVariant() bool {
717 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700718 if library, ok := mod.compiler.(libraryInterface); ok {
719 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700720 }
721 }
722 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
723}
724
725func (mod *Module) BuildSharedVariant() bool {
726 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700727 if library, ok := mod.compiler.(libraryInterface); ok {
728 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700729 }
730 }
731 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
732}
733
Ivan Lozano183a3212019-10-18 14:18:45 -0700734func (mod *Module) Module() android.Module {
735 return mod
736}
737
Ivan Lozano183a3212019-10-18 14:18:45 -0700738func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400739 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700740}
741
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400742func (mod *Module) CoverageFiles() android.Paths {
743 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800744 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400745 }
746 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
747}
748
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400749// Rust does not produce gcno files, and therefore does not produce a coverage archive.
750func (mod *Module) CoverageOutputFile() android.OptionalPath {
751 return android.OptionalPath{}
752}
753
754func (mod *Module) IsNdk(config android.Config) bool {
755 return false
756}
757
758func (mod *Module) IsStubs() bool {
759 return false
760}
761
Jiyong Park459feca2020-12-15 11:02:21 +0900762func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900763 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900764 return false
765 }
766
Jiyong Park459feca2020-12-15 11:02:21 +0900767 // The apex variant is not installable because it is included in the APEX and won't appear
768 // in the system partition as a standalone file.
769 if !apexInfo.IsForPlatform() {
770 return false
771 }
772
Jiyong Parke54f07e2021-04-07 15:08:04 +0900773 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900774}
775
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500776func (ctx moduleContext) apexVariationName() string {
777 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
778}
779
Ivan Lozano183a3212019-10-18 14:18:45 -0700780var _ cc.LinkableInterface = (*Module)(nil)
781
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782func (mod *Module) Init() android.Module {
783 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500784 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785
Yi Kong46c6e592022-01-20 22:55:00 +0800786 if mod.afdo != nil {
787 mod.AddProperties(mod.afdo.props()...)
788 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789 if mod.compiler != nil {
790 mod.AddProperties(mod.compiler.compilerProps()...)
791 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400792 if mod.coverage != nil {
793 mod.AddProperties(mod.coverage.props()...)
794 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200795 if mod.clippy != nil {
796 mod.AddProperties(mod.clippy.props()...)
797 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400798 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700799 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400800 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500801 if mod.sanitize != nil {
802 mod.AddProperties(mod.sanitize.props()...)
803 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400804
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900806 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807
808 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809 return mod
810}
811
812func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
813 return &Module{
814 hod: hod,
815 multilib: multilib,
816 }
817}
818func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
819 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800820 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400821 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200822 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500823 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824 return module
825}
826
827type ModuleContext interface {
828 android.ModuleContext
829 ModuleContextIntf
830}
831
832type BaseModuleContext interface {
833 android.BaseModuleContext
834 ModuleContextIntf
835}
836
837type DepsContext interface {
838 android.BottomUpMutatorContext
839 ModuleContextIntf
840}
841
842type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200843 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845}
846
847type depsContext struct {
848 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849}
850
851type moduleContext struct {
852 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853}
854
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200855type baseModuleContext struct {
856 android.BaseModuleContext
857}
858
859func (ctx *moduleContext) RustModule() *Module {
860 return ctx.Module().(*Module)
861}
862
863func (ctx *moduleContext) toolchain() config.Toolchain {
864 return ctx.RustModule().toolchain(ctx)
865}
866
867func (ctx *depsContext) RustModule() *Module {
868 return ctx.Module().(*Module)
869}
870
871func (ctx *depsContext) toolchain() config.Toolchain {
872 return ctx.RustModule().toolchain(ctx)
873}
874
875func (ctx *baseModuleContext) RustModule() *Module {
876 return ctx.Module().(*Module)
877}
878
879func (ctx *baseModuleContext) toolchain() config.Toolchain {
880 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400881}
882
883func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700884 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
885 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
886 return false
887 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400888 return mod.compiler != nil && mod.compiler.nativeCoverage()
889}
890
Ivan Lozanod7586b62021-04-01 09:49:36 -0400891func (mod *Module) EverInstallable() bool {
892 return mod.compiler != nil &&
893 // Check to see whether the module is actually ever installable.
894 mod.compiler.everInstallable()
895}
896
897func (mod *Module) Installable() *bool {
898 return mod.Properties.Installable
899}
900
Ivan Lozano872d5792022-03-23 17:31:39 -0400901func (mod *Module) ProcMacro() bool {
902 if pm, ok := mod.compiler.(procMacroInterface); ok {
903 return pm.ProcMacro()
904 }
905 return false
906}
907
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
909 if mod.cachedToolchain == nil {
910 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
911 }
912 return mod.cachedToolchain
913}
914
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200915func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
916 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
917}
918
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
920}
921
922func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
923 ctx := &moduleContext{
924 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926
Jiyong Park99644e92020-11-17 22:21:02 +0900927 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
928 if !apexInfo.IsForPlatform() {
929 mod.hideApexVariantFromMake = true
930 }
931
Ivan Lozanoffee3342019-08-27 12:03:00 -0700932 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500933 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
934
Ivan Lozanof1868af2022-04-12 13:08:36 -0400935 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700936
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937 if !toolchain.Supported() {
938 // This toolchain's unsupported, there's nothing to do for this mod.
939 return
940 }
941
942 deps := mod.depsToPaths(ctx)
943 flags := Flags{
944 Toolchain: toolchain,
945 }
946
Ivan Lozano67eada32021-09-23 11:50:33 -0400947 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800948 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500949 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800950 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951 if mod.compiler != nil {
952 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400953 flags = mod.compiler.cfgFlags(ctx, flags)
954 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400955 }
956 if mod.coverage != nil {
957 flags, deps = mod.coverage.flags(ctx, flags, deps)
958 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200959 if mod.clippy != nil {
960 flags, deps = mod.clippy.flags(ctx, flags, deps)
961 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500962 if mod.sanitize != nil {
963 flags, deps = mod.sanitize.flags(ctx, flags, deps)
964 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400965
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200966 // SourceProvider needs to call GenerateSource() before compiler calls
967 // compile() so it can provide the source. A SourceProvider has
968 // multiple variants (e.g. source, rlib, dylib). Only the "source"
969 // variant is responsible for effectively generating the source. The
970 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400971 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200972 if mod.compiler.(libraryInterface).source() {
973 mod.sourceProvider.GenerateSource(ctx, deps)
974 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
975 } else {
976 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
977 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700978 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200979 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400980 }
981
982 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100983 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700984 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400985 if ctx.Failed() {
986 return
987 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700988 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
989 if buildOutput.kytheFile != nil {
990 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
991 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400992 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900993
Dan Albert06feee92021-03-19 15:06:02 -0700994 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000995 if mod.docTimestampFile.Valid() {
996 ctx.CheckbuildFile(mod.docTimestampFile.Path())
997 }
Dan Albert06feee92021-03-19 15:06:02 -0700998
Ivan Lozano1921e802021-05-20 13:39:16 -0400999 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1000 // RECOVERY_SNAPSHOT_VERSION is current.
1001 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1002 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1003 lib.collectHeadersForSnapshot(ctx, deps)
1004 }
1005 }
1006
Jiyong Park459feca2020-12-15 11:02:21 +09001007 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001008 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001009 // If the module has been specifically configure to not be installed then
1010 // hide from make as otherwise it will break when running inside make as the
1011 // output path to install will not be specified. Not all uninstallable
1012 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001013 // side dependencies. In particular, proc-macros need to be captured in the
1014 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001015 mod.HideFromMake()
1016 } else if !mod.installable(apexInfo) {
1017 mod.SkipInstall()
1018 }
1019
1020 // Still call install though, the installs will be stored as PackageSpecs to allow
1021 // using the outputs in a genrule.
1022 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001023 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001024 if ctx.Failed() {
1025 return
1026 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001027 }
Chris Wailes74be7642021-07-22 16:20:28 -07001028
1029 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030 }
1031}
1032
1033func (mod *Module) deps(ctx DepsContext) Deps {
1034 deps := Deps{}
1035
1036 if mod.compiler != nil {
1037 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001038 }
1039 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001040 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001041 }
1042
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001043 if mod.coverage != nil {
1044 deps = mod.coverage.deps(ctx, deps)
1045 }
1046
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001047 if mod.sanitize != nil {
1048 deps = mod.sanitize.deps(ctx, deps)
1049 }
1050
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1052 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001053 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1055 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1056 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001057 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001058 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001059 return deps
1060
1061}
1062
Ivan Lozanoffee3342019-08-27 12:03:00 -07001063type dependencyTag struct {
1064 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001065 name string
1066 library bool
1067 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001068 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069}
1070
Jiyong Park65b62242020-11-25 12:44:59 +09001071// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1072// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1073func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001074 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001075}
1076
1077var _ android.InstallNeededDependencyTag = dependencyTag{}
1078
Colin Cross65cb3142021-12-10 23:05:02 +00001079func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1080 if d.library && d.dynamic {
1081 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1082 }
1083 return nil
1084}
1085
1086var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1087
Ivan Lozanoffee3342019-08-27 12:03:00 -07001088var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001089 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1090 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001091 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001092 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001093 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001094 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001095 dataLibDepTag = dependencyTag{name: "data lib"}
1096 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001097)
1098
Jiyong Park99644e92020-11-17 22:21:02 +09001099func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1100 tag, ok := depTag.(dependencyTag)
1101 return ok && tag == dylibDepTag
1102}
1103
Jiyong Park94e22fd2021-04-08 18:19:15 +09001104func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1105 tag, ok := depTag.(dependencyTag)
1106 return ok && tag == rlibDepTag
1107}
1108
Matthew Maurer0f003b12020-06-29 14:34:06 -07001109type autoDep struct {
1110 variation string
1111 depTag dependencyTag
1112}
1113
1114var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001115 rlibVariation = "rlib"
1116 dylibVariation = "dylib"
1117 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1118 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001119)
1120
1121type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001122 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001123}
1124
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001125func (mod *Module) begin(ctx BaseModuleContext) {
1126 if mod.coverage != nil {
1127 mod.coverage.begin(ctx)
1128 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001129 if mod.sanitize != nil {
1130 mod.sanitize.begin(ctx)
1131 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001132}
1133
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001134func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001135 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001136 return p.prebuilt()
1137 }
1138 return nil
1139}
1140
Justin Yun24b246a2023-03-16 10:36:16 +09001141func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1142 if rustDep, ok := dep.(*Module); ok {
1143 // Use base module name for snapshots when exporting to Makefile.
1144 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1145 baseName := rustDep.BaseModuleName()
1146 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1147 }
1148 }
1149 return cc.MakeLibName(ctx, c, dep, depName)
1150}
1151
Ivan Lozanoffee3342019-08-27 12:03:00 -07001152func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1153 var depPaths PathDeps
1154
1155 directRlibDeps := []*Module{}
1156 directDylibDeps := []*Module{}
1157 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001158 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001159 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001160 directSrcProvidersDeps := []*Module{}
1161 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001162
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001163 // For the dependency from platform to apex, use the latest stubs
1164 mod.apexSdkVersion = android.FutureApiLevel
1165 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1166 if !apexInfo.IsForPlatform() {
1167 mod.apexSdkVersion = apexInfo.MinSdkVersion
1168 }
1169
1170 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1171 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1172 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1173 // (b/144430859)
1174 mod.apexSdkVersion = android.FutureApiLevel
1175 }
1176
Spandan Das604f3762023-03-16 22:51:40 +00001177 skipModuleList := map[string]bool{}
1178
1179 var apiImportInfo multitree.ApiImportInfo
1180 hasApiImportInfo := false
1181
1182 ctx.VisitDirectDeps(func(dep android.Module) {
1183 if dep.Name() == "api_imports" {
1184 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1185 hasApiImportInfo = true
1186 }
1187 })
1188
1189 if hasApiImportInfo {
1190 targetStubModuleList := map[string]string{}
1191 targetOrigModuleList := map[string]string{}
1192
1193 // Search for dependency which both original module and API imported library with APEX stub exists
1194 ctx.VisitDirectDeps(func(dep android.Module) {
1195 depName := ctx.OtherModuleName(dep)
1196 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1197 targetStubModuleList[apiLibrary] = depName
1198 }
1199 })
1200 ctx.VisitDirectDeps(func(dep android.Module) {
1201 depName := ctx.OtherModuleName(dep)
1202 if origLibrary, ok := targetStubModuleList[depName]; ok {
1203 targetOrigModuleList[origLibrary] = depName
1204 }
1205 })
1206
1207 // Decide which library should be used between original and API imported library
1208 ctx.VisitDirectDeps(func(dep android.Module) {
1209 depName := ctx.OtherModuleName(dep)
1210 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1211 if cc.ShouldUseStubForApex(ctx, dep) {
1212 skipModuleList[depName] = true
1213 } else {
1214 skipModuleList[apiLibrary] = true
1215 }
1216 }
1217 })
1218 }
1219
Ivan Lozanoffee3342019-08-27 12:03:00 -07001220 ctx.VisitDirectDeps(func(dep android.Module) {
1221 depName := ctx.OtherModuleName(dep)
1222 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001223
Spandan Das604f3762023-03-16 22:51:40 +00001224 if _, exists := skipModuleList[depName]; exists {
1225 return
1226 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001227
1228 if depTag == android.DarwinUniversalVariantTag {
1229 return
1230 }
1231
Ivan Lozano89435d12020-07-31 11:01:18 -04001232 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001233 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001234 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001235
Ivan Lozanoffee3342019-08-27 12:03:00 -07001236 switch depTag {
1237 case dylibDepTag:
1238 dylib, ok := rustDep.compiler.(libraryInterface)
1239 if !ok || !dylib.dylib() {
1240 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1241 return
1242 }
1243 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001244 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001245 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1246
Ivan Lozanoffee3342019-08-27 12:03:00 -07001247 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001248
Ivan Lozanoffee3342019-08-27 12:03:00 -07001249 rlib, ok := rustDep.compiler.(libraryInterface)
1250 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001251 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001252 return
1253 }
1254 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001255 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001256 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1257
Ivan Lozanoffee3342019-08-27 12:03:00 -07001258 case procMacroDepTag:
1259 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001260 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001261 }
1262
1263 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001264 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1265 // OS/Arch variant is used.
1266 var helper string
1267 if ctx.Host() {
1268 helper = "missing 'host_supported'?"
1269 } else {
1270 helper = "device module defined?"
1271 }
1272
1273 if dep.Target().Os != ctx.Os() {
1274 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1275 return
1276 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1277 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1278 return
1279 }
1280 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001281 }
1282
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001283 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001284 if depTag != procMacroDepTag {
1285 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1286 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1287 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1288 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001289 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001290 }
1291
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001293 linkFile := rustDep.UnstrippedOutputFile()
1294 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001295 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1296 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001297 }
1298 }
1299
Ivan Lozano89435d12020-07-31 11:01:18 -04001300 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001301 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001302 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001303 if _, ok := ccDep.(*Module); !ok {
1304 if ccDep.Module().Target().Os != ctx.Os() {
1305 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1306 return
1307 }
1308 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1309 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1310 return
1311 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001312 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001313 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001314 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001315 if !ctx.Config().AllowMissingDependencies() {
1316 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1317 } else {
1318 ctx.AddMissingDependencies([]string{depName})
1319 }
1320 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001321 }
1322
Colin Crossa86ea0e2023-08-01 09:57:22 -07001323 linkPath := linkPathFromFilePath(linkObject.Path())
1324
Ivan Lozanoffee3342019-08-27 12:03:00 -07001325 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001326 switch {
1327 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001328 if cc.IsWholeStaticLib(depTag) {
1329 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1330 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001331 if mod.Binary() {
1332 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1333 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1334 // final linkage, pass the args directly to the linker to handle these cases.
1335 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1336 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001337 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001338 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001339 } else {
1340 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 -05001341 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001342 }
1343
1344 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1345 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001346 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001347 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1348
Colin Cross0de8a1e2020-09-18 14:15:30 -07001349 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1350 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1351 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1352 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1353 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001354 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001355
1356 // Record baseLibName for snapshots.
1357 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1358
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001359 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001360 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001361 // For the shared lib dependencies, we may link to the stub variant
1362 // of the dependency depending on the context (e.g. if this
1363 // dependency crosses the APEX boundaries).
1364 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1365
1366 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1367 // object (either from stub or non-stub) to use.
1368 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001369 if !linkObject.Valid() {
1370 if !ctx.Config().AllowMissingDependencies() {
1371 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1372 } else {
1373 ctx.AddMissingDependencies([]string{depName})
1374 }
1375 return
1376 }
Jiyong Park7d55b612021-06-11 17:22:09 +09001377 linkPath = linkPathFromFilePath(linkObject.Path())
1378
Ivan Lozanoffee3342019-08-27 12:03:00 -07001379 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001380 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001381 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1382 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1383 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1384 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001385 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001386
1387 // Record baseLibName for snapshots.
1388 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1389
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001390 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001391 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001392 case cc.IsHeaderDepTag(depTag):
1393 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1394 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1395 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1396 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001397 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001398 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001399 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001400 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001401 }
1402
1403 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001404 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1405 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001406 if linkObject.Valid() {
1407 lib.exportLinkObjects(linkObject.Path())
1408 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001409 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001410 } else {
1411 switch {
1412 case depTag == cc.CrtBeginDepTag:
1413 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1414 case depTag == cc.CrtEndDepTag:
1415 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1416 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001417 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001418
1419 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001420 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001421 // These are usually genrules which don't have per-target variants.
1422 directSrcDeps = append(directSrcDeps, srcDep)
1423 }
1424 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001425 })
1426
1427 var rlibDepFiles RustLibraries
1428 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001429 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001430 }
1431 var dylibDepFiles RustLibraries
1432 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001433 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001434 }
1435 var procMacroDepFiles RustLibraries
1436 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001437 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001438 }
1439
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001440 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001441 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001442 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443 }
1444
Ivan Lozanoffee3342019-08-27 12:03:00 -07001445 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001446 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001447 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001448 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001449 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001450 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001451 }
1452
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001453 var srcProviderDepFiles android.Paths
1454 for _, dep := range directSrcProvidersDeps {
1455 srcs, _ := dep.OutputFiles("")
1456 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1457 }
1458 for _, dep := range directSrcDeps {
1459 srcs := dep.Srcs()
1460 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1461 }
1462
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1464 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001465 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001466 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001467 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001468
1469 // Dedup exported flags from dependencies
1470 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001471 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001473 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1474 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1475 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476
1477 return depPaths
1478}
1479
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001480func (mod *Module) InstallInData() bool {
1481 if mod.compiler == nil {
1482 return false
1483 }
1484 return mod.compiler.inData()
1485}
1486
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001487func (mod *Module) InstallInRamdisk() bool {
1488 return mod.InRamdisk()
1489}
1490
1491func (mod *Module) InstallInVendorRamdisk() bool {
1492 return mod.InVendorRamdisk()
1493}
1494
1495func (mod *Module) InstallInRecovery() bool {
1496 return mod.InRecovery()
1497}
1498
Ivan Lozanoffee3342019-08-27 12:03:00 -07001499func linkPathFromFilePath(filepath android.Path) string {
1500 return strings.Split(filepath.String(), filepath.Base())[0]
1501}
Ivan Lozanod648c432020-02-06 12:05:10 -05001502
Spandan Das604f3762023-03-16 22:51:40 +00001503// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1504func (r *Module) usePublicApi() bool {
1505 return r.Device() && r.UseSdk()
1506}
1507
1508// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1509func (r *Module) useVendorApi() bool {
1510 return r.Device() && (r.InVendor() || r.InProduct())
1511}
1512
Ivan Lozanoffee3342019-08-27 12:03:00 -07001513func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1514 ctx := &depsContext{
1515 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001516 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001517
1518 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001519 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001520 var snapshotInfo *cc.SnapshotInfo
1521
Kiyoung Kim487689e2022-07-26 09:48:22 +09001522 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001523 if mod.usePublicApi() || mod.useVendorApi() {
1524 for idx, lib := range deps.SharedLibs {
1525 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1526 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001527 }
1528
Ivan Lozano1921e802021-05-20 13:39:16 -04001529 if ctx.Os() == android.Android {
1530 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1531 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001532
Ivan Lozano2b081132020-09-08 12:46:52 -04001533 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001534 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001535 stdLinkage = "rlib-std"
1536 }
1537
1538 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001539
Ivan Lozano2b081132020-09-08 12:46:52 -04001540 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1541 rlibDepVariations = append(rlibDepVariations,
1542 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1543 }
1544
Ivan Lozano1921e802021-05-20 13:39:16 -04001545 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001546 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001547 for _, lib := range deps.Rlibs {
1548 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001549 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001550
Ivan Lozano2d407632022-04-07 12:59:11 -04001551 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001552 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001553
1554 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001555 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1556 for _, lib := range deps.Dylibs {
1557 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1558 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001559
Ivan Lozano1921e802021-05-20 13:39:16 -04001560 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001561 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1562 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001563 for _, lib := range deps.Rustlibs {
1564 if autoDep.depTag == rlibDepTag {
1565 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001566 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001567 } else {
1568 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1569 // Check for the existence of the dylib deptag variant. Select it if available,
1570 // otherwise select the rlib variant.
1571 autoDepVariations := append(commonDepVariations,
1572 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001573
1574 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1575
1576 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1577 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001578 } else {
1579 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001580 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001581 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001582 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001583 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001584 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001585 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001586 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001587 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001588 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001589 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001590 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001591 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001592 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001593 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001594 for _, lib := range deps.Stdlibs {
1595 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1596 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001597 }
1598 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001599
1600 for _, lib := range deps.SharedLibs {
1601 depTag := cc.SharedDepTag()
1602 name, version := cc.StubsLibNameAndVersion(lib)
1603
1604 variations := []blueprint.Variation{
1605 {Mutator: "link", Variation: "shared"},
1606 }
Spandan Das604f3762023-03-16 22:51:40 +00001607 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1608 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1609 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1610 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1611 }
1612
1613 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1614 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1615 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001616 }
1617
1618 for _, lib := range deps.WholeStaticLibs {
1619 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001620 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001621
1622 actx.AddVariationDependencies([]blueprint.Variation{
1623 {Mutator: "link", Variation: "static"},
1624 }, depTag, lib)
1625 }
1626
1627 for _, lib := range deps.StaticLibs {
1628 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001629 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001630
1631 actx.AddVariationDependencies([]blueprint.Variation{
1632 {Mutator: "link", Variation: "static"},
1633 }, depTag, lib)
1634 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001635
Zach Johnson3df4e632020-11-06 11:56:27 -08001636 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1637
Colin Cross565cafd2020-09-25 18:47:38 -07001638 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001639 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001640 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001641 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001642 }
Colin Crossfe605e12022-01-23 20:46:16 -08001643 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001644 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001645 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001646 }
1647
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001648 if mod.sourceProvider != nil {
1649 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1650 bindgen.Properties.Custom_bindgen != "" {
1651 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1652 bindgen.Properties.Custom_bindgen)
1653 }
1654 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001655
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001656 actx.AddVariationDependencies([]blueprint.Variation{
1657 {Mutator: "link", Variation: "shared"},
1658 }, dataLibDepTag, deps.DataLibs...)
1659
1660 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1661
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001662 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001663 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001664
1665 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001666}
1667
Ivan Lozano2d407632022-04-07 12:59:11 -04001668// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001669func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1670 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001671 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1672}
1673
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001674func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1675 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1676 actx.AddVariationDependencies(variations, depTag, lib)
1677}
1678
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001679func BeginMutator(ctx android.BottomUpMutatorContext) {
1680 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1681 mod.beginMutator(ctx)
1682 }
1683}
1684
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001685func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1686 ctx := &baseModuleContext{
1687 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001688 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001689
1690 mod.begin(ctx)
1691}
1692
Ivan Lozanoffee3342019-08-27 12:03:00 -07001693func (mod *Module) Name() string {
1694 name := mod.ModuleBase.Name()
1695 if p, ok := mod.compiler.(interface {
1696 Name(string) string
1697 }); ok {
1698 name = p.Name(name)
1699 }
1700 return name
1701}
1702
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001703func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001704 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001705 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001706 }
1707}
1708
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001709var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001710var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001711
1712func (mod *Module) HostToolPath() android.OptionalPath {
1713 if !mod.Host() {
1714 return android.OptionalPath{}
1715 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001716 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1717 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001718 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1719 // Even though proc-macros aren't strictly "tools", since they target the compiler
1720 // and act as compiler plugins, we treat them similarly.
1721 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001722 }
1723 return android.OptionalPath{}
1724}
1725
Jiyong Park99644e92020-11-17 22:21:02 +09001726var _ android.ApexModule = (*Module)(nil)
1727
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001728func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001729 return String(mod.Properties.Min_sdk_version)
1730}
1731
Jiyong Park45bf82e2020-12-15 22:29:02 +09001732// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001733func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001734 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001735 if minSdkVersion == "apex_inherit" {
1736 return nil
1737 }
1738 if minSdkVersion == "" {
1739 return fmt.Errorf("min_sdk_version is not specificed")
1740 }
1741
1742 // Not using nativeApiLevelFromUser because the context here is not
1743 // necessarily a native context.
1744 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1745 if err != nil {
1746 return err
1747 }
1748
1749 if ver.GreaterThan(sdkVersion) {
1750 return fmt.Errorf("newer SDK(%v)", ver)
1751 }
Jiyong Park99644e92020-11-17 22:21:02 +09001752 return nil
1753}
1754
Jiyong Park45bf82e2020-12-15 22:29:02 +09001755// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001756func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1757 depTag := ctx.OtherModuleDependencyTag(dep)
1758
1759 if ccm, ok := dep.(*cc.Module); ok {
1760 if ccm.HasStubsVariants() {
1761 if cc.IsSharedDepTag(depTag) {
1762 // dynamic dep to a stubs lib crosses APEX boundary
1763 return false
1764 }
1765 if cc.IsRuntimeDepTag(depTag) {
1766 // runtime dep to a stubs lib also crosses APEX boundary
1767 return false
1768 }
1769
1770 if cc.IsHeaderDepTag(depTag) {
1771 return false
1772 }
1773 }
1774 if mod.Static() && cc.IsSharedDepTag(depTag) {
1775 // shared_lib dependency from a static lib is considered as crossing
1776 // the APEX boundary because the dependency doesn't actually is
1777 // linked; the dependency is used only during the compilation phase.
1778 return false
1779 }
1780 }
1781
Matthew Maurer581b6d82022-09-29 16:46:25 -07001782 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001783 return false
1784 }
1785
1786 return true
1787}
1788
1789// Overrides ApexModule.IsInstallabeToApex()
1790func (mod *Module) IsInstallableToApex() bool {
1791 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001792 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001793 return true
1794 }
1795 if _, ok := mod.compiler.(*binaryDecorator); ok {
1796 return true
1797 }
1798 }
1799 return false
1800}
1801
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001802// If a library file has a "lib" prefix, extract the library name without the prefix.
1803func libNameFromFilePath(filepath android.Path) (string, bool) {
1804 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1805 if strings.HasPrefix(libName, "lib") {
1806 libName = libName[3:]
1807 return libName, true
1808 }
1809 return "", false
1810}
1811
Sasha Smundaka76acba2022-04-18 20:12:56 -07001812func kytheExtractRustFactory() android.Singleton {
1813 return &kytheExtractRustSingleton{}
1814}
1815
1816type kytheExtractRustSingleton struct {
1817}
1818
1819func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1820 var xrefTargets android.Paths
1821 ctx.VisitAllModules(func(module android.Module) {
1822 if rustModule, ok := module.(xref); ok {
1823 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1824 }
1825 })
1826 if len(xrefTargets) > 0 {
1827 ctx.Phony("xref_rust", xrefTargets...)
1828 }
1829}
1830
Jihoon Kangf78a8902022-09-01 22:47:07 +00001831func (c *Module) Partition() string {
1832 return ""
1833}
1834
Ivan Lozanoffee3342019-08-27 12:03:00 -07001835var Bool = proptools.Bool
1836var BoolDefault = proptools.BoolDefault
1837var String = proptools.String
1838var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001839
1840var _ android.OutputFileProducer = (*Module)(nil)