blob: d180bb21f2193a77a46c317bd78c09b8df234bf3 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Sasha Smundaka76acba2022-04-18 20:12:56 -070018 "android/soong/bloaty"
Ivan Lozano183a3212019-10-18 14:18:45 -070019 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "strings"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020027 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070028 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000029 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070030 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040031 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032)
33
34var pctx = android.NewPackageContext("android/soong/rust")
35
36func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 android.RegisterModuleType("rust_defaults", defaultsFactory)
38 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
39 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040040 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040041 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050042 })
43 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
Inseob Kim3b244062023-07-11 13:31:36 +090046 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070047 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020048 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000049 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070050}
51
52type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040053 GlobalRustFlags []string // Flags that apply globally to rust
54 GlobalLinkFlags []string // Flags that apply globally to linker
55 RustFlags []string // Flags that apply to rust
56 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020057 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070058 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070059 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040060 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070062 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050066 AndroidMkRlibs []string `blueprint:"mutated"`
67 AndroidMkDylibs []string `blueprint:"mutated"`
68 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
69 AndroidMkSharedLibs []string `blueprint:"mutated"`
70 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040071
Ivan Lozano6a884432020-12-02 09:15:16 -050072 ImageVariationPrefix string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
75
Ivan Lozanoc08897c2021-04-02 12:41:32 -040076 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
77 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
78 // appended before SubName.
79 RustSubName string `blueprint:"mutated"`
80
Ivan Lozano6a884432020-12-02 09:15:16 -050081 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050082 CoreVariantNeeded bool `blueprint:"mutated"`
83 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070084 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080085 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050086 ExtraVariants []string `blueprint:"mutated"`
87
Ivan Lozanoa2268632021-07-22 10:52:06 -040088 // Allows this module to use non-APEX version of libraries. Useful
89 // for building binaries that are started before APEXes are activated.
90 Bootstrap *bool
91
Ivan Lozano1921e802021-05-20 13:39:16 -040092 // Used by vendor snapshot to record dependencies from snapshot modules.
93 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090094 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040095
Matthew Maurerc6868382021-07-13 14:12:37 -070096 // Make this module available when building for ramdisk.
97 // On device without a dedicated recovery partition, the module is only
98 // available after switching root into
99 // /first_stage_ramdisk. To expose the module before switching root, install
100 // the recovery variant instead.
101 Ramdisk_available *bool
102
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500103 // Make this module available when building for vendor ramdisk.
104 // On device without a dedicated recovery partition, the module is only
105 // available after switching root into
106 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800107 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500108 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400109
Ivan Lozano1921e802021-05-20 13:39:16 -0400110 // Normally Soong uses the directory structure to decide which modules
111 // should be included (framework) or excluded (non-framework) from the
112 // different snapshots (vendor, recovery, etc.), but this property
113 // allows a partner to exclude a module normally thought of as a
114 // framework module from the vendor snapshot.
115 Exclude_from_vendor_snapshot *bool
116
117 // Normally Soong uses the directory structure to decide which modules
118 // should be included (framework) or excluded (non-framework) from the
119 // different snapshots (vendor, recovery, etc.), but this property
120 // allows a partner to exclude a module normally thought of as a
121 // framework module from the recovery snapshot.
122 Exclude_from_recovery_snapshot *bool
123
Matthew Maurer460ee942021-02-11 12:31:46 -0800124 // Make this module available when building for recovery
125 Recovery_available *bool
126
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500127 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
128 Min_sdk_version *string
129
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900130 HideFromMake bool `blueprint:"mutated"`
131 PreventInstall bool `blueprint:"mutated"`
132
133 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134}
135
136type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700137 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138
Ivan Lozano6a884432020-12-02 09:15:16 -0500139 VendorProperties cc.VendorProperties
140
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141 Properties BaseProperties
142
143 hod android.HostOrDeviceSupported
144 multilib android.Multilib
145
Ivan Lozano6a884432020-12-02 09:15:16 -0500146 makeLinkType string
147
Yi Kong46c6e592022-01-20 22:55:00 +0800148 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400150 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200151 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500152 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400154 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700155 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400157 // Output file to be installed, may be stripped or unstripped.
158 outputFile android.OptionalPath
159
Sasha Smundaka76acba2022-04-18 20:12:56 -0700160 // Cross-reference input file
161 kytheFiles android.Paths
162
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400163 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900164
165 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500166
167 // For apex variants, this is set as apex.min_sdk_version
168 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700169}
170
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171func (mod *Module) Header() bool {
172 //TODO: If Rust libraries provide header variants, this needs to be updated.
173 return false
174}
175
176func (mod *Module) SetPreventInstall() {
177 mod.Properties.PreventInstall = true
178}
179
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500180func (mod *Module) SetHideFromMake() {
181 mod.Properties.HideFromMake = true
182}
183
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900184func (mod *Module) HiddenFromMake() bool {
185 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500189 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
190 // nil since we need compiler to actually sanitize.
191 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500192}
193
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194func (mod *Module) IsPrebuilt() bool {
195 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
196 return true
197 }
198 return false
199}
200
Ivan Lozano43845682020-07-09 21:03:28 -0400201func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
202 switch tag {
203 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700204 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400205 return mod.sourceProvider.Srcs(), nil
206 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900207 if mod.OutputFile().Valid() {
208 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400209 }
210 return android.Paths{}, nil
211 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500212 case "unstripped":
213 if mod.compiler != nil {
214 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
215 }
216 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400217 default:
218 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
219 }
220}
221
Ivan Lozano52767be2019-10-18 14:49:46 -0700222func (mod *Module) SelectedStl() string {
223 return ""
224}
225
Ivan Lozano2b262972019-11-21 12:30:50 -0800226func (mod *Module) NonCcVariants() bool {
227 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400228 if _, ok := mod.compiler.(libraryInterface); ok {
229 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800230 }
231 }
232 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
233}
234
Ivan Lozano52767be2019-10-18 14:49:46 -0700235func (mod *Module) Static() bool {
236 if mod.compiler != nil {
237 if library, ok := mod.compiler.(libraryInterface); ok {
238 return library.static()
239 }
240 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400241 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700242}
243
244func (mod *Module) Shared() bool {
245 if mod.compiler != nil {
246 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400247 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700248 }
249 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400250 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700251}
252
Ivan Lozanod7586b62021-04-01 09:49:36 -0400253func (mod *Module) Dylib() bool {
254 if mod.compiler != nil {
255 if library, ok := mod.compiler.(libraryInterface); ok {
256 return library.dylib()
257 }
258 }
259 return false
260}
261
262func (mod *Module) Rlib() bool {
263 if mod.compiler != nil {
264 if library, ok := mod.compiler.(libraryInterface); ok {
265 return library.rlib()
266 }
267 }
268 return false
269}
270
271func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400272 if binary, ok := mod.compiler.(binaryInterface); ok {
273 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400274 }
275 return false
276}
277
Justin Yun5e035862021-06-29 20:50:37 +0900278func (mod *Module) StaticExecutable() bool {
279 if !mod.Binary() {
280 return false
281 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400282 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900283}
284
Ivan Lozanod7586b62021-04-01 09:49:36 -0400285func (mod *Module) Object() bool {
286 // Rust has no modules which produce only object files.
287 return false
288}
289
Ivan Lozano52767be2019-10-18 14:49:46 -0700290func (mod *Module) Toc() android.OptionalPath {
291 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400292 if lib, ok := mod.compiler.(libraryInterface); ok {
293 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700294 }
295 }
296 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
297}
298
Colin Crossc511bc52020-04-07 16:50:32 +0000299func (mod *Module) UseSdk() bool {
300 return false
301}
302
Ivan Lozanod7586b62021-04-01 09:49:36 -0400303func (mod *Module) RelativeInstallPath() string {
304 if mod.compiler != nil {
305 return mod.compiler.relativeInstallPath()
306 }
307 return ""
308}
309
Ivan Lozano52767be2019-10-18 14:49:46 -0700310func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500311 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700312}
313
Jiyong Park7d55b612021-06-11 17:22:09 +0900314func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400315 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900316}
317
Ivan Lozano52767be2019-10-18 14:49:46 -0700318func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400319 return true
320}
321
322func (mod *Module) SubName() string {
323 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700324}
325
326func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500327 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700328 return false
329}
330
Ivan Lozanof9e21722020-12-02 09:00:51 -0500331func (mod *Module) IsVndkExt() bool {
332 return false
333}
334
Ivan Lozanod7586b62021-04-01 09:49:36 -0400335func (mod *Module) IsVndkSp() bool {
336 return false
337}
338
Ivan Lozanof1868af2022-04-12 13:08:36 -0400339func (mod *Module) IsVndkPrebuiltLibrary() bool {
340 // Rust modules do not provide VNDK prebuilts
341 return false
342}
343
344func (mod *Module) IsVendorPublicLibrary() bool {
345 return mod.VendorProperties.IsVendorPublicLibrary
346}
347
348func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
349 // Rust modules to not provide Sdk variants
350 return false
351}
352
Colin Cross127bb8b2020-12-16 16:46:01 -0800353func (c *Module) IsVndkPrivate() bool {
354 return false
355}
356
357func (c *Module) IsLlndk() bool {
358 return false
359}
360
361func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500362 return false
363}
364
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400365func (mod *Module) KernelHeadersDecorator() bool {
366 return false
367}
368
Colin Cross1f3f1302021-04-26 18:37:44 -0700369func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400370 return false
371}
372
Colin Cross5271fea2021-04-27 13:06:04 -0700373func (m *Module) NeedsVendorPublicLibraryVariants() bool {
374 return false
375}
376
Ivan Lozanod7586b62021-04-01 09:49:36 -0400377func (mod *Module) HasLlndkStubs() bool {
378 return false
379}
380
381func (mod *Module) StubsVersion() string {
382 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
383}
384
Ivan Lozano52767be2019-10-18 14:49:46 -0700385func (mod *Module) SdkVersion() string {
386 return ""
387}
388
Colin Crossc511bc52020-04-07 16:50:32 +0000389func (mod *Module) AlwaysSdk() bool {
390 return false
391}
392
Jiyong Park2286afd2020-06-16 21:58:53 +0900393func (mod *Module) IsSdkVariant() bool {
394 return false
395}
396
Colin Cross1348ce32020-10-01 13:37:16 -0700397func (mod *Module) SplitPerApiLevel() bool {
398 return false
399}
400
Sasha Smundaka76acba2022-04-18 20:12:56 -0700401func (mod *Module) XrefRustFiles() android.Paths {
402 return mod.kytheFiles
403}
404
Ivan Lozanoffee3342019-08-27 12:03:00 -0700405type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400406 Dylibs []string
407 Rlibs []string
408 Rustlibs []string
409 Stdlibs []string
410 ProcMacros []string
411 SharedLibs []string
412 StaticLibs []string
413 WholeStaticLibs []string
414 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400416 // Used for data dependencies adjacent to tests
417 DataLibs []string
418 DataBins []string
419
Colin Crossfe605e12022-01-23 20:46:16 -0800420 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421}
422
423type PathDeps struct {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700424 DyLibs RustLibraries
425 RLibs RustLibraries
426 LibDeps android.Paths
427 WholeStaticLibs android.Paths
428 ProcMacros RustLibraries
429 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500430
431 // depFlags and depLinkFlags are rustc and linker (clang) flags.
432 depFlags []string
433 depLinkFlags []string
434
435 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
436 // Both of these are exported and propagate to dependencies.
437 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700438 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700439
Ivan Lozano45901ed2020-07-24 16:05:01 -0400440 // Used by bindgen modules which call clang
441 depClangFlags []string
442 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400443 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400444 depSystemIncludePaths android.Paths
445
Colin Crossfe605e12022-01-23 20:46:16 -0800446 CrtBegin android.Paths
447 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700448
449 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500450 SrcDeps android.Paths
451 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700452}
453
454type RustLibraries []RustLibrary
455
456type RustLibrary struct {
457 Path android.Path
458 CrateName string
459}
460
461type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100462 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700463 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400464 cfgFlags(ctx ModuleContext, flags Flags) Flags
465 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700467 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468 compilerDeps(ctx DepsContext, deps Deps) Deps
469 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700470 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100472 // Output directory in which source-generated code from dependencies is
473 // copied. This is equivalent to Cargo's OUT_DIR variable.
474 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700475
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400476 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
477 CargoPkgVersion() string
478
479 // CargoEnvCompat returns whether Cargo environment variables should be used.
480 CargoEnvCompat() bool
481
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800482 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200483 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400485 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486
487 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400488
489 Disabled() bool
490 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400491
Ivan Lozanodd055472020-09-28 13:22:45 -0400492 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500493 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900494
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400495 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900496 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400497}
498
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700500 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700501 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700502}
503
Sasha Smundaka76acba2022-04-18 20:12:56 -0700504type xref interface {
505 XrefRustFiles() android.Paths
506}
507
Matthew Maurerbb3add12020-06-25 09:34:12 -0700508type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400509 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700510 linkObjects android.Paths
511 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700512}
513
Matthew Maurerbb3add12020-06-25 09:34:12 -0700514func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
515 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
516}
517
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700518func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
519 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
520}
521
522func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
523 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400524}
525
Colin Cross0de8a1e2020-09-18 14:15:30 -0700526func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
527 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700528 LinkDirs: flagExporter.linkDirs,
529 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700530 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700531 })
532}
533
Matthew Maurerbb3add12020-06-25 09:34:12 -0700534var _ exportedFlagsProducer = (*flagExporter)(nil)
535
536func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700537 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700538}
539
Colin Cross0de8a1e2020-09-18 14:15:30 -0700540type FlagExporterInfo struct {
541 Flags []string
542 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700543 LinkObjects android.Paths
544 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700545}
546
547var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
548
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400549func (mod *Module) isCoverageVariant() bool {
550 return mod.coverage.Properties.IsCoverageVariant
551}
552
553var _ cc.Coverage = (*Module)(nil)
554
555func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
556 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
557}
558
Ivan Lozanod7586b62021-04-01 09:49:36 -0400559func (mod *Module) VndkVersion() string {
560 return mod.Properties.VndkVersion
561}
562
563func (mod *Module) PreventInstall() bool {
564 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400565}
566
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400567func (mod *Module) MarkAsCoverageVariant(coverage bool) {
568 mod.coverage.Properties.IsCoverageVariant = coverage
569}
570
571func (mod *Module) EnableCoverageIfNeeded() {
572 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700573}
574
575func defaultsFactory() android.Module {
576 return DefaultsFactory()
577}
578
579type Defaults struct {
580 android.ModuleBase
581 android.DefaultsModuleBase
582}
583
584func DefaultsFactory(props ...interface{}) android.Module {
585 module := &Defaults{}
586
587 module.AddProperties(props...)
588 module.AddProperties(
589 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800590 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500591 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100592 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400593 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594 &BaseCompilerProperties{},
595 &BinaryCompilerProperties{},
596 &LibraryCompilerProperties{},
597 &ProcMacroCompilerProperties{},
598 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400599 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700600 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400601 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400602 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200603 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500604 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605 )
606
607 android.InitDefaultsModule(module)
608 return module
609}
610
611func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700612 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700613}
614
Ivan Lozano183a3212019-10-18 14:18:45 -0700615func (mod *Module) CcLibrary() bool {
616 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500617 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700618 return true
619 }
620 }
621 return false
622}
623
624func (mod *Module) CcLibraryInterface() bool {
625 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400626 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
627 // VariantIs{Static,Shared} is set.
628 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700629 return true
630 }
631 }
632 return false
633}
634
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400635func (mod *Module) RustLibraryInterface() bool {
636 if mod.compiler != nil {
637 if _, ok := mod.compiler.(libraryInterface); ok {
638 return true
639 }
640 }
641 return false
642}
643
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500644func (mod *Module) IsFuzzModule() bool {
645 if _, ok := mod.compiler.(*fuzzDecorator); ok {
646 return true
647 }
648 return false
649}
650
651func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
652 return mod.FuzzModule
653}
654
655func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
656 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
657 return fuzzer.fuzzPackagedModule
658 }
659 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
660}
661
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000662func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500663 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
664 return fuzzer.sharedLibraries
665 }
666 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
667}
668
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400669func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400670 if mod.compiler != nil {
671 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400672 }
673 return nil
674}
675
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800676func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700677 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700678 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800679 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700680 }
681 }
682 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
683}
684
685func (mod *Module) SetStatic() {
686 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700687 if library, ok := mod.compiler.(libraryInterface); ok {
688 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700689 return
690 }
691 }
692 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
693}
694
695func (mod *Module) SetShared() {
696 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700697 if library, ok := mod.compiler.(libraryInterface); ok {
698 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700699 return
700 }
701 }
702 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
703}
704
Ivan Lozano183a3212019-10-18 14:18:45 -0700705func (mod *Module) BuildStaticVariant() bool {
706 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700707 if library, ok := mod.compiler.(libraryInterface); ok {
708 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700709 }
710 }
711 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
712}
713
714func (mod *Module) BuildSharedVariant() bool {
715 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700716 if library, ok := mod.compiler.(libraryInterface); ok {
717 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700718 }
719 }
720 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
721}
722
Ivan Lozano183a3212019-10-18 14:18:45 -0700723func (mod *Module) Module() android.Module {
724 return mod
725}
726
Ivan Lozano183a3212019-10-18 14:18:45 -0700727func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400728 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700729}
730
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400731func (mod *Module) CoverageFiles() android.Paths {
732 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800733 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400734 }
735 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
736}
737
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400738// Rust does not produce gcno files, and therefore does not produce a coverage archive.
739func (mod *Module) CoverageOutputFile() android.OptionalPath {
740 return android.OptionalPath{}
741}
742
743func (mod *Module) IsNdk(config android.Config) bool {
744 return false
745}
746
747func (mod *Module) IsStubs() bool {
748 return false
749}
750
Jiyong Park459feca2020-12-15 11:02:21 +0900751func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900752 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900753 return false
754 }
755
Jiyong Park459feca2020-12-15 11:02:21 +0900756 // The apex variant is not installable because it is included in the APEX and won't appear
757 // in the system partition as a standalone file.
758 if !apexInfo.IsForPlatform() {
759 return false
760 }
761
Jiyong Parke54f07e2021-04-07 15:08:04 +0900762 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900763}
764
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500765func (ctx moduleContext) apexVariationName() string {
766 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
767}
768
Ivan Lozano183a3212019-10-18 14:18:45 -0700769var _ cc.LinkableInterface = (*Module)(nil)
770
Ivan Lozanoffee3342019-08-27 12:03:00 -0700771func (mod *Module) Init() android.Module {
772 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500773 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700774
Yi Kong46c6e592022-01-20 22:55:00 +0800775 if mod.afdo != nil {
776 mod.AddProperties(mod.afdo.props()...)
777 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 if mod.compiler != nil {
779 mod.AddProperties(mod.compiler.compilerProps()...)
780 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400781 if mod.coverage != nil {
782 mod.AddProperties(mod.coverage.props()...)
783 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200784 if mod.clippy != nil {
785 mod.AddProperties(mod.clippy.props()...)
786 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400787 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700788 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400789 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500790 if mod.sanitize != nil {
791 mod.AddProperties(mod.sanitize.props()...)
792 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400793
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900795 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796
797 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 return mod
799}
800
801func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
802 return &Module{
803 hod: hod,
804 multilib: multilib,
805 }
806}
807func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
808 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800809 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400810 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200811 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500812 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813 return module
814}
815
816type ModuleContext interface {
817 android.ModuleContext
818 ModuleContextIntf
819}
820
821type BaseModuleContext interface {
822 android.BaseModuleContext
823 ModuleContextIntf
824}
825
826type DepsContext interface {
827 android.BottomUpMutatorContext
828 ModuleContextIntf
829}
830
831type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200832 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834}
835
836type depsContext struct {
837 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838}
839
840type moduleContext struct {
841 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842}
843
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200844type baseModuleContext struct {
845 android.BaseModuleContext
846}
847
848func (ctx *moduleContext) RustModule() *Module {
849 return ctx.Module().(*Module)
850}
851
852func (ctx *moduleContext) toolchain() config.Toolchain {
853 return ctx.RustModule().toolchain(ctx)
854}
855
856func (ctx *depsContext) RustModule() *Module {
857 return ctx.Module().(*Module)
858}
859
860func (ctx *depsContext) toolchain() config.Toolchain {
861 return ctx.RustModule().toolchain(ctx)
862}
863
864func (ctx *baseModuleContext) RustModule() *Module {
865 return ctx.Module().(*Module)
866}
867
868func (ctx *baseModuleContext) toolchain() config.Toolchain {
869 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400870}
871
872func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700873 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
874 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
875 return false
876 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400877 return mod.compiler != nil && mod.compiler.nativeCoverage()
878}
879
Ivan Lozanod7586b62021-04-01 09:49:36 -0400880func (mod *Module) EverInstallable() bool {
881 return mod.compiler != nil &&
882 // Check to see whether the module is actually ever installable.
883 mod.compiler.everInstallable()
884}
885
886func (mod *Module) Installable() *bool {
887 return mod.Properties.Installable
888}
889
Ivan Lozano872d5792022-03-23 17:31:39 -0400890func (mod *Module) ProcMacro() bool {
891 if pm, ok := mod.compiler.(procMacroInterface); ok {
892 return pm.ProcMacro()
893 }
894 return false
895}
896
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
898 if mod.cachedToolchain == nil {
899 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
900 }
901 return mod.cachedToolchain
902}
903
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200904func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
905 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
906}
907
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
909}
910
911func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
912 ctx := &moduleContext{
913 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915
Jiyong Park99644e92020-11-17 22:21:02 +0900916 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
917 if !apexInfo.IsForPlatform() {
918 mod.hideApexVariantFromMake = true
919 }
920
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500922 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
923
Ivan Lozanof1868af2022-04-12 13:08:36 -0400924 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700925
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926 if !toolchain.Supported() {
927 // This toolchain's unsupported, there's nothing to do for this mod.
928 return
929 }
930
931 deps := mod.depsToPaths(ctx)
932 flags := Flags{
933 Toolchain: toolchain,
934 }
935
Ivan Lozano67eada32021-09-23 11:50:33 -0400936 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800937 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500938 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800939 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 if mod.compiler != nil {
941 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400942 flags = mod.compiler.cfgFlags(ctx, flags)
943 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400944 }
945 if mod.coverage != nil {
946 flags, deps = mod.coverage.flags(ctx, flags, deps)
947 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200948 if mod.clippy != nil {
949 flags, deps = mod.clippy.flags(ctx, flags, deps)
950 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500951 if mod.sanitize != nil {
952 flags, deps = mod.sanitize.flags(ctx, flags, deps)
953 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400954
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200955 // SourceProvider needs to call GenerateSource() before compiler calls
956 // compile() so it can provide the source. A SourceProvider has
957 // multiple variants (e.g. source, rlib, dylib). Only the "source"
958 // variant is responsible for effectively generating the source. The
959 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400960 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200961 if mod.compiler.(libraryInterface).source() {
962 mod.sourceProvider.GenerateSource(ctx, deps)
963 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
964 } else {
965 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
966 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700967 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200968 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400969 }
970
971 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100972 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700973 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400974 if ctx.Failed() {
975 return
976 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700977 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
978 if buildOutput.kytheFile != nil {
979 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
980 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400981 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900982
Dan Albert06feee92021-03-19 15:06:02 -0700983 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000984 if mod.docTimestampFile.Valid() {
985 ctx.CheckbuildFile(mod.docTimestampFile.Path())
986 }
Dan Albert06feee92021-03-19 15:06:02 -0700987
Ivan Lozano1921e802021-05-20 13:39:16 -0400988 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
989 // RECOVERY_SNAPSHOT_VERSION is current.
990 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
991 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
992 lib.collectHeadersForSnapshot(ctx, deps)
993 }
994 }
995
Jiyong Park459feca2020-12-15 11:02:21 +0900996 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400997 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900998 // If the module has been specifically configure to not be installed then
999 // hide from make as otherwise it will break when running inside make as the
1000 // output path to install will not be specified. Not all uninstallable
1001 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001002 // side dependencies. In particular, proc-macros need to be captured in the
1003 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001004 mod.HideFromMake()
1005 } else if !mod.installable(apexInfo) {
1006 mod.SkipInstall()
1007 }
1008
1009 // Still call install though, the installs will be stored as PackageSpecs to allow
1010 // using the outputs in a genrule.
1011 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001012 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001013 if ctx.Failed() {
1014 return
1015 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001016 }
Chris Wailes74be7642021-07-22 16:20:28 -07001017
1018 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019 }
1020}
1021
1022func (mod *Module) deps(ctx DepsContext) Deps {
1023 deps := Deps{}
1024
1025 if mod.compiler != nil {
1026 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001027 }
1028 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001029 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030 }
1031
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001032 if mod.coverage != nil {
1033 deps = mod.coverage.deps(ctx, deps)
1034 }
1035
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001036 if mod.sanitize != nil {
1037 deps = mod.sanitize.deps(ctx, deps)
1038 }
1039
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1041 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001042 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1044 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1045 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001046 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001047 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048 return deps
1049
1050}
1051
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052type dependencyTag struct {
1053 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001054 name string
1055 library bool
1056 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001057 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001058}
1059
Jiyong Park65b62242020-11-25 12:44:59 +09001060// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1061// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1062func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001063 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001064}
1065
1066var _ android.InstallNeededDependencyTag = dependencyTag{}
1067
Colin Cross65cb3142021-12-10 23:05:02 +00001068func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1069 if d.library && d.dynamic {
1070 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1071 }
1072 return nil
1073}
1074
1075var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1076
Ivan Lozanoffee3342019-08-27 12:03:00 -07001077var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001078 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1079 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001080 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001081 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001082 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001083 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001084 dataLibDepTag = dependencyTag{name: "data lib"}
1085 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086)
1087
Jiyong Park99644e92020-11-17 22:21:02 +09001088func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1089 tag, ok := depTag.(dependencyTag)
1090 return ok && tag == dylibDepTag
1091}
1092
Jiyong Park94e22fd2021-04-08 18:19:15 +09001093func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1094 tag, ok := depTag.(dependencyTag)
1095 return ok && tag == rlibDepTag
1096}
1097
Matthew Maurer0f003b12020-06-29 14:34:06 -07001098type autoDep struct {
1099 variation string
1100 depTag dependencyTag
1101}
1102
1103var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001104 rlibVariation = "rlib"
1105 dylibVariation = "dylib"
1106 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1107 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001108)
1109
1110type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001111 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001112}
1113
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001114func (mod *Module) begin(ctx BaseModuleContext) {
1115 if mod.coverage != nil {
1116 mod.coverage.begin(ctx)
1117 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001118 if mod.sanitize != nil {
1119 mod.sanitize.begin(ctx)
1120 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001121}
1122
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001123func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001124 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001125 return p.prebuilt()
1126 }
1127 return nil
1128}
1129
Justin Yun24b246a2023-03-16 10:36:16 +09001130func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1131 if rustDep, ok := dep.(*Module); ok {
1132 // Use base module name for snapshots when exporting to Makefile.
1133 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1134 baseName := rustDep.BaseModuleName()
1135 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1136 }
1137 }
1138 return cc.MakeLibName(ctx, c, dep, depName)
1139}
1140
Ivan Lozanoffee3342019-08-27 12:03:00 -07001141func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1142 var depPaths PathDeps
1143
1144 directRlibDeps := []*Module{}
1145 directDylibDeps := []*Module{}
1146 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001147 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001148 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001149 directSrcProvidersDeps := []*Module{}
1150 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001151
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001152 // For the dependency from platform to apex, use the latest stubs
1153 mod.apexSdkVersion = android.FutureApiLevel
1154 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1155 if !apexInfo.IsForPlatform() {
1156 mod.apexSdkVersion = apexInfo.MinSdkVersion
1157 }
1158
1159 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1160 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1161 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1162 // (b/144430859)
1163 mod.apexSdkVersion = android.FutureApiLevel
1164 }
1165
Spandan Das604f3762023-03-16 22:51:40 +00001166 skipModuleList := map[string]bool{}
1167
1168 var apiImportInfo multitree.ApiImportInfo
1169 hasApiImportInfo := false
1170
1171 ctx.VisitDirectDeps(func(dep android.Module) {
1172 if dep.Name() == "api_imports" {
1173 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1174 hasApiImportInfo = true
1175 }
1176 })
1177
1178 if hasApiImportInfo {
1179 targetStubModuleList := map[string]string{}
1180 targetOrigModuleList := map[string]string{}
1181
1182 // Search for dependency which both original module and API imported library with APEX stub exists
1183 ctx.VisitDirectDeps(func(dep android.Module) {
1184 depName := ctx.OtherModuleName(dep)
1185 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1186 targetStubModuleList[apiLibrary] = depName
1187 }
1188 })
1189 ctx.VisitDirectDeps(func(dep android.Module) {
1190 depName := ctx.OtherModuleName(dep)
1191 if origLibrary, ok := targetStubModuleList[depName]; ok {
1192 targetOrigModuleList[origLibrary] = depName
1193 }
1194 })
1195
1196 // Decide which library should be used between original and API imported library
1197 ctx.VisitDirectDeps(func(dep android.Module) {
1198 depName := ctx.OtherModuleName(dep)
1199 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1200 if cc.ShouldUseStubForApex(ctx, dep) {
1201 skipModuleList[depName] = true
1202 } else {
1203 skipModuleList[apiLibrary] = true
1204 }
1205 }
1206 })
1207 }
1208
Ivan Lozanoffee3342019-08-27 12:03:00 -07001209 ctx.VisitDirectDeps(func(dep android.Module) {
1210 depName := ctx.OtherModuleName(dep)
1211 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001212
Spandan Das604f3762023-03-16 22:51:40 +00001213 if _, exists := skipModuleList[depName]; exists {
1214 return
1215 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001216 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001217 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001218 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001219
Ivan Lozanoffee3342019-08-27 12:03:00 -07001220 switch depTag {
1221 case dylibDepTag:
1222 dylib, ok := rustDep.compiler.(libraryInterface)
1223 if !ok || !dylib.dylib() {
1224 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1225 return
1226 }
1227 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001228 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001229 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001230
Ivan Lozanoffee3342019-08-27 12:03:00 -07001231 rlib, ok := rustDep.compiler.(libraryInterface)
1232 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001233 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001234 return
1235 }
1236 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001237 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001238 case procMacroDepTag:
1239 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001240 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001241 }
1242
1243 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001244 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1245 // OS/Arch variant is used.
1246 var helper string
1247 if ctx.Host() {
1248 helper = "missing 'host_supported'?"
1249 } else {
1250 helper = "device module defined?"
1251 }
1252
1253 if dep.Target().Os != ctx.Os() {
1254 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1255 return
1256 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1257 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1258 return
1259 }
1260 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001261 }
1262
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001263 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001264 if depTag != procMacroDepTag {
1265 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1266 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1267 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1268 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001269 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001270 }
1271
Ivan Lozanoffee3342019-08-27 12:03:00 -07001272 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001273 linkFile := rustDep.UnstrippedOutputFile()
1274 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001275 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1276 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001277 }
1278 }
1279
Ivan Lozano89435d12020-07-31 11:01:18 -04001280 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001281 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001282 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001283 if _, ok := ccDep.(*Module); !ok {
1284 if ccDep.Module().Target().Os != ctx.Os() {
1285 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1286 return
1287 }
1288 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1289 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1290 return
1291 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001292 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001293 linkObject := ccDep.OutputFile()
1294 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001295
Ivan Lozano2093af22020-08-25 12:48:19 -04001296 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001297 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1298 }
1299
1300 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001301 switch {
1302 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001303 if cc.IsWholeStaticLib(depTag) {
1304 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1305 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001306 if mod.Binary() {
1307 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1308 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1309 // final linkage, pass the args directly to the linker to handle these cases.
1310 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1311 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001312 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001313 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001314 } else {
1315 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 -05001316 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001317 }
1318
1319 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1320 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001321 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001322 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1323
Colin Cross0de8a1e2020-09-18 14:15:30 -07001324 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1325 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1326 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1327 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1328 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001329 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001330
1331 // Record baseLibName for snapshots.
1332 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1333
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001334 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001335 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001336 // For the shared lib dependencies, we may link to the stub variant
1337 // of the dependency depending on the context (e.g. if this
1338 // dependency crosses the APEX boundaries).
1339 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1340
1341 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1342 // object (either from stub or non-stub) to use.
1343 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1344 linkPath = linkPathFromFilePath(linkObject.Path())
1345
Ivan Lozanoffee3342019-08-27 12:03:00 -07001346 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001347 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001348 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1349 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1350 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1351 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001352 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001353
1354 // Record baseLibName for snapshots.
1355 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1356
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001357 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001358 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001359 case cc.IsHeaderDepTag(depTag):
1360 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1361 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1362 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1363 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001364 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001365 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001366 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001367 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001368 }
1369
1370 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001371 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1372 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001373 if linkObject.Valid() {
1374 lib.exportLinkObjects(linkObject.Path())
1375 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001376 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001377 } else {
1378 switch {
1379 case depTag == cc.CrtBeginDepTag:
1380 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1381 case depTag == cc.CrtEndDepTag:
1382 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1383 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001384 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001385
1386 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001387 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001388 // These are usually genrules which don't have per-target variants.
1389 directSrcDeps = append(directSrcDeps, srcDep)
1390 }
1391 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001392 })
1393
1394 var rlibDepFiles RustLibraries
1395 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001396 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001397 }
1398 var dylibDepFiles RustLibraries
1399 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001400 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001401 }
1402 var procMacroDepFiles RustLibraries
1403 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001404 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001405 }
1406
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001407 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001408 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001409 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001410 }
1411
Ivan Lozanoffee3342019-08-27 12:03:00 -07001412 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001413 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001414 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001415 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001416 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001417 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001418 }
1419
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001420 var srcProviderDepFiles android.Paths
1421 for _, dep := range directSrcProvidersDeps {
1422 srcs, _ := dep.OutputFiles("")
1423 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1424 }
1425 for _, dep := range directSrcDeps {
1426 srcs := dep.Srcs()
1427 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1428 }
1429
Ivan Lozanoffee3342019-08-27 12:03:00 -07001430 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1431 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001432 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001433 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001434 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001435
1436 // Dedup exported flags from dependencies
1437 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001438 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001439 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001440 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1441 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1442 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443
1444 return depPaths
1445}
1446
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001447func (mod *Module) InstallInData() bool {
1448 if mod.compiler == nil {
1449 return false
1450 }
1451 return mod.compiler.inData()
1452}
1453
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001454func (mod *Module) InstallInRamdisk() bool {
1455 return mod.InRamdisk()
1456}
1457
1458func (mod *Module) InstallInVendorRamdisk() bool {
1459 return mod.InVendorRamdisk()
1460}
1461
1462func (mod *Module) InstallInRecovery() bool {
1463 return mod.InRecovery()
1464}
1465
Ivan Lozanoffee3342019-08-27 12:03:00 -07001466func linkPathFromFilePath(filepath android.Path) string {
1467 return strings.Split(filepath.String(), filepath.Base())[0]
1468}
Ivan Lozanod648c432020-02-06 12:05:10 -05001469
Spandan Das604f3762023-03-16 22:51:40 +00001470// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1471func (r *Module) usePublicApi() bool {
1472 return r.Device() && r.UseSdk()
1473}
1474
1475// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1476func (r *Module) useVendorApi() bool {
1477 return r.Device() && (r.InVendor() || r.InProduct())
1478}
1479
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1481 ctx := &depsContext{
1482 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001484
1485 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001486 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001487 var snapshotInfo *cc.SnapshotInfo
1488
Kiyoung Kim487689e2022-07-26 09:48:22 +09001489 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001490 if mod.usePublicApi() || mod.useVendorApi() {
1491 for idx, lib := range deps.SharedLibs {
1492 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1493 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001494 }
1495
Ivan Lozano1921e802021-05-20 13:39:16 -04001496 if ctx.Os() == android.Android {
1497 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1498 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001499
Ivan Lozano2b081132020-09-08 12:46:52 -04001500 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001501 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001502 stdLinkage = "rlib-std"
1503 }
1504
1505 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001506
Ivan Lozano2b081132020-09-08 12:46:52 -04001507 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1508 rlibDepVariations = append(rlibDepVariations,
1509 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1510 }
1511
Ivan Lozano1921e802021-05-20 13:39:16 -04001512 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001513 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001514 for _, lib := range deps.Rlibs {
1515 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001516 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001517
Ivan Lozano2d407632022-04-07 12:59:11 -04001518 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001519 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001520
1521 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001522 actx.AddVariationDependencies(
1523 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001524 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001525 dylibDepTag, deps.Dylibs...)
1526
Ivan Lozano1921e802021-05-20 13:39:16 -04001527 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001528 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1529 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001530 for _, lib := range deps.Rustlibs {
1531 if autoDep.depTag == rlibDepTag {
1532 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001533 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001534 } else {
1535 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1536 // Check for the existence of the dylib deptag variant. Select it if available,
1537 // otherwise select the rlib variant.
1538 autoDepVariations := append(commonDepVariations,
1539 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1540 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1541 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
1542 } else {
1543 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001544 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001545 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001546 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001547 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001548 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001549 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001550 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001551 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001552 for _, lib := range deps.Stdlibs {
1553 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001554 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001555
1556 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1557 depTag, lib)
1558 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001559 } else {
1560 actx.AddVariationDependencies(
1561 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1562 dylibDepTag, deps.Stdlibs...)
1563 }
1564 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001565
1566 for _, lib := range deps.SharedLibs {
1567 depTag := cc.SharedDepTag()
1568 name, version := cc.StubsLibNameAndVersion(lib)
1569
1570 variations := []blueprint.Variation{
1571 {Mutator: "link", Variation: "shared"},
1572 }
Spandan Das604f3762023-03-16 22:51:40 +00001573 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1574 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1575 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1576 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1577 }
1578
1579 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1580 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1581 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001582 }
1583
1584 for _, lib := range deps.WholeStaticLibs {
1585 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001586 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001587
1588 actx.AddVariationDependencies([]blueprint.Variation{
1589 {Mutator: "link", Variation: "static"},
1590 }, depTag, lib)
1591 }
1592
1593 for _, lib := range deps.StaticLibs {
1594 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001595 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001596
1597 actx.AddVariationDependencies([]blueprint.Variation{
1598 {Mutator: "link", Variation: "static"},
1599 }, depTag, lib)
1600 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001601
Zach Johnson3df4e632020-11-06 11:56:27 -08001602 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1603
Colin Cross565cafd2020-09-25 18:47:38 -07001604 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001605 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001606 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001607 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001608 }
Colin Crossfe605e12022-01-23 20:46:16 -08001609 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001610 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001611 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001612 }
1613
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001614 if mod.sourceProvider != nil {
1615 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1616 bindgen.Properties.Custom_bindgen != "" {
1617 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1618 bindgen.Properties.Custom_bindgen)
1619 }
1620 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001621
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001622 actx.AddVariationDependencies([]blueprint.Variation{
1623 {Mutator: "link", Variation: "shared"},
1624 }, dataLibDepTag, deps.DataLibs...)
1625
1626 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1627
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001628 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001629 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001630
1631 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001632}
1633
Ivan Lozano2d407632022-04-07 12:59:11 -04001634// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001635func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1636 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001637 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1638}
1639
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001640func BeginMutator(ctx android.BottomUpMutatorContext) {
1641 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1642 mod.beginMutator(ctx)
1643 }
1644}
1645
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001646func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1647 ctx := &baseModuleContext{
1648 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001649 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001650
1651 mod.begin(ctx)
1652}
1653
Ivan Lozanoffee3342019-08-27 12:03:00 -07001654func (mod *Module) Name() string {
1655 name := mod.ModuleBase.Name()
1656 if p, ok := mod.compiler.(interface {
1657 Name(string) string
1658 }); ok {
1659 name = p.Name(name)
1660 }
1661 return name
1662}
1663
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001664func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001665 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001666 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001667 }
1668}
1669
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001670var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001671var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001672
1673func (mod *Module) HostToolPath() android.OptionalPath {
1674 if !mod.Host() {
1675 return android.OptionalPath{}
1676 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001677 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1678 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001679 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1680 // Even though proc-macros aren't strictly "tools", since they target the compiler
1681 // and act as compiler plugins, we treat them similarly.
1682 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001683 }
1684 return android.OptionalPath{}
1685}
1686
Jiyong Park99644e92020-11-17 22:21:02 +09001687var _ android.ApexModule = (*Module)(nil)
1688
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001689func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001690 return String(mod.Properties.Min_sdk_version)
1691}
1692
Jiyong Park45bf82e2020-12-15 22:29:02 +09001693// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001694func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001695 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001696 if minSdkVersion == "apex_inherit" {
1697 return nil
1698 }
1699 if minSdkVersion == "" {
1700 return fmt.Errorf("min_sdk_version is not specificed")
1701 }
1702
1703 // Not using nativeApiLevelFromUser because the context here is not
1704 // necessarily a native context.
1705 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1706 if err != nil {
1707 return err
1708 }
1709
1710 if ver.GreaterThan(sdkVersion) {
1711 return fmt.Errorf("newer SDK(%v)", ver)
1712 }
Jiyong Park99644e92020-11-17 22:21:02 +09001713 return nil
1714}
1715
Jiyong Park45bf82e2020-12-15 22:29:02 +09001716// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001717func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1718 depTag := ctx.OtherModuleDependencyTag(dep)
1719
1720 if ccm, ok := dep.(*cc.Module); ok {
1721 if ccm.HasStubsVariants() {
1722 if cc.IsSharedDepTag(depTag) {
1723 // dynamic dep to a stubs lib crosses APEX boundary
1724 return false
1725 }
1726 if cc.IsRuntimeDepTag(depTag) {
1727 // runtime dep to a stubs lib also crosses APEX boundary
1728 return false
1729 }
1730
1731 if cc.IsHeaderDepTag(depTag) {
1732 return false
1733 }
1734 }
1735 if mod.Static() && cc.IsSharedDepTag(depTag) {
1736 // shared_lib dependency from a static lib is considered as crossing
1737 // the APEX boundary because the dependency doesn't actually is
1738 // linked; the dependency is used only during the compilation phase.
1739 return false
1740 }
1741 }
1742
Matthew Maurer581b6d82022-09-29 16:46:25 -07001743 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001744 return false
1745 }
1746
1747 return true
1748}
1749
1750// Overrides ApexModule.IsInstallabeToApex()
1751func (mod *Module) IsInstallableToApex() bool {
1752 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001753 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001754 return true
1755 }
1756 if _, ok := mod.compiler.(*binaryDecorator); ok {
1757 return true
1758 }
1759 }
1760 return false
1761}
1762
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001763// If a library file has a "lib" prefix, extract the library name without the prefix.
1764func libNameFromFilePath(filepath android.Path) (string, bool) {
1765 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1766 if strings.HasPrefix(libName, "lib") {
1767 libName = libName[3:]
1768 return libName, true
1769 }
1770 return "", false
1771}
1772
Sasha Smundaka76acba2022-04-18 20:12:56 -07001773func kytheExtractRustFactory() android.Singleton {
1774 return &kytheExtractRustSingleton{}
1775}
1776
1777type kytheExtractRustSingleton struct {
1778}
1779
1780func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1781 var xrefTargets android.Paths
1782 ctx.VisitAllModules(func(module android.Module) {
1783 if rustModule, ok := module.(xref); ok {
1784 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1785 }
1786 })
1787 if len(xrefTargets) > 0 {
1788 ctx.Phony("xref_rust", xrefTargets...)
1789 }
1790}
1791
Jihoon Kangf78a8902022-09-01 22:47:07 +00001792func (c *Module) Partition() string {
1793 return ""
1794}
1795
Ivan Lozanoffee3342019-08-27 12:03:00 -07001796var Bool = proptools.Bool
1797var BoolDefault = proptools.BoolDefault
1798var String = proptools.String
1799var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001800
1801var _ android.OutputFileProducer = (*Module)(nil)