blob: e524c9fb631d9eeba3a8bf33f0a2019359ff2eb9 [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 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000048 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070049}
50
51type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040052 GlobalRustFlags []string // Flags that apply globally to rust
53 GlobalLinkFlags []string // Flags that apply globally to linker
54 RustFlags []string // Flags that apply to rust
55 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020056 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070057 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070058 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040059 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070061 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070062}
63
64type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050065 AndroidMkRlibs []string `blueprint:"mutated"`
66 AndroidMkDylibs []string `blueprint:"mutated"`
67 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
68 AndroidMkSharedLibs []string `blueprint:"mutated"`
69 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040070
Ivan Lozano6a884432020-12-02 09:15:16 -050071 ImageVariationPrefix string `blueprint:"mutated"`
72 VndkVersion string `blueprint:"mutated"`
73 SubName string `blueprint:"mutated"`
74
Ivan Lozanoc08897c2021-04-02 12:41:32 -040075 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
76 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
77 // appended before SubName.
78 RustSubName string `blueprint:"mutated"`
79
Ivan Lozano6a884432020-12-02 09:15:16 -050080 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050081 CoreVariantNeeded bool `blueprint:"mutated"`
82 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070083 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080084 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 ExtraVariants []string `blueprint:"mutated"`
86
Ivan Lozanoa2268632021-07-22 10:52:06 -040087 // Allows this module to use non-APEX version of libraries. Useful
88 // for building binaries that are started before APEXes are activated.
89 Bootstrap *bool
90
Ivan Lozano1921e802021-05-20 13:39:16 -040091 // Used by vendor snapshot to record dependencies from snapshot modules.
92 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090093 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040094
Matthew Maurerc6868382021-07-13 14:12:37 -070095 // Make this module available when building for ramdisk.
96 // On device without a dedicated recovery partition, the module is only
97 // available after switching root into
98 // /first_stage_ramdisk. To expose the module before switching root, install
99 // the recovery variant instead.
100 Ramdisk_available *bool
101
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500102 // Make this module available when building for vendor ramdisk.
103 // On device without a dedicated recovery partition, the module is only
104 // available after switching root into
105 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800106 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500107 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400108
Ivan Lozano1921e802021-05-20 13:39:16 -0400109 // Normally Soong uses the directory structure to decide which modules
110 // should be included (framework) or excluded (non-framework) from the
111 // different snapshots (vendor, recovery, etc.), but this property
112 // allows a partner to exclude a module normally thought of as a
113 // framework module from the vendor snapshot.
114 Exclude_from_vendor_snapshot *bool
115
116 // Normally Soong uses the directory structure to decide which modules
117 // should be included (framework) or excluded (non-framework) from the
118 // different snapshots (vendor, recovery, etc.), but this property
119 // allows a partner to exclude a module normally thought of as a
120 // framework module from the recovery snapshot.
121 Exclude_from_recovery_snapshot *bool
122
Matthew Maurer460ee942021-02-11 12:31:46 -0800123 // Make this module available when building for recovery
124 Recovery_available *bool
125
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500126 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
127 Min_sdk_version *string
128
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900129 HideFromMake bool `blueprint:"mutated"`
130 PreventInstall bool `blueprint:"mutated"`
131
132 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133}
134
135type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700136 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137
Ivan Lozano6a884432020-12-02 09:15:16 -0500138 VendorProperties cc.VendorProperties
139
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 Properties BaseProperties
141
142 hod android.HostOrDeviceSupported
143 multilib android.Multilib
144
Ivan Lozano6a884432020-12-02 09:15:16 -0500145 makeLinkType string
146
Yi Kong46c6e592022-01-20 22:55:00 +0800147 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400149 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200150 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500151 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400153 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700154 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400155
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400156 // Output file to be installed, may be stripped or unstripped.
157 outputFile android.OptionalPath
158
Sasha Smundaka76acba2022-04-18 20:12:56 -0700159 // Cross-reference input file
160 kytheFiles android.Paths
161
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400162 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900163
164 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500165
166 // For apex variants, this is set as apex.min_sdk_version
167 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700168}
169
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500170func (mod *Module) Header() bool {
171 //TODO: If Rust libraries provide header variants, this needs to be updated.
172 return false
173}
174
175func (mod *Module) SetPreventInstall() {
176 mod.Properties.PreventInstall = true
177}
178
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179func (mod *Module) SetHideFromMake() {
180 mod.Properties.HideFromMake = true
181}
182
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900183func (mod *Module) HiddenFromMake() bool {
184 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400185}
186
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500187func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500188 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
189 // nil since we need compiler to actually sanitize.
190 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500191}
192
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500193func (mod *Module) IsPrebuilt() bool {
194 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
195 return true
196 }
197 return false
198}
199
Ivan Lozano43845682020-07-09 21:03:28 -0400200func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
201 switch tag {
202 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700203 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400204 return mod.sourceProvider.Srcs(), nil
205 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900206 if mod.OutputFile().Valid() {
207 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400208 }
209 return android.Paths{}, nil
210 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500211 case "unstripped":
212 if mod.compiler != nil {
213 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
214 }
215 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400216 default:
217 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
218 }
219}
220
Ivan Lozano52767be2019-10-18 14:49:46 -0700221func (mod *Module) SelectedStl() string {
222 return ""
223}
224
Ivan Lozano2b262972019-11-21 12:30:50 -0800225func (mod *Module) NonCcVariants() bool {
226 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400227 if _, ok := mod.compiler.(libraryInterface); ok {
228 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800229 }
230 }
231 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
232}
233
Ivan Lozano52767be2019-10-18 14:49:46 -0700234func (mod *Module) Static() bool {
235 if mod.compiler != nil {
236 if library, ok := mod.compiler.(libraryInterface); ok {
237 return library.static()
238 }
239 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400240 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700241}
242
243func (mod *Module) Shared() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400246 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700247 }
248 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700250}
251
Ivan Lozanod7586b62021-04-01 09:49:36 -0400252func (mod *Module) Dylib() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok {
255 return library.dylib()
256 }
257 }
258 return false
259}
260
261func (mod *Module) Rlib() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok {
264 return library.rlib()
265 }
266 }
267 return false
268}
269
270func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400271 if binary, ok := mod.compiler.(binaryInterface); ok {
272 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400273 }
274 return false
275}
276
Justin Yun5e035862021-06-29 20:50:37 +0900277func (mod *Module) StaticExecutable() bool {
278 if !mod.Binary() {
279 return false
280 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400281 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900282}
283
Ivan Lozanod7586b62021-04-01 09:49:36 -0400284func (mod *Module) Object() bool {
285 // Rust has no modules which produce only object files.
286 return false
287}
288
Ivan Lozano52767be2019-10-18 14:49:46 -0700289func (mod *Module) Toc() android.OptionalPath {
290 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400291 if lib, ok := mod.compiler.(libraryInterface); ok {
292 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700293 }
294 }
295 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
296}
297
Colin Crossc511bc52020-04-07 16:50:32 +0000298func (mod *Module) UseSdk() bool {
299 return false
300}
301
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302func (mod *Module) RelativeInstallPath() string {
303 if mod.compiler != nil {
304 return mod.compiler.relativeInstallPath()
305 }
306 return ""
307}
308
Ivan Lozano52767be2019-10-18 14:49:46 -0700309func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500310 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700311}
312
Jiyong Park7d55b612021-06-11 17:22:09 +0900313func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400314 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900315}
316
Ivan Lozano52767be2019-10-18 14:49:46 -0700317func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400318 return true
319}
320
321func (mod *Module) SubName() string {
322 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700323}
324
325func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500326 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700327 return false
328}
329
Ivan Lozanof9e21722020-12-02 09:00:51 -0500330func (mod *Module) IsVndkExt() bool {
331 return false
332}
333
Ivan Lozanod7586b62021-04-01 09:49:36 -0400334func (mod *Module) IsVndkSp() bool {
335 return false
336}
337
Ivan Lozanof1868af2022-04-12 13:08:36 -0400338func (mod *Module) IsVndkPrebuiltLibrary() bool {
339 // Rust modules do not provide VNDK prebuilts
340 return false
341}
342
343func (mod *Module) IsVendorPublicLibrary() bool {
344 return mod.VendorProperties.IsVendorPublicLibrary
345}
346
347func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
348 // Rust modules to not provide Sdk variants
349 return false
350}
351
Colin Cross127bb8b2020-12-16 16:46:01 -0800352func (c *Module) IsVndkPrivate() bool {
353 return false
354}
355
356func (c *Module) IsLlndk() bool {
357 return false
358}
359
360func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500361 return false
362}
363
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400364func (mod *Module) KernelHeadersDecorator() bool {
365 return false
366}
367
Colin Cross1f3f1302021-04-26 18:37:44 -0700368func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400369 return false
370}
371
Colin Cross5271fea2021-04-27 13:06:04 -0700372func (m *Module) NeedsVendorPublicLibraryVariants() bool {
373 return false
374}
375
Ivan Lozanod7586b62021-04-01 09:49:36 -0400376func (mod *Module) HasLlndkStubs() bool {
377 return false
378}
379
380func (mod *Module) StubsVersion() string {
381 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
382}
383
Ivan Lozano52767be2019-10-18 14:49:46 -0700384func (mod *Module) SdkVersion() string {
385 return ""
386}
387
Colin Crossc511bc52020-04-07 16:50:32 +0000388func (mod *Module) AlwaysSdk() bool {
389 return false
390}
391
Jiyong Park2286afd2020-06-16 21:58:53 +0900392func (mod *Module) IsSdkVariant() bool {
393 return false
394}
395
Colin Cross1348ce32020-10-01 13:37:16 -0700396func (mod *Module) SplitPerApiLevel() bool {
397 return false
398}
399
Sasha Smundaka76acba2022-04-18 20:12:56 -0700400func (mod *Module) XrefRustFiles() android.Paths {
401 return mod.kytheFiles
402}
403
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400405 Dylibs []string
406 Rlibs []string
407 Rustlibs []string
408 Stdlibs []string
409 ProcMacros []string
410 SharedLibs []string
411 StaticLibs []string
412 WholeStaticLibs []string
413 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400415 // Used for data dependencies adjacent to tests
416 DataLibs []string
417 DataBins []string
418
Colin Crossfe605e12022-01-23 20:46:16 -0800419 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420}
421
422type PathDeps struct {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700423 DyLibs RustLibraries
424 RLibs RustLibraries
425 LibDeps android.Paths
426 WholeStaticLibs android.Paths
427 ProcMacros RustLibraries
428 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500429
430 // depFlags and depLinkFlags are rustc and linker (clang) flags.
431 depFlags []string
432 depLinkFlags []string
433
434 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
435 // Both of these are exported and propagate to dependencies.
436 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700437 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700438
Ivan Lozano45901ed2020-07-24 16:05:01 -0400439 // Used by bindgen modules which call clang
440 depClangFlags []string
441 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400442 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400443 depSystemIncludePaths android.Paths
444
Colin Crossfe605e12022-01-23 20:46:16 -0800445 CrtBegin android.Paths
446 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700447
448 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500449 SrcDeps android.Paths
450 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700451}
452
453type RustLibraries []RustLibrary
454
455type RustLibrary struct {
456 Path android.Path
457 CrateName string
458}
459
460type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100461 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700462 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400463 cfgFlags(ctx ModuleContext, flags Flags) Flags
464 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700465 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700466 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467 compilerDeps(ctx DepsContext, deps Deps) Deps
468 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700469 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700470
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100471 // Output directory in which source-generated code from dependencies is
472 // copied. This is equivalent to Cargo's OUT_DIR variable.
473 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700474
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400475 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
476 CargoPkgVersion() string
477
478 // CargoEnvCompat returns whether Cargo environment variables should be used.
479 CargoEnvCompat() bool
480
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800481 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200482 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700483 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400484 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400485
486 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400487
488 Disabled() bool
489 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400490
Ivan Lozanodd055472020-09-28 13:22:45 -0400491 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500492 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900493
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400494 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900495 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400496}
497
Matthew Maurerbb3add12020-06-25 09:34:12 -0700498type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700500 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700501}
502
Sasha Smundaka76acba2022-04-18 20:12:56 -0700503type xref interface {
504 XrefRustFiles() android.Paths
505}
506
Matthew Maurerbb3add12020-06-25 09:34:12 -0700507type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400508 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700509 linkObjects android.Paths
510 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700511}
512
Matthew Maurerbb3add12020-06-25 09:34:12 -0700513func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
514 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
515}
516
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700517func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
518 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
519}
520
521func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
522 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400523}
524
Colin Cross0de8a1e2020-09-18 14:15:30 -0700525func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
526 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700527 LinkDirs: flagExporter.linkDirs,
528 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700529 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700530 })
531}
532
Matthew Maurerbb3add12020-06-25 09:34:12 -0700533var _ exportedFlagsProducer = (*flagExporter)(nil)
534
535func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700536 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700537}
538
Colin Cross0de8a1e2020-09-18 14:15:30 -0700539type FlagExporterInfo struct {
540 Flags []string
541 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700542 LinkObjects android.Paths
543 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700544}
545
546var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
547
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400548func (mod *Module) isCoverageVariant() bool {
549 return mod.coverage.Properties.IsCoverageVariant
550}
551
552var _ cc.Coverage = (*Module)(nil)
553
554func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
555 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
556}
557
Ivan Lozanod7586b62021-04-01 09:49:36 -0400558func (mod *Module) VndkVersion() string {
559 return mod.Properties.VndkVersion
560}
561
562func (mod *Module) PreventInstall() bool {
563 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400564}
565
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400566func (mod *Module) MarkAsCoverageVariant(coverage bool) {
567 mod.coverage.Properties.IsCoverageVariant = coverage
568}
569
570func (mod *Module) EnableCoverageIfNeeded() {
571 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572}
573
574func defaultsFactory() android.Module {
575 return DefaultsFactory()
576}
577
578type Defaults struct {
579 android.ModuleBase
580 android.DefaultsModuleBase
581}
582
583func DefaultsFactory(props ...interface{}) android.Module {
584 module := &Defaults{}
585
586 module.AddProperties(props...)
587 module.AddProperties(
588 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800589 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500590 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100591 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400592 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593 &BaseCompilerProperties{},
594 &BinaryCompilerProperties{},
595 &LibraryCompilerProperties{},
596 &ProcMacroCompilerProperties{},
597 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400598 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700599 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400600 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400601 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200602 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500603 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604 )
605
606 android.InitDefaultsModule(module)
607 return module
608}
609
610func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700611 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700612}
613
Ivan Lozano183a3212019-10-18 14:18:45 -0700614func (mod *Module) CcLibrary() bool {
615 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500616 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700617 return true
618 }
619 }
620 return false
621}
622
623func (mod *Module) CcLibraryInterface() bool {
624 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400625 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
626 // VariantIs{Static,Shared} is set.
627 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700628 return true
629 }
630 }
631 return false
632}
633
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400634func (mod *Module) RustLibraryInterface() bool {
635 if mod.compiler != nil {
636 if _, ok := mod.compiler.(libraryInterface); ok {
637 return true
638 }
639 }
640 return false
641}
642
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500643func (mod *Module) IsFuzzModule() bool {
644 if _, ok := mod.compiler.(*fuzzDecorator); ok {
645 return true
646 }
647 return false
648}
649
650func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
651 return mod.FuzzModule
652}
653
654func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
655 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
656 return fuzzer.fuzzPackagedModule
657 }
658 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
659}
660
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000661func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500662 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
663 return fuzzer.sharedLibraries
664 }
665 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
666}
667
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400668func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400669 if mod.compiler != nil {
670 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400671 }
672 return nil
673}
674
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800675func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700676 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700677 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800678 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700679 }
680 }
681 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
682}
683
684func (mod *Module) SetStatic() {
685 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700686 if library, ok := mod.compiler.(libraryInterface); ok {
687 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700688 return
689 }
690 }
691 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
692}
693
694func (mod *Module) SetShared() {
695 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700696 if library, ok := mod.compiler.(libraryInterface); ok {
697 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700698 return
699 }
700 }
701 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
702}
703
Ivan Lozano183a3212019-10-18 14:18:45 -0700704func (mod *Module) BuildStaticVariant() bool {
705 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700706 if library, ok := mod.compiler.(libraryInterface); ok {
707 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700708 }
709 }
710 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
711}
712
713func (mod *Module) BuildSharedVariant() bool {
714 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700715 if library, ok := mod.compiler.(libraryInterface); ok {
716 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700717 }
718 }
719 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
720}
721
Ivan Lozano183a3212019-10-18 14:18:45 -0700722func (mod *Module) Module() android.Module {
723 return mod
724}
725
Ivan Lozano183a3212019-10-18 14:18:45 -0700726func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400727 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700728}
729
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400730func (mod *Module) CoverageFiles() android.Paths {
731 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800732 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400733 }
734 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
735}
736
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400737// Rust does not produce gcno files, and therefore does not produce a coverage archive.
738func (mod *Module) CoverageOutputFile() android.OptionalPath {
739 return android.OptionalPath{}
740}
741
742func (mod *Module) IsNdk(config android.Config) bool {
743 return false
744}
745
746func (mod *Module) IsStubs() bool {
747 return false
748}
749
Jiyong Park459feca2020-12-15 11:02:21 +0900750func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900751 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900752 return false
753 }
754
Jiyong Park459feca2020-12-15 11:02:21 +0900755 // The apex variant is not installable because it is included in the APEX and won't appear
756 // in the system partition as a standalone file.
757 if !apexInfo.IsForPlatform() {
758 return false
759 }
760
Jiyong Parke54f07e2021-04-07 15:08:04 +0900761 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900762}
763
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500764func (ctx moduleContext) apexVariationName() string {
765 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
766}
767
Ivan Lozano183a3212019-10-18 14:18:45 -0700768var _ cc.LinkableInterface = (*Module)(nil)
769
Ivan Lozanoffee3342019-08-27 12:03:00 -0700770func (mod *Module) Init() android.Module {
771 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500772 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773
Yi Kong46c6e592022-01-20 22:55:00 +0800774 if mod.afdo != nil {
775 mod.AddProperties(mod.afdo.props()...)
776 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777 if mod.compiler != nil {
778 mod.AddProperties(mod.compiler.compilerProps()...)
779 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400780 if mod.coverage != nil {
781 mod.AddProperties(mod.coverage.props()...)
782 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200783 if mod.clippy != nil {
784 mod.AddProperties(mod.clippy.props()...)
785 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400786 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700787 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400788 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500789 if mod.sanitize != nil {
790 mod.AddProperties(mod.sanitize.props()...)
791 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400792
Ivan Lozanoffee3342019-08-27 12:03:00 -0700793 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900794 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795
796 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797 return mod
798}
799
800func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
801 return &Module{
802 hod: hod,
803 multilib: multilib,
804 }
805}
806func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
807 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800808 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400809 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200810 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500811 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 return module
813}
814
815type ModuleContext interface {
816 android.ModuleContext
817 ModuleContextIntf
818}
819
820type BaseModuleContext interface {
821 android.BaseModuleContext
822 ModuleContextIntf
823}
824
825type DepsContext interface {
826 android.BottomUpMutatorContext
827 ModuleContextIntf
828}
829
830type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200831 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700832 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833}
834
835type depsContext struct {
836 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700837}
838
839type moduleContext struct {
840 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841}
842
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200843type baseModuleContext struct {
844 android.BaseModuleContext
845}
846
847func (ctx *moduleContext) RustModule() *Module {
848 return ctx.Module().(*Module)
849}
850
851func (ctx *moduleContext) toolchain() config.Toolchain {
852 return ctx.RustModule().toolchain(ctx)
853}
854
855func (ctx *depsContext) RustModule() *Module {
856 return ctx.Module().(*Module)
857}
858
859func (ctx *depsContext) toolchain() config.Toolchain {
860 return ctx.RustModule().toolchain(ctx)
861}
862
863func (ctx *baseModuleContext) RustModule() *Module {
864 return ctx.Module().(*Module)
865}
866
867func (ctx *baseModuleContext) toolchain() config.Toolchain {
868 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400869}
870
871func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700872 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
873 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
874 return false
875 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400876 return mod.compiler != nil && mod.compiler.nativeCoverage()
877}
878
Ivan Lozanod7586b62021-04-01 09:49:36 -0400879func (mod *Module) EverInstallable() bool {
880 return mod.compiler != nil &&
881 // Check to see whether the module is actually ever installable.
882 mod.compiler.everInstallable()
883}
884
885func (mod *Module) Installable() *bool {
886 return mod.Properties.Installable
887}
888
Ivan Lozano872d5792022-03-23 17:31:39 -0400889func (mod *Module) ProcMacro() bool {
890 if pm, ok := mod.compiler.(procMacroInterface); ok {
891 return pm.ProcMacro()
892 }
893 return false
894}
895
Ivan Lozanoffee3342019-08-27 12:03:00 -0700896func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
897 if mod.cachedToolchain == nil {
898 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
899 }
900 return mod.cachedToolchain
901}
902
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200903func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
904 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
905}
906
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
908}
909
910func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
911 ctx := &moduleContext{
912 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914
Jiyong Park99644e92020-11-17 22:21:02 +0900915 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
916 if !apexInfo.IsForPlatform() {
917 mod.hideApexVariantFromMake = true
918 }
919
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500921 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
922
Ivan Lozanof1868af2022-04-12 13:08:36 -0400923 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700924
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 if !toolchain.Supported() {
926 // This toolchain's unsupported, there's nothing to do for this mod.
927 return
928 }
929
930 deps := mod.depsToPaths(ctx)
931 flags := Flags{
932 Toolchain: toolchain,
933 }
934
Ivan Lozano67eada32021-09-23 11:50:33 -0400935 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800936 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500937 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800938 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939 if mod.compiler != nil {
940 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400941 flags = mod.compiler.cfgFlags(ctx, flags)
942 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400943 }
944 if mod.coverage != nil {
945 flags, deps = mod.coverage.flags(ctx, flags, deps)
946 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200947 if mod.clippy != nil {
948 flags, deps = mod.clippy.flags(ctx, flags, deps)
949 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500950 if mod.sanitize != nil {
951 flags, deps = mod.sanitize.flags(ctx, flags, deps)
952 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400953
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200954 // SourceProvider needs to call GenerateSource() before compiler calls
955 // compile() so it can provide the source. A SourceProvider has
956 // multiple variants (e.g. source, rlib, dylib). Only the "source"
957 // variant is responsible for effectively generating the source. The
958 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400959 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200960 if mod.compiler.(libraryInterface).source() {
961 mod.sourceProvider.GenerateSource(ctx, deps)
962 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
963 } else {
964 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
965 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700966 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200967 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400968 }
969
970 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100971 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700972 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400973 if ctx.Failed() {
974 return
975 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700976 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
977 if buildOutput.kytheFile != nil {
978 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
979 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400980 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900981
Dan Albert06feee92021-03-19 15:06:02 -0700982 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000983 if mod.docTimestampFile.Valid() {
984 ctx.CheckbuildFile(mod.docTimestampFile.Path())
985 }
Dan Albert06feee92021-03-19 15:06:02 -0700986
Ivan Lozano1921e802021-05-20 13:39:16 -0400987 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
988 // RECOVERY_SNAPSHOT_VERSION is current.
989 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
990 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
991 lib.collectHeadersForSnapshot(ctx, deps)
992 }
993 }
994
Jiyong Park459feca2020-12-15 11:02:21 +0900995 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400996 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900997 // If the module has been specifically configure to not be installed then
998 // hide from make as otherwise it will break when running inside make as the
999 // output path to install will not be specified. Not all uninstallable
1000 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001001 // side dependencies. In particular, proc-macros need to be captured in the
1002 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001003 mod.HideFromMake()
1004 } else if !mod.installable(apexInfo) {
1005 mod.SkipInstall()
1006 }
1007
1008 // Still call install though, the installs will be stored as PackageSpecs to allow
1009 // using the outputs in a genrule.
1010 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001011 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001012 if ctx.Failed() {
1013 return
1014 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001015 }
Chris Wailes74be7642021-07-22 16:20:28 -07001016
1017 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018 }
1019}
1020
1021func (mod *Module) deps(ctx DepsContext) Deps {
1022 deps := Deps{}
1023
1024 if mod.compiler != nil {
1025 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001026 }
1027 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001028 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 }
1030
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001031 if mod.coverage != nil {
1032 deps = mod.coverage.deps(ctx, deps)
1033 }
1034
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001035 if mod.sanitize != nil {
1036 deps = mod.sanitize.deps(ctx, deps)
1037 }
1038
Ivan Lozanoffee3342019-08-27 12:03:00 -07001039 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1040 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001041 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1043 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1044 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001045 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001046 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 return deps
1048
1049}
1050
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051type dependencyTag struct {
1052 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001053 name string
1054 library bool
1055 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001056 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001057}
1058
Jiyong Park65b62242020-11-25 12:44:59 +09001059// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1060// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1061func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001062 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001063}
1064
1065var _ android.InstallNeededDependencyTag = dependencyTag{}
1066
Colin Cross65cb3142021-12-10 23:05:02 +00001067func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1068 if d.library && d.dynamic {
1069 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1070 }
1071 return nil
1072}
1073
1074var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1075
Ivan Lozanoffee3342019-08-27 12:03:00 -07001076var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001077 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1078 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001079 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001080 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001081 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001082 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001083 dataLibDepTag = dependencyTag{name: "data lib"}
1084 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001085)
1086
Jiyong Park99644e92020-11-17 22:21:02 +09001087func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1088 tag, ok := depTag.(dependencyTag)
1089 return ok && tag == dylibDepTag
1090}
1091
Jiyong Park94e22fd2021-04-08 18:19:15 +09001092func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1093 tag, ok := depTag.(dependencyTag)
1094 return ok && tag == rlibDepTag
1095}
1096
Matthew Maurer0f003b12020-06-29 14:34:06 -07001097type autoDep struct {
1098 variation string
1099 depTag dependencyTag
1100}
1101
1102var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001103 rlibVariation = "rlib"
1104 dylibVariation = "dylib"
1105 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1106 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001107)
1108
1109type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001110 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001111}
1112
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001113func (mod *Module) begin(ctx BaseModuleContext) {
1114 if mod.coverage != nil {
1115 mod.coverage.begin(ctx)
1116 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001117 if mod.sanitize != nil {
1118 mod.sanitize.begin(ctx)
1119 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001120}
1121
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001122func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001123 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001124 return p.prebuilt()
1125 }
1126 return nil
1127}
1128
Justin Yun24b246a2023-03-16 10:36:16 +09001129func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1130 if rustDep, ok := dep.(*Module); ok {
1131 // Use base module name for snapshots when exporting to Makefile.
1132 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1133 baseName := rustDep.BaseModuleName()
1134 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1135 }
1136 }
1137 return cc.MakeLibName(ctx, c, dep, depName)
1138}
1139
Ivan Lozanoffee3342019-08-27 12:03:00 -07001140func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1141 var depPaths PathDeps
1142
1143 directRlibDeps := []*Module{}
1144 directDylibDeps := []*Module{}
1145 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001146 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001147 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001148 directSrcProvidersDeps := []*Module{}
1149 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001150
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001151 // For the dependency from platform to apex, use the latest stubs
1152 mod.apexSdkVersion = android.FutureApiLevel
1153 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1154 if !apexInfo.IsForPlatform() {
1155 mod.apexSdkVersion = apexInfo.MinSdkVersion
1156 }
1157
1158 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1159 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1160 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1161 // (b/144430859)
1162 mod.apexSdkVersion = android.FutureApiLevel
1163 }
1164
Spandan Das604f3762023-03-16 22:51:40 +00001165 skipModuleList := map[string]bool{}
1166
1167 var apiImportInfo multitree.ApiImportInfo
1168 hasApiImportInfo := false
1169
1170 ctx.VisitDirectDeps(func(dep android.Module) {
1171 if dep.Name() == "api_imports" {
1172 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1173 hasApiImportInfo = true
1174 }
1175 })
1176
1177 if hasApiImportInfo {
1178 targetStubModuleList := map[string]string{}
1179 targetOrigModuleList := map[string]string{}
1180
1181 // Search for dependency which both original module and API imported library with APEX stub exists
1182 ctx.VisitDirectDeps(func(dep android.Module) {
1183 depName := ctx.OtherModuleName(dep)
1184 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1185 targetStubModuleList[apiLibrary] = depName
1186 }
1187 })
1188 ctx.VisitDirectDeps(func(dep android.Module) {
1189 depName := ctx.OtherModuleName(dep)
1190 if origLibrary, ok := targetStubModuleList[depName]; ok {
1191 targetOrigModuleList[origLibrary] = depName
1192 }
1193 })
1194
1195 // Decide which library should be used between original and API imported library
1196 ctx.VisitDirectDeps(func(dep android.Module) {
1197 depName := ctx.OtherModuleName(dep)
1198 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1199 if cc.ShouldUseStubForApex(ctx, dep) {
1200 skipModuleList[depName] = true
1201 } else {
1202 skipModuleList[apiLibrary] = true
1203 }
1204 }
1205 })
1206 }
1207
Ivan Lozanoffee3342019-08-27 12:03:00 -07001208 ctx.VisitDirectDeps(func(dep android.Module) {
1209 depName := ctx.OtherModuleName(dep)
1210 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001211
Spandan Das604f3762023-03-16 22:51:40 +00001212 if _, exists := skipModuleList[depName]; exists {
1213 return
1214 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001215 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001216 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001217 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001218
Ivan Lozanoffee3342019-08-27 12:03:00 -07001219 switch depTag {
1220 case dylibDepTag:
1221 dylib, ok := rustDep.compiler.(libraryInterface)
1222 if !ok || !dylib.dylib() {
1223 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1224 return
1225 }
1226 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001227 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001228 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001229
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230 rlib, ok := rustDep.compiler.(libraryInterface)
1231 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001232 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001233 return
1234 }
1235 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001236 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237 case procMacroDepTag:
1238 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001239 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001240 }
1241
1242 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001243 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1244 // OS/Arch variant is used.
1245 var helper string
1246 if ctx.Host() {
1247 helper = "missing 'host_supported'?"
1248 } else {
1249 helper = "device module defined?"
1250 }
1251
1252 if dep.Target().Os != ctx.Os() {
1253 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1254 return
1255 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1256 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1257 return
1258 }
1259 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001260 }
1261
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001262 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001263 if depTag != procMacroDepTag {
1264 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1265 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1266 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1267 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001268 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269 }
1270
Ivan Lozanoffee3342019-08-27 12:03:00 -07001271 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001272 linkFile := rustDep.UnstrippedOutputFile()
1273 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001274 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1275 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001276 }
1277 }
1278
Ivan Lozano89435d12020-07-31 11:01:18 -04001279 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001280 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001281 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001282 if _, ok := ccDep.(*Module); !ok {
1283 if ccDep.Module().Target().Os != ctx.Os() {
1284 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1285 return
1286 }
1287 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1288 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1289 return
1290 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001291 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001292 linkObject := ccDep.OutputFile()
1293 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001294
Ivan Lozano2093af22020-08-25 12:48:19 -04001295 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001296 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1297 }
1298
1299 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001300 switch {
1301 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001302 if cc.IsWholeStaticLib(depTag) {
1303 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1304 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001305 if mod.Binary() {
1306 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1307 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1308 // final linkage, pass the args directly to the linker to handle these cases.
1309 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1310 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001311 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001312 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001313 } else {
1314 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 -05001315 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001316 }
1317
1318 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1319 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001320 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001321 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1322
Colin Cross0de8a1e2020-09-18 14:15:30 -07001323 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1324 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1325 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1326 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1327 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001328 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001329
1330 // Record baseLibName for snapshots.
1331 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1332
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001333 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001334 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001335 // For the shared lib dependencies, we may link to the stub variant
1336 // of the dependency depending on the context (e.g. if this
1337 // dependency crosses the APEX boundaries).
1338 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1339
1340 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1341 // object (either from stub or non-stub) to use.
1342 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1343 linkPath = linkPathFromFilePath(linkObject.Path())
1344
Ivan Lozanoffee3342019-08-27 12:03:00 -07001345 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001346 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001347 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1348 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1349 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1350 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001351 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001352
1353 // Record baseLibName for snapshots.
1354 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1355
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001356 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001357 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001358 case cc.IsHeaderDepTag(depTag):
1359 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1360 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1361 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1362 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001363 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001364 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001365 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001366 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001367 }
1368
1369 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001370 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1371 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001372 if linkObject.Valid() {
1373 lib.exportLinkObjects(linkObject.Path())
1374 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001375 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001376 } else {
1377 switch {
1378 case depTag == cc.CrtBeginDepTag:
1379 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1380 case depTag == cc.CrtEndDepTag:
1381 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1382 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001383 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001384
1385 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001386 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001387 // These are usually genrules which don't have per-target variants.
1388 directSrcDeps = append(directSrcDeps, srcDep)
1389 }
1390 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001391 })
1392
1393 var rlibDepFiles RustLibraries
1394 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001395 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001396 }
1397 var dylibDepFiles RustLibraries
1398 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001399 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001400 }
1401 var procMacroDepFiles RustLibraries
1402 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001403 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001404 }
1405
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001406 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001407 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001408 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001409 }
1410
Ivan Lozanoffee3342019-08-27 12:03:00 -07001411 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001412 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001413 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001414 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001415 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001416 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001417 }
1418
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001419 var srcProviderDepFiles android.Paths
1420 for _, dep := range directSrcProvidersDeps {
1421 srcs, _ := dep.OutputFiles("")
1422 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1423 }
1424 for _, dep := range directSrcDeps {
1425 srcs := dep.Srcs()
1426 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1427 }
1428
Ivan Lozanoffee3342019-08-27 12:03:00 -07001429 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1430 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001431 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001432 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001433 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001434
1435 // Dedup exported flags from dependencies
1436 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001437 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001438 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001439 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1440 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1441 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001442
1443 return depPaths
1444}
1445
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001446func (mod *Module) InstallInData() bool {
1447 if mod.compiler == nil {
1448 return false
1449 }
1450 return mod.compiler.inData()
1451}
1452
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001453func (mod *Module) InstallInRamdisk() bool {
1454 return mod.InRamdisk()
1455}
1456
1457func (mod *Module) InstallInVendorRamdisk() bool {
1458 return mod.InVendorRamdisk()
1459}
1460
1461func (mod *Module) InstallInRecovery() bool {
1462 return mod.InRecovery()
1463}
1464
Ivan Lozanoffee3342019-08-27 12:03:00 -07001465func linkPathFromFilePath(filepath android.Path) string {
1466 return strings.Split(filepath.String(), filepath.Base())[0]
1467}
Ivan Lozanod648c432020-02-06 12:05:10 -05001468
Spandan Das604f3762023-03-16 22:51:40 +00001469// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1470func (r *Module) usePublicApi() bool {
1471 return r.Device() && r.UseSdk()
1472}
1473
1474// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1475func (r *Module) useVendorApi() bool {
1476 return r.Device() && (r.InVendor() || r.InProduct())
1477}
1478
Ivan Lozanoffee3342019-08-27 12:03:00 -07001479func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1480 ctx := &depsContext{
1481 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001482 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001483
1484 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001485 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001486 var snapshotInfo *cc.SnapshotInfo
1487
Kiyoung Kim487689e2022-07-26 09:48:22 +09001488 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001489 if mod.usePublicApi() || mod.useVendorApi() {
1490 for idx, lib := range deps.SharedLibs {
1491 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1492 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001493 }
1494
Ivan Lozano1921e802021-05-20 13:39:16 -04001495 if ctx.Os() == android.Android {
1496 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1497 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001498
Ivan Lozano2b081132020-09-08 12:46:52 -04001499 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001500 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001501 stdLinkage = "rlib-std"
1502 }
1503
1504 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001505
Ivan Lozano2b081132020-09-08 12:46:52 -04001506 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1507 rlibDepVariations = append(rlibDepVariations,
1508 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1509 }
1510
Ivan Lozano1921e802021-05-20 13:39:16 -04001511 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001512 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001513 for _, lib := range deps.Rlibs {
1514 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001515 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001516
Ivan Lozano2d407632022-04-07 12:59:11 -04001517 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001518 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001519
1520 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001521 actx.AddVariationDependencies(
1522 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001523 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001524 dylibDepTag, deps.Dylibs...)
1525
Ivan Lozano1921e802021-05-20 13:39:16 -04001526 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001527 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1528 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001529 for _, lib := range deps.Rustlibs {
1530 if autoDep.depTag == rlibDepTag {
1531 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001532 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001533 } else {
1534 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1535 // Check for the existence of the dylib deptag variant. Select it if available,
1536 // otherwise select the rlib variant.
1537 autoDepVariations := append(commonDepVariations,
1538 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1539 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1540 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
1541 } else {
1542 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001543 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001544 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001545 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001546 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001547 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001548 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001549 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001550 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001551 for _, lib := range deps.Stdlibs {
1552 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001553 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001554
1555 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1556 depTag, lib)
1557 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001558 } else {
1559 actx.AddVariationDependencies(
1560 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1561 dylibDepTag, deps.Stdlibs...)
1562 }
1563 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001564
1565 for _, lib := range deps.SharedLibs {
1566 depTag := cc.SharedDepTag()
1567 name, version := cc.StubsLibNameAndVersion(lib)
1568
1569 variations := []blueprint.Variation{
1570 {Mutator: "link", Variation: "shared"},
1571 }
Spandan Das604f3762023-03-16 22:51:40 +00001572 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1573 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1574 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1575 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1576 }
1577
1578 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1579 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1580 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001581 }
1582
1583 for _, lib := range deps.WholeStaticLibs {
1584 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001585 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001586
1587 actx.AddVariationDependencies([]blueprint.Variation{
1588 {Mutator: "link", Variation: "static"},
1589 }, depTag, lib)
1590 }
1591
1592 for _, lib := range deps.StaticLibs {
1593 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001594 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001595
1596 actx.AddVariationDependencies([]blueprint.Variation{
1597 {Mutator: "link", Variation: "static"},
1598 }, depTag, lib)
1599 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001600
Zach Johnson3df4e632020-11-06 11:56:27 -08001601 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1602
Colin Cross565cafd2020-09-25 18:47:38 -07001603 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001604 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001605 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001606 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001607 }
Colin Crossfe605e12022-01-23 20:46:16 -08001608 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001609 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001610 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001611 }
1612
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001613 if mod.sourceProvider != nil {
1614 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1615 bindgen.Properties.Custom_bindgen != "" {
1616 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1617 bindgen.Properties.Custom_bindgen)
1618 }
1619 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001620
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001621 actx.AddVariationDependencies([]blueprint.Variation{
1622 {Mutator: "link", Variation: "shared"},
1623 }, dataLibDepTag, deps.DataLibs...)
1624
1625 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1626
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001627 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001628 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001629
1630 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001631}
1632
Ivan Lozano2d407632022-04-07 12:59:11 -04001633// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001634func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1635 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001636 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1637}
1638
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001639func BeginMutator(ctx android.BottomUpMutatorContext) {
1640 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1641 mod.beginMutator(ctx)
1642 }
1643}
1644
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001645func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1646 ctx := &baseModuleContext{
1647 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001648 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001649
1650 mod.begin(ctx)
1651}
1652
Ivan Lozanoffee3342019-08-27 12:03:00 -07001653func (mod *Module) Name() string {
1654 name := mod.ModuleBase.Name()
1655 if p, ok := mod.compiler.(interface {
1656 Name(string) string
1657 }); ok {
1658 name = p.Name(name)
1659 }
1660 return name
1661}
1662
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001663func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001664 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001665 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001666 }
1667}
1668
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001669var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001670var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001671
1672func (mod *Module) HostToolPath() android.OptionalPath {
1673 if !mod.Host() {
1674 return android.OptionalPath{}
1675 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001676 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1677 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001678 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1679 // Even though proc-macros aren't strictly "tools", since they target the compiler
1680 // and act as compiler plugins, we treat them similarly.
1681 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001682 }
1683 return android.OptionalPath{}
1684}
1685
Jiyong Park99644e92020-11-17 22:21:02 +09001686var _ android.ApexModule = (*Module)(nil)
1687
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001688func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001689 return String(mod.Properties.Min_sdk_version)
1690}
1691
Jiyong Park45bf82e2020-12-15 22:29:02 +09001692// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001693func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001694 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001695 if minSdkVersion == "apex_inherit" {
1696 return nil
1697 }
1698 if minSdkVersion == "" {
1699 return fmt.Errorf("min_sdk_version is not specificed")
1700 }
1701
1702 // Not using nativeApiLevelFromUser because the context here is not
1703 // necessarily a native context.
1704 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1705 if err != nil {
1706 return err
1707 }
1708
1709 if ver.GreaterThan(sdkVersion) {
1710 return fmt.Errorf("newer SDK(%v)", ver)
1711 }
Jiyong Park99644e92020-11-17 22:21:02 +09001712 return nil
1713}
1714
Jiyong Park45bf82e2020-12-15 22:29:02 +09001715// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001716func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1717 depTag := ctx.OtherModuleDependencyTag(dep)
1718
1719 if ccm, ok := dep.(*cc.Module); ok {
1720 if ccm.HasStubsVariants() {
1721 if cc.IsSharedDepTag(depTag) {
1722 // dynamic dep to a stubs lib crosses APEX boundary
1723 return false
1724 }
1725 if cc.IsRuntimeDepTag(depTag) {
1726 // runtime dep to a stubs lib also crosses APEX boundary
1727 return false
1728 }
1729
1730 if cc.IsHeaderDepTag(depTag) {
1731 return false
1732 }
1733 }
1734 if mod.Static() && cc.IsSharedDepTag(depTag) {
1735 // shared_lib dependency from a static lib is considered as crossing
1736 // the APEX boundary because the dependency doesn't actually is
1737 // linked; the dependency is used only during the compilation phase.
1738 return false
1739 }
1740 }
1741
Matthew Maurer581b6d82022-09-29 16:46:25 -07001742 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001743 return false
1744 }
1745
1746 return true
1747}
1748
1749// Overrides ApexModule.IsInstallabeToApex()
1750func (mod *Module) IsInstallableToApex() bool {
1751 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001752 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001753 return true
1754 }
1755 if _, ok := mod.compiler.(*binaryDecorator); ok {
1756 return true
1757 }
1758 }
1759 return false
1760}
1761
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001762// If a library file has a "lib" prefix, extract the library name without the prefix.
1763func libNameFromFilePath(filepath android.Path) (string, bool) {
1764 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1765 if strings.HasPrefix(libName, "lib") {
1766 libName = libName[3:]
1767 return libName, true
1768 }
1769 return "", false
1770}
1771
Sasha Smundaka76acba2022-04-18 20:12:56 -07001772func kytheExtractRustFactory() android.Singleton {
1773 return &kytheExtractRustSingleton{}
1774}
1775
1776type kytheExtractRustSingleton struct {
1777}
1778
1779func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1780 var xrefTargets android.Paths
1781 ctx.VisitAllModules(func(module android.Module) {
1782 if rustModule, ok := module.(xref); ok {
1783 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1784 }
1785 })
1786 if len(xrefTargets) > 0 {
1787 ctx.Phony("xref_rust", xrefTargets...)
1788 }
1789}
1790
Jihoon Kangf78a8902022-09-01 22:47:07 +00001791func (c *Module) Partition() string {
1792 return ""
1793}
1794
Ivan Lozanoffee3342019-08-27 12:03:00 -07001795var Bool = proptools.Bool
1796var BoolDefault = proptools.BoolDefault
1797var String = proptools.String
1798var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001799
1800var _ android.OutputFileProducer = (*Module)(nil)