blob: edd04ac724096bf164434aaed29bf36fb6679581 [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"
Vinh Tranbcb5f572023-08-24 11:10:01 -040019 "android/soong/ui/metrics/bp2build_metrics_proto"
Ivan Lozano183a3212019-10-18 14:18:45 -070020 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070021 "strings"
22
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
27 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020028 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070029 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000030 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070031 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040032 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070033)
34
35var pctx = android.NewPackageContext("android/soong/rust")
36
37func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070038 android.RegisterModuleType("rust_defaults", defaultsFactory)
39 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
40 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040041 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040042 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050043 })
44 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070046 })
Inseob Kim3b244062023-07-11 13:31:36 +090047 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020049 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000050 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070051}
52
53type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040054 GlobalRustFlags []string // Flags that apply globally to rust
55 GlobalLinkFlags []string // Flags that apply globally to linker
56 RustFlags []string // Flags that apply to rust
57 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070059 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070060 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070063 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070064}
65
66type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050067 AndroidMkRlibs []string `blueprint:"mutated"`
68 AndroidMkDylibs []string `blueprint:"mutated"`
69 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050070 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040071
Ivan Lozano6a884432020-12-02 09:15:16 -050072 ImageVariationPrefix string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
75
Ivan Lozanoc08897c2021-04-02 12:41:32 -040076 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
77 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
78 // appended before SubName.
79 RustSubName string `blueprint:"mutated"`
80
Ivan Lozano6a884432020-12-02 09:15:16 -050081 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050082 CoreVariantNeeded bool `blueprint:"mutated"`
83 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070084 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080085 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050086 ExtraVariants []string `blueprint:"mutated"`
87
Ivan Lozanoa2268632021-07-22 10:52:06 -040088 // Allows this module to use non-APEX version of libraries. Useful
89 // for building binaries that are started before APEXes are activated.
90 Bootstrap *bool
91
Ivan Lozano1921e802021-05-20 13:39:16 -040092 // Used by vendor snapshot to record dependencies from snapshot modules.
93 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090094 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040095 SnapshotRlibs []string `blueprint:"mutated"`
96 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040097
Matthew Maurerc6868382021-07-13 14:12:37 -070098 // Make this module available when building for ramdisk.
99 // On device without a dedicated recovery partition, the module is only
100 // available after switching root into
101 // /first_stage_ramdisk. To expose the module before switching root, install
102 // the recovery variant instead.
103 Ramdisk_available *bool
104
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500105 // Make this module available when building for vendor ramdisk.
106 // On device without a dedicated recovery partition, the module is only
107 // available after switching root into
108 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800109 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500110 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400111
Ivan Lozano1921e802021-05-20 13:39:16 -0400112 // Normally Soong uses the directory structure to decide which modules
113 // should be included (framework) or excluded (non-framework) from the
114 // different snapshots (vendor, recovery, etc.), but this property
115 // allows a partner to exclude a module normally thought of as a
116 // framework module from the vendor snapshot.
117 Exclude_from_vendor_snapshot *bool
118
119 // Normally Soong uses the directory structure to decide which modules
120 // should be included (framework) or excluded (non-framework) from the
121 // different snapshots (vendor, recovery, etc.), but this property
122 // allows a partner to exclude a module normally thought of as a
123 // framework module from the recovery snapshot.
124 Exclude_from_recovery_snapshot *bool
125
Matthew Maurer460ee942021-02-11 12:31:46 -0800126 // Make this module available when building for recovery
127 Recovery_available *bool
128
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500129 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
130 Min_sdk_version *string
131
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900132 HideFromMake bool `blueprint:"mutated"`
133 PreventInstall bool `blueprint:"mutated"`
134
135 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136}
137
138type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700139 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140
Ivan Lozano6a884432020-12-02 09:15:16 -0500141 VendorProperties cc.VendorProperties
142
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143 Properties BaseProperties
144
145 hod android.HostOrDeviceSupported
146 multilib android.Multilib
147
Ivan Lozano6a884432020-12-02 09:15:16 -0500148 makeLinkType string
149
Yi Kong46c6e592022-01-20 22:55:00 +0800150 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400152 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200153 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500154 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700157 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400159 // Output file to be installed, may be stripped or unstripped.
160 outputFile android.OptionalPath
161
Sasha Smundaka76acba2022-04-18 20:12:56 -0700162 // Cross-reference input file
163 kytheFiles android.Paths
164
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400165 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900166
167 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500168
169 // For apex variants, this is set as apex.min_sdk_version
170 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700171
172 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400173
174 android.BazelModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700175}
176
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500177func (mod *Module) Header() bool {
178 //TODO: If Rust libraries provide header variants, this needs to be updated.
179 return false
180}
181
182func (mod *Module) SetPreventInstall() {
183 mod.Properties.PreventInstall = true
184}
185
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500186func (mod *Module) SetHideFromMake() {
187 mod.Properties.HideFromMake = true
188}
189
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900190func (mod *Module) HiddenFromMake() bool {
191 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400192}
193
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500195 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
196 // nil since we need compiler to actually sanitize.
197 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500198}
199
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500200func (mod *Module) IsPrebuilt() bool {
201 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
202 return true
203 }
204 return false
205}
206
Ivan Lozano43845682020-07-09 21:03:28 -0400207func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
208 switch tag {
209 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700210 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400211 return mod.sourceProvider.Srcs(), nil
212 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900213 if mod.OutputFile().Valid() {
214 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400215 }
216 return android.Paths{}, nil
217 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500218 case "unstripped":
219 if mod.compiler != nil {
220 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
221 }
222 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400223 default:
224 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
225 }
226}
227
Ivan Lozano52767be2019-10-18 14:49:46 -0700228func (mod *Module) SelectedStl() string {
229 return ""
230}
231
Ivan Lozano2b262972019-11-21 12:30:50 -0800232func (mod *Module) NonCcVariants() bool {
233 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400234 if _, ok := mod.compiler.(libraryInterface); ok {
235 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800236 }
237 }
238 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
239}
240
Ivan Lozano52767be2019-10-18 14:49:46 -0700241func (mod *Module) Static() bool {
242 if mod.compiler != nil {
243 if library, ok := mod.compiler.(libraryInterface); ok {
244 return library.static()
245 }
246 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400247 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700248}
249
250func (mod *Module) Shared() bool {
251 if mod.compiler != nil {
252 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400253 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700254 }
255 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400256 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700257}
258
Ivan Lozanod7586b62021-04-01 09:49:36 -0400259func (mod *Module) Dylib() bool {
260 if mod.compiler != nil {
261 if library, ok := mod.compiler.(libraryInterface); ok {
262 return library.dylib()
263 }
264 }
265 return false
266}
267
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400268func (mod *Module) RlibStd() bool {
269 if mod.compiler != nil {
270 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
271 return library.rlibStd()
272 }
273 }
274 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
275}
276
Ivan Lozanod7586b62021-04-01 09:49:36 -0400277func (mod *Module) Rlib() bool {
278 if mod.compiler != nil {
279 if library, ok := mod.compiler.(libraryInterface); ok {
280 return library.rlib()
281 }
282 }
283 return false
284}
285
286func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400287 if binary, ok := mod.compiler.(binaryInterface); ok {
288 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400289 }
290 return false
291}
292
Justin Yun5e035862021-06-29 20:50:37 +0900293func (mod *Module) StaticExecutable() bool {
294 if !mod.Binary() {
295 return false
296 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400297 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900298}
299
Ivan Lozanod7586b62021-04-01 09:49:36 -0400300func (mod *Module) Object() bool {
301 // Rust has no modules which produce only object files.
302 return false
303}
304
Ivan Lozano52767be2019-10-18 14:49:46 -0700305func (mod *Module) Toc() android.OptionalPath {
306 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400307 if lib, ok := mod.compiler.(libraryInterface); ok {
308 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700309 }
310 }
311 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
312}
313
Colin Crossc511bc52020-04-07 16:50:32 +0000314func (mod *Module) UseSdk() bool {
315 return false
316}
317
Ivan Lozanod7586b62021-04-01 09:49:36 -0400318func (mod *Module) RelativeInstallPath() string {
319 if mod.compiler != nil {
320 return mod.compiler.relativeInstallPath()
321 }
322 return ""
323}
324
Ivan Lozano52767be2019-10-18 14:49:46 -0700325func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500326 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700327}
328
Jiyong Park7d55b612021-06-11 17:22:09 +0900329func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400330 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900331}
332
Ivan Lozano52767be2019-10-18 14:49:46 -0700333func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400334 return true
335}
336
337func (mod *Module) SubName() string {
338 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700339}
340
341func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500342 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700343 return false
344}
345
Ivan Lozanof9e21722020-12-02 09:00:51 -0500346func (mod *Module) IsVndkExt() bool {
347 return false
348}
349
Ivan Lozanod7586b62021-04-01 09:49:36 -0400350func (mod *Module) IsVndkSp() bool {
351 return false
352}
353
Ivan Lozanof1868af2022-04-12 13:08:36 -0400354func (mod *Module) IsVndkPrebuiltLibrary() bool {
355 // Rust modules do not provide VNDK prebuilts
356 return false
357}
358
359func (mod *Module) IsVendorPublicLibrary() bool {
360 return mod.VendorProperties.IsVendorPublicLibrary
361}
362
363func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
364 // Rust modules to not provide Sdk variants
365 return false
366}
367
Colin Cross127bb8b2020-12-16 16:46:01 -0800368func (c *Module) IsVndkPrivate() bool {
369 return false
370}
371
372func (c *Module) IsLlndk() bool {
373 return false
374}
375
376func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500377 return false
378}
379
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400380func (mod *Module) KernelHeadersDecorator() bool {
381 return false
382}
383
Colin Cross1f3f1302021-04-26 18:37:44 -0700384func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400385 return false
386}
387
Colin Cross5271fea2021-04-27 13:06:04 -0700388func (m *Module) NeedsVendorPublicLibraryVariants() bool {
389 return false
390}
391
Ivan Lozanod7586b62021-04-01 09:49:36 -0400392func (mod *Module) HasLlndkStubs() bool {
393 return false
394}
395
396func (mod *Module) StubsVersion() string {
397 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
398}
399
Ivan Lozano52767be2019-10-18 14:49:46 -0700400func (mod *Module) SdkVersion() string {
401 return ""
402}
403
Colin Crossc511bc52020-04-07 16:50:32 +0000404func (mod *Module) AlwaysSdk() bool {
405 return false
406}
407
Jiyong Park2286afd2020-06-16 21:58:53 +0900408func (mod *Module) IsSdkVariant() bool {
409 return false
410}
411
Colin Cross1348ce32020-10-01 13:37:16 -0700412func (mod *Module) SplitPerApiLevel() bool {
413 return false
414}
415
Sasha Smundaka76acba2022-04-18 20:12:56 -0700416func (mod *Module) XrefRustFiles() android.Paths {
417 return mod.kytheFiles
418}
419
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400421 Dylibs []string
422 Rlibs []string
423 Rustlibs []string
424 Stdlibs []string
425 ProcMacros []string
426 SharedLibs []string
427 StaticLibs []string
428 WholeStaticLibs []string
429 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400431 // Used for data dependencies adjacent to tests
432 DataLibs []string
433 DataBins []string
434
Colin Crossfe605e12022-01-23 20:46:16 -0800435 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700436}
437
438type PathDeps struct {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700439 DyLibs RustLibraries
440 RLibs RustLibraries
441 LibDeps android.Paths
442 WholeStaticLibs android.Paths
443 ProcMacros RustLibraries
444 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500445
446 // depFlags and depLinkFlags are rustc and linker (clang) flags.
447 depFlags []string
448 depLinkFlags []string
449
450 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
451 // Both of these are exported and propagate to dependencies.
452 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700453 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700454
Ivan Lozano45901ed2020-07-24 16:05:01 -0400455 // Used by bindgen modules which call clang
456 depClangFlags []string
457 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400458 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400459 depSystemIncludePaths android.Paths
460
Colin Crossfe605e12022-01-23 20:46:16 -0800461 CrtBegin android.Paths
462 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700463
464 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500465 SrcDeps android.Paths
466 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467}
468
469type RustLibraries []RustLibrary
470
471type RustLibrary struct {
472 Path android.Path
473 CrateName string
474}
475
476type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100477 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400479 cfgFlags(ctx ModuleContext, flags Flags) Flags
480 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700482 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700483 compilerDeps(ctx DepsContext, deps Deps) Deps
484 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700485 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700486
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100487 // Output directory in which source-generated code from dependencies is
488 // copied. This is equivalent to Cargo's OUT_DIR variable.
489 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700490
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400491 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
492 CargoPkgVersion() string
493
494 // CargoEnvCompat returns whether Cargo environment variables should be used.
495 CargoEnvCompat() bool
496
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800497 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200498 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700499 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400500 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400501
502 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400503
504 Disabled() bool
505 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400506
Ivan Lozanodd055472020-09-28 13:22:45 -0400507 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500508 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900509
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400510 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900511 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512}
513
Matthew Maurerbb3add12020-06-25 09:34:12 -0700514type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700515 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700516 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700517}
518
Sasha Smundaka76acba2022-04-18 20:12:56 -0700519type xref interface {
520 XrefRustFiles() android.Paths
521}
522
Matthew Maurerbb3add12020-06-25 09:34:12 -0700523type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400524 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700525 linkObjects android.Paths
526 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700527}
528
Matthew Maurerbb3add12020-06-25 09:34:12 -0700529func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
530 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
531}
532
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700533func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
534 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
535}
536
537func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
538 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400539}
540
Colin Cross0de8a1e2020-09-18 14:15:30 -0700541func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
542 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700543 LinkDirs: flagExporter.linkDirs,
544 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700545 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700546 })
547}
548
Matthew Maurerbb3add12020-06-25 09:34:12 -0700549var _ exportedFlagsProducer = (*flagExporter)(nil)
550
551func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700552 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700553}
554
Colin Cross0de8a1e2020-09-18 14:15:30 -0700555type FlagExporterInfo struct {
556 Flags []string
557 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700558 LinkObjects android.Paths
559 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700560}
561
562var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
563
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400564func (mod *Module) isCoverageVariant() bool {
565 return mod.coverage.Properties.IsCoverageVariant
566}
567
568var _ cc.Coverage = (*Module)(nil)
569
570func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
571 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
572}
573
Ivan Lozanod7586b62021-04-01 09:49:36 -0400574func (mod *Module) VndkVersion() string {
575 return mod.Properties.VndkVersion
576}
577
578func (mod *Module) PreventInstall() bool {
579 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400580}
581
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400582func (mod *Module) MarkAsCoverageVariant(coverage bool) {
583 mod.coverage.Properties.IsCoverageVariant = coverage
584}
585
586func (mod *Module) EnableCoverageIfNeeded() {
587 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588}
589
590func defaultsFactory() android.Module {
591 return DefaultsFactory()
592}
593
594type Defaults struct {
595 android.ModuleBase
596 android.DefaultsModuleBase
597}
598
599func DefaultsFactory(props ...interface{}) android.Module {
600 module := &Defaults{}
601
602 module.AddProperties(props...)
603 module.AddProperties(
604 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800605 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500606 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100607 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400608 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700609 &BaseCompilerProperties{},
610 &BinaryCompilerProperties{},
611 &LibraryCompilerProperties{},
612 &ProcMacroCompilerProperties{},
613 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400614 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700615 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400616 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400617 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200618 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500619 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000620 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700621 )
622
623 android.InitDefaultsModule(module)
624 return module
625}
626
627func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700628 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700629}
630
Ivan Lozano183a3212019-10-18 14:18:45 -0700631func (mod *Module) CcLibrary() bool {
632 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500633 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700634 return true
635 }
636 }
637 return false
638}
639
640func (mod *Module) CcLibraryInterface() bool {
641 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400642 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
643 // VariantIs{Static,Shared} is set.
644 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700645 return true
646 }
647 }
648 return false
649}
650
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400651func (mod *Module) RustLibraryInterface() bool {
652 if mod.compiler != nil {
653 if _, ok := mod.compiler.(libraryInterface); ok {
654 return true
655 }
656 }
657 return false
658}
659
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500660func (mod *Module) IsFuzzModule() bool {
661 if _, ok := mod.compiler.(*fuzzDecorator); ok {
662 return true
663 }
664 return false
665}
666
667func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
668 return mod.FuzzModule
669}
670
671func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
672 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
673 return fuzzer.fuzzPackagedModule
674 }
675 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
676}
677
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000678func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500679 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
680 return fuzzer.sharedLibraries
681 }
682 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
683}
684
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400685func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400686 if mod.compiler != nil {
687 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400688 }
689 return nil
690}
691
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800692func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700693 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700694 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800695 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700696 }
697 }
698 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
699}
700
701func (mod *Module) SetStatic() {
702 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700703 if library, ok := mod.compiler.(libraryInterface); ok {
704 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700705 return
706 }
707 }
708 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
709}
710
711func (mod *Module) SetShared() {
712 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700713 if library, ok := mod.compiler.(libraryInterface); ok {
714 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700715 return
716 }
717 }
718 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
719}
720
Ivan Lozano183a3212019-10-18 14:18:45 -0700721func (mod *Module) BuildStaticVariant() bool {
722 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700723 if library, ok := mod.compiler.(libraryInterface); ok {
724 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700725 }
726 }
727 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
728}
729
730func (mod *Module) BuildSharedVariant() bool {
731 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700732 if library, ok := mod.compiler.(libraryInterface); ok {
733 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700734 }
735 }
736 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
737}
738
Ivan Lozano183a3212019-10-18 14:18:45 -0700739func (mod *Module) Module() android.Module {
740 return mod
741}
742
Ivan Lozano183a3212019-10-18 14:18:45 -0700743func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400744 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700745}
746
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400747func (mod *Module) CoverageFiles() android.Paths {
748 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800749 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400750 }
751 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
752}
753
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400754// Rust does not produce gcno files, and therefore does not produce a coverage archive.
755func (mod *Module) CoverageOutputFile() android.OptionalPath {
756 return android.OptionalPath{}
757}
758
759func (mod *Module) IsNdk(config android.Config) bool {
760 return false
761}
762
763func (mod *Module) IsStubs() bool {
764 return false
765}
766
Jiyong Park459feca2020-12-15 11:02:21 +0900767func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900768 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900769 return false
770 }
771
Jiyong Park459feca2020-12-15 11:02:21 +0900772 // The apex variant is not installable because it is included in the APEX and won't appear
773 // in the system partition as a standalone file.
774 if !apexInfo.IsForPlatform() {
775 return false
776 }
777
Jiyong Parke54f07e2021-04-07 15:08:04 +0900778 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900779}
780
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500781func (ctx moduleContext) apexVariationName() string {
782 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
783}
784
Ivan Lozano183a3212019-10-18 14:18:45 -0700785var _ cc.LinkableInterface = (*Module)(nil)
786
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787func (mod *Module) Init() android.Module {
788 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500789 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790
Yi Kong46c6e592022-01-20 22:55:00 +0800791 if mod.afdo != nil {
792 mod.AddProperties(mod.afdo.props()...)
793 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 if mod.compiler != nil {
795 mod.AddProperties(mod.compiler.compilerProps()...)
796 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400797 if mod.coverage != nil {
798 mod.AddProperties(mod.coverage.props()...)
799 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200800 if mod.clippy != nil {
801 mod.AddProperties(mod.clippy.props()...)
802 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400803 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700804 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400805 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500806 if mod.sanitize != nil {
807 mod.AddProperties(mod.sanitize.props()...)
808 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400809
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900811 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812
813 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700814 return mod
815}
816
817func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
818 return &Module{
819 hod: hod,
820 multilib: multilib,
821 }
822}
823func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
824 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800825 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400826 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200827 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500828 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 return module
830}
831
832type ModuleContext interface {
833 android.ModuleContext
834 ModuleContextIntf
835}
836
837type BaseModuleContext interface {
838 android.BaseModuleContext
839 ModuleContextIntf
840}
841
842type DepsContext interface {
843 android.BottomUpMutatorContext
844 ModuleContextIntf
845}
846
847type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200848 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850}
851
852type depsContext struct {
853 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854}
855
856type moduleContext struct {
857 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858}
859
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200860type baseModuleContext struct {
861 android.BaseModuleContext
862}
863
864func (ctx *moduleContext) RustModule() *Module {
865 return ctx.Module().(*Module)
866}
867
868func (ctx *moduleContext) toolchain() config.Toolchain {
869 return ctx.RustModule().toolchain(ctx)
870}
871
872func (ctx *depsContext) RustModule() *Module {
873 return ctx.Module().(*Module)
874}
875
876func (ctx *depsContext) toolchain() config.Toolchain {
877 return ctx.RustModule().toolchain(ctx)
878}
879
880func (ctx *baseModuleContext) RustModule() *Module {
881 return ctx.Module().(*Module)
882}
883
884func (ctx *baseModuleContext) toolchain() config.Toolchain {
885 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400886}
887
888func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700889 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
890 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
891 return false
892 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400893 return mod.compiler != nil && mod.compiler.nativeCoverage()
894}
895
Ivan Lozanod7586b62021-04-01 09:49:36 -0400896func (mod *Module) EverInstallable() bool {
897 return mod.compiler != nil &&
898 // Check to see whether the module is actually ever installable.
899 mod.compiler.everInstallable()
900}
901
902func (mod *Module) Installable() *bool {
903 return mod.Properties.Installable
904}
905
Ivan Lozano872d5792022-03-23 17:31:39 -0400906func (mod *Module) ProcMacro() bool {
907 if pm, ok := mod.compiler.(procMacroInterface); ok {
908 return pm.ProcMacro()
909 }
910 return false
911}
912
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
914 if mod.cachedToolchain == nil {
915 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
916 }
917 return mod.cachedToolchain
918}
919
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200920func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
921 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
922}
923
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
925}
926
927func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
928 ctx := &moduleContext{
929 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931
Jiyong Park99644e92020-11-17 22:21:02 +0900932 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
933 if !apexInfo.IsForPlatform() {
934 mod.hideApexVariantFromMake = true
935 }
936
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500938 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
939
Ivan Lozanof1868af2022-04-12 13:08:36 -0400940 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700941
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942 if !toolchain.Supported() {
943 // This toolchain's unsupported, there's nothing to do for this mod.
944 return
945 }
946
947 deps := mod.depsToPaths(ctx)
948 flags := Flags{
949 Toolchain: toolchain,
950 }
951
Ivan Lozano67eada32021-09-23 11:50:33 -0400952 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800953 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500954 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800955 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956 if mod.compiler != nil {
957 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400958 flags = mod.compiler.cfgFlags(ctx, flags)
959 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400960 }
961 if mod.coverage != nil {
962 flags, deps = mod.coverage.flags(ctx, flags, deps)
963 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200964 if mod.clippy != nil {
965 flags, deps = mod.clippy.flags(ctx, flags, deps)
966 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500967 if mod.sanitize != nil {
968 flags, deps = mod.sanitize.flags(ctx, flags, deps)
969 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400970
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200971 // SourceProvider needs to call GenerateSource() before compiler calls
972 // compile() so it can provide the source. A SourceProvider has
973 // multiple variants (e.g. source, rlib, dylib). Only the "source"
974 // variant is responsible for effectively generating the source. The
975 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400976 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200977 if mod.compiler.(libraryInterface).source() {
978 mod.sourceProvider.GenerateSource(ctx, deps)
979 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
980 } else {
981 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
982 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700983 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200984 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400985 }
986
987 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100988 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700989 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400990 if ctx.Failed() {
991 return
992 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700993 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
994 if buildOutput.kytheFile != nil {
995 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
996 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400997 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900998
Dan Albert06feee92021-03-19 15:06:02 -0700999 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001000 if mod.docTimestampFile.Valid() {
1001 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1002 }
Dan Albert06feee92021-03-19 15:06:02 -07001003
Ivan Lozano1921e802021-05-20 13:39:16 -04001004 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1005 // RECOVERY_SNAPSHOT_VERSION is current.
1006 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1007 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1008 lib.collectHeadersForSnapshot(ctx, deps)
1009 }
1010 }
1011
Jiyong Park459feca2020-12-15 11:02:21 +09001012 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001013 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001014 // If the module has been specifically configure to not be installed then
1015 // hide from make as otherwise it will break when running inside make as the
1016 // output path to install will not be specified. Not all uninstallable
1017 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001018 // side dependencies. In particular, proc-macros need to be captured in the
1019 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001020 mod.HideFromMake()
1021 } else if !mod.installable(apexInfo) {
1022 mod.SkipInstall()
1023 }
1024
1025 // Still call install though, the installs will be stored as PackageSpecs to allow
1026 // using the outputs in a genrule.
1027 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001028 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001029 if ctx.Failed() {
1030 return
1031 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001032 }
Chris Wailes74be7642021-07-22 16:20:28 -07001033
1034 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035 }
1036}
1037
1038func (mod *Module) deps(ctx DepsContext) Deps {
1039 deps := Deps{}
1040
1041 if mod.compiler != nil {
1042 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001043 }
1044 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001045 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001046 }
1047
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001048 if mod.coverage != nil {
1049 deps = mod.coverage.deps(ctx, deps)
1050 }
1051
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001052 if mod.sanitize != nil {
1053 deps = mod.sanitize.deps(ctx, deps)
1054 }
1055
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1057 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001058 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001059 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1060 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1061 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001062 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001063 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001064 return deps
1065
1066}
1067
Ivan Lozanoffee3342019-08-27 12:03:00 -07001068type dependencyTag struct {
1069 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001070 name string
1071 library bool
1072 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001073 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074}
1075
Jiyong Park65b62242020-11-25 12:44:59 +09001076// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1077// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1078func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001079 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001080}
1081
1082var _ android.InstallNeededDependencyTag = dependencyTag{}
1083
Colin Cross65cb3142021-12-10 23:05:02 +00001084func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1085 if d.library && d.dynamic {
1086 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1087 }
1088 return nil
1089}
1090
1091var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1092
Ivan Lozanoffee3342019-08-27 12:03:00 -07001093var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001094 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1095 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001096 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001097 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001098 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001099 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001100 dataLibDepTag = dependencyTag{name: "data lib"}
1101 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001102)
1103
Jiyong Park99644e92020-11-17 22:21:02 +09001104func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1105 tag, ok := depTag.(dependencyTag)
1106 return ok && tag == dylibDepTag
1107}
1108
Jiyong Park94e22fd2021-04-08 18:19:15 +09001109func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1110 tag, ok := depTag.(dependencyTag)
1111 return ok && tag == rlibDepTag
1112}
1113
Matthew Maurer0f003b12020-06-29 14:34:06 -07001114type autoDep struct {
1115 variation string
1116 depTag dependencyTag
1117}
1118
1119var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001120 rlibVariation = "rlib"
1121 dylibVariation = "dylib"
1122 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1123 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001124)
1125
1126type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001127 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001128}
1129
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001130func (mod *Module) begin(ctx BaseModuleContext) {
1131 if mod.coverage != nil {
1132 mod.coverage.begin(ctx)
1133 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001134 if mod.sanitize != nil {
1135 mod.sanitize.begin(ctx)
1136 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001137}
1138
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001139func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001140 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001141 return p.prebuilt()
1142 }
1143 return nil
1144}
1145
Justin Yun24b246a2023-03-16 10:36:16 +09001146func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1147 if rustDep, ok := dep.(*Module); ok {
1148 // Use base module name for snapshots when exporting to Makefile.
1149 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1150 baseName := rustDep.BaseModuleName()
1151 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1152 }
1153 }
1154 return cc.MakeLibName(ctx, c, dep, depName)
1155}
1156
Ivan Lozanoffee3342019-08-27 12:03:00 -07001157func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1158 var depPaths PathDeps
1159
1160 directRlibDeps := []*Module{}
1161 directDylibDeps := []*Module{}
1162 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001163 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001164 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001165 directSrcProvidersDeps := []*Module{}
1166 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001167
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001168 // For the dependency from platform to apex, use the latest stubs
1169 mod.apexSdkVersion = android.FutureApiLevel
1170 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1171 if !apexInfo.IsForPlatform() {
1172 mod.apexSdkVersion = apexInfo.MinSdkVersion
1173 }
1174
1175 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1176 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1177 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1178 // (b/144430859)
1179 mod.apexSdkVersion = android.FutureApiLevel
1180 }
1181
Spandan Das604f3762023-03-16 22:51:40 +00001182 skipModuleList := map[string]bool{}
1183
1184 var apiImportInfo multitree.ApiImportInfo
1185 hasApiImportInfo := false
1186
1187 ctx.VisitDirectDeps(func(dep android.Module) {
1188 if dep.Name() == "api_imports" {
1189 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1190 hasApiImportInfo = true
1191 }
1192 })
1193
1194 if hasApiImportInfo {
1195 targetStubModuleList := map[string]string{}
1196 targetOrigModuleList := map[string]string{}
1197
1198 // Search for dependency which both original module and API imported library with APEX stub exists
1199 ctx.VisitDirectDeps(func(dep android.Module) {
1200 depName := ctx.OtherModuleName(dep)
1201 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1202 targetStubModuleList[apiLibrary] = depName
1203 }
1204 })
1205 ctx.VisitDirectDeps(func(dep android.Module) {
1206 depName := ctx.OtherModuleName(dep)
1207 if origLibrary, ok := targetStubModuleList[depName]; ok {
1208 targetOrigModuleList[origLibrary] = depName
1209 }
1210 })
1211
1212 // Decide which library should be used between original and API imported library
1213 ctx.VisitDirectDeps(func(dep android.Module) {
1214 depName := ctx.OtherModuleName(dep)
1215 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1216 if cc.ShouldUseStubForApex(ctx, dep) {
1217 skipModuleList[depName] = true
1218 } else {
1219 skipModuleList[apiLibrary] = true
1220 }
1221 }
1222 })
1223 }
1224
Cole Faustb6e6f992023-08-17 17:42:26 -07001225 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1226 var directAndroidMkSharedLibs []string
1227
Ivan Lozanoffee3342019-08-27 12:03:00 -07001228 ctx.VisitDirectDeps(func(dep android.Module) {
1229 depName := ctx.OtherModuleName(dep)
1230 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001231
Spandan Das604f3762023-03-16 22:51:40 +00001232 if _, exists := skipModuleList[depName]; exists {
1233 return
1234 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001235
1236 if depTag == android.DarwinUniversalVariantTag {
1237 return
1238 }
1239
Ivan Lozano89435d12020-07-31 11:01:18 -04001240 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001241 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001242 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001243
Ivan Lozanoffee3342019-08-27 12:03:00 -07001244 switch depTag {
1245 case dylibDepTag:
1246 dylib, ok := rustDep.compiler.(libraryInterface)
1247 if !ok || !dylib.dylib() {
1248 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1249 return
1250 }
1251 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001252 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001253 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1254
Ivan Lozanoffee3342019-08-27 12:03:00 -07001255 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001256
Ivan Lozanoffee3342019-08-27 12:03:00 -07001257 rlib, ok := rustDep.compiler.(libraryInterface)
1258 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001259 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001260 return
1261 }
1262 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001263 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001264 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1265
Ivan Lozanoffee3342019-08-27 12:03:00 -07001266 case procMacroDepTag:
1267 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001268 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001269 }
1270
Cole Faustb6e6f992023-08-17 17:42:26 -07001271 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1272
Paul Duffind5cf92e2021-07-09 17:38:55 +01001273 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001274 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1275 // OS/Arch variant is used.
1276 var helper string
1277 if ctx.Host() {
1278 helper = "missing 'host_supported'?"
1279 } else {
1280 helper = "device module defined?"
1281 }
1282
1283 if dep.Target().Os != ctx.Os() {
1284 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1285 return
1286 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1287 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1288 return
1289 }
1290 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001291 }
1292
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001293 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001294 if depTag != procMacroDepTag {
1295 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1296 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1297 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1298 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001299 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001300 }
1301
Ivan Lozanoffee3342019-08-27 12:03:00 -07001302 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001303 linkFile := rustDep.UnstrippedOutputFile()
1304 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001305 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1306 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001307 }
1308 }
1309
Ivan Lozano89435d12020-07-31 11:01:18 -04001310 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001311 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001312 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001313 if _, ok := ccDep.(*Module); !ok {
1314 if ccDep.Module().Target().Os != ctx.Os() {
1315 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1316 return
1317 }
1318 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1319 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1320 return
1321 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001322 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001323 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001324 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001325 if !ctx.Config().AllowMissingDependencies() {
1326 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1327 } else {
1328 ctx.AddMissingDependencies([]string{depName})
1329 }
1330 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001331 }
1332
Colin Crossa86ea0e2023-08-01 09:57:22 -07001333 linkPath := linkPathFromFilePath(linkObject.Path())
1334
Ivan Lozanoffee3342019-08-27 12:03:00 -07001335 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001336 switch {
1337 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001338 if cc.IsWholeStaticLib(depTag) {
1339 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1340 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001341 if mod.Binary() {
1342 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1343 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1344 // final linkage, pass the args directly to the linker to handle these cases.
1345 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1346 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001347 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001348 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001349 } else {
1350 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 -05001351 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001352 }
1353
1354 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1355 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001356 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001357 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1358
Colin Cross0de8a1e2020-09-18 14:15:30 -07001359 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.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1363 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001364 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001365
1366 // Record baseLibName for snapshots.
1367 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1368
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001369 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001370 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001371 // For the shared lib dependencies, we may link to the stub variant
1372 // of the dependency depending on the context (e.g. if this
1373 // dependency crosses the APEX boundaries).
1374 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1375
1376 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1377 // object (either from stub or non-stub) to use.
1378 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001379 if !linkObject.Valid() {
1380 if !ctx.Config().AllowMissingDependencies() {
1381 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1382 } else {
1383 ctx.AddMissingDependencies([]string{depName})
1384 }
1385 return
1386 }
Jiyong Park7d55b612021-06-11 17:22:09 +09001387 linkPath = linkPathFromFilePath(linkObject.Path())
1388
Ivan Lozanoffee3342019-08-27 12:03:00 -07001389 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001390 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001391 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1392 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1393 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1394 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001395 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001396
1397 // Record baseLibName for snapshots.
1398 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1399
Cole Faustb6e6f992023-08-17 17:42:26 -07001400 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001401 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001402 case cc.IsHeaderDepTag(depTag):
1403 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1404 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1405 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1406 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001407 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001408 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001409 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001410 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001411 }
1412
1413 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001414 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1415 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001416 if linkObject.Valid() {
1417 lib.exportLinkObjects(linkObject.Path())
1418 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001419 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001420 } else {
1421 switch {
1422 case depTag == cc.CrtBeginDepTag:
1423 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1424 case depTag == cc.CrtEndDepTag:
1425 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1426 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001427 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001428
1429 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001430 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001431 // These are usually genrules which don't have per-target variants.
1432 directSrcDeps = append(directSrcDeps, srcDep)
1433 }
1434 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001435 })
1436
Cole Faustb6e6f992023-08-17 17:42:26 -07001437 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1438
Ivan Lozanoffee3342019-08-27 12:03:00 -07001439 var rlibDepFiles RustLibraries
1440 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001441 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001442 }
1443 var dylibDepFiles RustLibraries
1444 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001445 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001446 }
1447 var procMacroDepFiles RustLibraries
1448 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001449 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001450 }
1451
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001452 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001453 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001454 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 }
1456
Ivan Lozanoffee3342019-08-27 12:03:00 -07001457 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001458 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001459 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001460 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001461 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001462 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463 }
1464
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001465 var srcProviderDepFiles android.Paths
1466 for _, dep := range directSrcProvidersDeps {
1467 srcs, _ := dep.OutputFiles("")
1468 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1469 }
1470 for _, dep := range directSrcDeps {
1471 srcs := dep.Srcs()
1472 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1473 }
1474
Ivan Lozanoffee3342019-08-27 12:03:00 -07001475 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1476 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001477 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001478 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001479 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480
1481 // Dedup exported flags from dependencies
1482 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001483 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001484 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001485 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1486 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1487 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001488
1489 return depPaths
1490}
1491
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001492func (mod *Module) InstallInData() bool {
1493 if mod.compiler == nil {
1494 return false
1495 }
1496 return mod.compiler.inData()
1497}
1498
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001499func (mod *Module) InstallInRamdisk() bool {
1500 return mod.InRamdisk()
1501}
1502
1503func (mod *Module) InstallInVendorRamdisk() bool {
1504 return mod.InVendorRamdisk()
1505}
1506
1507func (mod *Module) InstallInRecovery() bool {
1508 return mod.InRecovery()
1509}
1510
Ivan Lozanoffee3342019-08-27 12:03:00 -07001511func linkPathFromFilePath(filepath android.Path) string {
1512 return strings.Split(filepath.String(), filepath.Base())[0]
1513}
Ivan Lozanod648c432020-02-06 12:05:10 -05001514
Spandan Das604f3762023-03-16 22:51:40 +00001515// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1516func (r *Module) usePublicApi() bool {
1517 return r.Device() && r.UseSdk()
1518}
1519
1520// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1521func (r *Module) useVendorApi() bool {
1522 return r.Device() && (r.InVendor() || r.InProduct())
1523}
1524
Ivan Lozanoffee3342019-08-27 12:03:00 -07001525func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1526 ctx := &depsContext{
1527 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001528 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001529
1530 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001531 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001532 var snapshotInfo *cc.SnapshotInfo
1533
Kiyoung Kim487689e2022-07-26 09:48:22 +09001534 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001535 if mod.usePublicApi() || mod.useVendorApi() {
1536 for idx, lib := range deps.SharedLibs {
1537 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1538 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001539 }
1540
Ivan Lozano1921e802021-05-20 13:39:16 -04001541 if ctx.Os() == android.Android {
1542 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1543 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001544
Ivan Lozano2b081132020-09-08 12:46:52 -04001545 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001546 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001547 stdLinkage = "rlib-std"
1548 }
1549
1550 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001551
Ivan Lozano2b081132020-09-08 12:46:52 -04001552 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1553 rlibDepVariations = append(rlibDepVariations,
1554 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1555 }
1556
Ivan Lozano1921e802021-05-20 13:39:16 -04001557 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001558 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001559 for _, lib := range deps.Rlibs {
1560 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001561 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001562
Ivan Lozano2d407632022-04-07 12:59:11 -04001563 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001564 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001565
1566 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001567 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1568 for _, lib := range deps.Dylibs {
1569 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1570 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001571
Ivan Lozano1921e802021-05-20 13:39:16 -04001572 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001573 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1574 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001575 for _, lib := range deps.Rustlibs {
1576 if autoDep.depTag == rlibDepTag {
1577 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001578 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001579 } else {
1580 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1581 // Check for the existence of the dylib deptag variant. Select it if available,
1582 // otherwise select the rlib variant.
1583 autoDepVariations := append(commonDepVariations,
1584 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001585
1586 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1587
1588 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1589 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001590 } else {
1591 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001592 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001593 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001594 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001595 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001596 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001597 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001598 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001599 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001600 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001601 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001602 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001603 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001604 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001605 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001606 for _, lib := range deps.Stdlibs {
1607 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1608 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001609 }
1610 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001611
1612 for _, lib := range deps.SharedLibs {
1613 depTag := cc.SharedDepTag()
1614 name, version := cc.StubsLibNameAndVersion(lib)
1615
1616 variations := []blueprint.Variation{
1617 {Mutator: "link", Variation: "shared"},
1618 }
Spandan Das604f3762023-03-16 22:51:40 +00001619 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1620 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1621 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1622 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1623 }
1624
1625 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1626 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1627 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001628 }
1629
1630 for _, lib := range deps.WholeStaticLibs {
1631 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001632 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001633
1634 actx.AddVariationDependencies([]blueprint.Variation{
1635 {Mutator: "link", Variation: "static"},
1636 }, depTag, lib)
1637 }
1638
1639 for _, lib := range deps.StaticLibs {
1640 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001641 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001642
1643 actx.AddVariationDependencies([]blueprint.Variation{
1644 {Mutator: "link", Variation: "static"},
1645 }, depTag, lib)
1646 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001647
Zach Johnson3df4e632020-11-06 11:56:27 -08001648 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1649
Colin Cross565cafd2020-09-25 18:47:38 -07001650 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001651 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001652 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001653 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001654 }
Colin Crossfe605e12022-01-23 20:46:16 -08001655 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001656 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001657 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001658 }
1659
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001660 if mod.sourceProvider != nil {
1661 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1662 bindgen.Properties.Custom_bindgen != "" {
1663 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1664 bindgen.Properties.Custom_bindgen)
1665 }
1666 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001667
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001668 actx.AddVariationDependencies([]blueprint.Variation{
1669 {Mutator: "link", Variation: "shared"},
1670 }, dataLibDepTag, deps.DataLibs...)
1671
1672 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1673
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001674 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001675 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001676
1677 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001678}
1679
Ivan Lozano2d407632022-04-07 12:59:11 -04001680// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001681func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1682 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001683 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1684}
1685
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001686func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1687 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1688 actx.AddVariationDependencies(variations, depTag, lib)
1689}
1690
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001691func BeginMutator(ctx android.BottomUpMutatorContext) {
1692 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1693 mod.beginMutator(ctx)
1694 }
1695}
1696
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001697func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1698 ctx := &baseModuleContext{
1699 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001700 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001701
1702 mod.begin(ctx)
1703}
1704
Ivan Lozanoffee3342019-08-27 12:03:00 -07001705func (mod *Module) Name() string {
1706 name := mod.ModuleBase.Name()
1707 if p, ok := mod.compiler.(interface {
1708 Name(string) string
1709 }); ok {
1710 name = p.Name(name)
1711 }
1712 return name
1713}
1714
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001715func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001716 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001717 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001718 }
1719}
1720
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001721var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001722var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001723
1724func (mod *Module) HostToolPath() android.OptionalPath {
1725 if !mod.Host() {
1726 return android.OptionalPath{}
1727 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001728 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1729 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001730 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1731 // Even though proc-macros aren't strictly "tools", since they target the compiler
1732 // and act as compiler plugins, we treat them similarly.
1733 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001734 }
1735 return android.OptionalPath{}
1736}
1737
Jiyong Park99644e92020-11-17 22:21:02 +09001738var _ android.ApexModule = (*Module)(nil)
1739
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001740func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001741 return String(mod.Properties.Min_sdk_version)
1742}
1743
Jiyong Park45bf82e2020-12-15 22:29:02 +09001744// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001745func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001746 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001747 if minSdkVersion == "apex_inherit" {
1748 return nil
1749 }
1750 if minSdkVersion == "" {
1751 return fmt.Errorf("min_sdk_version is not specificed")
1752 }
1753
1754 // Not using nativeApiLevelFromUser because the context here is not
1755 // necessarily a native context.
1756 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1757 if err != nil {
1758 return err
1759 }
1760
1761 if ver.GreaterThan(sdkVersion) {
1762 return fmt.Errorf("newer SDK(%v)", ver)
1763 }
Jiyong Park99644e92020-11-17 22:21:02 +09001764 return nil
1765}
1766
Jiyong Park45bf82e2020-12-15 22:29:02 +09001767// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001768func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1769 depTag := ctx.OtherModuleDependencyTag(dep)
1770
1771 if ccm, ok := dep.(*cc.Module); ok {
1772 if ccm.HasStubsVariants() {
1773 if cc.IsSharedDepTag(depTag) {
1774 // dynamic dep to a stubs lib crosses APEX boundary
1775 return false
1776 }
1777 if cc.IsRuntimeDepTag(depTag) {
1778 // runtime dep to a stubs lib also crosses APEX boundary
1779 return false
1780 }
1781
1782 if cc.IsHeaderDepTag(depTag) {
1783 return false
1784 }
1785 }
1786 if mod.Static() && cc.IsSharedDepTag(depTag) {
1787 // shared_lib dependency from a static lib is considered as crossing
1788 // the APEX boundary because the dependency doesn't actually is
1789 // linked; the dependency is used only during the compilation phase.
1790 return false
1791 }
1792 }
1793
Matthew Maurer581b6d82022-09-29 16:46:25 -07001794 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001795 return false
1796 }
1797
1798 return true
1799}
1800
1801// Overrides ApexModule.IsInstallabeToApex()
1802func (mod *Module) IsInstallableToApex() bool {
1803 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001804 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001805 return true
1806 }
1807 if _, ok := mod.compiler.(*binaryDecorator); ok {
1808 return true
1809 }
1810 }
1811 return false
1812}
1813
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001814// If a library file has a "lib" prefix, extract the library name without the prefix.
1815func libNameFromFilePath(filepath android.Path) (string, bool) {
1816 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1817 if strings.HasPrefix(libName, "lib") {
1818 libName = libName[3:]
1819 return libName, true
1820 }
1821 return "", false
1822}
1823
Sasha Smundaka76acba2022-04-18 20:12:56 -07001824func kytheExtractRustFactory() android.Singleton {
1825 return &kytheExtractRustSingleton{}
1826}
1827
1828type kytheExtractRustSingleton struct {
1829}
1830
1831func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1832 var xrefTargets android.Paths
1833 ctx.VisitAllModules(func(module android.Module) {
1834 if rustModule, ok := module.(xref); ok {
1835 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1836 }
1837 })
1838 if len(xrefTargets) > 0 {
1839 ctx.Phony("xref_rust", xrefTargets...)
1840 }
1841}
1842
Jihoon Kangf78a8902022-09-01 22:47:07 +00001843func (c *Module) Partition() string {
1844 return ""
1845}
1846
Vinh Tranbcb5f572023-08-24 11:10:01 -04001847func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
1848 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1849 libraryBp2build(ctx, m)
1850 } else {
1851 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1852 }
1853}
1854
Ivan Lozanoffee3342019-08-27 12:03:00 -07001855var Bool = proptools.Bool
1856var BoolDefault = proptools.BoolDefault
1857var String = proptools.String
1858var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001859
1860var _ android.OutputFileProducer = (*Module)(nil)