blob: 689ff38c0840fdaf0d31019991e4b25fcbb14591 [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"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050069 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 Lozanoadd122a2023-07-13 11:01:41 -040094 SnapshotRlibs []string `blueprint:"mutated"`
95 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040096
Matthew Maurerc6868382021-07-13 14:12:37 -070097 // Make this module available when building for ramdisk.
98 // On device without a dedicated recovery partition, the module is only
99 // available after switching root into
100 // /first_stage_ramdisk. To expose the module before switching root, install
101 // the recovery variant instead.
102 Ramdisk_available *bool
103
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500104 // Make this module available when building for vendor ramdisk.
105 // On device without a dedicated recovery partition, the module is only
106 // available after switching root into
107 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800108 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500109 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400110
Ivan Lozano1921e802021-05-20 13:39:16 -0400111 // Normally Soong uses the directory structure to decide which modules
112 // should be included (framework) or excluded (non-framework) from the
113 // different snapshots (vendor, recovery, etc.), but this property
114 // allows a partner to exclude a module normally thought of as a
115 // framework module from the vendor snapshot.
116 Exclude_from_vendor_snapshot *bool
117
118 // Normally Soong uses the directory structure to decide which modules
119 // should be included (framework) or excluded (non-framework) from the
120 // different snapshots (vendor, recovery, etc.), but this property
121 // allows a partner to exclude a module normally thought of as a
122 // framework module from the recovery snapshot.
123 Exclude_from_recovery_snapshot *bool
124
Matthew Maurer460ee942021-02-11 12:31:46 -0800125 // Make this module available when building for recovery
126 Recovery_available *bool
127
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500128 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
129 Min_sdk_version *string
130
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900131 HideFromMake bool `blueprint:"mutated"`
132 PreventInstall bool `blueprint:"mutated"`
133
134 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135}
136
137type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700138 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139
Ivan Lozano6a884432020-12-02 09:15:16 -0500140 VendorProperties cc.VendorProperties
141
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142 Properties BaseProperties
143
144 hod android.HostOrDeviceSupported
145 multilib android.Multilib
146
Ivan Lozano6a884432020-12-02 09:15:16 -0500147 makeLinkType string
148
Yi Kong46c6e592022-01-20 22:55:00 +0800149 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400151 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200152 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500153 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400155 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700156 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400158 // Output file to be installed, may be stripped or unstripped.
159 outputFile android.OptionalPath
160
Sasha Smundaka76acba2022-04-18 20:12:56 -0700161 // Cross-reference input file
162 kytheFiles android.Paths
163
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400164 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900165
166 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500167
168 // For apex variants, this is set as apex.min_sdk_version
169 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700170
171 transitiveAndroidMkSharedLibs *android.DepSet[string]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172}
173
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500174func (mod *Module) Header() bool {
175 //TODO: If Rust libraries provide header variants, this needs to be updated.
176 return false
177}
178
179func (mod *Module) SetPreventInstall() {
180 mod.Properties.PreventInstall = true
181}
182
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500183func (mod *Module) SetHideFromMake() {
184 mod.Properties.HideFromMake = true
185}
186
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900187func (mod *Module) HiddenFromMake() bool {
188 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400189}
190
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500191func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500192 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
193 // nil since we need compiler to actually sanitize.
194 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500195}
196
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500197func (mod *Module) IsPrebuilt() bool {
198 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
199 return true
200 }
201 return false
202}
203
Ivan Lozano43845682020-07-09 21:03:28 -0400204func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
205 switch tag {
206 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700207 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400208 return mod.sourceProvider.Srcs(), nil
209 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900210 if mod.OutputFile().Valid() {
211 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400212 }
213 return android.Paths{}, nil
214 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500215 case "unstripped":
216 if mod.compiler != nil {
217 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
218 }
219 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400220 default:
221 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
222 }
223}
224
Ivan Lozano52767be2019-10-18 14:49:46 -0700225func (mod *Module) SelectedStl() string {
226 return ""
227}
228
Ivan Lozano2b262972019-11-21 12:30:50 -0800229func (mod *Module) NonCcVariants() bool {
230 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400231 if _, ok := mod.compiler.(libraryInterface); ok {
232 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800233 }
234 }
235 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
236}
237
Ivan Lozano52767be2019-10-18 14:49:46 -0700238func (mod *Module) Static() bool {
239 if mod.compiler != nil {
240 if library, ok := mod.compiler.(libraryInterface); ok {
241 return library.static()
242 }
243 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400244 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700245}
246
247func (mod *Module) Shared() bool {
248 if mod.compiler != nil {
249 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400250 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700251 }
252 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400253 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700254}
255
Ivan Lozanod7586b62021-04-01 09:49:36 -0400256func (mod *Module) Dylib() bool {
257 if mod.compiler != nil {
258 if library, ok := mod.compiler.(libraryInterface); ok {
259 return library.dylib()
260 }
261 }
262 return false
263}
264
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400265func (mod *Module) RlibStd() bool {
266 if mod.compiler != nil {
267 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
268 return library.rlibStd()
269 }
270 }
271 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
272}
273
Ivan Lozanod7586b62021-04-01 09:49:36 -0400274func (mod *Module) Rlib() bool {
275 if mod.compiler != nil {
276 if library, ok := mod.compiler.(libraryInterface); ok {
277 return library.rlib()
278 }
279 }
280 return false
281}
282
283func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400284 if binary, ok := mod.compiler.(binaryInterface); ok {
285 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400286 }
287 return false
288}
289
Justin Yun5e035862021-06-29 20:50:37 +0900290func (mod *Module) StaticExecutable() bool {
291 if !mod.Binary() {
292 return false
293 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400294 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900295}
296
Ivan Lozanod7586b62021-04-01 09:49:36 -0400297func (mod *Module) Object() bool {
298 // Rust has no modules which produce only object files.
299 return false
300}
301
Ivan Lozano52767be2019-10-18 14:49:46 -0700302func (mod *Module) Toc() android.OptionalPath {
303 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400304 if lib, ok := mod.compiler.(libraryInterface); ok {
305 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700306 }
307 }
308 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
309}
310
Colin Crossc511bc52020-04-07 16:50:32 +0000311func (mod *Module) UseSdk() bool {
312 return false
313}
314
Ivan Lozanod7586b62021-04-01 09:49:36 -0400315func (mod *Module) RelativeInstallPath() string {
316 if mod.compiler != nil {
317 return mod.compiler.relativeInstallPath()
318 }
319 return ""
320}
321
Ivan Lozano52767be2019-10-18 14:49:46 -0700322func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500323 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700324}
325
Jiyong Park7d55b612021-06-11 17:22:09 +0900326func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400327 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900328}
329
Ivan Lozano52767be2019-10-18 14:49:46 -0700330func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400331 return true
332}
333
334func (mod *Module) SubName() string {
335 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700336}
337
338func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500339 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700340 return false
341}
342
Ivan Lozanof9e21722020-12-02 09:00:51 -0500343func (mod *Module) IsVndkExt() bool {
344 return false
345}
346
Ivan Lozanod7586b62021-04-01 09:49:36 -0400347func (mod *Module) IsVndkSp() bool {
348 return false
349}
350
Ivan Lozanof1868af2022-04-12 13:08:36 -0400351func (mod *Module) IsVndkPrebuiltLibrary() bool {
352 // Rust modules do not provide VNDK prebuilts
353 return false
354}
355
356func (mod *Module) IsVendorPublicLibrary() bool {
357 return mod.VendorProperties.IsVendorPublicLibrary
358}
359
360func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
361 // Rust modules to not provide Sdk variants
362 return false
363}
364
Colin Cross127bb8b2020-12-16 16:46:01 -0800365func (c *Module) IsVndkPrivate() bool {
366 return false
367}
368
369func (c *Module) IsLlndk() bool {
370 return false
371}
372
373func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500374 return false
375}
376
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400377func (mod *Module) KernelHeadersDecorator() bool {
378 return false
379}
380
Colin Cross1f3f1302021-04-26 18:37:44 -0700381func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400382 return false
383}
384
Colin Cross5271fea2021-04-27 13:06:04 -0700385func (m *Module) NeedsVendorPublicLibraryVariants() bool {
386 return false
387}
388
Ivan Lozanod7586b62021-04-01 09:49:36 -0400389func (mod *Module) HasLlndkStubs() bool {
390 return false
391}
392
393func (mod *Module) StubsVersion() string {
394 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
395}
396
Ivan Lozano52767be2019-10-18 14:49:46 -0700397func (mod *Module) SdkVersion() string {
398 return ""
399}
400
Colin Crossc511bc52020-04-07 16:50:32 +0000401func (mod *Module) AlwaysSdk() bool {
402 return false
403}
404
Jiyong Park2286afd2020-06-16 21:58:53 +0900405func (mod *Module) IsSdkVariant() bool {
406 return false
407}
408
Colin Cross1348ce32020-10-01 13:37:16 -0700409func (mod *Module) SplitPerApiLevel() bool {
410 return false
411}
412
Sasha Smundaka76acba2022-04-18 20:12:56 -0700413func (mod *Module) XrefRustFiles() android.Paths {
414 return mod.kytheFiles
415}
416
Ivan Lozanoffee3342019-08-27 12:03:00 -0700417type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400418 Dylibs []string
419 Rlibs []string
420 Rustlibs []string
421 Stdlibs []string
422 ProcMacros []string
423 SharedLibs []string
424 StaticLibs []string
425 WholeStaticLibs []string
426 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400428 // Used for data dependencies adjacent to tests
429 DataLibs []string
430 DataBins []string
431
Colin Crossfe605e12022-01-23 20:46:16 -0800432 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433}
434
435type PathDeps struct {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700436 DyLibs RustLibraries
437 RLibs RustLibraries
438 LibDeps android.Paths
439 WholeStaticLibs android.Paths
440 ProcMacros RustLibraries
441 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500442
443 // depFlags and depLinkFlags are rustc and linker (clang) flags.
444 depFlags []string
445 depLinkFlags []string
446
447 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
448 // Both of these are exported and propagate to dependencies.
449 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700450 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700451
Ivan Lozano45901ed2020-07-24 16:05:01 -0400452 // Used by bindgen modules which call clang
453 depClangFlags []string
454 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400455 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400456 depSystemIncludePaths android.Paths
457
Colin Crossfe605e12022-01-23 20:46:16 -0800458 CrtBegin android.Paths
459 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700460
461 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500462 SrcDeps android.Paths
463 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700464}
465
466type RustLibraries []RustLibrary
467
468type RustLibrary struct {
469 Path android.Path
470 CrateName string
471}
472
473type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100474 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400476 cfgFlags(ctx ModuleContext, flags Flags) Flags
477 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700479 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700480 compilerDeps(ctx DepsContext, deps Deps) Deps
481 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700482 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700483
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100484 // Output directory in which source-generated code from dependencies is
485 // copied. This is equivalent to Cargo's OUT_DIR variable.
486 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700487
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400488 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
489 CargoPkgVersion() string
490
491 // CargoEnvCompat returns whether Cargo environment variables should be used.
492 CargoEnvCompat() bool
493
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800494 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200495 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700496 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400497 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400498
499 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400500
501 Disabled() bool
502 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400503
Ivan Lozanodd055472020-09-28 13:22:45 -0400504 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500505 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900506
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400507 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900508 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400509}
510
Matthew Maurerbb3add12020-06-25 09:34:12 -0700511type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700512 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700513 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700514}
515
Sasha Smundaka76acba2022-04-18 20:12:56 -0700516type xref interface {
517 XrefRustFiles() android.Paths
518}
519
Matthew Maurerbb3add12020-06-25 09:34:12 -0700520type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400521 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700522 linkObjects android.Paths
523 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700524}
525
Matthew Maurerbb3add12020-06-25 09:34:12 -0700526func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
527 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
528}
529
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700530func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
531 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
532}
533
534func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
535 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400536}
537
Colin Cross0de8a1e2020-09-18 14:15:30 -0700538func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
539 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700540 LinkDirs: flagExporter.linkDirs,
541 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700542 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700543 })
544}
545
Matthew Maurerbb3add12020-06-25 09:34:12 -0700546var _ exportedFlagsProducer = (*flagExporter)(nil)
547
548func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700549 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700550}
551
Colin Cross0de8a1e2020-09-18 14:15:30 -0700552type FlagExporterInfo struct {
553 Flags []string
554 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700555 LinkObjects android.Paths
556 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700557}
558
559var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
560
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400561func (mod *Module) isCoverageVariant() bool {
562 return mod.coverage.Properties.IsCoverageVariant
563}
564
565var _ cc.Coverage = (*Module)(nil)
566
567func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
568 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
569}
570
Ivan Lozanod7586b62021-04-01 09:49:36 -0400571func (mod *Module) VndkVersion() string {
572 return mod.Properties.VndkVersion
573}
574
575func (mod *Module) PreventInstall() bool {
576 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400577}
578
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400579func (mod *Module) MarkAsCoverageVariant(coverage bool) {
580 mod.coverage.Properties.IsCoverageVariant = coverage
581}
582
583func (mod *Module) EnableCoverageIfNeeded() {
584 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700585}
586
587func defaultsFactory() android.Module {
588 return DefaultsFactory()
589}
590
591type Defaults struct {
592 android.ModuleBase
593 android.DefaultsModuleBase
594}
595
596func DefaultsFactory(props ...interface{}) android.Module {
597 module := &Defaults{}
598
599 module.AddProperties(props...)
600 module.AddProperties(
601 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800602 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500603 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100604 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400605 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606 &BaseCompilerProperties{},
607 &BinaryCompilerProperties{},
608 &LibraryCompilerProperties{},
609 &ProcMacroCompilerProperties{},
610 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400611 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700612 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400613 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400614 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200615 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500616 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000617 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700618 )
619
620 android.InitDefaultsModule(module)
621 return module
622}
623
624func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700625 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700626}
627
Ivan Lozano183a3212019-10-18 14:18:45 -0700628func (mod *Module) CcLibrary() bool {
629 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500630 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700631 return true
632 }
633 }
634 return false
635}
636
637func (mod *Module) CcLibraryInterface() bool {
638 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400639 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
640 // VariantIs{Static,Shared} is set.
641 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700642 return true
643 }
644 }
645 return false
646}
647
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400648func (mod *Module) RustLibraryInterface() bool {
649 if mod.compiler != nil {
650 if _, ok := mod.compiler.(libraryInterface); ok {
651 return true
652 }
653 }
654 return false
655}
656
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500657func (mod *Module) IsFuzzModule() bool {
658 if _, ok := mod.compiler.(*fuzzDecorator); ok {
659 return true
660 }
661 return false
662}
663
664func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
665 return mod.FuzzModule
666}
667
668func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
669 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
670 return fuzzer.fuzzPackagedModule
671 }
672 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
673}
674
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000675func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500676 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
677 return fuzzer.sharedLibraries
678 }
679 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
680}
681
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400682func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400683 if mod.compiler != nil {
684 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400685 }
686 return nil
687}
688
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800689func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700690 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700691 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800692 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700693 }
694 }
695 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
696}
697
698func (mod *Module) SetStatic() {
699 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700700 if library, ok := mod.compiler.(libraryInterface); ok {
701 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700702 return
703 }
704 }
705 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
706}
707
708func (mod *Module) SetShared() {
709 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700710 if library, ok := mod.compiler.(libraryInterface); ok {
711 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700712 return
713 }
714 }
715 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
716}
717
Ivan Lozano183a3212019-10-18 14:18:45 -0700718func (mod *Module) BuildStaticVariant() bool {
719 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700720 if library, ok := mod.compiler.(libraryInterface); ok {
721 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700722 }
723 }
724 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
725}
726
727func (mod *Module) BuildSharedVariant() bool {
728 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700729 if library, ok := mod.compiler.(libraryInterface); ok {
730 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700731 }
732 }
733 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
734}
735
Ivan Lozano183a3212019-10-18 14:18:45 -0700736func (mod *Module) Module() android.Module {
737 return mod
738}
739
Ivan Lozano183a3212019-10-18 14:18:45 -0700740func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400741 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700742}
743
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400744func (mod *Module) CoverageFiles() android.Paths {
745 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800746 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400747 }
748 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
749}
750
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400751// Rust does not produce gcno files, and therefore does not produce a coverage archive.
752func (mod *Module) CoverageOutputFile() android.OptionalPath {
753 return android.OptionalPath{}
754}
755
756func (mod *Module) IsNdk(config android.Config) bool {
757 return false
758}
759
760func (mod *Module) IsStubs() bool {
761 return false
762}
763
Jiyong Park459feca2020-12-15 11:02:21 +0900764func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900765 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900766 return false
767 }
768
Jiyong Park459feca2020-12-15 11:02:21 +0900769 // The apex variant is not installable because it is included in the APEX and won't appear
770 // in the system partition as a standalone file.
771 if !apexInfo.IsForPlatform() {
772 return false
773 }
774
Jiyong Parke54f07e2021-04-07 15:08:04 +0900775 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900776}
777
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500778func (ctx moduleContext) apexVariationName() string {
779 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
780}
781
Ivan Lozano183a3212019-10-18 14:18:45 -0700782var _ cc.LinkableInterface = (*Module)(nil)
783
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784func (mod *Module) Init() android.Module {
785 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500786 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787
Yi Kong46c6e592022-01-20 22:55:00 +0800788 if mod.afdo != nil {
789 mod.AddProperties(mod.afdo.props()...)
790 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700791 if mod.compiler != nil {
792 mod.AddProperties(mod.compiler.compilerProps()...)
793 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400794 if mod.coverage != nil {
795 mod.AddProperties(mod.coverage.props()...)
796 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200797 if mod.clippy != nil {
798 mod.AddProperties(mod.clippy.props()...)
799 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400800 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700801 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400802 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500803 if mod.sanitize != nil {
804 mod.AddProperties(mod.sanitize.props()...)
805 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400806
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900808 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809
810 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 return mod
812}
813
814func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
815 return &Module{
816 hod: hod,
817 multilib: multilib,
818 }
819}
820func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
821 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800822 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400823 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200824 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500825 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 return module
827}
828
829type ModuleContext interface {
830 android.ModuleContext
831 ModuleContextIntf
832}
833
834type BaseModuleContext interface {
835 android.BaseModuleContext
836 ModuleContextIntf
837}
838
839type DepsContext interface {
840 android.BottomUpMutatorContext
841 ModuleContextIntf
842}
843
844type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200845 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700847}
848
849type depsContext struct {
850 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851}
852
853type moduleContext struct {
854 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855}
856
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200857type baseModuleContext struct {
858 android.BaseModuleContext
859}
860
861func (ctx *moduleContext) RustModule() *Module {
862 return ctx.Module().(*Module)
863}
864
865func (ctx *moduleContext) toolchain() config.Toolchain {
866 return ctx.RustModule().toolchain(ctx)
867}
868
869func (ctx *depsContext) RustModule() *Module {
870 return ctx.Module().(*Module)
871}
872
873func (ctx *depsContext) toolchain() config.Toolchain {
874 return ctx.RustModule().toolchain(ctx)
875}
876
877func (ctx *baseModuleContext) RustModule() *Module {
878 return ctx.Module().(*Module)
879}
880
881func (ctx *baseModuleContext) toolchain() config.Toolchain {
882 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400883}
884
885func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700886 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
887 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
888 return false
889 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400890 return mod.compiler != nil && mod.compiler.nativeCoverage()
891}
892
Ivan Lozanod7586b62021-04-01 09:49:36 -0400893func (mod *Module) EverInstallable() bool {
894 return mod.compiler != nil &&
895 // Check to see whether the module is actually ever installable.
896 mod.compiler.everInstallable()
897}
898
899func (mod *Module) Installable() *bool {
900 return mod.Properties.Installable
901}
902
Ivan Lozano872d5792022-03-23 17:31:39 -0400903func (mod *Module) ProcMacro() bool {
904 if pm, ok := mod.compiler.(procMacroInterface); ok {
905 return pm.ProcMacro()
906 }
907 return false
908}
909
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
911 if mod.cachedToolchain == nil {
912 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
913 }
914 return mod.cachedToolchain
915}
916
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200917func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
918 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
919}
920
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
922}
923
924func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
925 ctx := &moduleContext{
926 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700928
Jiyong Park99644e92020-11-17 22:21:02 +0900929 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
930 if !apexInfo.IsForPlatform() {
931 mod.hideApexVariantFromMake = true
932 }
933
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500935 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
936
Ivan Lozanof1868af2022-04-12 13:08:36 -0400937 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700938
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939 if !toolchain.Supported() {
940 // This toolchain's unsupported, there's nothing to do for this mod.
941 return
942 }
943
944 deps := mod.depsToPaths(ctx)
945 flags := Flags{
946 Toolchain: toolchain,
947 }
948
Ivan Lozano67eada32021-09-23 11:50:33 -0400949 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800950 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500951 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800952 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953 if mod.compiler != nil {
954 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400955 flags = mod.compiler.cfgFlags(ctx, flags)
956 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400957 }
958 if mod.coverage != nil {
959 flags, deps = mod.coverage.flags(ctx, flags, deps)
960 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200961 if mod.clippy != nil {
962 flags, deps = mod.clippy.flags(ctx, flags, deps)
963 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500964 if mod.sanitize != nil {
965 flags, deps = mod.sanitize.flags(ctx, flags, deps)
966 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400967
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200968 // SourceProvider needs to call GenerateSource() before compiler calls
969 // compile() so it can provide the source. A SourceProvider has
970 // multiple variants (e.g. source, rlib, dylib). Only the "source"
971 // variant is responsible for effectively generating the source. The
972 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400973 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200974 if mod.compiler.(libraryInterface).source() {
975 mod.sourceProvider.GenerateSource(ctx, deps)
976 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
977 } else {
978 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
979 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700980 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200981 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400982 }
983
984 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100985 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700986 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400987 if ctx.Failed() {
988 return
989 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700990 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
991 if buildOutput.kytheFile != nil {
992 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
993 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400994 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900995
Dan Albert06feee92021-03-19 15:06:02 -0700996 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000997 if mod.docTimestampFile.Valid() {
998 ctx.CheckbuildFile(mod.docTimestampFile.Path())
999 }
Dan Albert06feee92021-03-19 15:06:02 -07001000
Ivan Lozano1921e802021-05-20 13:39:16 -04001001 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1002 // RECOVERY_SNAPSHOT_VERSION is current.
1003 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1004 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1005 lib.collectHeadersForSnapshot(ctx, deps)
1006 }
1007 }
1008
Jiyong Park459feca2020-12-15 11:02:21 +09001009 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001010 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001011 // If the module has been specifically configure to not be installed then
1012 // hide from make as otherwise it will break when running inside make as the
1013 // output path to install will not be specified. Not all uninstallable
1014 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001015 // side dependencies. In particular, proc-macros need to be captured in the
1016 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001017 mod.HideFromMake()
1018 } else if !mod.installable(apexInfo) {
1019 mod.SkipInstall()
1020 }
1021
1022 // Still call install though, the installs will be stored as PackageSpecs to allow
1023 // using the outputs in a genrule.
1024 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001025 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001026 if ctx.Failed() {
1027 return
1028 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001029 }
Chris Wailes74be7642021-07-22 16:20:28 -07001030
1031 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032 }
1033}
1034
1035func (mod *Module) deps(ctx DepsContext) Deps {
1036 deps := Deps{}
1037
1038 if mod.compiler != nil {
1039 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001040 }
1041 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001042 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 }
1044
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001045 if mod.coverage != nil {
1046 deps = mod.coverage.deps(ctx, deps)
1047 }
1048
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001049 if mod.sanitize != nil {
1050 deps = mod.sanitize.deps(ctx, deps)
1051 }
1052
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1054 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001055 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1057 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1058 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001059 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001060 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061 return deps
1062
1063}
1064
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065type dependencyTag struct {
1066 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001067 name string
1068 library bool
1069 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001070 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001071}
1072
Jiyong Park65b62242020-11-25 12:44:59 +09001073// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1074// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1075func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001076 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001077}
1078
1079var _ android.InstallNeededDependencyTag = dependencyTag{}
1080
Colin Cross65cb3142021-12-10 23:05:02 +00001081func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1082 if d.library && d.dynamic {
1083 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1084 }
1085 return nil
1086}
1087
1088var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1089
Ivan Lozanoffee3342019-08-27 12:03:00 -07001090var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001091 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1092 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001093 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001094 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001095 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001096 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001097 dataLibDepTag = dependencyTag{name: "data lib"}
1098 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001099)
1100
Jiyong Park99644e92020-11-17 22:21:02 +09001101func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1102 tag, ok := depTag.(dependencyTag)
1103 return ok && tag == dylibDepTag
1104}
1105
Jiyong Park94e22fd2021-04-08 18:19:15 +09001106func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1107 tag, ok := depTag.(dependencyTag)
1108 return ok && tag == rlibDepTag
1109}
1110
Matthew Maurer0f003b12020-06-29 14:34:06 -07001111type autoDep struct {
1112 variation string
1113 depTag dependencyTag
1114}
1115
1116var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001117 rlibVariation = "rlib"
1118 dylibVariation = "dylib"
1119 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1120 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001121)
1122
1123type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001124 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001125}
1126
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001127func (mod *Module) begin(ctx BaseModuleContext) {
1128 if mod.coverage != nil {
1129 mod.coverage.begin(ctx)
1130 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001131 if mod.sanitize != nil {
1132 mod.sanitize.begin(ctx)
1133 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001134}
1135
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001136func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001137 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001138 return p.prebuilt()
1139 }
1140 return nil
1141}
1142
Justin Yun24b246a2023-03-16 10:36:16 +09001143func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1144 if rustDep, ok := dep.(*Module); ok {
1145 // Use base module name for snapshots when exporting to Makefile.
1146 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1147 baseName := rustDep.BaseModuleName()
1148 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1149 }
1150 }
1151 return cc.MakeLibName(ctx, c, dep, depName)
1152}
1153
Ivan Lozanoffee3342019-08-27 12:03:00 -07001154func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1155 var depPaths PathDeps
1156
1157 directRlibDeps := []*Module{}
1158 directDylibDeps := []*Module{}
1159 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001160 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001161 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001162 directSrcProvidersDeps := []*Module{}
1163 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001164
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001165 // For the dependency from platform to apex, use the latest stubs
1166 mod.apexSdkVersion = android.FutureApiLevel
1167 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1168 if !apexInfo.IsForPlatform() {
1169 mod.apexSdkVersion = apexInfo.MinSdkVersion
1170 }
1171
1172 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1173 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1174 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1175 // (b/144430859)
1176 mod.apexSdkVersion = android.FutureApiLevel
1177 }
1178
Spandan Das604f3762023-03-16 22:51:40 +00001179 skipModuleList := map[string]bool{}
1180
1181 var apiImportInfo multitree.ApiImportInfo
1182 hasApiImportInfo := false
1183
1184 ctx.VisitDirectDeps(func(dep android.Module) {
1185 if dep.Name() == "api_imports" {
1186 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1187 hasApiImportInfo = true
1188 }
1189 })
1190
1191 if hasApiImportInfo {
1192 targetStubModuleList := map[string]string{}
1193 targetOrigModuleList := map[string]string{}
1194
1195 // Search for dependency which both original module and API imported library with APEX stub exists
1196 ctx.VisitDirectDeps(func(dep android.Module) {
1197 depName := ctx.OtherModuleName(dep)
1198 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1199 targetStubModuleList[apiLibrary] = depName
1200 }
1201 })
1202 ctx.VisitDirectDeps(func(dep android.Module) {
1203 depName := ctx.OtherModuleName(dep)
1204 if origLibrary, ok := targetStubModuleList[depName]; ok {
1205 targetOrigModuleList[origLibrary] = depName
1206 }
1207 })
1208
1209 // Decide which library should be used between original and API imported library
1210 ctx.VisitDirectDeps(func(dep android.Module) {
1211 depName := ctx.OtherModuleName(dep)
1212 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1213 if cc.ShouldUseStubForApex(ctx, dep) {
1214 skipModuleList[depName] = true
1215 } else {
1216 skipModuleList[apiLibrary] = true
1217 }
1218 }
1219 })
1220 }
1221
Cole Faustb6e6f992023-08-17 17:42:26 -07001222 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1223 var directAndroidMkSharedLibs []string
1224
Ivan Lozanoffee3342019-08-27 12:03:00 -07001225 ctx.VisitDirectDeps(func(dep android.Module) {
1226 depName := ctx.OtherModuleName(dep)
1227 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001228
Spandan Das604f3762023-03-16 22:51:40 +00001229 if _, exists := skipModuleList[depName]; exists {
1230 return
1231 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001232
1233 if depTag == android.DarwinUniversalVariantTag {
1234 return
1235 }
1236
Ivan Lozano89435d12020-07-31 11:01:18 -04001237 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001238 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001239 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001240
Ivan Lozanoffee3342019-08-27 12:03:00 -07001241 switch depTag {
1242 case dylibDepTag:
1243 dylib, ok := rustDep.compiler.(libraryInterface)
1244 if !ok || !dylib.dylib() {
1245 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1246 return
1247 }
1248 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001249 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001250 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1251
Ivan Lozanoffee3342019-08-27 12:03:00 -07001252 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001253
Ivan Lozanoffee3342019-08-27 12:03:00 -07001254 rlib, ok := rustDep.compiler.(libraryInterface)
1255 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001256 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001257 return
1258 }
1259 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001260 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001261 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1262
Ivan Lozanoffee3342019-08-27 12:03:00 -07001263 case procMacroDepTag:
1264 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001265 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001266 }
1267
Cole Faustb6e6f992023-08-17 17:42:26 -07001268 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1269
Paul Duffind5cf92e2021-07-09 17:38:55 +01001270 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001271 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1272 // OS/Arch variant is used.
1273 var helper string
1274 if ctx.Host() {
1275 helper = "missing 'host_supported'?"
1276 } else {
1277 helper = "device module defined?"
1278 }
1279
1280 if dep.Target().Os != ctx.Os() {
1281 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1282 return
1283 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1284 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1285 return
1286 }
1287 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001288 }
1289
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001290 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001291 if depTag != procMacroDepTag {
1292 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1293 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1294 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1295 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001296 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001297 }
1298
Ivan Lozanoffee3342019-08-27 12:03:00 -07001299 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001300 linkFile := rustDep.UnstrippedOutputFile()
1301 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001302 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1303 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001304 }
1305 }
1306
Ivan Lozano89435d12020-07-31 11:01:18 -04001307 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001308 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001309 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001310 if _, ok := ccDep.(*Module); !ok {
1311 if ccDep.Module().Target().Os != ctx.Os() {
1312 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1313 return
1314 }
1315 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1316 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1317 return
1318 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001319 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001320 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001321 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001322 if !ctx.Config().AllowMissingDependencies() {
1323 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1324 } else {
1325 ctx.AddMissingDependencies([]string{depName})
1326 }
1327 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001328 }
1329
Colin Crossa86ea0e2023-08-01 09:57:22 -07001330 linkPath := linkPathFromFilePath(linkObject.Path())
1331
Ivan Lozanoffee3342019-08-27 12:03:00 -07001332 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001333 switch {
1334 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001335 if cc.IsWholeStaticLib(depTag) {
1336 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1337 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001338 if mod.Binary() {
1339 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1340 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1341 // final linkage, pass the args directly to the linker to handle these cases.
1342 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1343 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001344 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001345 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001346 } else {
1347 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 -05001348 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001349 }
1350
1351 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1352 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001353 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001354 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1355
Colin Cross0de8a1e2020-09-18 14:15:30 -07001356 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1357 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1358 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1359 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1360 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001361 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001362
1363 // Record baseLibName for snapshots.
1364 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1365
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001366 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001367 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001368 // For the shared lib dependencies, we may link to the stub variant
1369 // of the dependency depending on the context (e.g. if this
1370 // dependency crosses the APEX boundaries).
1371 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1372
1373 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1374 // object (either from stub or non-stub) to use.
1375 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001376 if !linkObject.Valid() {
1377 if !ctx.Config().AllowMissingDependencies() {
1378 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1379 } else {
1380 ctx.AddMissingDependencies([]string{depName})
1381 }
1382 return
1383 }
Jiyong Park7d55b612021-06-11 17:22:09 +09001384 linkPath = linkPathFromFilePath(linkObject.Path())
1385
Ivan Lozanoffee3342019-08-27 12:03:00 -07001386 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001387 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001388 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1389 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1390 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1391 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001392 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001393
1394 // Record baseLibName for snapshots.
1395 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1396
Cole Faustb6e6f992023-08-17 17:42:26 -07001397 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001398 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001399 case cc.IsHeaderDepTag(depTag):
1400 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1401 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1402 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1403 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001404 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001405 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001406 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001407 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001408 }
1409
1410 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001411 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1412 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001413 if linkObject.Valid() {
1414 lib.exportLinkObjects(linkObject.Path())
1415 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001416 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001417 } else {
1418 switch {
1419 case depTag == cc.CrtBeginDepTag:
1420 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1421 case depTag == cc.CrtEndDepTag:
1422 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1423 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001424 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001425
1426 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001427 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001428 // These are usually genrules which don't have per-target variants.
1429 directSrcDeps = append(directSrcDeps, srcDep)
1430 }
1431 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001432 })
1433
Cole Faustb6e6f992023-08-17 17:42:26 -07001434 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1435
Ivan Lozanoffee3342019-08-27 12:03:00 -07001436 var rlibDepFiles RustLibraries
1437 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001438 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001439 }
1440 var dylibDepFiles RustLibraries
1441 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001442 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443 }
1444 var procMacroDepFiles RustLibraries
1445 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001446 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 }
1448
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001449 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001450 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001451 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001452 }
1453
Ivan Lozanoffee3342019-08-27 12:03:00 -07001454 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001455 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001456 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001457 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001458 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001459 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001460 }
1461
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001462 var srcProviderDepFiles android.Paths
1463 for _, dep := range directSrcProvidersDeps {
1464 srcs, _ := dep.OutputFiles("")
1465 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1466 }
1467 for _, dep := range directSrcDeps {
1468 srcs := dep.Srcs()
1469 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1470 }
1471
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1473 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001474 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001475 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001476 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001477
1478 // Dedup exported flags from dependencies
1479 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001480 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001481 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001482 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1483 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1484 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001485
1486 return depPaths
1487}
1488
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001489func (mod *Module) InstallInData() bool {
1490 if mod.compiler == nil {
1491 return false
1492 }
1493 return mod.compiler.inData()
1494}
1495
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001496func (mod *Module) InstallInRamdisk() bool {
1497 return mod.InRamdisk()
1498}
1499
1500func (mod *Module) InstallInVendorRamdisk() bool {
1501 return mod.InVendorRamdisk()
1502}
1503
1504func (mod *Module) InstallInRecovery() bool {
1505 return mod.InRecovery()
1506}
1507
Ivan Lozanoffee3342019-08-27 12:03:00 -07001508func linkPathFromFilePath(filepath android.Path) string {
1509 return strings.Split(filepath.String(), filepath.Base())[0]
1510}
Ivan Lozanod648c432020-02-06 12:05:10 -05001511
Spandan Das604f3762023-03-16 22:51:40 +00001512// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1513func (r *Module) usePublicApi() bool {
1514 return r.Device() && r.UseSdk()
1515}
1516
1517// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1518func (r *Module) useVendorApi() bool {
1519 return r.Device() && (r.InVendor() || r.InProduct())
1520}
1521
Ivan Lozanoffee3342019-08-27 12:03:00 -07001522func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1523 ctx := &depsContext{
1524 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001525 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001526
1527 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001528 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001529 var snapshotInfo *cc.SnapshotInfo
1530
Kiyoung Kim487689e2022-07-26 09:48:22 +09001531 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001532 if mod.usePublicApi() || mod.useVendorApi() {
1533 for idx, lib := range deps.SharedLibs {
1534 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1535 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001536 }
1537
Ivan Lozano1921e802021-05-20 13:39:16 -04001538 if ctx.Os() == android.Android {
1539 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1540 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001541
Ivan Lozano2b081132020-09-08 12:46:52 -04001542 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001543 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001544 stdLinkage = "rlib-std"
1545 }
1546
1547 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001548
Ivan Lozano2b081132020-09-08 12:46:52 -04001549 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1550 rlibDepVariations = append(rlibDepVariations,
1551 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1552 }
1553
Ivan Lozano1921e802021-05-20 13:39:16 -04001554 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001555 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001556 for _, lib := range deps.Rlibs {
1557 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001558 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001559
Ivan Lozano2d407632022-04-07 12:59:11 -04001560 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001561 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001562
1563 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001564 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1565 for _, lib := range deps.Dylibs {
1566 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1567 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001568
Ivan Lozano1921e802021-05-20 13:39:16 -04001569 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001570 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1571 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001572 for _, lib := range deps.Rustlibs {
1573 if autoDep.depTag == rlibDepTag {
1574 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001575 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001576 } else {
1577 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1578 // Check for the existence of the dylib deptag variant. Select it if available,
1579 // otherwise select the rlib variant.
1580 autoDepVariations := append(commonDepVariations,
1581 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001582
1583 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1584
1585 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1586 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001587 } else {
1588 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001589 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001590 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001591 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001592 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001593 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001594 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001595 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001596 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001597 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001598 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001599 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001600 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001601 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001602 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001603 for _, lib := range deps.Stdlibs {
1604 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1605 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001606 }
1607 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001608
1609 for _, lib := range deps.SharedLibs {
1610 depTag := cc.SharedDepTag()
1611 name, version := cc.StubsLibNameAndVersion(lib)
1612
1613 variations := []blueprint.Variation{
1614 {Mutator: "link", Variation: "shared"},
1615 }
Spandan Das604f3762023-03-16 22:51:40 +00001616 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1617 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1618 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1619 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1620 }
1621
1622 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1623 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1624 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001625 }
1626
1627 for _, lib := range deps.WholeStaticLibs {
1628 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001629 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001630
1631 actx.AddVariationDependencies([]blueprint.Variation{
1632 {Mutator: "link", Variation: "static"},
1633 }, depTag, lib)
1634 }
1635
1636 for _, lib := range deps.StaticLibs {
1637 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001638 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001639
1640 actx.AddVariationDependencies([]blueprint.Variation{
1641 {Mutator: "link", Variation: "static"},
1642 }, depTag, lib)
1643 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001644
Zach Johnson3df4e632020-11-06 11:56:27 -08001645 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1646
Colin Cross565cafd2020-09-25 18:47:38 -07001647 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001648 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001649 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001650 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001651 }
Colin Crossfe605e12022-01-23 20:46:16 -08001652 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001653 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001654 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001655 }
1656
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001657 if mod.sourceProvider != nil {
1658 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1659 bindgen.Properties.Custom_bindgen != "" {
1660 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1661 bindgen.Properties.Custom_bindgen)
1662 }
1663 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001664
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001665 actx.AddVariationDependencies([]blueprint.Variation{
1666 {Mutator: "link", Variation: "shared"},
1667 }, dataLibDepTag, deps.DataLibs...)
1668
1669 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1670
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001671 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001672 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001673
1674 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001675}
1676
Ivan Lozano2d407632022-04-07 12:59:11 -04001677// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001678func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1679 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001680 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1681}
1682
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001683func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1684 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1685 actx.AddVariationDependencies(variations, depTag, lib)
1686}
1687
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001688func BeginMutator(ctx android.BottomUpMutatorContext) {
1689 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1690 mod.beginMutator(ctx)
1691 }
1692}
1693
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001694func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1695 ctx := &baseModuleContext{
1696 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001697 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001698
1699 mod.begin(ctx)
1700}
1701
Ivan Lozanoffee3342019-08-27 12:03:00 -07001702func (mod *Module) Name() string {
1703 name := mod.ModuleBase.Name()
1704 if p, ok := mod.compiler.(interface {
1705 Name(string) string
1706 }); ok {
1707 name = p.Name(name)
1708 }
1709 return name
1710}
1711
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001712func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001713 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001714 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001715 }
1716}
1717
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001718var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001719var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001720
1721func (mod *Module) HostToolPath() android.OptionalPath {
1722 if !mod.Host() {
1723 return android.OptionalPath{}
1724 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001725 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1726 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001727 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1728 // Even though proc-macros aren't strictly "tools", since they target the compiler
1729 // and act as compiler plugins, we treat them similarly.
1730 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001731 }
1732 return android.OptionalPath{}
1733}
1734
Jiyong Park99644e92020-11-17 22:21:02 +09001735var _ android.ApexModule = (*Module)(nil)
1736
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001737func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001738 return String(mod.Properties.Min_sdk_version)
1739}
1740
Jiyong Park45bf82e2020-12-15 22:29:02 +09001741// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001742func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001743 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001744 if minSdkVersion == "apex_inherit" {
1745 return nil
1746 }
1747 if minSdkVersion == "" {
1748 return fmt.Errorf("min_sdk_version is not specificed")
1749 }
1750
1751 // Not using nativeApiLevelFromUser because the context here is not
1752 // necessarily a native context.
1753 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1754 if err != nil {
1755 return err
1756 }
1757
1758 if ver.GreaterThan(sdkVersion) {
1759 return fmt.Errorf("newer SDK(%v)", ver)
1760 }
Jiyong Park99644e92020-11-17 22:21:02 +09001761 return nil
1762}
1763
Jiyong Park45bf82e2020-12-15 22:29:02 +09001764// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001765func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1766 depTag := ctx.OtherModuleDependencyTag(dep)
1767
1768 if ccm, ok := dep.(*cc.Module); ok {
1769 if ccm.HasStubsVariants() {
1770 if cc.IsSharedDepTag(depTag) {
1771 // dynamic dep to a stubs lib crosses APEX boundary
1772 return false
1773 }
1774 if cc.IsRuntimeDepTag(depTag) {
1775 // runtime dep to a stubs lib also crosses APEX boundary
1776 return false
1777 }
1778
1779 if cc.IsHeaderDepTag(depTag) {
1780 return false
1781 }
1782 }
1783 if mod.Static() && cc.IsSharedDepTag(depTag) {
1784 // shared_lib dependency from a static lib is considered as crossing
1785 // the APEX boundary because the dependency doesn't actually is
1786 // linked; the dependency is used only during the compilation phase.
1787 return false
1788 }
1789 }
1790
Matthew Maurer581b6d82022-09-29 16:46:25 -07001791 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001792 return false
1793 }
1794
1795 return true
1796}
1797
1798// Overrides ApexModule.IsInstallabeToApex()
1799func (mod *Module) IsInstallableToApex() bool {
1800 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001801 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001802 return true
1803 }
1804 if _, ok := mod.compiler.(*binaryDecorator); ok {
1805 return true
1806 }
1807 }
1808 return false
1809}
1810
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001811// If a library file has a "lib" prefix, extract the library name without the prefix.
1812func libNameFromFilePath(filepath android.Path) (string, bool) {
1813 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1814 if strings.HasPrefix(libName, "lib") {
1815 libName = libName[3:]
1816 return libName, true
1817 }
1818 return "", false
1819}
1820
Sasha Smundaka76acba2022-04-18 20:12:56 -07001821func kytheExtractRustFactory() android.Singleton {
1822 return &kytheExtractRustSingleton{}
1823}
1824
1825type kytheExtractRustSingleton struct {
1826}
1827
1828func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1829 var xrefTargets android.Paths
1830 ctx.VisitAllModules(func(module android.Module) {
1831 if rustModule, ok := module.(xref); ok {
1832 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1833 }
1834 })
1835 if len(xrefTargets) > 0 {
1836 ctx.Phony("xref_rust", xrefTargets...)
1837 }
1838}
1839
Jihoon Kangf78a8902022-09-01 22:47:07 +00001840func (c *Module) Partition() string {
1841 return ""
1842}
1843
Ivan Lozanoffee3342019-08-27 12:03:00 -07001844var Bool = proptools.Bool
1845var BoolDefault = proptools.BoolDefault
1846var String = proptools.String
1847var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001848
1849var _ android.OutputFileProducer = (*Module)(nil)