blob: 1c43495975b9adc20872366200423781b47a179d [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 })
44 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070046 })
47 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020048 pctx.ImportAs("cc_config", "android/soong/cc/config")
Sasha Smundaka76acba2022-04-18 20:12:56 -070049 android.InitRegistrationContext.RegisterSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070050}
51
52type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040053 GlobalRustFlags []string // Flags that apply globally to rust
54 GlobalLinkFlags []string // Flags that apply globally to linker
55 RustFlags []string // Flags that apply to rust
56 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020057 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070058 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070059 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040060 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070062 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050066 AndroidMkRlibs []string `blueprint:"mutated"`
67 AndroidMkDylibs []string `blueprint:"mutated"`
68 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
69 AndroidMkSharedLibs []string `blueprint:"mutated"`
70 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040071
Ivan Lozano6a884432020-12-02 09:15:16 -050072 ImageVariationPrefix string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
75
Ivan Lozanoc08897c2021-04-02 12:41:32 -040076 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
77 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
78 // appended before SubName.
79 RustSubName string `blueprint:"mutated"`
80
Ivan Lozano6a884432020-12-02 09:15:16 -050081 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050082 CoreVariantNeeded bool `blueprint:"mutated"`
83 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070084 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080085 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050086 ExtraVariants []string `blueprint:"mutated"`
87
Ivan Lozanoa2268632021-07-22 10:52:06 -040088 // Allows this module to use non-APEX version of libraries. Useful
89 // for building binaries that are started before APEXes are activated.
90 Bootstrap *bool
91
Ivan Lozano1921e802021-05-20 13:39:16 -040092 // Used by vendor snapshot to record dependencies from snapshot modules.
93 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090094 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040095
Matthew Maurerc6868382021-07-13 14:12:37 -070096 // Make this module available when building for ramdisk.
97 // On device without a dedicated recovery partition, the module is only
98 // available after switching root into
99 // /first_stage_ramdisk. To expose the module before switching root, install
100 // the recovery variant instead.
101 Ramdisk_available *bool
102
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500103 // Make this module available when building for vendor ramdisk.
104 // On device without a dedicated recovery partition, the module is only
105 // available after switching root into
106 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800107 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500108 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400109
Ivan Lozano1921e802021-05-20 13:39:16 -0400110 // Normally Soong uses the directory structure to decide which modules
111 // should be included (framework) or excluded (non-framework) from the
112 // different snapshots (vendor, recovery, etc.), but this property
113 // allows a partner to exclude a module normally thought of as a
114 // framework module from the vendor snapshot.
115 Exclude_from_vendor_snapshot *bool
116
117 // Normally Soong uses the directory structure to decide which modules
118 // should be included (framework) or excluded (non-framework) from the
119 // different snapshots (vendor, recovery, etc.), but this property
120 // allows a partner to exclude a module normally thought of as a
121 // framework module from the recovery snapshot.
122 Exclude_from_recovery_snapshot *bool
123
Matthew Maurer460ee942021-02-11 12:31:46 -0800124 // Make this module available when building for recovery
125 Recovery_available *bool
126
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500127 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
128 Min_sdk_version *string
129
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900130 HideFromMake bool `blueprint:"mutated"`
131 PreventInstall bool `blueprint:"mutated"`
132
133 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134}
135
136type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700137 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138
Ivan Lozano6a884432020-12-02 09:15:16 -0500139 VendorProperties cc.VendorProperties
140
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141 Properties BaseProperties
142
143 hod android.HostOrDeviceSupported
144 multilib android.Multilib
145
Ivan Lozano6a884432020-12-02 09:15:16 -0500146 makeLinkType string
147
Yi Kong46c6e592022-01-20 22:55:00 +0800148 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400150 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200151 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500152 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400154 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700155 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400157 // Output file to be installed, may be stripped or unstripped.
158 outputFile android.OptionalPath
159
Sasha Smundaka76acba2022-04-18 20:12:56 -0700160 // Cross-reference input file
161 kytheFiles android.Paths
162
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400163 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900164
165 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500166
167 // For apex variants, this is set as apex.min_sdk_version
168 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700169}
170
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171func (mod *Module) Header() bool {
172 //TODO: If Rust libraries provide header variants, this needs to be updated.
173 return false
174}
175
176func (mod *Module) SetPreventInstall() {
177 mod.Properties.PreventInstall = true
178}
179
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500180func (mod *Module) SetHideFromMake() {
181 mod.Properties.HideFromMake = true
182}
183
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900184func (mod *Module) HiddenFromMake() bool {
185 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500189 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
190 // nil since we need compiler to actually sanitize.
191 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500192}
193
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194func (mod *Module) IsPrebuilt() bool {
195 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
196 return true
197 }
198 return false
199}
200
Ivan Lozano43845682020-07-09 21:03:28 -0400201func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
202 switch tag {
203 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700204 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400205 return mod.sourceProvider.Srcs(), nil
206 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900207 if mod.OutputFile().Valid() {
208 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400209 }
210 return android.Paths{}, nil
211 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500212 case "unstripped":
213 if mod.compiler != nil {
214 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
215 }
216 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400217 default:
218 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
219 }
220}
221
Ivan Lozano52767be2019-10-18 14:49:46 -0700222func (mod *Module) SelectedStl() string {
223 return ""
224}
225
Ivan Lozano2b262972019-11-21 12:30:50 -0800226func (mod *Module) NonCcVariants() bool {
227 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400228 if _, ok := mod.compiler.(libraryInterface); ok {
229 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800230 }
231 }
232 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
233}
234
Ivan Lozano52767be2019-10-18 14:49:46 -0700235func (mod *Module) Static() bool {
236 if mod.compiler != nil {
237 if library, ok := mod.compiler.(libraryInterface); ok {
238 return library.static()
239 }
240 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400241 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700242}
243
244func (mod *Module) Shared() bool {
245 if mod.compiler != nil {
246 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400247 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700248 }
249 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400250 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700251}
252
Ivan Lozanod7586b62021-04-01 09:49:36 -0400253func (mod *Module) Dylib() bool {
254 if mod.compiler != nil {
255 if library, ok := mod.compiler.(libraryInterface); ok {
256 return library.dylib()
257 }
258 }
259 return false
260}
261
262func (mod *Module) Rlib() bool {
263 if mod.compiler != nil {
264 if library, ok := mod.compiler.(libraryInterface); ok {
265 return library.rlib()
266 }
267 }
268 return false
269}
270
271func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400272 if binary, ok := mod.compiler.(binaryInterface); ok {
273 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400274 }
275 return false
276}
277
Justin Yun5e035862021-06-29 20:50:37 +0900278func (mod *Module) StaticExecutable() bool {
279 if !mod.Binary() {
280 return false
281 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400282 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900283}
284
Ivan Lozanod7586b62021-04-01 09:49:36 -0400285func (mod *Module) Object() bool {
286 // Rust has no modules which produce only object files.
287 return false
288}
289
Ivan Lozano52767be2019-10-18 14:49:46 -0700290func (mod *Module) Toc() android.OptionalPath {
291 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400292 if lib, ok := mod.compiler.(libraryInterface); ok {
293 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700294 }
295 }
296 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
297}
298
Colin Crossc511bc52020-04-07 16:50:32 +0000299func (mod *Module) UseSdk() bool {
300 return false
301}
302
Ivan Lozanod7586b62021-04-01 09:49:36 -0400303func (mod *Module) RelativeInstallPath() string {
304 if mod.compiler != nil {
305 return mod.compiler.relativeInstallPath()
306 }
307 return ""
308}
309
Ivan Lozano52767be2019-10-18 14:49:46 -0700310func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500311 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700312}
313
Jiyong Park7d55b612021-06-11 17:22:09 +0900314func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400315 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900316}
317
Ivan Lozano52767be2019-10-18 14:49:46 -0700318func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400319 return true
320}
321
322func (mod *Module) SubName() string {
323 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700324}
325
326func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500327 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700328 return false
329}
330
Ivan Lozanof9e21722020-12-02 09:00:51 -0500331func (mod *Module) IsVndkExt() bool {
332 return false
333}
334
Ivan Lozanod7586b62021-04-01 09:49:36 -0400335func (mod *Module) IsVndkSp() bool {
336 return false
337}
338
Ivan Lozanof1868af2022-04-12 13:08:36 -0400339func (mod *Module) IsVndkPrebuiltLibrary() bool {
340 // Rust modules do not provide VNDK prebuilts
341 return false
342}
343
344func (mod *Module) IsVendorPublicLibrary() bool {
345 return mod.VendorProperties.IsVendorPublicLibrary
346}
347
348func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
349 // Rust modules to not provide Sdk variants
350 return false
351}
352
Colin Cross127bb8b2020-12-16 16:46:01 -0800353func (c *Module) IsVndkPrivate() bool {
354 return false
355}
356
357func (c *Module) IsLlndk() bool {
358 return false
359}
360
361func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500362 return false
363}
364
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400365func (mod *Module) KernelHeadersDecorator() bool {
366 return false
367}
368
Colin Cross1f3f1302021-04-26 18:37:44 -0700369func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400370 return false
371}
372
Colin Cross5271fea2021-04-27 13:06:04 -0700373func (m *Module) NeedsVendorPublicLibraryVariants() bool {
374 return false
375}
376
Ivan Lozanod7586b62021-04-01 09:49:36 -0400377func (mod *Module) HasLlndkStubs() bool {
378 return false
379}
380
381func (mod *Module) StubsVersion() string {
382 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
383}
384
Ivan Lozano52767be2019-10-18 14:49:46 -0700385func (mod *Module) SdkVersion() string {
386 return ""
387}
388
Colin Crossc511bc52020-04-07 16:50:32 +0000389func (mod *Module) AlwaysSdk() bool {
390 return false
391}
392
Jiyong Park2286afd2020-06-16 21:58:53 +0900393func (mod *Module) IsSdkVariant() bool {
394 return false
395}
396
Colin Cross1348ce32020-10-01 13:37:16 -0700397func (mod *Module) SplitPerApiLevel() bool {
398 return false
399}
400
Sasha Smundaka76acba2022-04-18 20:12:56 -0700401func (mod *Module) XrefRustFiles() android.Paths {
402 return mod.kytheFiles
403}
404
Ivan Lozanoffee3342019-08-27 12:03:00 -0700405type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400406 Dylibs []string
407 Rlibs []string
408 Rustlibs []string
409 Stdlibs []string
410 ProcMacros []string
411 SharedLibs []string
412 StaticLibs []string
413 WholeStaticLibs []string
414 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400416 // Used for data dependencies adjacent to tests
417 DataLibs []string
418 DataBins []string
419
Colin Crossfe605e12022-01-23 20:46:16 -0800420 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421}
422
423type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500424 DyLibs RustLibraries
425 RLibs RustLibraries
426 SharedLibs android.Paths
427 SharedLibDeps android.Paths
428 StaticLibs android.Paths
429 ProcMacros RustLibraries
Yi Kong46c6e592022-01-20 22:55:00 +0800430 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500431
432 // depFlags and depLinkFlags are rustc and linker (clang) flags.
433 depFlags []string
434 depLinkFlags []string
435
436 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
437 // Both of these are exported and propagate to dependencies.
438 linkDirs []string
439 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700440
Ivan Lozano45901ed2020-07-24 16:05:01 -0400441 // Used by bindgen modules which call clang
442 depClangFlags []string
443 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400444 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400445 depSystemIncludePaths android.Paths
446
Colin Crossfe605e12022-01-23 20:46:16 -0800447 CrtBegin android.Paths
448 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700449
450 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500451 SrcDeps android.Paths
452 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453}
454
455type RustLibraries []RustLibrary
456
457type RustLibrary struct {
458 Path android.Path
459 CrateName string
460}
461
462type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100463 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700464 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400465 cfgFlags(ctx ModuleContext, flags Flags) Flags
466 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700467 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700468 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700469 compilerDeps(ctx DepsContext, deps Deps) Deps
470 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700471 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700472
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100473 // Output directory in which source-generated code from dependencies is
474 // copied. This is equivalent to Cargo's OUT_DIR variable.
475 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700476
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400477 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
478 CargoPkgVersion() string
479
480 // CargoEnvCompat returns whether Cargo environment variables should be used.
481 CargoEnvCompat() bool
482
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800483 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200484 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700485 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400486 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400487
488 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400489
490 Disabled() bool
491 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400492
Ivan Lozanodd055472020-09-28 13:22:45 -0400493 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500494 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900495
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400496 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900497 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400498}
499
Matthew Maurerbb3add12020-06-25 09:34:12 -0700500type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700501 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400502 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700503}
504
Sasha Smundaka76acba2022-04-18 20:12:56 -0700505type xref interface {
506 XrefRustFiles() android.Paths
507}
508
Matthew Maurerbb3add12020-06-25 09:34:12 -0700509type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400510 linkDirs []string
511 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700512}
513
Matthew Maurerbb3add12020-06-25 09:34:12 -0700514func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
515 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
516}
517
Ivan Lozano2093af22020-08-25 12:48:19 -0400518func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
519 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
520}
521
Colin Cross0de8a1e2020-09-18 14:15:30 -0700522func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
523 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700524 LinkDirs: flagExporter.linkDirs,
525 LinkObjects: flagExporter.linkObjects,
526 })
527}
528
Matthew Maurerbb3add12020-06-25 09:34:12 -0700529var _ exportedFlagsProducer = (*flagExporter)(nil)
530
531func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700532 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700533}
534
Colin Cross0de8a1e2020-09-18 14:15:30 -0700535type FlagExporterInfo struct {
536 Flags []string
537 LinkDirs []string // TODO: this should be android.Paths
538 LinkObjects []string // TODO: this should be android.Paths
539}
540
541var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
542
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400543func (mod *Module) isCoverageVariant() bool {
544 return mod.coverage.Properties.IsCoverageVariant
545}
546
547var _ cc.Coverage = (*Module)(nil)
548
549func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
550 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
551}
552
Ivan Lozanod7586b62021-04-01 09:49:36 -0400553func (mod *Module) VndkVersion() string {
554 return mod.Properties.VndkVersion
555}
556
557func (mod *Module) PreventInstall() bool {
558 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559}
560
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400561func (mod *Module) MarkAsCoverageVariant(coverage bool) {
562 mod.coverage.Properties.IsCoverageVariant = coverage
563}
564
565func (mod *Module) EnableCoverageIfNeeded() {
566 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567}
568
569func defaultsFactory() android.Module {
570 return DefaultsFactory()
571}
572
573type Defaults struct {
574 android.ModuleBase
575 android.DefaultsModuleBase
576}
577
578func DefaultsFactory(props ...interface{}) android.Module {
579 module := &Defaults{}
580
581 module.AddProperties(props...)
582 module.AddProperties(
583 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800584 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500585 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100586 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400587 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588 &BaseCompilerProperties{},
589 &BinaryCompilerProperties{},
590 &LibraryCompilerProperties{},
591 &ProcMacroCompilerProperties{},
592 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400593 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700594 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400595 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400596 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200597 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500598 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599 )
600
601 android.InitDefaultsModule(module)
602 return module
603}
604
605func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700606 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700607}
608
Ivan Lozano183a3212019-10-18 14:18:45 -0700609func (mod *Module) CcLibrary() bool {
610 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500611 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700612 return true
613 }
614 }
615 return false
616}
617
618func (mod *Module) CcLibraryInterface() bool {
619 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400620 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
621 // VariantIs{Static,Shared} is set.
622 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700623 return true
624 }
625 }
626 return false
627}
628
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500629func (mod *Module) IsFuzzModule() bool {
630 if _, ok := mod.compiler.(*fuzzDecorator); ok {
631 return true
632 }
633 return false
634}
635
636func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
637 return mod.FuzzModule
638}
639
640func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
641 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
642 return fuzzer.fuzzPackagedModule
643 }
644 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
645}
646
647func (mod *Module) FuzzSharedLibraries() android.Paths {
648 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
649 return fuzzer.sharedLibraries
650 }
651 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
652}
653
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400654func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400655 if mod.compiler != nil {
656 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400657 }
658 return nil
659}
660
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800661func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700662 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700663 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800664 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700665 }
666 }
667 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
668}
669
670func (mod *Module) SetStatic() {
671 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700672 if library, ok := mod.compiler.(libraryInterface); ok {
673 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700674 return
675 }
676 }
677 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
678}
679
680func (mod *Module) SetShared() {
681 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700682 if library, ok := mod.compiler.(libraryInterface); ok {
683 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700684 return
685 }
686 }
687 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
688}
689
Ivan Lozano183a3212019-10-18 14:18:45 -0700690func (mod *Module) BuildStaticVariant() bool {
691 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700692 if library, ok := mod.compiler.(libraryInterface); ok {
693 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700694 }
695 }
696 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
697}
698
699func (mod *Module) BuildSharedVariant() bool {
700 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700701 if library, ok := mod.compiler.(libraryInterface); ok {
702 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700703 }
704 }
705 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
706}
707
Ivan Lozano183a3212019-10-18 14:18:45 -0700708func (mod *Module) Module() android.Module {
709 return mod
710}
711
Ivan Lozano183a3212019-10-18 14:18:45 -0700712func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400713 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700714}
715
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400716func (mod *Module) CoverageFiles() android.Paths {
717 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800718 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400719 }
720 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
721}
722
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400723// Rust does not produce gcno files, and therefore does not produce a coverage archive.
724func (mod *Module) CoverageOutputFile() android.OptionalPath {
725 return android.OptionalPath{}
726}
727
728func (mod *Module) IsNdk(config android.Config) bool {
729 return false
730}
731
732func (mod *Module) IsStubs() bool {
733 return false
734}
735
Jiyong Park459feca2020-12-15 11:02:21 +0900736func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900737 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900738 return false
739 }
740
Jiyong Park459feca2020-12-15 11:02:21 +0900741 // The apex variant is not installable because it is included in the APEX and won't appear
742 // in the system partition as a standalone file.
743 if !apexInfo.IsForPlatform() {
744 return false
745 }
746
Jiyong Parke54f07e2021-04-07 15:08:04 +0900747 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900748}
749
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500750func (ctx moduleContext) apexVariationName() string {
751 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
752}
753
Ivan Lozano183a3212019-10-18 14:18:45 -0700754var _ cc.LinkableInterface = (*Module)(nil)
755
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756func (mod *Module) Init() android.Module {
757 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500758 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759
Yi Kong46c6e592022-01-20 22:55:00 +0800760 if mod.afdo != nil {
761 mod.AddProperties(mod.afdo.props()...)
762 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763 if mod.compiler != nil {
764 mod.AddProperties(mod.compiler.compilerProps()...)
765 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766 if mod.coverage != nil {
767 mod.AddProperties(mod.coverage.props()...)
768 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200769 if mod.clippy != nil {
770 mod.AddProperties(mod.clippy.props()...)
771 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400772 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700773 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400774 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500775 if mod.sanitize != nil {
776 mod.AddProperties(mod.sanitize.props()...)
777 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900780 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781
782 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 return mod
784}
785
786func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
787 return &Module{
788 hod: hod,
789 multilib: multilib,
790 }
791}
792func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
793 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800794 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400795 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200796 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500797 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 return module
799}
800
801type ModuleContext interface {
802 android.ModuleContext
803 ModuleContextIntf
804}
805
806type BaseModuleContext interface {
807 android.BaseModuleContext
808 ModuleContextIntf
809}
810
811type DepsContext interface {
812 android.BottomUpMutatorContext
813 ModuleContextIntf
814}
815
816type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200817 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819}
820
821type depsContext struct {
822 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823}
824
825type moduleContext struct {
826 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827}
828
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200829type baseModuleContext struct {
830 android.BaseModuleContext
831}
832
833func (ctx *moduleContext) RustModule() *Module {
834 return ctx.Module().(*Module)
835}
836
837func (ctx *moduleContext) toolchain() config.Toolchain {
838 return ctx.RustModule().toolchain(ctx)
839}
840
841func (ctx *depsContext) RustModule() *Module {
842 return ctx.Module().(*Module)
843}
844
845func (ctx *depsContext) toolchain() config.Toolchain {
846 return ctx.RustModule().toolchain(ctx)
847}
848
849func (ctx *baseModuleContext) RustModule() *Module {
850 return ctx.Module().(*Module)
851}
852
853func (ctx *baseModuleContext) toolchain() config.Toolchain {
854 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400855}
856
857func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700858 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
859 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
860 return false
861 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400862 return mod.compiler != nil && mod.compiler.nativeCoverage()
863}
864
Ivan Lozanod7586b62021-04-01 09:49:36 -0400865func (mod *Module) EverInstallable() bool {
866 return mod.compiler != nil &&
867 // Check to see whether the module is actually ever installable.
868 mod.compiler.everInstallable()
869}
870
871func (mod *Module) Installable() *bool {
872 return mod.Properties.Installable
873}
874
Ivan Lozano872d5792022-03-23 17:31:39 -0400875func (mod *Module) ProcMacro() bool {
876 if pm, ok := mod.compiler.(procMacroInterface); ok {
877 return pm.ProcMacro()
878 }
879 return false
880}
881
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
883 if mod.cachedToolchain == nil {
884 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
885 }
886 return mod.cachedToolchain
887}
888
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200889func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
890 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
891}
892
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
894}
895
896func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
897 ctx := &moduleContext{
898 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900
Jiyong Park99644e92020-11-17 22:21:02 +0900901 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
902 if !apexInfo.IsForPlatform() {
903 mod.hideApexVariantFromMake = true
904 }
905
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500907 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
908
Ivan Lozanof1868af2022-04-12 13:08:36 -0400909 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 if !toolchain.Supported() {
912 // This toolchain's unsupported, there's nothing to do for this mod.
913 return
914 }
915
916 deps := mod.depsToPaths(ctx)
917 flags := Flags{
918 Toolchain: toolchain,
919 }
920
Ivan Lozano67eada32021-09-23 11:50:33 -0400921 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800922 if mod.afdo != nil {
923 flags, deps = mod.afdo.flags(ctx, flags, deps)
924 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 if mod.compiler != nil {
926 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400927 flags = mod.compiler.cfgFlags(ctx, flags)
928 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400929 }
930 if mod.coverage != nil {
931 flags, deps = mod.coverage.flags(ctx, flags, deps)
932 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200933 if mod.clippy != nil {
934 flags, deps = mod.clippy.flags(ctx, flags, deps)
935 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500936 if mod.sanitize != nil {
937 flags, deps = mod.sanitize.flags(ctx, flags, deps)
938 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400939
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200940 // SourceProvider needs to call GenerateSource() before compiler calls
941 // compile() so it can provide the source. A SourceProvider has
942 // multiple variants (e.g. source, rlib, dylib). Only the "source"
943 // variant is responsible for effectively generating the source. The
944 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400945 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200946 if mod.compiler.(libraryInterface).source() {
947 mod.sourceProvider.GenerateSource(ctx, deps)
948 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
949 } else {
950 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
951 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700952 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200953 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400954 }
955
956 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100957 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700958 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400959 if ctx.Failed() {
960 return
961 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700962 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
963 if buildOutput.kytheFile != nil {
964 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
965 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400966 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900967
Dan Albert06feee92021-03-19 15:06:02 -0700968 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000969 if mod.docTimestampFile.Valid() {
970 ctx.CheckbuildFile(mod.docTimestampFile.Path())
971 }
Dan Albert06feee92021-03-19 15:06:02 -0700972
Ivan Lozano1921e802021-05-20 13:39:16 -0400973 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
974 // RECOVERY_SNAPSHOT_VERSION is current.
975 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
976 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
977 lib.collectHeadersForSnapshot(ctx, deps)
978 }
979 }
980
Jiyong Park459feca2020-12-15 11:02:21 +0900981 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400982 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900983 // If the module has been specifically configure to not be installed then
984 // hide from make as otherwise it will break when running inside make as the
985 // output path to install will not be specified. Not all uninstallable
986 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400987 // side dependencies. In particular, proc-macros need to be captured in the
988 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900989 mod.HideFromMake()
990 } else if !mod.installable(apexInfo) {
991 mod.SkipInstall()
992 }
993
994 // Still call install though, the installs will be stored as PackageSpecs to allow
995 // using the outputs in a genrule.
996 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200997 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900998 if ctx.Failed() {
999 return
1000 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001001 }
Chris Wailes74be7642021-07-22 16:20:28 -07001002
1003 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001004 }
1005}
1006
1007func (mod *Module) deps(ctx DepsContext) Deps {
1008 deps := Deps{}
1009
1010 if mod.compiler != nil {
1011 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001012 }
1013 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001014 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015 }
1016
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001017 if mod.coverage != nil {
1018 deps = mod.coverage.deps(ctx, deps)
1019 }
1020
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001021 if mod.sanitize != nil {
1022 deps = mod.sanitize.deps(ctx, deps)
1023 }
1024
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1026 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001027 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001028 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1029 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1030 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001031 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001032 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033 return deps
1034
1035}
1036
Ivan Lozanoffee3342019-08-27 12:03:00 -07001037type dependencyTag struct {
1038 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001039 name string
1040 library bool
1041 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001042 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043}
1044
Jiyong Park65b62242020-11-25 12:44:59 +09001045// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1046// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1047func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001048 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001049}
1050
1051var _ android.InstallNeededDependencyTag = dependencyTag{}
1052
Colin Cross65cb3142021-12-10 23:05:02 +00001053func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1054 if d.library && d.dynamic {
1055 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1056 }
1057 return nil
1058}
1059
1060var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1061
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001063 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1064 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001065 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001066 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001067 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001068 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001069 dataLibDepTag = dependencyTag{name: "data lib"}
1070 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001071)
1072
Jiyong Park99644e92020-11-17 22:21:02 +09001073func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1074 tag, ok := depTag.(dependencyTag)
1075 return ok && tag == dylibDepTag
1076}
1077
Jiyong Park94e22fd2021-04-08 18:19:15 +09001078func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1079 tag, ok := depTag.(dependencyTag)
1080 return ok && tag == rlibDepTag
1081}
1082
Matthew Maurer0f003b12020-06-29 14:34:06 -07001083type autoDep struct {
1084 variation string
1085 depTag dependencyTag
1086}
1087
1088var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001089 rlibVariation = "rlib"
1090 dylibVariation = "dylib"
1091 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1092 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001093)
1094
1095type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001096 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001097}
1098
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001099func (mod *Module) begin(ctx BaseModuleContext) {
1100 if mod.coverage != nil {
1101 mod.coverage.begin(ctx)
1102 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001103 if mod.sanitize != nil {
1104 mod.sanitize.begin(ctx)
1105 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001106}
1107
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001108func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001109 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001110 return p.prebuilt()
1111 }
1112 return nil
1113}
1114
Justin Yun24b246a2023-03-16 10:36:16 +09001115func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1116 if rustDep, ok := dep.(*Module); ok {
1117 // Use base module name for snapshots when exporting to Makefile.
1118 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1119 baseName := rustDep.BaseModuleName()
1120 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1121 }
1122 }
1123 return cc.MakeLibName(ctx, c, dep, depName)
1124}
1125
Ivan Lozanoffee3342019-08-27 12:03:00 -07001126func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1127 var depPaths PathDeps
1128
1129 directRlibDeps := []*Module{}
1130 directDylibDeps := []*Module{}
1131 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001132 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001133 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001134 directSrcProvidersDeps := []*Module{}
1135 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001137 // For the dependency from platform to apex, use the latest stubs
1138 mod.apexSdkVersion = android.FutureApiLevel
1139 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1140 if !apexInfo.IsForPlatform() {
1141 mod.apexSdkVersion = apexInfo.MinSdkVersion
1142 }
1143
1144 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1145 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1146 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1147 // (b/144430859)
1148 mod.apexSdkVersion = android.FutureApiLevel
1149 }
1150
Spandan Das604f3762023-03-16 22:51:40 +00001151 skipModuleList := map[string]bool{}
1152
1153 var apiImportInfo multitree.ApiImportInfo
1154 hasApiImportInfo := false
1155
1156 ctx.VisitDirectDeps(func(dep android.Module) {
1157 if dep.Name() == "api_imports" {
1158 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1159 hasApiImportInfo = true
1160 }
1161 })
1162
1163 if hasApiImportInfo {
1164 targetStubModuleList := map[string]string{}
1165 targetOrigModuleList := map[string]string{}
1166
1167 // Search for dependency which both original module and API imported library with APEX stub exists
1168 ctx.VisitDirectDeps(func(dep android.Module) {
1169 depName := ctx.OtherModuleName(dep)
1170 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1171 targetStubModuleList[apiLibrary] = depName
1172 }
1173 })
1174 ctx.VisitDirectDeps(func(dep android.Module) {
1175 depName := ctx.OtherModuleName(dep)
1176 if origLibrary, ok := targetStubModuleList[depName]; ok {
1177 targetOrigModuleList[origLibrary] = depName
1178 }
1179 })
1180
1181 // Decide which library should be used between original and API imported library
1182 ctx.VisitDirectDeps(func(dep android.Module) {
1183 depName := ctx.OtherModuleName(dep)
1184 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1185 if cc.ShouldUseStubForApex(ctx, dep) {
1186 skipModuleList[depName] = true
1187 } else {
1188 skipModuleList[apiLibrary] = true
1189 }
1190 }
1191 })
1192 }
1193
Ivan Lozanoffee3342019-08-27 12:03:00 -07001194 ctx.VisitDirectDeps(func(dep android.Module) {
1195 depName := ctx.OtherModuleName(dep)
1196 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001197
Spandan Das604f3762023-03-16 22:51:40 +00001198 if _, exists := skipModuleList[depName]; exists {
1199 return
1200 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001201 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001202 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001203 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001204
Ivan Lozanoffee3342019-08-27 12:03:00 -07001205 switch depTag {
1206 case dylibDepTag:
1207 dylib, ok := rustDep.compiler.(libraryInterface)
1208 if !ok || !dylib.dylib() {
1209 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1210 return
1211 }
1212 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001213 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001214 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001215
Ivan Lozanoffee3342019-08-27 12:03:00 -07001216 rlib, ok := rustDep.compiler.(libraryInterface)
1217 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001218 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001219 return
1220 }
1221 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001222 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001223 case procMacroDepTag:
1224 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001225 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001226 }
1227
1228 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001229 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1230 // OS/Arch variant is used.
1231 var helper string
1232 if ctx.Host() {
1233 helper = "missing 'host_supported'?"
1234 } else {
1235 helper = "device module defined?"
1236 }
1237
1238 if dep.Target().Os != ctx.Os() {
1239 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1240 return
1241 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1242 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1243 return
1244 }
1245 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 }
1247
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001248 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001249 if depTag != procMacroDepTag {
1250 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1251 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1252 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1253 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001254 }
1255
Ivan Lozanoffee3342019-08-27 12:03:00 -07001256 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001257 linkFile := rustDep.UnstrippedOutputFile()
1258 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001259 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1260 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001261 }
1262 }
1263
Ivan Lozano89435d12020-07-31 11:01:18 -04001264 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001265 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001266 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001267 if _, ok := ccDep.(*Module); !ok {
1268 if ccDep.Module().Target().Os != ctx.Os() {
1269 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1270 return
1271 }
1272 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1273 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1274 return
1275 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001276 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001277 linkObject := ccDep.OutputFile()
1278 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001279
Ivan Lozano2093af22020-08-25 12:48:19 -04001280 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001281 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1282 }
1283
1284 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001285 switch {
1286 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001287 if cc.IsWholeStaticLib(depTag) {
1288 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1289 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001290 if mod.Binary() {
1291 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1292 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1293 // final linkage, pass the args directly to the linker to handle these cases.
1294 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1295 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001296 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001297 } else {
1298 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 -05001299 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001300 }
1301
1302 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1303 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001304 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001305 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1306
Colin Cross0de8a1e2020-09-18 14:15:30 -07001307 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1308 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1309 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1310 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1311 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001312 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001313
1314 // Record baseLibName for snapshots.
1315 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1316
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001317 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001318 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001319 // For the shared lib dependencies, we may link to the stub variant
1320 // of the dependency depending on the context (e.g. if this
1321 // dependency crosses the APEX boundaries).
1322 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1323
1324 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1325 // object (either from stub or non-stub) to use.
1326 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1327 linkPath = linkPathFromFilePath(linkObject.Path())
1328
Ivan Lozanoffee3342019-08-27 12:03:00 -07001329 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001330 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001331 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1332 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1333 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1334 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001335 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001336
1337 // Record baseLibName for snapshots.
1338 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1339
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001340 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001341 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001342 case cc.IsHeaderDepTag(depTag):
1343 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1344 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1345 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1346 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001347 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001348 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001349 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001350 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001351 }
1352
1353 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001354 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1355 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001356 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001357 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001358 } else {
1359 switch {
1360 case depTag == cc.CrtBeginDepTag:
1361 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1362 case depTag == cc.CrtEndDepTag:
1363 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1364 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001365 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001366
1367 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001368 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001369 // These are usually genrules which don't have per-target variants.
1370 directSrcDeps = append(directSrcDeps, srcDep)
1371 }
1372 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001373 })
1374
1375 var rlibDepFiles RustLibraries
1376 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001377 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001378 }
1379 var dylibDepFiles RustLibraries
1380 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001381 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001382 }
1383 var procMacroDepFiles RustLibraries
1384 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001385 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001386 }
1387
1388 var staticLibDepFiles android.Paths
1389 for _, dep := range directStaticLibDeps {
1390 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1391 }
1392
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001393 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001394 var sharedLibDepFiles android.Paths
1395 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001396 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1397 if dep.TableOfContents.Valid() {
1398 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001399 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001400 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001401 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001402 }
1403
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001404 var srcProviderDepFiles android.Paths
1405 for _, dep := range directSrcProvidersDeps {
1406 srcs, _ := dep.OutputFiles("")
1407 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1408 }
1409 for _, dep := range directSrcDeps {
1410 srcs := dep.Srcs()
1411 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1412 }
1413
Ivan Lozanoffee3342019-08-27 12:03:00 -07001414 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1415 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Sam Delmerico51d6d1c2023-03-28 16:54:00 -04001416 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001417 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001418 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1419 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001420 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001421
1422 // Dedup exported flags from dependencies
1423 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001424 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001425 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001426 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1427 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1428 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001429
1430 return depPaths
1431}
1432
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001433func (mod *Module) InstallInData() bool {
1434 if mod.compiler == nil {
1435 return false
1436 }
1437 return mod.compiler.inData()
1438}
1439
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001440func (mod *Module) InstallInRamdisk() bool {
1441 return mod.InRamdisk()
1442}
1443
1444func (mod *Module) InstallInVendorRamdisk() bool {
1445 return mod.InVendorRamdisk()
1446}
1447
1448func (mod *Module) InstallInRecovery() bool {
1449 return mod.InRecovery()
1450}
1451
Ivan Lozanoffee3342019-08-27 12:03:00 -07001452func linkPathFromFilePath(filepath android.Path) string {
1453 return strings.Split(filepath.String(), filepath.Base())[0]
1454}
Ivan Lozanod648c432020-02-06 12:05:10 -05001455
Spandan Das604f3762023-03-16 22:51:40 +00001456// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1457func (r *Module) usePublicApi() bool {
1458 return r.Device() && r.UseSdk()
1459}
1460
1461// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1462func (r *Module) useVendorApi() bool {
1463 return r.Device() && (r.InVendor() || r.InProduct())
1464}
1465
Ivan Lozanoffee3342019-08-27 12:03:00 -07001466func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1467 ctx := &depsContext{
1468 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001469 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001470
1471 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001472 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001473 var snapshotInfo *cc.SnapshotInfo
1474
Kiyoung Kim487689e2022-07-26 09:48:22 +09001475 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001476 if mod.usePublicApi() || mod.useVendorApi() {
1477 for idx, lib := range deps.SharedLibs {
1478 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1479 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001480 }
1481
Ivan Lozano1921e802021-05-20 13:39:16 -04001482 if ctx.Os() == android.Android {
1483 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1484 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001485
Ivan Lozano2b081132020-09-08 12:46:52 -04001486 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001487 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001488 stdLinkage = "rlib-std"
1489 }
1490
1491 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001492
Ivan Lozano2b081132020-09-08 12:46:52 -04001493 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1494 rlibDepVariations = append(rlibDepVariations,
1495 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1496 }
1497
Ivan Lozano1921e802021-05-20 13:39:16 -04001498 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001499 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001500 for _, lib := range deps.Rlibs {
1501 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001502 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001503
Ivan Lozano2d407632022-04-07 12:59:11 -04001504 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001505 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001506
1507 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001508 actx.AddVariationDependencies(
1509 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001510 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001511 dylibDepTag, deps.Dylibs...)
1512
Ivan Lozano1921e802021-05-20 13:39:16 -04001513 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001514 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1515 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001516 for _, lib := range deps.Rustlibs {
1517 if autoDep.depTag == rlibDepTag {
1518 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001519 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001520 } else {
1521 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1522 // Check for the existence of the dylib deptag variant. Select it if available,
1523 // otherwise select the rlib variant.
1524 autoDepVariations := append(commonDepVariations,
1525 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1526 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1527 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
1528 } else {
1529 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001530 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001531 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001532 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001533 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001534 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001535 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001536 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001537 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001538 for _, lib := range deps.Stdlibs {
1539 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001540 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001541
1542 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1543 depTag, lib)
1544 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001545 } else {
1546 actx.AddVariationDependencies(
1547 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1548 dylibDepTag, deps.Stdlibs...)
1549 }
1550 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001551
1552 for _, lib := range deps.SharedLibs {
1553 depTag := cc.SharedDepTag()
1554 name, version := cc.StubsLibNameAndVersion(lib)
1555
1556 variations := []blueprint.Variation{
1557 {Mutator: "link", Variation: "shared"},
1558 }
Spandan Das604f3762023-03-16 22:51:40 +00001559 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1560 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1561 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1562 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1563 }
1564
1565 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1566 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1567 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001568 }
1569
1570 for _, lib := range deps.WholeStaticLibs {
1571 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001572 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001573
1574 actx.AddVariationDependencies([]blueprint.Variation{
1575 {Mutator: "link", Variation: "static"},
1576 }, depTag, lib)
1577 }
1578
1579 for _, lib := range deps.StaticLibs {
1580 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001581 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001582
1583 actx.AddVariationDependencies([]blueprint.Variation{
1584 {Mutator: "link", Variation: "static"},
1585 }, depTag, lib)
1586 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001587
Zach Johnson3df4e632020-11-06 11:56:27 -08001588 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1589
Colin Cross565cafd2020-09-25 18:47:38 -07001590 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001591 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001592 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001593 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001594 }
Colin Crossfe605e12022-01-23 20:46:16 -08001595 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001596 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001597 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001598 }
1599
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001600 if mod.sourceProvider != nil {
1601 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1602 bindgen.Properties.Custom_bindgen != "" {
1603 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1604 bindgen.Properties.Custom_bindgen)
1605 }
1606 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001607
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001608 actx.AddVariationDependencies([]blueprint.Variation{
1609 {Mutator: "link", Variation: "shared"},
1610 }, dataLibDepTag, deps.DataLibs...)
1611
1612 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1613
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001614 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001615 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001616}
1617
Ivan Lozano2d407632022-04-07 12:59:11 -04001618// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001619func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1620 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001621 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1622}
1623
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001624func BeginMutator(ctx android.BottomUpMutatorContext) {
1625 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1626 mod.beginMutator(ctx)
1627 }
1628}
1629
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001630func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1631 ctx := &baseModuleContext{
1632 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001633 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001634
1635 mod.begin(ctx)
1636}
1637
Ivan Lozanoffee3342019-08-27 12:03:00 -07001638func (mod *Module) Name() string {
1639 name := mod.ModuleBase.Name()
1640 if p, ok := mod.compiler.(interface {
1641 Name(string) string
1642 }); ok {
1643 name = p.Name(name)
1644 }
1645 return name
1646}
1647
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001648func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001649 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001650 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001651 }
1652}
1653
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001654var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001655var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001656
1657func (mod *Module) HostToolPath() android.OptionalPath {
1658 if !mod.Host() {
1659 return android.OptionalPath{}
1660 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001661 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1662 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001663 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1664 // Even though proc-macros aren't strictly "tools", since they target the compiler
1665 // and act as compiler plugins, we treat them similarly.
1666 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001667 }
1668 return android.OptionalPath{}
1669}
1670
Jiyong Park99644e92020-11-17 22:21:02 +09001671var _ android.ApexModule = (*Module)(nil)
1672
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001673func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001674 return String(mod.Properties.Min_sdk_version)
1675}
1676
Jiyong Park45bf82e2020-12-15 22:29:02 +09001677// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001678func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001679 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001680 if minSdkVersion == "apex_inherit" {
1681 return nil
1682 }
1683 if minSdkVersion == "" {
1684 return fmt.Errorf("min_sdk_version is not specificed")
1685 }
1686
1687 // Not using nativeApiLevelFromUser because the context here is not
1688 // necessarily a native context.
1689 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1690 if err != nil {
1691 return err
1692 }
1693
1694 if ver.GreaterThan(sdkVersion) {
1695 return fmt.Errorf("newer SDK(%v)", ver)
1696 }
Jiyong Park99644e92020-11-17 22:21:02 +09001697 return nil
1698}
1699
Jiyong Park45bf82e2020-12-15 22:29:02 +09001700// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001701func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1702 depTag := ctx.OtherModuleDependencyTag(dep)
1703
1704 if ccm, ok := dep.(*cc.Module); ok {
1705 if ccm.HasStubsVariants() {
1706 if cc.IsSharedDepTag(depTag) {
1707 // dynamic dep to a stubs lib crosses APEX boundary
1708 return false
1709 }
1710 if cc.IsRuntimeDepTag(depTag) {
1711 // runtime dep to a stubs lib also crosses APEX boundary
1712 return false
1713 }
1714
1715 if cc.IsHeaderDepTag(depTag) {
1716 return false
1717 }
1718 }
1719 if mod.Static() && cc.IsSharedDepTag(depTag) {
1720 // shared_lib dependency from a static lib is considered as crossing
1721 // the APEX boundary because the dependency doesn't actually is
1722 // linked; the dependency is used only during the compilation phase.
1723 return false
1724 }
1725 }
1726
Matthew Maurer581b6d82022-09-29 16:46:25 -07001727 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001728 return false
1729 }
1730
1731 return true
1732}
1733
1734// Overrides ApexModule.IsInstallabeToApex()
1735func (mod *Module) IsInstallableToApex() bool {
1736 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001737 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001738 return true
1739 }
1740 if _, ok := mod.compiler.(*binaryDecorator); ok {
1741 return true
1742 }
1743 }
1744 return false
1745}
1746
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001747// If a library file has a "lib" prefix, extract the library name without the prefix.
1748func libNameFromFilePath(filepath android.Path) (string, bool) {
1749 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1750 if strings.HasPrefix(libName, "lib") {
1751 libName = libName[3:]
1752 return libName, true
1753 }
1754 return "", false
1755}
1756
Sasha Smundaka76acba2022-04-18 20:12:56 -07001757func kytheExtractRustFactory() android.Singleton {
1758 return &kytheExtractRustSingleton{}
1759}
1760
1761type kytheExtractRustSingleton struct {
1762}
1763
1764func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1765 var xrefTargets android.Paths
1766 ctx.VisitAllModules(func(module android.Module) {
1767 if rustModule, ok := module.(xref); ok {
1768 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1769 }
1770 })
1771 if len(xrefTargets) > 0 {
1772 ctx.Phony("xref_rust", xrefTargets...)
1773 }
1774}
1775
Jihoon Kangf78a8902022-09-01 22:47:07 +00001776func (c *Module) Partition() string {
1777 return ""
1778}
1779
Ivan Lozanoffee3342019-08-27 12:03:00 -07001780var Bool = proptools.Bool
1781var BoolDefault = proptools.BoolDefault
1782var String = proptools.String
1783var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001784
1785var _ android.OutputFileProducer = (*Module)(nil)