blob: 64288595f59df349ad3a8601a9f3c105023d295a [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"
Wei Lia1aa2972024-06-21 13:08:51 -070019 "strconv"
Chris Parsons637458d2023-09-19 20:09:00 +000020 "strings"
21
Sasha Smundaka76acba2022-04-18 20:12:56 -070022 "android/soong/bloaty"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090023
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "github.com/google/blueprint"
Colin Crossa14fb6a2024-10-23 16:57:06 -070025 "github.com/google/blueprint/depset"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "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"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
33)
34
35var pctx = android.NewPackageContext("android/soong/rust")
36
Yu Liu8024b922024-12-20 23:31:32 +000037type LibraryInfo struct {
38 Rlib bool
39 Dylib bool
40}
41
42type CompilerInfo struct {
43 StdLinkageForDevice RustLinkage
44 StdLinkageForNonDevice RustLinkage
45 NoStdlibs bool
46 LibraryInfo *LibraryInfo
47}
48
49type ProtobufDecoratorInfo struct{}
50
51type SourceProviderInfo struct {
52 ProtobufDecoratorInfo *ProtobufDecoratorInfo
53}
54
55type RustInfo struct {
56 AndroidMkSuffix string
57 RustSubName string
58 TransitiveAndroidMkSharedLibs depset.DepSet[string]
59 CompilerInfo *CompilerInfo
60 SnapshotInfo *cc.SnapshotInfo
61 SourceProviderInfo *SourceProviderInfo
62}
63
64var RustInfoProvider = blueprint.NewProvider[*RustInfo]()
65
Ivan Lozanoffee3342019-08-27 12:03:00 -070066func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070067 android.RegisterModuleType("rust_defaults", defaultsFactory)
Colin Cross8a49a3d2024-05-20 12:22:27 -070068 android.PreDepsMutators(registerPreDepsMutators)
69 android.PostDepsMutators(registerPostDepsMutators)
Inseob Kim3b244062023-07-11 13:31:36 +090070 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070071 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020072 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000073 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070074}
75
Colin Cross8a49a3d2024-05-20 12:22:27 -070076func registerPreDepsMutators(ctx android.RegisterMutatorsContext) {
77 ctx.Transition("rust_libraries", &libraryTransitionMutator{})
78 ctx.Transition("rust_stdlinkage", &libstdTransitionMutator{})
Colin Cross8a962802024-10-09 15:29:27 -070079 ctx.BottomUp("rust_begin", BeginMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070080}
81
82func registerPostDepsMutators(ctx android.RegisterMutatorsContext) {
Colin Cross8a962802024-10-09 15:29:27 -070083 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator)
Colin Cross8a49a3d2024-05-20 12:22:27 -070084}
85
Ivan Lozanoffee3342019-08-27 12:03:00 -070086type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040087 GlobalRustFlags []string // Flags that apply globally to rust
88 GlobalLinkFlags []string // Flags that apply globally to linker
89 RustFlags []string // Flags that apply to rust
90 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020091 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070092 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070093 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040094 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020095 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070096 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070097}
98
99type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -0500100 AndroidMkRlibs []string `blueprint:"mutated"`
101 AndroidMkDylibs []string `blueprint:"mutated"`
102 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -0500103 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1dbfa142024-03-29 14:48:11 +0000104 AndroidMkHeaderLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -0400105
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900106 ImageVariation string `blueprint:"mutated"`
107 VndkVersion string `blueprint:"mutated"`
108 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -0500109
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400110 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
111 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
112 // appended before SubName.
113 RustSubName string `blueprint:"mutated"`
114
Ivan Lozano6a884432020-12-02 09:15:16 -0500115 // Set by imageMutator
Jihoon Kang47e91842024-06-19 00:51:16 +0000116 ProductVariantNeeded bool `blueprint:"mutated"`
117 VendorVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500118 CoreVariantNeeded bool `blueprint:"mutated"`
119 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -0700120 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -0800121 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500122 ExtraVariants []string `blueprint:"mutated"`
123
Ivan Lozanoa2268632021-07-22 10:52:06 -0400124 // Allows this module to use non-APEX version of libraries. Useful
125 // for building binaries that are started before APEXes are activated.
126 Bootstrap *bool
127
Ivan Lozano1921e802021-05-20 13:39:16 -0400128 // Used by vendor snapshot to record dependencies from snapshot modules.
129 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +0900130 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400131 SnapshotRlibs []string `blueprint:"mutated"`
132 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400133
Matthew Maurerc6868382021-07-13 14:12:37 -0700134 // Make this module available when building for ramdisk.
135 // On device without a dedicated recovery partition, the module is only
136 // available after switching root into
137 // /first_stage_ramdisk. To expose the module before switching root, install
138 // the recovery variant instead.
139 Ramdisk_available *bool
140
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500141 // Make this module available when building for vendor ramdisk.
142 // On device without a dedicated recovery partition, the module is only
143 // available after switching root into
144 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800145 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500146 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400147
Ivan Lozano1921e802021-05-20 13:39:16 -0400148 // Normally Soong uses the directory structure to decide which modules
149 // should be included (framework) or excluded (non-framework) from the
150 // different snapshots (vendor, recovery, etc.), but this property
151 // allows a partner to exclude a module normally thought of as a
152 // framework module from the vendor snapshot.
153 Exclude_from_vendor_snapshot *bool
154
155 // Normally Soong uses the directory structure to decide which modules
156 // should be included (framework) or excluded (non-framework) from the
157 // different snapshots (vendor, recovery, etc.), but this property
158 // allows a partner to exclude a module normally thought of as a
159 // framework module from the recovery snapshot.
160 Exclude_from_recovery_snapshot *bool
161
Matthew Maurer460ee942021-02-11 12:31:46 -0800162 // Make this module available when building for recovery
163 Recovery_available *bool
164
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000165 // The API level that this module is built against. The APIs of this API level will be
166 // visible at build time, but use of any APIs newer than min_sdk_version will render the
167 // module unloadable on older devices. In the future it will be possible to weakly-link new
168 // APIs, making the behavior match Java: such modules will load on older devices, but
169 // calling new APIs on devices that do not support them will result in a crash.
170 //
171 // This property has the same behavior as sdk_version does for Java modules. For those
172 // familiar with Android Gradle, the property behaves similarly to how compileSdkVersion
173 // does for Java code.
174 //
175 // In addition, setting this property causes two variants to be built, one for the platform
176 // and one for apps.
177 Sdk_version *string
178
179 // Minimum OS API level supported by this C or C++ module. This property becomes the value
180 // of the __ANDROID_API__ macro. When the C or C++ module is included in an APEX or an APK,
181 // this property is also used to ensure that the min_sdk_version of the containing module is
182 // not older (i.e. less) than this module's min_sdk_version. When not set, this property
183 // defaults to the value of sdk_version. When this is set to "apex_inherit", this tracks
184 // min_sdk_version of the containing APEX. When the module
185 // is not built for an APEX, "apex_inherit" defaults to sdk_version.
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500186 Min_sdk_version *string
187
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000188 // Variant is an SDK variant created by sdkMutator
189 IsSdkVariant bool `blueprint:"mutated"`
190
191 // Set by factories of module types that can only be referenced from variants compiled against
192 // the SDK.
193 AlwaysSdk bool `blueprint:"mutated"`
194
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900195 HideFromMake bool `blueprint:"mutated"`
196 PreventInstall bool `blueprint:"mutated"`
197
198 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700199}
200
201type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700202 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700203
Ivan Lozano6a884432020-12-02 09:15:16 -0500204 VendorProperties cc.VendorProperties
205
Ivan Lozanoffee3342019-08-27 12:03:00 -0700206 Properties BaseProperties
207
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000208 hod android.HostOrDeviceSupported
209 multilib android.Multilib
210 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211
Ivan Lozano6a884432020-12-02 09:15:16 -0500212 makeLinkType string
213
Yi Kong46c6e592022-01-20 22:55:00 +0800214 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700215 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400216 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200217 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500218 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700219 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400220 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700221 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400222
Ivan Lozano0a468a42024-05-13 21:03:34 -0400223 exportedLinkDirs []string
224
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400225 // Output file to be installed, may be stripped or unstripped.
226 outputFile android.OptionalPath
227
Sasha Smundaka76acba2022-04-18 20:12:56 -0700228 // Cross-reference input file
229 kytheFiles android.Paths
230
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400231 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900232
233 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500234
235 // For apex variants, this is set as apex.min_sdk_version
236 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700237
Colin Crossa14fb6a2024-10-23 16:57:06 -0700238 transitiveAndroidMkSharedLibs depset.DepSet[string]
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000239
240 // Shared flags among stubs build rules of this module
241 sharedFlags cc.SharedFlags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242}
243
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500244func (mod *Module) Header() bool {
245 //TODO: If Rust libraries provide header variants, this needs to be updated.
246 return false
247}
248
249func (mod *Module) SetPreventInstall() {
250 mod.Properties.PreventInstall = true
251}
252
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500253func (mod *Module) SetHideFromMake() {
254 mod.Properties.HideFromMake = true
255}
256
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900257func (mod *Module) HiddenFromMake() bool {
258 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400259}
260
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500261func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500262 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
263 // nil since we need compiler to actually sanitize.
264 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500265}
266
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500267func (mod *Module) IsPrebuilt() bool {
268 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
269 return true
270 }
271 return false
272}
273
Ivan Lozano52767be2019-10-18 14:49:46 -0700274func (mod *Module) SelectedStl() string {
275 return ""
276}
277
Ivan Lozano2b262972019-11-21 12:30:50 -0800278func (mod *Module) NonCcVariants() bool {
279 if mod.compiler != nil {
Ivan Lozano0a468a42024-05-13 21:03:34 -0400280 if library, ok := mod.compiler.(libraryInterface); ok {
281 return library.buildRlib() || library.buildDylib()
Ivan Lozano2b262972019-11-21 12:30:50 -0800282 }
283 }
284 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
285}
286
Ivan Lozano52767be2019-10-18 14:49:46 -0700287func (mod *Module) Static() bool {
288 if mod.compiler != nil {
289 if library, ok := mod.compiler.(libraryInterface); ok {
290 return library.static()
291 }
292 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400293 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700294}
295
296func (mod *Module) Shared() bool {
297 if mod.compiler != nil {
298 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400299 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700300 }
301 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400302 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700303}
304
Ivan Lozanod7586b62021-04-01 09:49:36 -0400305func (mod *Module) Dylib() bool {
306 if mod.compiler != nil {
307 if library, ok := mod.compiler.(libraryInterface); ok {
308 return library.dylib()
309 }
310 }
311 return false
312}
313
Ivan Lozanod106efe2023-09-21 23:30:26 -0400314func (mod *Module) Source() bool {
315 if mod.compiler != nil {
316 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
317 return library.source()
318 }
319 }
320 return false
321}
322
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400323func (mod *Module) RlibStd() bool {
324 if mod.compiler != nil {
325 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
326 return library.rlibStd()
327 }
328 }
329 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
330}
331
Ivan Lozanod7586b62021-04-01 09:49:36 -0400332func (mod *Module) Rlib() bool {
333 if mod.compiler != nil {
334 if library, ok := mod.compiler.(libraryInterface); ok {
335 return library.rlib()
336 }
337 }
338 return false
339}
340
341func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400342 if binary, ok := mod.compiler.(binaryInterface); ok {
343 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400344 }
345 return false
346}
347
Justin Yun5e035862021-06-29 20:50:37 +0900348func (mod *Module) StaticExecutable() bool {
349 if !mod.Binary() {
350 return false
351 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400352 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900353}
354
Ashutosh Agarwal46e4fad2024-08-27 17:13:12 +0000355func (mod *Module) ApexExclude() bool {
356 if mod.compiler != nil {
357 if library, ok := mod.compiler.(libraryInterface); ok {
358 return library.apexExclude()
359 }
360 }
361 return false
362}
363
Ivan Lozanod7586b62021-04-01 09:49:36 -0400364func (mod *Module) Object() bool {
365 // Rust has no modules which produce only object files.
366 return false
367}
368
Ivan Lozano52767be2019-10-18 14:49:46 -0700369func (mod *Module) Toc() android.OptionalPath {
370 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400371 if lib, ok := mod.compiler.(libraryInterface); ok {
372 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700373 }
374 }
375 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
376}
377
Colin Crossc511bc52020-04-07 16:50:32 +0000378func (mod *Module) UseSdk() bool {
379 return false
380}
381
Ivan Lozanod7586b62021-04-01 09:49:36 -0400382func (mod *Module) RelativeInstallPath() string {
383 if mod.compiler != nil {
384 return mod.compiler.relativeInstallPath()
385 }
386 return ""
387}
388
Ivan Lozano52767be2019-10-18 14:49:46 -0700389func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500390 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700391}
392
Jiyong Park7d55b612021-06-11 17:22:09 +0900393func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400394 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900395}
396
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400397func (mod *Module) SubName() string {
398 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700399}
400
Ivan Lozanof1868af2022-04-12 13:08:36 -0400401func (mod *Module) IsVndkPrebuiltLibrary() bool {
402 // Rust modules do not provide VNDK prebuilts
403 return false
404}
405
406func (mod *Module) IsVendorPublicLibrary() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000407 // Rust modules do not currently support vendor_public_library
408 return false
Ivan Lozanof1868af2022-04-12 13:08:36 -0400409}
410
411func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
412 // Rust modules to not provide Sdk variants
413 return false
414}
415
Colin Cross127bb8b2020-12-16 16:46:01 -0800416func (c *Module) IsVndkPrivate() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000417 // Rust modules do not currently support VNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800418 return false
419}
420
421func (c *Module) IsLlndk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000422 // Rust modules do not currently support LLNDK variants
Colin Cross127bb8b2020-12-16 16:46:01 -0800423 return false
424}
425
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400426func (mod *Module) KernelHeadersDecorator() bool {
427 return false
428}
429
Colin Cross1f3f1302021-04-26 18:37:44 -0700430func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000431 // Rust modules do not currently support LLNDK variants
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400432 return false
433}
434
Colin Cross5271fea2021-04-27 13:06:04 -0700435func (m *Module) NeedsVendorPublicLibraryVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000436 // Rust modules do not currently support vendor_public_library
Colin Cross5271fea2021-04-27 13:06:04 -0700437 return false
438}
439
Ivan Lozanod7586b62021-04-01 09:49:36 -0400440func (mod *Module) HasLlndkStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000441 // Rust modules do not currently support LLNDK stubs
Ivan Lozanod7586b62021-04-01 09:49:36 -0400442 return false
443}
444
Ivan Lozano52767be2019-10-18 14:49:46 -0700445func (mod *Module) SdkVersion() string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000446 return String(mod.Properties.Sdk_version)
Ivan Lozano52767be2019-10-18 14:49:46 -0700447}
448
Colin Crossc511bc52020-04-07 16:50:32 +0000449func (mod *Module) AlwaysSdk() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000450 return mod.Properties.AlwaysSdk
Colin Crossc511bc52020-04-07 16:50:32 +0000451}
452
Jiyong Park2286afd2020-06-16 21:58:53 +0900453func (mod *Module) IsSdkVariant() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000454 return mod.Properties.IsSdkVariant
Jiyong Park2286afd2020-06-16 21:58:53 +0900455}
456
Colin Cross1348ce32020-10-01 13:37:16 -0700457func (mod *Module) SplitPerApiLevel() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000458 return cc.CanUseSdk(mod) && mod.IsCrt()
Colin Cross1348ce32020-10-01 13:37:16 -0700459}
460
Sasha Smundaka76acba2022-04-18 20:12:56 -0700461func (mod *Module) XrefRustFiles() android.Paths {
462 return mod.kytheFiles
463}
464
Ivan Lozanoffee3342019-08-27 12:03:00 -0700465type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400466 Dylibs []string
467 Rlibs []string
468 Rustlibs []string
469 Stdlibs []string
470 ProcMacros []string
471 SharedLibs []string
472 StaticLibs []string
473 WholeStaticLibs []string
474 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400476 // Used for data dependencies adjacent to tests
477 DataLibs []string
478 DataBins []string
479
Colin Crossfe605e12022-01-23 20:46:16 -0800480 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481}
482
483type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700484 DyLibs RustLibraries
485 RLibs RustLibraries
486 SharedLibs android.Paths
487 SharedLibDeps android.Paths
488 StaticLibs android.Paths
489 ProcMacros RustLibraries
490 AfdoProfiles android.Paths
Ivan Lozanof4589012024-11-20 22:18:11 +0000491 LinkerDeps android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500492
493 // depFlags and depLinkFlags are rustc and linker (clang) flags.
494 depFlags []string
495 depLinkFlags []string
496
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400497 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500498 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000499 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700500 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700501
Ivan Lozano0a468a42024-05-13 21:03:34 -0400502 // exportedLinkDirs are exported linkDirs for direct rlib dependencies to
503 // cc_library_static dependants of rlibs.
504 // Track them separately from linkDirs so superfluous -L flags don't get emitted.
505 exportedLinkDirs []string
506
Ivan Lozano45901ed2020-07-24 16:05:01 -0400507 // Used by bindgen modules which call clang
508 depClangFlags []string
509 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400510 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400511 depSystemIncludePaths android.Paths
512
Colin Crossfe605e12022-01-23 20:46:16 -0800513 CrtBegin android.Paths
514 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700515
516 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500517 SrcDeps android.Paths
518 srcProviderFiles android.Paths
Colin Crossb614cd42024-10-11 12:52:21 -0700519
520 directImplementationDeps android.Paths
521 transitiveImplementationDeps []depset.DepSet[android.Path]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700522}
523
524type RustLibraries []RustLibrary
525
526type RustLibrary struct {
527 Path android.Path
528 CrateName string
529}
530
Matthew Maurerbb3add12020-06-25 09:34:12 -0700531type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000532 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700533 exportLinkObjects(...string)
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 {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000541 linkDirs []string
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400542 ccLinkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700543 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700544}
545
Wen-yi Chu41326c12023-09-22 03:58:59 +0000546func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
547 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700548}
549
Colin Cross004bd3f2023-10-02 11:39:17 -0700550func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
551 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400552}
553
Colin Cross0de8a1e2020-09-18 14:15:30 -0700554func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800555 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700556 LinkDirs: flagExporter.linkDirs,
557 LinkObjects: flagExporter.linkObjects,
558 })
559}
560
Matthew Maurerbb3add12020-06-25 09:34:12 -0700561var _ exportedFlagsProducer = (*flagExporter)(nil)
562
563func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700564 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700565}
566
Colin Cross0de8a1e2020-09-18 14:15:30 -0700567type FlagExporterInfo struct {
568 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000569 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700570 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700571}
572
Colin Crossbc7d76c2023-12-12 16:39:03 -0800573var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700574
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400575func (mod *Module) isCoverageVariant() bool {
576 return mod.coverage.Properties.IsCoverageVariant
577}
578
579var _ cc.Coverage = (*Module)(nil)
580
Colin Crosse1a85552024-06-14 12:17:37 -0700581func (mod *Module) IsNativeCoverageNeeded(ctx cc.IsNativeCoverageNeededContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400582 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
583}
584
Ivan Lozanod7586b62021-04-01 09:49:36 -0400585func (mod *Module) VndkVersion() string {
586 return mod.Properties.VndkVersion
587}
588
Ivan Lozano0a468a42024-05-13 21:03:34 -0400589func (mod *Module) ExportedCrateLinkDirs() []string {
590 return mod.exportedLinkDirs
591}
592
Ivan Lozanod7586b62021-04-01 09:49:36 -0400593func (mod *Module) PreventInstall() bool {
594 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400595}
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000596func (c *Module) ForceDisableSanitizers() {
597 c.sanitize.Properties.ForceDisable = true
598}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400599
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400600func (mod *Module) MarkAsCoverageVariant(coverage bool) {
601 mod.coverage.Properties.IsCoverageVariant = coverage
602}
603
604func (mod *Module) EnableCoverageIfNeeded() {
605 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
608func defaultsFactory() android.Module {
609 return DefaultsFactory()
610}
611
612type Defaults struct {
613 android.ModuleBase
614 android.DefaultsModuleBase
615}
616
617func DefaultsFactory(props ...interface{}) android.Module {
618 module := &Defaults{}
619
620 module.AddProperties(props...)
621 module.AddProperties(
622 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800623 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500624 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100625 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400626 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700627 &BaseCompilerProperties{},
628 &BinaryCompilerProperties{},
629 &LibraryCompilerProperties{},
630 &ProcMacroCompilerProperties{},
631 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400632 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700633 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400634 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400635 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200636 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500637 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000638 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700639 )
640
641 android.InitDefaultsModule(module)
642 return module
643}
644
645func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700646 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700647}
648
Ivan Lozano183a3212019-10-18 14:18:45 -0700649func (mod *Module) CcLibrary() bool {
650 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500651 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700652 return true
653 }
654 }
655 return false
656}
657
658func (mod *Module) CcLibraryInterface() bool {
659 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400660 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
661 // VariantIs{Static,Shared} is set.
Ivan Lozano806efd32024-12-11 21:38:53 +0000662 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic() || lib.buildRlib()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700663 return true
664 }
665 }
666 return false
667}
668
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400669func (mod *Module) RustLibraryInterface() bool {
670 if mod.compiler != nil {
671 if _, ok := mod.compiler.(libraryInterface); ok {
672 return true
673 }
674 }
675 return false
676}
677
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500678func (mod *Module) IsFuzzModule() bool {
679 if _, ok := mod.compiler.(*fuzzDecorator); ok {
680 return true
681 }
682 return false
683}
684
685func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
686 return mod.FuzzModule
687}
688
689func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
690 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
691 return fuzzer.fuzzPackagedModule
692 }
693 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
694}
695
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000696func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500697 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
698 return fuzzer.sharedLibraries
699 }
700 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
701}
702
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400703func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400704 if mod.compiler != nil {
705 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400706 }
707 return nil
708}
709
Ivan Lozano183a3212019-10-18 14:18:45 -0700710func (mod *Module) SetStatic() {
711 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700712 if library, ok := mod.compiler.(libraryInterface); ok {
713 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700714 return
715 }
716 }
717 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
718}
719
720func (mod *Module) SetShared() {
721 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700722 if library, ok := mod.compiler.(libraryInterface); ok {
723 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700724 return
725 }
726 }
727 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
728}
729
Ivan Lozano183a3212019-10-18 14:18:45 -0700730func (mod *Module) BuildStaticVariant() bool {
731 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700732 if library, ok := mod.compiler.(libraryInterface); ok {
733 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700734 }
735 }
736 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
737}
738
Ivan Lozanofd47b1a2024-05-17 14:13:41 -0400739func (mod *Module) BuildRlibVariant() bool {
740 if mod.compiler != nil {
741 if library, ok := mod.compiler.(libraryInterface); ok {
742 return library.buildRlib()
743 }
744 }
745 panic(fmt.Errorf("BuildRlibVariant called on non-library module: %q", mod.BaseModuleName()))
746}
747
Ivan Lozano183a3212019-10-18 14:18:45 -0700748func (mod *Module) BuildSharedVariant() bool {
749 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700750 if library, ok := mod.compiler.(libraryInterface); ok {
751 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700752 }
753 }
754 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
755}
756
Ivan Lozano183a3212019-10-18 14:18:45 -0700757func (mod *Module) Module() android.Module {
758 return mod
759}
760
Ivan Lozano183a3212019-10-18 14:18:45 -0700761func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400762 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700763}
764
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400765func (mod *Module) CoverageFiles() android.Paths {
766 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800767 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400768 }
769 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
770}
771
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400772// Rust does not produce gcno files, and therefore does not produce a coverage archive.
773func (mod *Module) CoverageOutputFile() android.OptionalPath {
774 return android.OptionalPath{}
775}
776
777func (mod *Module) IsNdk(config android.Config) bool {
778 return false
779}
780
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000781func (mod *Module) IsStubs() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000782 if lib, ok := mod.compiler.(libraryInterface); ok {
783 return lib.BuildStubs()
784 }
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000785 return false
786}
787
Spandan Das10c41362024-12-03 01:33:09 +0000788func (mod *Module) HasStubsVariants() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000789 if lib, ok := mod.compiler.(libraryInterface); ok {
790 return lib.HasStubsVariants()
791 }
Spandan Das10c41362024-12-03 01:33:09 +0000792 return false
793}
794
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000795func (mod *Module) ApexSdkVersion() android.ApiLevel {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000796 return mod.apexSdkVersion
797}
798
799func (mod *Module) RustApexExclude() bool {
800 return mod.ApexExclude()
801}
802
803func (mod *Module) getSharedFlags() *cc.SharedFlags {
804 shared := &mod.sharedFlags
805 if shared.FlagsMap == nil {
806 shared.NumSharedFlags = 0
807 shared.FlagsMap = make(map[string]string)
808 }
809 return shared
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000810}
811
812func (mod *Module) ImplementationModuleNameForMake(ctx android.BaseModuleContext) string {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000813 name := mod.BaseModuleName()
814 if versioned, ok := mod.compiler.(cc.VersionedInterface); ok {
815 name = versioned.ImplementationModuleName(name)
816 }
817 return name
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000818}
819
820func (mod *Module) Multilib() string {
821 return mod.Arch().ArchType.Multilib
822}
823
824func (mod *Module) IsCrt() bool {
825 // Rust does not currently provide any crt modules.
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400826 return false
827}
828
Jiyong Park459feca2020-12-15 11:02:21 +0900829func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900830 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900831 return false
832 }
833
Jiyong Park459feca2020-12-15 11:02:21 +0900834 // The apex variant is not installable because it is included in the APEX and won't appear
835 // in the system partition as a standalone file.
836 if !apexInfo.IsForPlatform() {
837 return false
838 }
839
Jiyong Parke54f07e2021-04-07 15:08:04 +0900840 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900841}
842
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500843func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800844 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
845 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500846}
847
Ivan Lozano183a3212019-10-18 14:18:45 -0700848var _ cc.LinkableInterface = (*Module)(nil)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000849var _ cc.VersionedLinkableInterface = (*Module)(nil)
Ivan Lozano183a3212019-10-18 14:18:45 -0700850
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851func (mod *Module) Init() android.Module {
852 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500853 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854
Yi Kong46c6e592022-01-20 22:55:00 +0800855 if mod.afdo != nil {
856 mod.AddProperties(mod.afdo.props()...)
857 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858 if mod.compiler != nil {
859 mod.AddProperties(mod.compiler.compilerProps()...)
860 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400861 if mod.coverage != nil {
862 mod.AddProperties(mod.coverage.props()...)
863 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200864 if mod.clippy != nil {
865 mod.AddProperties(mod.clippy.props()...)
866 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400867 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700868 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400869 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500870 if mod.sanitize != nil {
871 mod.AddProperties(mod.sanitize.props()...)
872 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400873
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900875 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876
877 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 return mod
879}
880
881func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
882 return &Module{
883 hod: hod,
884 multilib: multilib,
885 }
886}
887func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
888 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800889 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400890 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200891 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500892 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893 return module
894}
895
896type ModuleContext interface {
897 android.ModuleContext
898 ModuleContextIntf
899}
900
901type BaseModuleContext interface {
902 android.BaseModuleContext
903 ModuleContextIntf
904}
905
906type DepsContext interface {
907 android.BottomUpMutatorContext
908 ModuleContextIntf
909}
910
911type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200912 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914}
915
916type depsContext struct {
917 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918}
919
920type moduleContext struct {
921 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922}
923
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200924type baseModuleContext struct {
925 android.BaseModuleContext
926}
927
928func (ctx *moduleContext) RustModule() *Module {
929 return ctx.Module().(*Module)
930}
931
932func (ctx *moduleContext) toolchain() config.Toolchain {
933 return ctx.RustModule().toolchain(ctx)
934}
935
936func (ctx *depsContext) RustModule() *Module {
937 return ctx.Module().(*Module)
938}
939
940func (ctx *depsContext) toolchain() config.Toolchain {
941 return ctx.RustModule().toolchain(ctx)
942}
943
944func (ctx *baseModuleContext) RustModule() *Module {
945 return ctx.Module().(*Module)
946}
947
948func (ctx *baseModuleContext) toolchain() config.Toolchain {
949 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400950}
951
952func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700953 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
954 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
955 return false
956 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400957 return mod.compiler != nil && mod.compiler.nativeCoverage()
958}
959
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000960func (mod *Module) SetStl(s string) {
961 // STL is a CC concept; do nothing for Rust
962}
963
964func (mod *Module) SetSdkVersion(s string) {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +0000965 mod.Properties.Sdk_version = StringPtr(s)
Ivan Lozano9eaacc82024-10-30 14:28:17 +0000966}
967
968func (mod *Module) SetMinSdkVersion(s string) {
969 mod.Properties.Min_sdk_version = StringPtr(s)
970}
971
972func (mod *Module) VersionedInterface() cc.VersionedInterface {
973 if _, ok := mod.compiler.(cc.VersionedInterface); ok {
974 return mod.compiler.(cc.VersionedInterface)
975 }
976 return nil
977}
978
Ivan Lozanod7586b62021-04-01 09:49:36 -0400979func (mod *Module) EverInstallable() bool {
980 return mod.compiler != nil &&
981 // Check to see whether the module is actually ever installable.
982 mod.compiler.everInstallable()
983}
984
985func (mod *Module) Installable() *bool {
986 return mod.Properties.Installable
987}
988
Ivan Lozano872d5792022-03-23 17:31:39 -0400989func (mod *Module) ProcMacro() bool {
990 if pm, ok := mod.compiler.(procMacroInterface); ok {
991 return pm.ProcMacro()
992 }
993 return false
994}
995
Ivan Lozanoffee3342019-08-27 12:03:00 -0700996func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
997 if mod.cachedToolchain == nil {
998 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
999 }
1000 return mod.cachedToolchain
1001}
1002
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +02001003func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
1004 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
1005}
1006
Ivan Lozanoffee3342019-08-27 12:03:00 -07001007func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
1008}
1009
1010func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
1011 ctx := &moduleContext{
1012 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001013 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014
Colin Crossff694a82023-12-13 15:54:49 -08001015 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +09001016 if !apexInfo.IsForPlatform() {
1017 mod.hideApexVariantFromMake = true
1018 }
1019
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -05001021 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
1022
Ivan Lozanof1868af2022-04-12 13:08:36 -04001023 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -07001024
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 if !toolchain.Supported() {
1026 // This toolchain's unsupported, there's nothing to do for this mod.
1027 return
1028 }
1029
1030 deps := mod.depsToPaths(ctx)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001031 // Export linkDirs for CC rust generatedlibs
1032 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.exportedLinkDirs...)
1033 mod.exportedLinkDirs = append(mod.exportedLinkDirs, deps.linkDirs...)
1034
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035 flags := Flags{
1036 Toolchain: toolchain,
1037 }
1038
Ivan Lozano67eada32021-09-23 11:50:33 -04001039 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +08001040 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -05001041 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +08001042 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 if mod.compiler != nil {
1044 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -04001045 flags = mod.compiler.cfgFlags(ctx, flags)
Jihoon Kang091ffd82024-10-03 01:13:24 +00001046 flags = mod.compiler.featureFlags(ctx, mod, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001047 }
1048 if mod.coverage != nil {
1049 flags, deps = mod.coverage.flags(ctx, flags, deps)
1050 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +02001051 if mod.clippy != nil {
1052 flags, deps = mod.clippy.flags(ctx, flags, deps)
1053 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001054 if mod.sanitize != nil {
1055 flags, deps = mod.sanitize.flags(ctx, flags, deps)
1056 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001057
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001058 // SourceProvider needs to call GenerateSource() before compiler calls
1059 // compile() so it can provide the source. A SourceProvider has
1060 // multiple variants (e.g. source, rlib, dylib). Only the "source"
1061 // variant is responsible for effectively generating the source. The
1062 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001063 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001064 if mod.compiler.(libraryInterface).source() {
1065 mod.sourceProvider.GenerateSource(ctx, deps)
1066 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
1067 } else {
1068 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
1069 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -07001070 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001071 }
Colin Crossa6182ab2024-08-21 10:47:44 -07001072 ctx.CheckbuildFile(mod.sourceProvider.Srcs()...)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001073 }
1074
1075 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +01001076 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001077 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001078 if ctx.Failed() {
1079 return
1080 }
Sasha Smundaka76acba2022-04-18 20:12:56 -07001081 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
Colin Crossa6182ab2024-08-21 10:47:44 -07001082 ctx.CheckbuildFile(buildOutput.outputFile)
Sasha Smundaka76acba2022-04-18 20:12:56 -07001083 if buildOutput.kytheFile != nil {
1084 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1085 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001086 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001087
Dan Albert06feee92021-03-19 15:06:02 -07001088 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
1089
Colin Crossff694a82023-12-13 15:54:49 -08001090 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -04001091 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001092 // If the module has been specifically configure to not be installed then
1093 // hide from make as otherwise it will break when running inside make as the
1094 // output path to install will not be specified. Not all uninstallable
1095 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001096 // side dependencies. In particular, proc-macros need to be captured in the
1097 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001098 mod.HideFromMake()
Spandan Das034af2c2024-10-30 21:45:09 +00001099 mod.SkipInstall()
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001100 } else if !mod.installable(apexInfo) {
1101 mod.SkipInstall()
1102 }
1103
1104 // Still call install though, the installs will be stored as PackageSpecs to allow
1105 // using the outputs in a genrule.
1106 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001107 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001108 if ctx.Failed() {
1109 return
1110 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001111 // Export your own directory as a linkDir
1112 mod.exportedLinkDirs = append(mod.exportedLinkDirs, linkPathFromFilePath(mod.OutputFile().Path()))
1113
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001114 }
Chris Wailes74be7642021-07-22 16:20:28 -07001115
Colin Crossb614cd42024-10-11 12:52:21 -07001116 android.SetProvider(ctx, cc.ImplementationDepInfoProvider, &cc.ImplementationDepInfo{
1117 ImplementationDeps: depset.New(depset.PREORDER, deps.directImplementationDeps, deps.transitiveImplementationDeps),
1118 })
1119
Chris Wailes74be7642021-07-22 16:20:28 -07001120 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001121 }
Wei Lia1aa2972024-06-21 13:08:51 -07001122
Yu Liuf6f85492025-01-13 21:02:36 +00001123 linkableInfo := cc.CreateCommonLinkableInfo(ctx, mod)
Yu Liu8024b922024-12-20 23:31:32 +00001124 linkableInfo.Static = mod.Static()
1125 linkableInfo.Shared = mod.Shared()
1126 linkableInfo.CrateName = mod.CrateName()
1127 linkableInfo.ExportedCrateLinkDirs = mod.ExportedCrateLinkDirs()
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001128 if lib, ok := mod.compiler.(cc.VersionedInterface); ok {
1129 linkableInfo.StubsVersion = lib.StubsVersion()
1130 }
1131
Yu Liu8024b922024-12-20 23:31:32 +00001132 android.SetProvider(ctx, cc.LinkableInfoProvider, linkableInfo)
1133
1134 rustInfo := &RustInfo{
1135 AndroidMkSuffix: mod.AndroidMkSuffix(),
1136 RustSubName: mod.Properties.RustSubName,
1137 TransitiveAndroidMkSharedLibs: mod.transitiveAndroidMkSharedLibs,
1138 }
1139 if mod.compiler != nil {
1140 rustInfo.CompilerInfo = &CompilerInfo{
1141 NoStdlibs: mod.compiler.noStdlibs(),
1142 StdLinkageForDevice: mod.compiler.stdLinkage(true),
1143 StdLinkageForNonDevice: mod.compiler.stdLinkage(false),
1144 }
1145 if lib, ok := mod.compiler.(libraryInterface); ok {
1146 rustInfo.CompilerInfo.LibraryInfo = &LibraryInfo{
1147 Dylib: lib.dylib(),
1148 Rlib: lib.rlib(),
1149 }
1150 }
1151 if lib, ok := mod.compiler.(cc.SnapshotInterface); ok {
1152 rustInfo.SnapshotInfo = &cc.SnapshotInfo{
1153 SnapshotAndroidMkSuffix: lib.SnapshotAndroidMkSuffix(),
1154 }
1155 }
1156 }
1157 if mod.sourceProvider != nil {
1158 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1159 rustInfo.SourceProviderInfo = &SourceProviderInfo{
1160 ProtobufDecoratorInfo: &ProtobufDecoratorInfo{},
1161 }
1162 }
1163 }
1164 android.SetProvider(ctx, RustInfoProvider, rustInfo)
Yu Liu986d98c2024-11-12 00:28:11 +00001165
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001166 ccInfo := &cc.CcInfo{
1167 IsPrebuilt: mod.IsPrebuilt(),
1168 }
1169
Ivan Lozano9587f452025-01-08 03:17:19 +00001170 // Define the linker info if compiler != nil because Rust currently
1171 // does compilation and linking in one step. If this changes in the future,
1172 // move this as appropriate.
1173 ccInfo.LinkerInfo = &cc.LinkerInfo{
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001174 WholeStaticLibs: mod.compiler.baseCompilerProps().Whole_static_libs,
1175 StaticLibs: mod.compiler.baseCompilerProps().Static_libs,
1176 SharedLibs: mod.compiler.baseCompilerProps().Shared_libs,
Ivan Lozano9587f452025-01-08 03:17:19 +00001177 }
1178
Ivan Lozano9eaacc82024-10-30 14:28:17 +00001179 android.SetProvider(ctx, cc.CcInfoProvider, ccInfo)
1180
mrziwang0cbd3b02024-06-20 16:39:25 -07001181 mod.setOutputFiles(ctx)
Wei Lia1aa2972024-06-21 13:08:51 -07001182
1183 buildComplianceMetadataInfo(ctx, mod, deps)
mrziwang0cbd3b02024-06-20 16:39:25 -07001184}
1185
1186func (mod *Module) setOutputFiles(ctx ModuleContext) {
1187 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
1188 ctx.SetOutputFiles(mod.sourceProvider.Srcs(), "")
1189 } else if mod.OutputFile().Valid() {
1190 ctx.SetOutputFiles(android.Paths{mod.OutputFile().Path()}, "")
1191 } else {
1192 ctx.SetOutputFiles(android.Paths{}, "")
1193 }
1194 if mod.compiler != nil {
1195 ctx.SetOutputFiles(android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), "unstripped")
1196 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001197}
1198
Wei Lia1aa2972024-06-21 13:08:51 -07001199func buildComplianceMetadataInfo(ctx *moduleContext, mod *Module, deps PathDeps) {
1200 // Dump metadata that can not be done in android/compliance-metadata.go
1201 metadataInfo := ctx.ComplianceMetadataInfo()
1202 metadataInfo.SetStringValue(android.ComplianceMetadataProp.IS_STATIC_LIB, strconv.FormatBool(mod.Static()))
1203 metadataInfo.SetStringValue(android.ComplianceMetadataProp.BUILT_FILES, mod.outputFile.String())
1204
1205 // Static libs
1206 staticDeps := ctx.GetDirectDepsWithTag(rlibDepTag)
1207 staticDepNames := make([]string, 0, len(staticDeps))
1208 for _, dep := range staticDeps {
1209 staticDepNames = append(staticDepNames, dep.Name())
1210 }
1211 ccStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(false))
1212 for _, dep := range ccStaticDeps {
1213 staticDepNames = append(staticDepNames, dep.Name())
1214 }
1215
1216 staticDepPaths := make([]string, 0, len(deps.StaticLibs)+len(deps.RLibs))
1217 // C static libraries
1218 for _, dep := range deps.StaticLibs {
1219 staticDepPaths = append(staticDepPaths, dep.String())
1220 }
1221 // Rust static libraries
1222 for _, dep := range deps.RLibs {
1223 staticDepPaths = append(staticDepPaths, dep.Path.String())
1224 }
1225 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1226 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEP_FILES, android.FirstUniqueStrings(staticDepPaths))
1227
1228 // C Whole static libs
1229 ccWholeStaticDeps := ctx.GetDirectDepsWithTag(cc.StaticDepTag(true))
1230 wholeStaticDepNames := make([]string, 0, len(ccWholeStaticDeps))
1231 for _, dep := range ccStaticDeps {
1232 wholeStaticDepNames = append(wholeStaticDepNames, dep.Name())
1233 }
1234 metadataInfo.SetListValue(android.ComplianceMetadataProp.STATIC_DEPS, android.FirstUniqueStrings(staticDepNames))
1235}
1236
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237func (mod *Module) deps(ctx DepsContext) Deps {
1238 deps := Deps{}
1239
1240 if mod.compiler != nil {
1241 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001242 }
1243 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001244 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 }
1246
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001247 if mod.coverage != nil {
1248 deps = mod.coverage.deps(ctx, deps)
1249 }
1250
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001251 if mod.sanitize != nil {
1252 deps = mod.sanitize.deps(ctx, deps)
1253 }
1254
Ivan Lozanoffee3342019-08-27 12:03:00 -07001255 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1256 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001257 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001258 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1259 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1260 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001261 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001262 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001263 return deps
1264
1265}
1266
Ivan Lozanoffee3342019-08-27 12:03:00 -07001267type dependencyTag struct {
1268 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001269 name string
1270 library bool
1271 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001272 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001273}
1274
Jiyong Park65b62242020-11-25 12:44:59 +09001275// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1276// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1277func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001278 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001279}
1280
1281var _ android.InstallNeededDependencyTag = dependencyTag{}
1282
Colin Cross65cb3142021-12-10 23:05:02 +00001283func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1284 if d.library && d.dynamic {
1285 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1286 }
1287 return nil
1288}
1289
Yu Liuc8884602024-03-15 18:48:38 +00001290func (d dependencyTag) PropagateAconfigValidation() bool {
1291 return d == rlibDepTag || d == sourceDepTag
1292}
1293
1294var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1295
Colin Cross65cb3142021-12-10 23:05:02 +00001296var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1297
Ivan Lozanoffee3342019-08-27 12:03:00 -07001298var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001299 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1300 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001301 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001302 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001303 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001304 dataLibDepTag = dependencyTag{name: "data lib"}
1305 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001306)
1307
Jiyong Park99644e92020-11-17 22:21:02 +09001308func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1309 tag, ok := depTag.(dependencyTag)
1310 return ok && tag == dylibDepTag
1311}
1312
Jiyong Park94e22fd2021-04-08 18:19:15 +09001313func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1314 tag, ok := depTag.(dependencyTag)
1315 return ok && tag == rlibDepTag
1316}
1317
Matthew Maurer0f003b12020-06-29 14:34:06 -07001318type autoDep struct {
1319 variation string
1320 depTag dependencyTag
1321}
1322
1323var (
Colin Cross8a49a3d2024-05-20 12:22:27 -07001324 sourceVariation = "source"
1325 rlibVariation = "rlib"
1326 dylibVariation = "dylib"
1327 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1328 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001329)
1330
1331type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001332 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001333}
1334
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001335func (mod *Module) begin(ctx BaseModuleContext) {
1336 if mod.coverage != nil {
1337 mod.coverage.begin(ctx)
1338 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001339 if mod.sanitize != nil {
1340 mod.sanitize.begin(ctx)
1341 }
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001342
1343 if mod.UseSdk() && mod.IsSdkVariant() {
1344 sdkVersion := ""
1345 if ctx.Device() {
1346 sdkVersion = mod.SdkVersion()
1347 }
1348 version, err := cc.NativeApiLevelFromUser(ctx, sdkVersion)
1349 if err != nil {
1350 ctx.PropertyErrorf("sdk_version", err.Error())
1351 mod.Properties.Sdk_version = nil
1352 } else {
1353 mod.Properties.Sdk_version = StringPtr(version.String())
1354 }
1355 }
1356
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001357}
1358
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001359func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001360 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001361 return p.prebuilt()
1362 }
1363 return nil
1364}
1365
Kiyoung Kim37693d02024-04-04 09:56:15 +09001366func (mod *Module) Symlinks() []string {
1367 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1368 return nil
1369}
1370
Yu Liu8024b922024-12-20 23:31:32 +00001371func rustMakeLibName(rustInfo *RustInfo, linkableInfo *cc.LinkableInfo, commonInfo *android.CommonModuleInfo, depName string) string {
1372 if rustInfo != nil {
Justin Yun24b246a2023-03-16 10:36:16 +09001373 // Use base module name for snapshots when exporting to Makefile.
Yu Liu8024b922024-12-20 23:31:32 +00001374 if rustInfo.SnapshotInfo != nil {
1375 baseName := linkableInfo.BaseModuleName
1376 return baseName + rustInfo.SnapshotInfo.SnapshotAndroidMkSuffix + rustInfo.AndroidMkSuffix
Justin Yun24b246a2023-03-16 10:36:16 +09001377 }
1378 }
Yu Liu8024b922024-12-20 23:31:32 +00001379 return cc.MakeLibName(nil, linkableInfo, commonInfo, depName)
Justin Yun24b246a2023-03-16 10:36:16 +09001380}
1381
Yu Liu8024b922024-12-20 23:31:32 +00001382func collectIncludedProtos(mod *Module, rustInfo *RustInfo, linkableInfo *cc.LinkableInfo) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001383 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001384 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
1385 protoMod.additionalCrates = append(protoMod.additionalCrates, linkableInfo.CrateName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001386 }
1387 }
1388}
Andrew Walbran52533232024-03-19 11:36:04 +00001389
Ivan Lozanoffee3342019-08-27 12:03:00 -07001390func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1391 var depPaths PathDeps
1392
Yu Liu8024b922024-12-20 23:31:32 +00001393 directRlibDeps := []*cc.LinkableInfo{}
1394 directDylibDeps := []*cc.LinkableInfo{}
1395 directProcMacroDeps := []*cc.LinkableInfo{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001396 directSharedLibDeps := []cc.SharedLibraryInfo{}
Yu Liu8024b922024-12-20 23:31:32 +00001397 directStaticLibDeps := [](*cc.LinkableInfo){}
1398 directSrcProvidersDeps := []*android.ModuleProxy{}
1399 directSrcDeps := []android.SourceFilesInfo{}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001400
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001401 // For the dependency from platform to apex, use the latest stubs
1402 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001403 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001404 if !apexInfo.IsForPlatform() {
1405 mod.apexSdkVersion = apexInfo.MinSdkVersion
1406 }
1407
1408 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1409 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1410 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1411 // (b/144430859)
1412 mod.apexSdkVersion = android.FutureApiLevel
1413 }
1414
Spandan Das604f3762023-03-16 22:51:40 +00001415 skipModuleList := map[string]bool{}
1416
Colin Crossa14fb6a2024-10-23 16:57:06 -07001417 var transitiveAndroidMkSharedLibs []depset.DepSet[string]
Cole Faustb6e6f992023-08-17 17:42:26 -07001418 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001419
Yu Liu8024b922024-12-20 23:31:32 +00001420 ctx.VisitDirectDepsProxy(func(dep android.ModuleProxy) {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001421 depName := ctx.OtherModuleName(dep)
1422 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano806efd32024-12-11 21:38:53 +00001423 modStdLinkage := mod.compiler.stdLinkage(ctx.Device())
1424
Spandan Das604f3762023-03-16 22:51:40 +00001425 if _, exists := skipModuleList[depName]; exists {
1426 return
1427 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001428
1429 if depTag == android.DarwinUniversalVariantTag {
1430 return
1431 }
1432
Yu Liu8024b922024-12-20 23:31:32 +00001433 rustInfo, hasRustInfo := android.OtherModuleProvider(ctx, dep, RustInfoProvider)
1434 ccInfo, _ := android.OtherModuleProvider(ctx, dep, cc.CcInfoProvider)
1435 linkableInfo, hasLinkableInfo := android.OtherModuleProvider(ctx, dep, cc.LinkableInfoProvider)
1436 commonInfo := android.OtherModuleProviderOrDefault(ctx, dep, android.CommonModuleInfoKey)
1437 if hasRustInfo && !linkableInfo.Static && !linkableInfo.Shared {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001438 //Handle Rust Modules
Yu Liu8024b922024-12-20 23:31:32 +00001439 makeLibName := rustMakeLibName(rustInfo, linkableInfo, &commonInfo, depName+rustInfo.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001440
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001441 switch {
1442 case depTag == dylibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001443 dylib := rustInfo.CompilerInfo.LibraryInfo
1444 if dylib == nil || !dylib.Dylib {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001445 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1446 return
1447 }
Yu Liu8024b922024-12-20 23:31:32 +00001448 directDylibDeps = append(directDylibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001449 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001450 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1451
Colin Crossb614cd42024-10-11 12:52:21 -07001452 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1453 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1454 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1455 }
1456
Yu Liu8024b922024-12-20 23:31:32 +00001457 if !rustInfo.CompilerInfo.NoStdlibs {
1458 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1459 if ctx.Device() {
1460 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1461 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001462 if rustDepStdLinkage != modStdLinkage {
1463 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1464 return
1465 }
1466 }
1467
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001468 case depTag == rlibDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001469 rlib := rustInfo.CompilerInfo.LibraryInfo
1470 if rlib == nil || !rlib.Rlib {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001471 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472 return
1473 }
Yu Liu8024b922024-12-20 23:31:32 +00001474 directRlibDeps = append(directRlibDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001475 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001476 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1477
Ivan Lozano0a468a42024-05-13 21:03:34 -04001478 // rust_ffi rlibs may export include dirs, so collect those here.
1479 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
1480 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
Yu Liu8024b922024-12-20 23:31:32 +00001481 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozano0a468a42024-05-13 21:03:34 -04001482
Colin Crossb614cd42024-10-11 12:52:21 -07001483 // rlibs are not installed, so don't add the output file to directImplementationDeps
1484 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1485 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1486 }
1487
Yu Liu8024b922024-12-20 23:31:32 +00001488 if !rustInfo.CompilerInfo.NoStdlibs {
1489 rustDepStdLinkage := rustInfo.CompilerInfo.StdLinkageForNonDevice
1490 if ctx.Device() {
1491 rustDepStdLinkage = rustInfo.CompilerInfo.StdLinkageForDevice
1492 }
Ivan Lozano806efd32024-12-11 21:38:53 +00001493 if rustDepStdLinkage != modStdLinkage {
1494 ctx.ModuleErrorf("Rust dependency %q has the wrong StdLinkage; expected %#v, got %#v", depName, modStdLinkage, rustDepStdLinkage)
1495 return
1496 }
1497 }
1498
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001499 case depTag == procMacroDepTag:
Yu Liu8024b922024-12-20 23:31:32 +00001500 directProcMacroDeps = append(directProcMacroDeps, linkableInfo)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001501 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001502 // proc_macro link dirs need to be exported, so collect those here.
Yu Liu8024b922024-12-20 23:31:32 +00001503 depPaths.exportedLinkDirs = append(depPaths.exportedLinkDirs, linkPathFromFilePath(linkableInfo.OutputFile.Path()))
Ivan Lozanod106efe2023-09-21 23:30:26 -04001504
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001505 case depTag == sourceDepTag:
Ivan Lozanod106efe2023-09-21 23:30:26 -04001506 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
Yu Liu8024b922024-12-20 23:31:32 +00001507 collectIncludedProtos(mod, rustInfo, linkableInfo)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001508 }
Ivan Lozanofd47b1a2024-05-17 14:13:41 -04001509 case cc.IsStaticDepTag(depTag):
1510 // Rust FFI rlibs should not be declared in a Rust modules
1511 // "static_libs" list as we can't handle them properly at the
1512 // moment (for example, they only produce an rlib-std variant).
1513 // Instead, a normal rust_library variant should be used.
1514 ctx.PropertyErrorf("static_libs",
1515 "found '%s' in static_libs; use a rust_library module in rustlibs instead of a rust_ffi module in static_libs",
1516 depName)
1517
Paul Duffind5cf92e2021-07-09 17:38:55 +01001518 }
1519
Yu Liu8024b922024-12-20 23:31:32 +00001520 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustInfo.TransitiveAndroidMkSharedLibs)
Cole Faustb6e6f992023-08-17 17:42:26 -07001521
Paul Duffind5cf92e2021-07-09 17:38:55 +01001522 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001523 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1524 // OS/Arch variant is used.
1525 var helper string
1526 if ctx.Host() {
1527 helper = "missing 'host_supported'?"
1528 } else {
1529 helper = "device module defined?"
1530 }
1531
Yu Liu8024b922024-12-20 23:31:32 +00001532 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001533 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1534 return
Yu Liu8024b922024-12-20 23:31:32 +00001535 } else if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001536 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1537 return
1538 }
Yu Liu8024b922024-12-20 23:31:32 +00001539 directSrcProvidersDeps = append(directSrcProvidersDeps, &dep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001540 }
1541
Ivan Lozano0a468a42024-05-13 21:03:34 -04001542 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001543 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001544 if depTag != procMacroDepTag {
Colin Cross0de8a1e2020-09-18 14:15:30 -07001545 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1546 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozano0a468a42024-05-13 21:03:34 -04001547 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001548 }
1549
Ivan Lozanoffee3342019-08-27 12:03:00 -07001550 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Yu Liu8024b922024-12-20 23:31:32 +00001551 linkFile := linkableInfo.UnstrippedOutputFile
Wen-yi Chu41326c12023-09-22 03:58:59 +00001552 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001553 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001554 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001555 }
1556 }
Ivan Lozano0a468a42024-05-13 21:03:34 -04001557
Ivan Lozanod106efe2023-09-21 23:30:26 -04001558 if depTag == sourceDepTag {
1559 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
Yu Liu8024b922024-12-20 23:31:32 +00001560 if rustInfo.SourceProviderInfo.ProtobufDecoratorInfo != nil {
Colin Cross313aa542023-12-13 13:47:44 -08001561 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001562 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1563 }
1564 }
1565 }
Yu Liu8024b922024-12-20 23:31:32 +00001566 } else if hasLinkableInfo {
Ivan Lozano52767be2019-10-18 14:49:46 -07001567 //Handle C dependencies
Yu Liu8024b922024-12-20 23:31:32 +00001568 makeLibName := cc.MakeLibName(ccInfo, linkableInfo, &commonInfo, depName)
1569 if !hasRustInfo {
1570 if commonInfo.Target.Os != ctx.Os() {
Ivan Lozano52767be2019-10-18 14:49:46 -07001571 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1572 return
1573 }
Yu Liu8024b922024-12-20 23:31:32 +00001574 if commonInfo.Target.Arch.ArchType != ctx.Arch().ArchType {
Ivan Lozano52767be2019-10-18 14:49:46 -07001575 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1576 return
1577 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001578 }
Yu Liu8024b922024-12-20 23:31:32 +00001579 linkObject := linkableInfo.OutputFile
Ivan Lozano2093af22020-08-25 12:48:19 -04001580 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001581 if !ctx.Config().AllowMissingDependencies() {
1582 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1583 } else {
1584 ctx.AddMissingDependencies([]string{depName})
1585 }
1586 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001587 }
1588
Wen-yi Chu41326c12023-09-22 03:58:59 +00001589 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001590
Ivan Lozanoffee3342019-08-27 12:03:00 -07001591 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001592 switch {
1593 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001594 if cc.IsWholeStaticLib(depTag) {
1595 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1596 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001597 if mod.Binary() {
1598 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1599 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1600 // final linkage, pass the args directly to the linker to handle these cases.
1601 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1602 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001603 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001604 } else {
1605 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 -05001606 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001607 }
1608
1609 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1610 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001611 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001612 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1613
Colin Cross313aa542023-12-13 13:47:44 -08001614 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001615 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1616 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1617 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1618 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Yu Liu8024b922024-12-20 23:31:32 +00001619 directStaticLibDeps = append(directStaticLibDeps, linkableInfo)
Justin Yun2b3ed642022-02-16 08:15:07 +09001620
1621 // Record baseLibName for snapshots.
1622 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1623
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001624 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001625 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001626 // For the shared lib dependencies, we may link to the stub variant
1627 // of the dependency depending on the context (e.g. if this
1628 // dependency crosses the APEX boundaries).
1629 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1630
Colin Crossb614cd42024-10-11 12:52:21 -07001631 if !sharedLibraryInfo.IsStubs {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00001632 // TODO(b/362509506): remove this additional check once all apex_exclude uses are switched to stubs.
1633 if !linkableInfo.RustApexExclude {
1634 depPaths.directImplementationDeps = append(depPaths.directImplementationDeps, android.OutputFileForModule(ctx, dep, ""))
1635 if info, ok := android.OtherModuleProvider(ctx, dep, cc.ImplementationDepInfoProvider); ok {
1636 depPaths.transitiveImplementationDeps = append(depPaths.transitiveImplementationDeps, info.ImplementationDeps)
1637 }
Colin Crossb614cd42024-10-11 12:52:21 -07001638 }
1639 }
1640
Jiyong Park7d55b612021-06-11 17:22:09 +09001641 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1642 // object (either from stub or non-stub) to use.
1643 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001644 if !linkObject.Valid() {
1645 if !ctx.Config().AllowMissingDependencies() {
1646 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1647 } else {
1648 ctx.AddMissingDependencies([]string{depName})
1649 }
1650 return
1651 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001652 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001653
Ivan Lozanoffee3342019-08-27 12:03:00 -07001654 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001655 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001656 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1657 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1658 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1659 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001660 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001661
1662 // Record baseLibName for snapshots.
1663 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1664
Cole Faustb6e6f992023-08-17 17:42:26 -07001665 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001666 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001667 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001668 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001669 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1670 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1671 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozano1dbfa142024-03-29 14:48:11 +00001672 mod.Properties.AndroidMkHeaderLibs = append(mod.Properties.AndroidMkHeaderLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001673 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001674 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001675 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001676 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001677 }
1678
1679 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001680 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1681 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001682 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001683 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001684 } else {
1685 switch {
1686 case depTag == cc.CrtBeginDepTag:
1687 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1688 case depTag == cc.CrtEndDepTag:
1689 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1690 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001691 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001692
Yu Liuc41eae52025-01-14 01:03:08 +00001693 if srcDep, ok := android.OtherModuleProvider(ctx, dep, android.SourceFilesInfoProvider); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001694 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001695 // These are usually genrules which don't have per-target variants.
1696 directSrcDeps = append(directSrcDeps, srcDep)
1697 }
1698 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001699 })
1700
Colin Crossa14fb6a2024-10-23 16:57:06 -07001701 mod.transitiveAndroidMkSharedLibs = depset.New[string](depset.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001702
1703 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001704 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001705 for _, dep := range directRlibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001706 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001707 if alias, aliased := aliases[crateName]; aliased {
1708 crateName = alias
1709 }
Yu Liu8024b922024-12-20 23:31:32 +00001710 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001711 }
1712 var dylibDepFiles RustLibraries
1713 for _, dep := range directDylibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001714 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001715 if alias, aliased := aliases[crateName]; aliased {
1716 crateName = alias
1717 }
Yu Liu8024b922024-12-20 23:31:32 +00001718 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001719 }
1720 var procMacroDepFiles RustLibraries
1721 for _, dep := range directProcMacroDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001722 crateName := dep.CrateName
Andrew Walbran52533232024-03-19 11:36:04 +00001723 if alias, aliased := aliases[crateName]; aliased {
1724 crateName = alias
1725 }
Yu Liu8024b922024-12-20 23:31:32 +00001726 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile, CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001727 }
1728
Colin Cross004bd3f2023-10-02 11:39:17 -07001729 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001730 for _, dep := range directStaticLibDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001731 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001732 }
1733
Colin Cross004bd3f2023-10-02 11:39:17 -07001734 var sharedLibFiles android.Paths
1735 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001736 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001737 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001738 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001739 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001740 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001741 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001742 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001743 }
1744
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001745 var srcProviderDepFiles android.Paths
1746 for _, dep := range directSrcProvidersDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001747 srcs := android.OutputFilesForModule(ctx, *dep, "")
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001748 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1749 }
1750 for _, dep := range directSrcDeps {
Yu Liu8024b922024-12-20 23:31:32 +00001751 srcs := dep.Srcs
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001752 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1753 }
1754
Wen-yi Chu41326c12023-09-22 03:58:59 +00001755 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1756 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001757 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1758 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1759 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001760 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001761 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001762
1763 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001764 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001765 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001766 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001767 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1768 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1769 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001770
1771 return depPaths
1772}
1773
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001774func (mod *Module) InstallInData() bool {
1775 if mod.compiler == nil {
1776 return false
1777 }
1778 return mod.compiler.inData()
1779}
1780
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001781func (mod *Module) InstallInRamdisk() bool {
1782 return mod.InRamdisk()
1783}
1784
1785func (mod *Module) InstallInVendorRamdisk() bool {
1786 return mod.InVendorRamdisk()
1787}
1788
1789func (mod *Module) InstallInRecovery() bool {
1790 return mod.InRecovery()
1791}
1792
Wen-yi Chu41326c12023-09-22 03:58:59 +00001793func linkPathFromFilePath(filepath android.Path) string {
1794 return strings.Split(filepath.String(), filepath.Base())[0]
1795}
1796
Spandan Das604f3762023-03-16 22:51:40 +00001797// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1798func (r *Module) usePublicApi() bool {
1799 return r.Device() && r.UseSdk()
1800}
1801
1802// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1803func (r *Module) useVendorApi() bool {
1804 return r.Device() && (r.InVendor() || r.InProduct())
1805}
1806
Ivan Lozanoffee3342019-08-27 12:03:00 -07001807func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1808 ctx := &depsContext{
1809 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001810 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001811
1812 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001813 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001814
1815 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001816 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001817 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001818
Ivan Lozano2b081132020-09-08 12:46:52 -04001819 stdLinkage := "dylib-std"
Ivan Lozano806efd32024-12-11 21:38:53 +00001820 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001821 stdLinkage = "rlib-std"
1822 }
1823
1824 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001825
Ivan Lozano2b081132020-09-08 12:46:52 -04001826 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1827 rlibDepVariations = append(rlibDepVariations,
1828 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1829 }
1830
Ivan Lozano1921e802021-05-20 13:39:16 -04001831 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001832 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001833 for _, lib := range deps.Rlibs {
1834 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001835 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001836 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001837
1838 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001839 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
Ivan Lozano0a468a42024-05-13 21:03:34 -04001840
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001841 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001842 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001843 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001844
Ivan Lozano1921e802021-05-20 13:39:16 -04001845 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001846 if deps.Rustlibs != nil {
1847 if !mod.compiler.Disabled() {
1848 for _, lib := range deps.Rustlibs {
1849 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1850 if autoDep.depTag == rlibDepTag {
1851 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001852 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1853
Ivan Lozanod106efe2023-09-21 23:30:26 -04001854 } else {
1855 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1856 // Check for the existence of the dylib deptag variant. Select it if available,
1857 // otherwise select the rlib variant.
1858 autoDepVariations := append(commonDepVariations,
1859 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Kiyoung Kim37693d02024-04-04 09:56:15 +09001860 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1861 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001862
Ivan Lozanod106efe2023-09-21 23:30:26 -04001863 } else {
1864 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001865 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1866
Ivan Lozanod106efe2023-09-21 23:30:26 -04001867 }
1868 }
1869 }
1870 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1871 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001872 srcProviderVariations := append(commonDepVariations,
Colin Cross8a49a3d2024-05-20 12:22:27 -07001873 blueprint.Variation{Mutator: "rust_libraries", Variation: sourceVariation})
Ivan Lozanod106efe2023-09-21 23:30:26 -04001874
Ivan Lozano0a468a42024-05-13 21:03:34 -04001875 // Only add rustlib dependencies if they're source providers themselves.
1876 // This is used to track which crate names need to be added to the source generated
1877 // in the rust_protobuf mod.rs.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001878 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001879 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001880 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001881 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001882 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001883 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001884
Ivan Lozano1921e802021-05-20 13:39:16 -04001885 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001886 if deps.Stdlibs != nil {
Ivan Lozano806efd32024-12-11 21:38:53 +00001887 if mod.compiler.stdLinkage(ctx.Device()) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001888 for _, lib := range deps.Stdlibs {
Colin Cross8a49a3d2024-05-20 12:22:27 -07001889 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001890 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001891 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001892 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001893 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001894 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1895
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001896 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001897 }
1898 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001899
1900 for _, lib := range deps.SharedLibs {
Colin Cross8acea3e2024-12-12 14:53:30 -08001901 depTag := cc.SharedDepTag()
Ivan Lozano1921e802021-05-20 13:39:16 -04001902 name, version := cc.StubsLibNameAndVersion(lib)
1903
1904 variations := []blueprint.Variation{
1905 {Mutator: "link", Variation: "shared"},
1906 }
Spandan Dasff665182024-09-11 18:48:44 +00001907 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001908 }
1909
1910 for _, lib := range deps.WholeStaticLibs {
1911 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001912
1913 actx.AddVariationDependencies([]blueprint.Variation{
1914 {Mutator: "link", Variation: "static"},
1915 }, depTag, lib)
1916 }
1917
1918 for _, lib := range deps.StaticLibs {
1919 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001920
1921 actx.AddVariationDependencies([]blueprint.Variation{
1922 {Mutator: "link", Variation: "static"},
1923 }, depTag, lib)
1924 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001925
Zach Johnson3df4e632020-11-06 11:56:27 -08001926 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1927
Colin Cross565cafd2020-09-25 18:47:38 -07001928 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001929 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001930 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001931 }
Colin Crossfe605e12022-01-23 20:46:16 -08001932 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001933 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001934 }
1935
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001936 if mod.sourceProvider != nil {
1937 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1938 bindgen.Properties.Custom_bindgen != "" {
1939 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1940 bindgen.Properties.Custom_bindgen)
1941 }
1942 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001943
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001944 actx.AddVariationDependencies([]blueprint.Variation{
1945 {Mutator: "link", Variation: "shared"},
1946 }, dataLibDepTag, deps.DataLibs...)
1947
1948 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1949
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001950 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001951 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001952
1953 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001954}
1955
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001956func BeginMutator(ctx android.BottomUpMutatorContext) {
Cole Fausta963b942024-04-11 17:43:00 -07001957 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001958 mod.beginMutator(ctx)
1959 }
1960}
1961
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001962func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1963 ctx := &baseModuleContext{
1964 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001965 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001966
1967 mod.begin(ctx)
1968}
1969
Ivan Lozanoffee3342019-08-27 12:03:00 -07001970func (mod *Module) Name() string {
1971 name := mod.ModuleBase.Name()
1972 if p, ok := mod.compiler.(interface {
1973 Name(string) string
1974 }); ok {
1975 name = p.Name(name)
1976 }
1977 return name
1978}
1979
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001980func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001981 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001982 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001983 }
1984}
1985
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001986var _ android.HostToolProvider = (*Module)(nil)
1987
1988func (mod *Module) HostToolPath() android.OptionalPath {
1989 if !mod.Host() {
1990 return android.OptionalPath{}
1991 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001992 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1993 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001994 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1995 // Even though proc-macros aren't strictly "tools", since they target the compiler
1996 // and act as compiler plugins, we treat them similarly.
1997 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001998 }
1999 return android.OptionalPath{}
2000}
2001
Jiyong Park99644e92020-11-17 22:21:02 +09002002var _ android.ApexModule = (*Module)(nil)
2003
Ivan Lozano24cf0362024-10-04 16:02:38 +00002004// If a module is marked for exclusion from apexes, don't provide apex variants.
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002005// TODO(b/362509506): remove this once all apex_exclude usages are removed.
Ivan Lozano24cf0362024-10-04 16:02:38 +00002006func (m *Module) CanHaveApexVariants() bool {
2007 if m.ApexExclude() {
2008 return false
2009 } else {
2010 return m.ApexModuleBase.CanHaveApexVariants()
2011 }
2012}
2013
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002014func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002015 return String(mod.Properties.Min_sdk_version)
2016}
2017
Jiyong Park45bf82e2020-12-15 22:29:02 +09002018// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09002019func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05002020 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05002021 if minSdkVersion == "apex_inherit" {
2022 return nil
2023 }
2024 if minSdkVersion == "" {
2025 return fmt.Errorf("min_sdk_version is not specificed")
2026 }
2027
2028 // Not using nativeApiLevelFromUser because the context here is not
2029 // necessarily a native context.
2030 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
2031 if err != nil {
2032 return err
2033 }
2034
2035 if ver.GreaterThan(sdkVersion) {
2036 return fmt.Errorf("newer SDK(%v)", ver)
2037 }
Jiyong Park99644e92020-11-17 22:21:02 +09002038 return nil
2039}
2040
Jiyong Park45bf82e2020-12-15 22:29:02 +09002041// Implements android.ApexModule
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002042func (mod *Module) AlwaysRequiresPlatformApexVariant() bool {
2043 // stub libraries and native bridge libraries are always available to platform
2044 // TODO(b/362509506): remove the ApexExclude() check once all apex_exclude uses are switched to stubs.
2045 return mod.IsStubs() || mod.Target().NativeBridge == android.NativeBridgeEnabled || mod.ApexExclude()
2046}
2047
2048// Implements android.ApexModule
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002049func (mod *Module) OutgoingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Matthew Maurer581b6d82022-09-29 16:46:25 -07002050 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09002051 return false
2052 }
2053
Colin Cross8acea3e2024-12-12 14:53:30 -08002054 if mod.Static() && cc.IsSharedDepTag(depTag) {
2055 // shared_lib dependency from a static lib is considered as crossing
2056 // the APEX boundary because the dependency doesn't actually is
2057 // linked; the dependency is used only during the compilation phase.
2058 return false
2059 }
2060
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002061 if depTag == cc.StubImplDepTag {
2062 // We don't track from an implementation library to its stubs.
2063 return false
2064 }
2065
2066 if cc.ExcludeInApexDepTag(depTag) {
2067 return false
2068 }
2069
2070 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2071 if mod.ApexExclude() {
2072 return false
2073 }
2074
Jiyong Park99644e92020-11-17 22:21:02 +09002075 return true
2076}
2077
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002078func (mod *Module) IncomingDepIsInSameApex(depTag blueprint.DependencyTag) bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002079 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2080 if mod.ApexExclude() {
2081 return false
2082 }
2083
2084 if mod.HasStubsVariants() {
2085 if cc.IsSharedDepTag(depTag) {
2086 // dynamic dep to a stubs lib crosses APEX boundary
2087 return false
2088 }
2089 if cc.IsRuntimeDepTag(depTag) {
2090 // runtime dep to a stubs lib also crosses APEX boundary
2091 return false
2092 }
2093 if cc.IsHeaderDepTag(depTag) {
2094 return false
2095 }
2096 }
2097 return true
Colin Crossf7bbd2f2024-12-05 13:57:10 -08002098}
2099
Jiyong Park99644e92020-11-17 22:21:02 +09002100// Overrides ApexModule.IsInstallabeToApex()
2101func (mod *Module) IsInstallableToApex() bool {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002102 // TODO(b/362509506): remove once all apex_exclude uses are switched to stubs.
2103 if mod.ApexExclude() {
2104 return false
2105 }
2106
Jiyong Park99644e92020-11-17 22:21:02 +09002107 if mod.compiler != nil {
Ivan Lozanoa8a1fa12024-10-30 18:15:59 +00002108 if lib, ok := mod.compiler.(libraryInterface); ok {
2109 return (lib.shared() || lib.dylib()) && !lib.BuildStubs()
Jiyong Park99644e92020-11-17 22:21:02 +09002110 }
2111 if _, ok := mod.compiler.(*binaryDecorator); ok {
2112 return true
2113 }
2114 }
2115 return false
2116}
2117
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05002118// If a library file has a "lib" prefix, extract the library name without the prefix.
2119func libNameFromFilePath(filepath android.Path) (string, bool) {
2120 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
2121 if strings.HasPrefix(libName, "lib") {
2122 libName = libName[3:]
2123 return libName, true
2124 }
2125 return "", false
2126}
2127
Sasha Smundaka76acba2022-04-18 20:12:56 -07002128func kytheExtractRustFactory() android.Singleton {
2129 return &kytheExtractRustSingleton{}
2130}
2131
2132type kytheExtractRustSingleton struct {
2133}
2134
2135func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
2136 var xrefTargets android.Paths
2137 ctx.VisitAllModules(func(module android.Module) {
2138 if rustModule, ok := module.(xref); ok {
2139 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
2140 }
2141 })
2142 if len(xrefTargets) > 0 {
2143 ctx.Phony("xref_rust", xrefTargets...)
2144 }
2145}
2146
Jihoon Kangf78a8902022-09-01 22:47:07 +00002147func (c *Module) Partition() string {
2148 return ""
2149}
2150
Ivan Lozanoffee3342019-08-27 12:03:00 -07002151var Bool = proptools.Bool
2152var BoolDefault = proptools.BoolDefault
2153var String = proptools.String
2154var StringPtr = proptools.StringPtr