blob: c4b89d5470b28db9301a1923fa6fd6a0bdf23306 [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 (
Chris Parsons637458d2023-09-19 20:09:00 +000018 "fmt"
19 "strings"
20
Sasha Smundaka76acba2022-04-18 20:12:56 -070021 "android/soong/bloaty"
Aditya Choudhary87b2ab22023-11-17 15:27:06 +000022 "android/soong/testing"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090023
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "github.com/google/blueprint"
25 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
28 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020029 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070030 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000031 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "android/soong/rust/config"
33)
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
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090072 ImageVariation string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
Ivan Lozano6a884432020-12-02 09:15:16 -050075
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
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000145 hod android.HostOrDeviceSupported
146 multilib android.Multilib
147 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148
Ivan Lozano6a884432020-12-02 09:15:16 -0500149 makeLinkType string
150
Yi Kong46c6e592022-01-20 22:55:00 +0800151 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400153 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200154 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500155 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700158 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400159
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400160 // Output file to be installed, may be stripped or unstripped.
161 outputFile android.OptionalPath
162
Sasha Smundaka76acba2022-04-18 20:12:56 -0700163 // Cross-reference input file
164 kytheFiles android.Paths
165
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400166 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900167
168 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500169
170 // For apex variants, this is set as apex.min_sdk_version
171 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700172
173 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400174
LaMont Jones0c971852023-12-07 21:56:59 +0000175 // Aconfig files for all transitive deps. Also exposed via TransitiveDeclarationsInfo
176 mergedAconfigFiles map[string]android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177}
178
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179func (mod *Module) Header() bool {
180 //TODO: If Rust libraries provide header variants, this needs to be updated.
181 return false
182}
183
184func (mod *Module) SetPreventInstall() {
185 mod.Properties.PreventInstall = true
186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SetHideFromMake() {
189 mod.Properties.HideFromMake = true
190}
191
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900192func (mod *Module) HiddenFromMake() bool {
193 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400194}
195
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500196func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500197 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
198 // nil since we need compiler to actually sanitize.
199 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500200}
201
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (mod *Module) IsPrebuilt() bool {
203 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
204 return true
205 }
206 return false
207}
208
Ivan Lozano43845682020-07-09 21:03:28 -0400209func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
210 switch tag {
211 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700212 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400213 return mod.sourceProvider.Srcs(), nil
214 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900215 if mod.OutputFile().Valid() {
216 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400217 }
218 return android.Paths{}, nil
219 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500220 case "unstripped":
221 if mod.compiler != nil {
222 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
223 }
224 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400225 default:
226 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
227 }
228}
229
Ivan Lozano52767be2019-10-18 14:49:46 -0700230func (mod *Module) SelectedStl() string {
231 return ""
232}
233
Ivan Lozano2b262972019-11-21 12:30:50 -0800234func (mod *Module) NonCcVariants() bool {
235 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400236 if _, ok := mod.compiler.(libraryInterface); ok {
237 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800238 }
239 }
240 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
241}
242
Ivan Lozano52767be2019-10-18 14:49:46 -0700243func (mod *Module) Static() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
246 return library.static()
247 }
248 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700250}
251
252func (mod *Module) Shared() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400255 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700256 }
257 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400258 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700259}
260
Ivan Lozanod7586b62021-04-01 09:49:36 -0400261func (mod *Module) Dylib() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok {
264 return library.dylib()
265 }
266 }
267 return false
268}
269
Ivan Lozanod106efe2023-09-21 23:30:26 -0400270func (mod *Module) Source() bool {
271 if mod.compiler != nil {
272 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
273 return library.source()
274 }
275 }
276 return false
277}
278
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400279func (mod *Module) RlibStd() bool {
280 if mod.compiler != nil {
281 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
282 return library.rlibStd()
283 }
284 }
285 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
286}
287
Ivan Lozanod7586b62021-04-01 09:49:36 -0400288func (mod *Module) Rlib() bool {
289 if mod.compiler != nil {
290 if library, ok := mod.compiler.(libraryInterface); ok {
291 return library.rlib()
292 }
293 }
294 return false
295}
296
297func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400298 if binary, ok := mod.compiler.(binaryInterface); ok {
299 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400300 }
301 return false
302}
303
Justin Yun5e035862021-06-29 20:50:37 +0900304func (mod *Module) StaticExecutable() bool {
305 if !mod.Binary() {
306 return false
307 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400308 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900309}
310
Ivan Lozanod7586b62021-04-01 09:49:36 -0400311func (mod *Module) Object() bool {
312 // Rust has no modules which produce only object files.
313 return false
314}
315
Ivan Lozano52767be2019-10-18 14:49:46 -0700316func (mod *Module) Toc() android.OptionalPath {
317 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400318 if lib, ok := mod.compiler.(libraryInterface); ok {
319 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700320 }
321 }
322 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
323}
324
Colin Crossc511bc52020-04-07 16:50:32 +0000325func (mod *Module) UseSdk() bool {
326 return false
327}
328
Ivan Lozanod7586b62021-04-01 09:49:36 -0400329func (mod *Module) RelativeInstallPath() string {
330 if mod.compiler != nil {
331 return mod.compiler.relativeInstallPath()
332 }
333 return ""
334}
335
Ivan Lozano52767be2019-10-18 14:49:46 -0700336func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500337 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700338}
339
Jiyong Park7d55b612021-06-11 17:22:09 +0900340func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400341 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900342}
343
Ivan Lozano52767be2019-10-18 14:49:46 -0700344func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400345 return true
346}
347
348func (mod *Module) SubName() string {
349 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700350}
351
352func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500353 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700354 return false
355}
356
Ivan Lozanof9e21722020-12-02 09:00:51 -0500357func (mod *Module) IsVndkExt() bool {
358 return false
359}
360
Ivan Lozanod7586b62021-04-01 09:49:36 -0400361func (mod *Module) IsVndkSp() bool {
362 return false
363}
364
Ivan Lozanof1868af2022-04-12 13:08:36 -0400365func (mod *Module) IsVndkPrebuiltLibrary() bool {
366 // Rust modules do not provide VNDK prebuilts
367 return false
368}
369
370func (mod *Module) IsVendorPublicLibrary() bool {
371 return mod.VendorProperties.IsVendorPublicLibrary
372}
373
374func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
375 // Rust modules to not provide Sdk variants
376 return false
377}
378
Colin Cross127bb8b2020-12-16 16:46:01 -0800379func (c *Module) IsVndkPrivate() bool {
380 return false
381}
382
383func (c *Module) IsLlndk() bool {
384 return false
385}
386
387func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500388 return false
389}
390
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400391func (mod *Module) KernelHeadersDecorator() bool {
392 return false
393}
394
Colin Cross1f3f1302021-04-26 18:37:44 -0700395func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400396 return false
397}
398
Colin Cross5271fea2021-04-27 13:06:04 -0700399func (m *Module) NeedsVendorPublicLibraryVariants() bool {
400 return false
401}
402
Ivan Lozanod7586b62021-04-01 09:49:36 -0400403func (mod *Module) HasLlndkStubs() bool {
404 return false
405}
406
407func (mod *Module) StubsVersion() string {
408 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
409}
410
Ivan Lozano52767be2019-10-18 14:49:46 -0700411func (mod *Module) SdkVersion() string {
412 return ""
413}
414
Colin Crossc511bc52020-04-07 16:50:32 +0000415func (mod *Module) AlwaysSdk() bool {
416 return false
417}
418
Jiyong Park2286afd2020-06-16 21:58:53 +0900419func (mod *Module) IsSdkVariant() bool {
420 return false
421}
422
Colin Cross1348ce32020-10-01 13:37:16 -0700423func (mod *Module) SplitPerApiLevel() bool {
424 return false
425}
426
Sasha Smundaka76acba2022-04-18 20:12:56 -0700427func (mod *Module) XrefRustFiles() android.Paths {
428 return mod.kytheFiles
429}
430
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400432 Dylibs []string
433 Rlibs []string
434 Rustlibs []string
435 Stdlibs []string
436 ProcMacros []string
437 SharedLibs []string
438 StaticLibs []string
439 WholeStaticLibs []string
440 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700441
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400442 // Used for data dependencies adjacent to tests
443 DataLibs []string
444 DataBins []string
445
Colin Crossfe605e12022-01-23 20:46:16 -0800446 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447}
448
449type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700450 DyLibs RustLibraries
451 RLibs RustLibraries
452 SharedLibs android.Paths
453 SharedLibDeps android.Paths
454 StaticLibs android.Paths
455 ProcMacros RustLibraries
456 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500457
458 // depFlags and depLinkFlags are rustc and linker (clang) flags.
459 depFlags []string
460 depLinkFlags []string
461
462 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
463 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000464 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700465 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700466
Ivan Lozano45901ed2020-07-24 16:05:01 -0400467 // Used by bindgen modules which call clang
468 depClangFlags []string
469 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400470 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400471 depSystemIncludePaths android.Paths
472
Colin Crossfe605e12022-01-23 20:46:16 -0800473 CrtBegin android.Paths
474 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700475
476 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500477 SrcDeps android.Paths
478 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479}
480
481type RustLibraries []RustLibrary
482
483type RustLibrary struct {
484 Path android.Path
485 CrateName string
486}
487
Matthew Maurerbb3add12020-06-25 09:34:12 -0700488type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000489 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700490 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700491}
492
Sasha Smundaka76acba2022-04-18 20:12:56 -0700493type xref interface {
494 XrefRustFiles() android.Paths
495}
496
Matthew Maurerbb3add12020-06-25 09:34:12 -0700497type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000498 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700499 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700500}
501
Wen-yi Chu41326c12023-09-22 03:58:59 +0000502func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
503 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700504}
505
Colin Cross004bd3f2023-10-02 11:39:17 -0700506func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
507 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400508}
509
Colin Cross0de8a1e2020-09-18 14:15:30 -0700510func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
Colin Cross40213022023-12-13 15:19:49 -0800511 android.SetProvider(ctx, FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700512 LinkDirs: flagExporter.linkDirs,
513 LinkObjects: flagExporter.linkObjects,
514 })
515}
516
Matthew Maurerbb3add12020-06-25 09:34:12 -0700517var _ exportedFlagsProducer = (*flagExporter)(nil)
518
519func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700520 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700521}
522
Colin Cross0de8a1e2020-09-18 14:15:30 -0700523type FlagExporterInfo struct {
524 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000525 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700526 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700527}
528
Colin Crossbc7d76c2023-12-12 16:39:03 -0800529var FlagExporterInfoProvider = blueprint.NewProvider[FlagExporterInfo]()
Colin Cross0de8a1e2020-09-18 14:15:30 -0700530
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531func (mod *Module) isCoverageVariant() bool {
532 return mod.coverage.Properties.IsCoverageVariant
533}
534
535var _ cc.Coverage = (*Module)(nil)
536
Colin Crossf5f4ad32024-01-19 15:41:48 -0800537func (mod *Module) IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400538 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
539}
540
Ivan Lozanod7586b62021-04-01 09:49:36 -0400541func (mod *Module) VndkVersion() string {
542 return mod.Properties.VndkVersion
543}
544
545func (mod *Module) PreventInstall() bool {
546 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400547}
548
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400549func (mod *Module) MarkAsCoverageVariant(coverage bool) {
550 mod.coverage.Properties.IsCoverageVariant = coverage
551}
552
553func (mod *Module) EnableCoverageIfNeeded() {
554 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555}
556
557func defaultsFactory() android.Module {
558 return DefaultsFactory()
559}
560
561type Defaults struct {
562 android.ModuleBase
563 android.DefaultsModuleBase
564}
565
566func DefaultsFactory(props ...interface{}) android.Module {
567 module := &Defaults{}
568
569 module.AddProperties(props...)
570 module.AddProperties(
571 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800572 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500573 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100574 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400575 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576 &BaseCompilerProperties{},
577 &BinaryCompilerProperties{},
578 &LibraryCompilerProperties{},
579 &ProcMacroCompilerProperties{},
580 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400581 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700582 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400583 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400584 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200585 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500586 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000587 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588 )
589
590 android.InitDefaultsModule(module)
591 return module
592}
593
594func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700595 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596}
597
Ivan Lozano183a3212019-10-18 14:18:45 -0700598func (mod *Module) CcLibrary() bool {
599 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500600 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700601 return true
602 }
603 }
604 return false
605}
606
607func (mod *Module) CcLibraryInterface() bool {
608 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400609 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
610 // VariantIs{Static,Shared} is set.
611 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700612 return true
613 }
614 }
615 return false
616}
617
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400618func (mod *Module) RustLibraryInterface() bool {
619 if mod.compiler != nil {
620 if _, ok := mod.compiler.(libraryInterface); ok {
621 return true
622 }
623 }
624 return false
625}
626
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500627func (mod *Module) IsFuzzModule() bool {
628 if _, ok := mod.compiler.(*fuzzDecorator); ok {
629 return true
630 }
631 return false
632}
633
634func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
635 return mod.FuzzModule
636}
637
638func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
639 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
640 return fuzzer.fuzzPackagedModule
641 }
642 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
643}
644
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000645func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500646 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
647 return fuzzer.sharedLibraries
648 }
649 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
650}
651
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400652func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400653 if mod.compiler != nil {
654 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400655 }
656 return nil
657}
658
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800659func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700660 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700661 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800662 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700663 }
664 }
665 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
666}
667
668func (mod *Module) SetStatic() {
669 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700670 if library, ok := mod.compiler.(libraryInterface); ok {
671 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700672 return
673 }
674 }
675 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
676}
677
678func (mod *Module) SetShared() {
679 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700680 if library, ok := mod.compiler.(libraryInterface); ok {
681 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700682 return
683 }
684 }
685 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
686}
687
Ivan Lozano183a3212019-10-18 14:18:45 -0700688func (mod *Module) BuildStaticVariant() bool {
689 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700690 if library, ok := mod.compiler.(libraryInterface); ok {
691 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700692 }
693 }
694 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
695}
696
697func (mod *Module) BuildSharedVariant() bool {
698 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700699 if library, ok := mod.compiler.(libraryInterface); ok {
700 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700701 }
702 }
703 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
704}
705
Ivan Lozano183a3212019-10-18 14:18:45 -0700706func (mod *Module) Module() android.Module {
707 return mod
708}
709
Ivan Lozano183a3212019-10-18 14:18:45 -0700710func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400711 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700712}
713
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400714func (mod *Module) CoverageFiles() android.Paths {
715 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800716 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400717 }
718 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
719}
720
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400721// Rust does not produce gcno files, and therefore does not produce a coverage archive.
722func (mod *Module) CoverageOutputFile() android.OptionalPath {
723 return android.OptionalPath{}
724}
725
726func (mod *Module) IsNdk(config android.Config) bool {
727 return false
728}
729
730func (mod *Module) IsStubs() bool {
731 return false
732}
733
Jiyong Park459feca2020-12-15 11:02:21 +0900734func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900735 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900736 return false
737 }
738
Jiyong Park459feca2020-12-15 11:02:21 +0900739 // The apex variant is not installable because it is included in the APEX and won't appear
740 // in the system partition as a standalone file.
741 if !apexInfo.IsForPlatform() {
742 return false
743 }
744
Jiyong Parke54f07e2021-04-07 15:08:04 +0900745 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900746}
747
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500748func (ctx moduleContext) apexVariationName() string {
Colin Crossff694a82023-12-13 15:54:49 -0800749 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
750 return apexInfo.ApexVariationName
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500751}
752
Ivan Lozano183a3212019-10-18 14:18:45 -0700753var _ cc.LinkableInterface = (*Module)(nil)
754
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755func (mod *Module) Init() android.Module {
756 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500757 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758
Yi Kong46c6e592022-01-20 22:55:00 +0800759 if mod.afdo != nil {
760 mod.AddProperties(mod.afdo.props()...)
761 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 if mod.compiler != nil {
763 mod.AddProperties(mod.compiler.compilerProps()...)
764 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400765 if mod.coverage != nil {
766 mod.AddProperties(mod.coverage.props()...)
767 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200768 if mod.clippy != nil {
769 mod.AddProperties(mod.clippy.props()...)
770 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400771 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700772 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400773 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500774 if mod.sanitize != nil {
775 mod.AddProperties(mod.sanitize.props()...)
776 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900779 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780
781 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782 return mod
783}
784
785func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
786 return &Module{
787 hod: hod,
788 multilib: multilib,
789 }
790}
791func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
792 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800793 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400794 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200795 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500796 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797 return module
798}
799
800type ModuleContext interface {
801 android.ModuleContext
802 ModuleContextIntf
803}
804
805type BaseModuleContext interface {
806 android.BaseModuleContext
807 ModuleContextIntf
808}
809
810type DepsContext interface {
811 android.BottomUpMutatorContext
812 ModuleContextIntf
813}
814
815type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200816 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818}
819
820type depsContext struct {
821 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822}
823
824type moduleContext struct {
825 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826}
827
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200828type baseModuleContext struct {
829 android.BaseModuleContext
830}
831
832func (ctx *moduleContext) RustModule() *Module {
833 return ctx.Module().(*Module)
834}
835
836func (ctx *moduleContext) toolchain() config.Toolchain {
837 return ctx.RustModule().toolchain(ctx)
838}
839
840func (ctx *depsContext) RustModule() *Module {
841 return ctx.Module().(*Module)
842}
843
844func (ctx *depsContext) toolchain() config.Toolchain {
845 return ctx.RustModule().toolchain(ctx)
846}
847
848func (ctx *baseModuleContext) RustModule() *Module {
849 return ctx.Module().(*Module)
850}
851
852func (ctx *baseModuleContext) toolchain() config.Toolchain {
853 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400854}
855
856func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700857 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
858 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
859 return false
860 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400861 return mod.compiler != nil && mod.compiler.nativeCoverage()
862}
863
Ivan Lozanod7586b62021-04-01 09:49:36 -0400864func (mod *Module) EverInstallable() bool {
865 return mod.compiler != nil &&
866 // Check to see whether the module is actually ever installable.
867 mod.compiler.everInstallable()
868}
869
870func (mod *Module) Installable() *bool {
871 return mod.Properties.Installable
872}
873
Ivan Lozano872d5792022-03-23 17:31:39 -0400874func (mod *Module) ProcMacro() bool {
875 if pm, ok := mod.compiler.(procMacroInterface); ok {
876 return pm.ProcMacro()
877 }
878 return false
879}
880
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
882 if mod.cachedToolchain == nil {
883 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
884 }
885 return mod.cachedToolchain
886}
887
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200888func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
889 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
890}
891
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
893}
894
895func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
896 ctx := &moduleContext{
897 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899
Colin Crossff694a82023-12-13 15:54:49 -0800900 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Jiyong Park99644e92020-11-17 22:21:02 +0900901 if !apexInfo.IsForPlatform() {
902 mod.hideApexVariantFromMake = true
903 }
904
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500906 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
907
Ivan Lozanof1868af2022-04-12 13:08:36 -0400908 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700909
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910 if !toolchain.Supported() {
911 // This toolchain's unsupported, there's nothing to do for this mod.
912 return
913 }
914
915 deps := mod.depsToPaths(ctx)
916 flags := Flags{
917 Toolchain: toolchain,
918 }
919
Ivan Lozano67eada32021-09-23 11:50:33 -0400920 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800921 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500922 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800923 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924 if mod.compiler != nil {
925 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400926 flags = mod.compiler.cfgFlags(ctx, flags)
927 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400928 }
929 if mod.coverage != nil {
930 flags, deps = mod.coverage.flags(ctx, flags, deps)
931 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200932 if mod.clippy != nil {
933 flags, deps = mod.clippy.flags(ctx, flags, deps)
934 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500935 if mod.sanitize != nil {
936 flags, deps = mod.sanitize.flags(ctx, flags, deps)
937 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400938
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200939 // SourceProvider needs to call GenerateSource() before compiler calls
940 // compile() so it can provide the source. A SourceProvider has
941 // multiple variants (e.g. source, rlib, dylib). Only the "source"
942 // variant is responsible for effectively generating the source. The
943 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400944 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200945 if mod.compiler.(libraryInterface).source() {
946 mod.sourceProvider.GenerateSource(ctx, deps)
947 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
948 } else {
949 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
950 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700951 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200952 }
Colin Cross40213022023-12-13 15:19:49 -0800953 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
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
Colin Crossff694a82023-12-13 15:54:49 -0800973 apexInfo, _ := android.ModuleProvider(actx, android.ApexInfoProvider)
Ivan Lozano872d5792022-03-23 17:31:39 -0400974 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900975 // If the module has been specifically configure to not be installed then
976 // hide from make as otherwise it will break when running inside make as the
977 // output path to install will not be specified. Not all uninstallable
978 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400979 // side dependencies. In particular, proc-macros need to be captured in the
980 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900981 mod.HideFromMake()
982 } else if !mod.installable(apexInfo) {
983 mod.SkipInstall()
984 }
985
986 // Still call install though, the installs will be stored as PackageSpecs to allow
987 // using the outputs in a genrule.
988 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200989 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900990 if ctx.Failed() {
991 return
992 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400993 }
Chris Wailes74be7642021-07-22 16:20:28 -0700994
995 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700996 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000997 if mod.testModule {
Colin Cross40213022023-12-13 15:19:49 -0800998 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000999 }
LaMont Jones0c971852023-12-07 21:56:59 +00001000
LaMont Jonesaa005ae2023-12-19 19:01:57 +00001001 android.CollectDependencyAconfigFiles(ctx, &mod.mergedAconfigFiles)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002}
1003
1004func (mod *Module) deps(ctx DepsContext) Deps {
1005 deps := Deps{}
1006
1007 if mod.compiler != nil {
1008 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001009 }
1010 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001011 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001012 }
1013
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001014 if mod.coverage != nil {
1015 deps = mod.coverage.deps(ctx, deps)
1016 }
1017
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001018 if mod.sanitize != nil {
1019 deps = mod.sanitize.deps(ctx, deps)
1020 }
1021
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1023 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001024 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1026 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1027 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001028 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001029 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030 return deps
1031
1032}
1033
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034type dependencyTag struct {
1035 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001036 name string
1037 library bool
1038 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001039 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040}
1041
Jiyong Park65b62242020-11-25 12:44:59 +09001042// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1043// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1044func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001045 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001046}
1047
1048var _ android.InstallNeededDependencyTag = dependencyTag{}
1049
Colin Cross65cb3142021-12-10 23:05:02 +00001050func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1051 if d.library && d.dynamic {
1052 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1053 }
1054 return nil
1055}
1056
Yu Liuc8884602024-03-15 18:48:38 +00001057func (d dependencyTag) PropagateAconfigValidation() bool {
1058 return d == rlibDepTag || d == sourceDepTag
1059}
1060
1061var _ android.PropagateAconfigValidationDependencyTag = dependencyTag{}
1062
Colin Cross65cb3142021-12-10 23:05:02 +00001063var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1064
Ivan Lozanoffee3342019-08-27 12:03:00 -07001065var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001066 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1067 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001068 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001069 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001070 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001071 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001072 dataLibDepTag = dependencyTag{name: "data lib"}
1073 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074)
1075
Jiyong Park99644e92020-11-17 22:21:02 +09001076func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1077 tag, ok := depTag.(dependencyTag)
1078 return ok && tag == dylibDepTag
1079}
1080
Jiyong Park94e22fd2021-04-08 18:19:15 +09001081func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1082 tag, ok := depTag.(dependencyTag)
1083 return ok && tag == rlibDepTag
1084}
1085
Matthew Maurer0f003b12020-06-29 14:34:06 -07001086type autoDep struct {
1087 variation string
1088 depTag dependencyTag
1089}
1090
1091var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001092 rlibVariation = "rlib"
1093 dylibVariation = "dylib"
1094 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1095 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001096)
1097
1098type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001099 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001100}
1101
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001102func (mod *Module) begin(ctx BaseModuleContext) {
1103 if mod.coverage != nil {
1104 mod.coverage.begin(ctx)
1105 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001106 if mod.sanitize != nil {
1107 mod.sanitize.begin(ctx)
1108 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001109}
1110
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001111func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001112 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001113 return p.prebuilt()
1114 }
1115 return nil
1116}
1117
Kiyoung Kim37693d02024-04-04 09:56:15 +09001118func (mod *Module) Symlinks() []string {
1119 // TODO update this to return the list of symlinks when Rust supports defining symlinks
1120 return nil
1121}
1122
Justin Yun24b246a2023-03-16 10:36:16 +09001123func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1124 if rustDep, ok := dep.(*Module); ok {
1125 // Use base module name for snapshots when exporting to Makefile.
1126 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1127 baseName := rustDep.BaseModuleName()
1128 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1129 }
1130 }
1131 return cc.MakeLibName(ctx, c, dep, depName)
1132}
1133
Ivan Lozanod106efe2023-09-21 23:30:26 -04001134func collectIncludedProtos(mod *Module, dep *Module) {
1135 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1136 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1137 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1138 }
1139 }
1140}
Andrew Walbran52533232024-03-19 11:36:04 +00001141
Ivan Lozanoffee3342019-08-27 12:03:00 -07001142func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1143 var depPaths PathDeps
1144
1145 directRlibDeps := []*Module{}
1146 directDylibDeps := []*Module{}
1147 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001148 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001149 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001150 directSrcProvidersDeps := []*Module{}
1151 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001152
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001153 // For the dependency from platform to apex, use the latest stubs
1154 mod.apexSdkVersion = android.FutureApiLevel
Colin Crossff694a82023-12-13 15:54:49 -08001155 apexInfo, _ := android.ModuleProvider(ctx, android.ApexInfoProvider)
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001156 if !apexInfo.IsForPlatform() {
1157 mod.apexSdkVersion = apexInfo.MinSdkVersion
1158 }
1159
1160 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1161 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1162 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1163 // (b/144430859)
1164 mod.apexSdkVersion = android.FutureApiLevel
1165 }
1166
Spandan Das604f3762023-03-16 22:51:40 +00001167 skipModuleList := map[string]bool{}
1168
1169 var apiImportInfo multitree.ApiImportInfo
1170 hasApiImportInfo := false
1171
1172 ctx.VisitDirectDeps(func(dep android.Module) {
1173 if dep.Name() == "api_imports" {
Colin Cross313aa542023-12-13 13:47:44 -08001174 apiImportInfo, _ = android.OtherModuleProvider(ctx, dep, multitree.ApiImportsProvider)
Spandan Das604f3762023-03-16 22:51:40 +00001175 hasApiImportInfo = true
1176 }
1177 })
1178
1179 if hasApiImportInfo {
1180 targetStubModuleList := map[string]string{}
1181 targetOrigModuleList := map[string]string{}
1182
1183 // Search for dependency which both original module and API imported library with APEX stub exists
1184 ctx.VisitDirectDeps(func(dep android.Module) {
1185 depName := ctx.OtherModuleName(dep)
1186 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1187 targetStubModuleList[apiLibrary] = depName
1188 }
1189 })
1190 ctx.VisitDirectDeps(func(dep android.Module) {
1191 depName := ctx.OtherModuleName(dep)
1192 if origLibrary, ok := targetStubModuleList[depName]; ok {
1193 targetOrigModuleList[origLibrary] = depName
1194 }
1195 })
1196
1197 // Decide which library should be used between original and API imported library
1198 ctx.VisitDirectDeps(func(dep android.Module) {
1199 depName := ctx.OtherModuleName(dep)
1200 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1201 if cc.ShouldUseStubForApex(ctx, dep) {
1202 skipModuleList[depName] = true
1203 } else {
1204 skipModuleList[apiLibrary] = true
1205 }
1206 }
1207 })
1208 }
1209
Cole Faustb6e6f992023-08-17 17:42:26 -07001210 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1211 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001212
Ivan Lozanoffee3342019-08-27 12:03:00 -07001213 ctx.VisitDirectDeps(func(dep android.Module) {
1214 depName := ctx.OtherModuleName(dep)
1215 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001216
Spandan Das604f3762023-03-16 22:51:40 +00001217 if _, exists := skipModuleList[depName]; exists {
1218 return
1219 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001220
1221 if depTag == android.DarwinUniversalVariantTag {
1222 return
1223 }
1224
Ivan Lozano89435d12020-07-31 11:01:18 -04001225 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001226 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001227 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001228
Ivan Lozanoffee3342019-08-27 12:03:00 -07001229 switch depTag {
1230 case dylibDepTag:
1231 dylib, ok := rustDep.compiler.(libraryInterface)
1232 if !ok || !dylib.dylib() {
1233 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1234 return
1235 }
1236 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001237 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001238 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1239
Ivan Lozanoffee3342019-08-27 12:03:00 -07001240 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001241
Ivan Lozanoffee3342019-08-27 12:03:00 -07001242 rlib, ok := rustDep.compiler.(libraryInterface)
1243 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001244 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 return
1246 }
1247 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001248 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001249 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1250
Ivan Lozanoffee3342019-08-27 12:03:00 -07001251 case procMacroDepTag:
1252 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001253 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001254
1255 case sourceDepTag:
1256 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1257 collectIncludedProtos(mod, rustDep)
1258 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001259 }
1260
Cole Faustb6e6f992023-08-17 17:42:26 -07001261 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1262
Paul Duffind5cf92e2021-07-09 17:38:55 +01001263 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001264 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1265 // OS/Arch variant is used.
1266 var helper string
1267 if ctx.Host() {
1268 helper = "missing 'host_supported'?"
1269 } else {
1270 helper = "device module defined?"
1271 }
1272
1273 if dep.Target().Os != ctx.Os() {
1274 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1275 return
1276 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1277 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1278 return
1279 }
1280 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001281 }
1282
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001283 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001284 if depTag != procMacroDepTag {
Colin Cross313aa542023-12-13 13:47:44 -08001285 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001286 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1287 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1288 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001289 }
1290
Ivan Lozanoffee3342019-08-27 12:03:00 -07001291 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001292 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001293 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001294 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001295 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001296 }
1297 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001298 if depTag == sourceDepTag {
1299 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1300 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
Colin Cross313aa542023-12-13 13:47:44 -08001301 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001302 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1303 }
1304 }
1305 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001306 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001307 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001308 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001309 if _, ok := ccDep.(*Module); !ok {
1310 if ccDep.Module().Target().Os != ctx.Os() {
1311 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1312 return
1313 }
1314 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1315 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1316 return
1317 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001318 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001319 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001320 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001321 if !ctx.Config().AllowMissingDependencies() {
1322 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1323 } else {
1324 ctx.AddMissingDependencies([]string{depName})
1325 }
1326 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001327 }
1328
Wen-yi Chu41326c12023-09-22 03:58:59 +00001329 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001330
Ivan Lozanoffee3342019-08-27 12:03:00 -07001331 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001332 switch {
1333 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001334 if cc.IsWholeStaticLib(depTag) {
1335 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1336 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001337 if mod.Binary() {
1338 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1339 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1340 // final linkage, pass the args directly to the linker to handle these cases.
1341 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1342 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001343 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001344 } else {
1345 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 -05001346 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001347 }
1348
1349 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1350 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001351 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001352 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1353
Colin Cross313aa542023-12-13 13:47:44 -08001354 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001355 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1356 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1357 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1358 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001359 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001360
1361 // Record baseLibName for snapshots.
1362 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1363
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001364 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001365 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001366 // For the shared lib dependencies, we may link to the stub variant
1367 // of the dependency depending on the context (e.g. if this
1368 // dependency crosses the APEX boundaries).
1369 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1370
1371 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1372 // object (either from stub or non-stub) to use.
1373 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001374 if !linkObject.Valid() {
1375 if !ctx.Config().AllowMissingDependencies() {
1376 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1377 } else {
1378 ctx.AddMissingDependencies([]string{depName})
1379 }
1380 return
1381 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001382 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001383
Ivan Lozanoffee3342019-08-27 12:03:00 -07001384 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001385 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001386 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1387 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1388 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1389 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001390 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001391
1392 // Record baseLibName for snapshots.
1393 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1394
Cole Faustb6e6f992023-08-17 17:42:26 -07001395 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001396 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001397 case cc.IsHeaderDepTag(depTag):
Colin Cross313aa542023-12-13 13:47:44 -08001398 exportedInfo, _ := android.OtherModuleProvider(ctx, dep, cc.FlagExporterInfoProvider)
Zach Johnson3df4e632020-11-06 11:56:27 -08001399 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1400 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1401 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001402 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001403 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001404 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001405 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001406 }
1407
1408 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001409 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1410 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001411 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001412 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001413 } else {
1414 switch {
1415 case depTag == cc.CrtBeginDepTag:
1416 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1417 case depTag == cc.CrtEndDepTag:
1418 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1419 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001420 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001421
1422 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001423 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001424 // These are usually genrules which don't have per-target variants.
1425 directSrcDeps = append(directSrcDeps, srcDep)
1426 }
1427 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001428 })
1429
Wen-yi Chu41326c12023-09-22 03:58:59 +00001430 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1431
1432 var rlibDepFiles RustLibraries
Andrew Walbran52533232024-03-19 11:36:04 +00001433 aliases := mod.compiler.Aliases()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001434 for _, dep := range directRlibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001435 crateName := dep.CrateName()
1436 if alias, aliased := aliases[crateName]; aliased {
1437 crateName = alias
1438 }
1439 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001440 }
1441 var dylibDepFiles RustLibraries
1442 for _, dep := range directDylibDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001443 crateName := dep.CrateName()
1444 if alias, aliased := aliases[crateName]; aliased {
1445 crateName = alias
1446 }
1447 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001448 }
1449 var procMacroDepFiles RustLibraries
1450 for _, dep := range directProcMacroDeps {
Andrew Walbran52533232024-03-19 11:36:04 +00001451 crateName := dep.CrateName()
1452 if alias, aliased := aliases[crateName]; aliased {
1453 crateName = alias
1454 }
1455 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: crateName})
Wen-yi Chu41326c12023-09-22 03:58:59 +00001456 }
1457
Colin Cross004bd3f2023-10-02 11:39:17 -07001458 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001459 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001460 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001461 }
1462
Colin Cross004bd3f2023-10-02 11:39:17 -07001463 var sharedLibFiles android.Paths
1464 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001465 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001466 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001467 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001468 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001469 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001470 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001471 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001472 }
1473
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001474 var srcProviderDepFiles android.Paths
1475 for _, dep := range directSrcProvidersDeps {
1476 srcs, _ := dep.OutputFiles("")
1477 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1478 }
1479 for _, dep := range directSrcDeps {
1480 srcs := dep.Srcs()
1481 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1482 }
1483
Wen-yi Chu41326c12023-09-22 03:58:59 +00001484 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1485 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001486 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1487 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1488 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001489 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001490 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001491
1492 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001493 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001494 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001495 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001496 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1497 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1498 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001499
1500 return depPaths
1501}
1502
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001503func (mod *Module) InstallInData() bool {
1504 if mod.compiler == nil {
1505 return false
1506 }
1507 return mod.compiler.inData()
1508}
1509
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001510func (mod *Module) InstallInRamdisk() bool {
1511 return mod.InRamdisk()
1512}
1513
1514func (mod *Module) InstallInVendorRamdisk() bool {
1515 return mod.InVendorRamdisk()
1516}
1517
1518func (mod *Module) InstallInRecovery() bool {
1519 return mod.InRecovery()
1520}
1521
Wen-yi Chu41326c12023-09-22 03:58:59 +00001522func linkPathFromFilePath(filepath android.Path) string {
1523 return strings.Split(filepath.String(), filepath.Base())[0]
1524}
1525
Spandan Das604f3762023-03-16 22:51:40 +00001526// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1527func (r *Module) usePublicApi() bool {
1528 return r.Device() && r.UseSdk()
1529}
1530
1531// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1532func (r *Module) useVendorApi() bool {
1533 return r.Device() && (r.InVendor() || r.InProduct())
1534}
1535
Ivan Lozanoffee3342019-08-27 12:03:00 -07001536func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1537 ctx := &depsContext{
1538 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001539 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001540
1541 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001542 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001543
Kiyoung Kim487689e2022-07-26 09:48:22 +09001544 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001545 if mod.usePublicApi() || mod.useVendorApi() {
1546 for idx, lib := range deps.SharedLibs {
1547 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1548 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001549 }
1550
Ivan Lozano1921e802021-05-20 13:39:16 -04001551 if ctx.Os() == android.Android {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001552 deps.SharedLibs, _ = cc.FilterNdkLibs(mod, ctx.Config(), deps.SharedLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001553 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001554
Ivan Lozano2b081132020-09-08 12:46:52 -04001555 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001556 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001557 stdLinkage = "rlib-std"
1558 }
1559
1560 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001561
Ivan Lozano2b081132020-09-08 12:46:52 -04001562 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1563 rlibDepVariations = append(rlibDepVariations,
1564 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1565 }
1566
Ivan Lozano1921e802021-05-20 13:39:16 -04001567 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001568 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001569 for _, lib := range deps.Rlibs {
1570 depTag := rlibDepTag
Ivan Lozano2d407632022-04-07 12:59:11 -04001571 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001572 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001573
1574 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001575 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1576 for _, lib := range deps.Dylibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001577 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001578 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001579
Ivan Lozano1921e802021-05-20 13:39:16 -04001580 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001581 if deps.Rustlibs != nil {
1582 if !mod.compiler.Disabled() {
1583 for _, lib := range deps.Rustlibs {
1584 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1585 if autoDep.depTag == rlibDepTag {
1586 // Handle the rlib deptag case
Kiyoung Kim37693d02024-04-04 09:56:15 +09001587 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1588
Ivan Lozanod106efe2023-09-21 23:30:26 -04001589 } else {
1590 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1591 // Check for the existence of the dylib deptag variant. Select it if available,
1592 // otherwise select the rlib variant.
1593 autoDepVariations := append(commonDepVariations,
1594 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1595
Kiyoung Kim37693d02024-04-04 09:56:15 +09001596 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1597 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001598
Ivan Lozanod106efe2023-09-21 23:30:26 -04001599 } else {
1600 // If there's no dylib dependency available, try to add the rlib dependency instead.
Kiyoung Kim37693d02024-04-04 09:56:15 +09001601 actx.AddVariationDependencies(rlibDepVariations, rlibDepTag, lib)
1602
Ivan Lozanod106efe2023-09-21 23:30:26 -04001603 }
1604 }
1605 }
1606 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1607 for _, lib := range deps.Rustlibs {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001608 srcProviderVariations := append(commonDepVariations,
1609 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1610
Kiyoung Kim37693d02024-04-04 09:56:15 +09001611 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, lib) {
Ivan Lozanod106efe2023-09-21 23:30:26 -04001612 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001613 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001614 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001615 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001616 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001617
Ivan Lozano1921e802021-05-20 13:39:16 -04001618 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001619 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001620 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001621 for _, lib := range deps.Stdlibs {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001622 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001623 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001624 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001625 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001626 for _, lib := range deps.Stdlibs {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001627 actx.AddVariationDependencies(dylibDepVariations, dylibDepTag, lib)
1628
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001629 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001630 }
1631 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001632
1633 for _, lib := range deps.SharedLibs {
1634 depTag := cc.SharedDepTag()
1635 name, version := cc.StubsLibNameAndVersion(lib)
1636
1637 variations := []blueprint.Variation{
1638 {Mutator: "link", Variation: "shared"},
1639 }
Spandan Das604f3762023-03-16 22:51:40 +00001640 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1641 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1642 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1643 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1644 }
1645
1646 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1647 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1648 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001649 }
1650
1651 for _, lib := range deps.WholeStaticLibs {
1652 depTag := cc.StaticDepTag(true)
Ivan Lozano1921e802021-05-20 13:39:16 -04001653
1654 actx.AddVariationDependencies([]blueprint.Variation{
1655 {Mutator: "link", Variation: "static"},
1656 }, depTag, lib)
1657 }
1658
1659 for _, lib := range deps.StaticLibs {
1660 depTag := cc.StaticDepTag(false)
Ivan Lozano1921e802021-05-20 13:39:16 -04001661
1662 actx.AddVariationDependencies([]blueprint.Variation{
1663 {Mutator: "link", Variation: "static"},
1664 }, depTag, lib)
1665 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001666
Zach Johnson3df4e632020-11-06 11:56:27 -08001667 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1668
Colin Cross565cafd2020-09-25 18:47:38 -07001669 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001670 for _, crt := range deps.CrtBegin {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001671 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001672 }
Colin Crossfe605e12022-01-23 20:46:16 -08001673 for _, crt := range deps.CrtEnd {
Kiyoung Kim37693d02024-04-04 09:56:15 +09001674 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, crt)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001675 }
1676
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001677 if mod.sourceProvider != nil {
1678 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1679 bindgen.Properties.Custom_bindgen != "" {
1680 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1681 bindgen.Properties.Custom_bindgen)
1682 }
1683 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001684
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001685 actx.AddVariationDependencies([]blueprint.Variation{
1686 {Mutator: "link", Variation: "shared"},
1687 }, dataLibDepTag, deps.DataLibs...)
1688
1689 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1690
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001691 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001692 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001693
1694 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001695}
1696
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001697func BeginMutator(ctx android.BottomUpMutatorContext) {
1698 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1699 mod.beginMutator(ctx)
1700 }
1701}
1702
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001703func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1704 ctx := &baseModuleContext{
1705 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001706 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001707
1708 mod.begin(ctx)
1709}
1710
Ivan Lozanoffee3342019-08-27 12:03:00 -07001711func (mod *Module) Name() string {
1712 name := mod.ModuleBase.Name()
1713 if p, ok := mod.compiler.(interface {
1714 Name(string) string
1715 }); ok {
1716 name = p.Name(name)
1717 }
1718 return name
1719}
1720
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001721func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001722 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001723 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001724 }
1725}
1726
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001727var _ android.HostToolProvider = (*Module)(nil)
1728
1729func (mod *Module) HostToolPath() android.OptionalPath {
1730 if !mod.Host() {
1731 return android.OptionalPath{}
1732 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001733 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1734 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001735 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1736 // Even though proc-macros aren't strictly "tools", since they target the compiler
1737 // and act as compiler plugins, we treat them similarly.
1738 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001739 }
1740 return android.OptionalPath{}
1741}
1742
Jiyong Park99644e92020-11-17 22:21:02 +09001743var _ android.ApexModule = (*Module)(nil)
1744
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001745func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001746 return String(mod.Properties.Min_sdk_version)
1747}
1748
Jiyong Park45bf82e2020-12-15 22:29:02 +09001749// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001750func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001751 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001752 if minSdkVersion == "apex_inherit" {
1753 return nil
1754 }
1755 if minSdkVersion == "" {
1756 return fmt.Errorf("min_sdk_version is not specificed")
1757 }
1758
1759 // Not using nativeApiLevelFromUser because the context here is not
1760 // necessarily a native context.
1761 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1762 if err != nil {
1763 return err
1764 }
1765
1766 if ver.GreaterThan(sdkVersion) {
1767 return fmt.Errorf("newer SDK(%v)", ver)
1768 }
Jiyong Park99644e92020-11-17 22:21:02 +09001769 return nil
1770}
1771
Jiyong Park45bf82e2020-12-15 22:29:02 +09001772// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001773func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1774 depTag := ctx.OtherModuleDependencyTag(dep)
1775
1776 if ccm, ok := dep.(*cc.Module); ok {
1777 if ccm.HasStubsVariants() {
1778 if cc.IsSharedDepTag(depTag) {
1779 // dynamic dep to a stubs lib crosses APEX boundary
1780 return false
1781 }
1782 if cc.IsRuntimeDepTag(depTag) {
1783 // runtime dep to a stubs lib also crosses APEX boundary
1784 return false
1785 }
1786
1787 if cc.IsHeaderDepTag(depTag) {
1788 return false
1789 }
1790 }
1791 if mod.Static() && cc.IsSharedDepTag(depTag) {
1792 // shared_lib dependency from a static lib is considered as crossing
1793 // the APEX boundary because the dependency doesn't actually is
1794 // linked; the dependency is used only during the compilation phase.
1795 return false
1796 }
1797 }
1798
Matthew Maurer581b6d82022-09-29 16:46:25 -07001799 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001800 return false
1801 }
1802
1803 return true
1804}
1805
1806// Overrides ApexModule.IsInstallabeToApex()
1807func (mod *Module) IsInstallableToApex() bool {
1808 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001809 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001810 return true
1811 }
1812 if _, ok := mod.compiler.(*binaryDecorator); ok {
1813 return true
1814 }
1815 }
1816 return false
1817}
1818
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001819// If a library file has a "lib" prefix, extract the library name without the prefix.
1820func libNameFromFilePath(filepath android.Path) (string, bool) {
1821 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1822 if strings.HasPrefix(libName, "lib") {
1823 libName = libName[3:]
1824 return libName, true
1825 }
1826 return "", false
1827}
1828
Sasha Smundaka76acba2022-04-18 20:12:56 -07001829func kytheExtractRustFactory() android.Singleton {
1830 return &kytheExtractRustSingleton{}
1831}
1832
1833type kytheExtractRustSingleton struct {
1834}
1835
1836func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1837 var xrefTargets android.Paths
1838 ctx.VisitAllModules(func(module android.Module) {
1839 if rustModule, ok := module.(xref); ok {
1840 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1841 }
1842 })
1843 if len(xrefTargets) > 0 {
1844 ctx.Phony("xref_rust", xrefTargets...)
1845 }
1846}
1847
Jihoon Kangf78a8902022-09-01 22:47:07 +00001848func (c *Module) Partition() string {
1849 return ""
1850}
1851
Ivan Lozanoffee3342019-08-27 12:03:00 -07001852var Bool = proptools.Bool
1853var BoolDefault = proptools.BoolDefault
1854var String = proptools.String
1855var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001856
1857var _ android.OutputFileProducer = (*Module)(nil)