blob: 1b8ebd921942a28cac6e5fe81da6e9224d19c48a [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Sasha Smundaka76acba2022-04-18 20:12:56 -070018 "android/soong/bloaty"
Ivan Lozano183a3212019-10-18 14:18:45 -070019 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "strings"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020027 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070028 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000029 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070030 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040031 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070032)
33
34var pctx = android.NewPackageContext("android/soong/rust")
35
36func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 android.RegisterModuleType("rust_defaults", defaultsFactory)
38 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
39 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040040 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040041 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050042 })
43 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000048 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070049}
50
51type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040052 GlobalRustFlags []string // Flags that apply globally to rust
53 GlobalLinkFlags []string // Flags that apply globally to linker
54 RustFlags []string // Flags that apply to rust
55 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020056 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070057 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070058 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040059 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070061 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070062}
63
64type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050065 AndroidMkRlibs []string `blueprint:"mutated"`
66 AndroidMkDylibs []string `blueprint:"mutated"`
67 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
68 AndroidMkSharedLibs []string `blueprint:"mutated"`
69 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040070
Ivan Lozano6a884432020-12-02 09:15:16 -050071 ImageVariationPrefix string `blueprint:"mutated"`
72 VndkVersion string `blueprint:"mutated"`
73 SubName string `blueprint:"mutated"`
74
Ivan Lozanoc08897c2021-04-02 12:41:32 -040075 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
76 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
77 // appended before SubName.
78 RustSubName string `blueprint:"mutated"`
79
Ivan Lozano6a884432020-12-02 09:15:16 -050080 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050081 CoreVariantNeeded bool `blueprint:"mutated"`
82 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070083 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080084 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 ExtraVariants []string `blueprint:"mutated"`
86
Ivan Lozanoa2268632021-07-22 10:52:06 -040087 // Allows this module to use non-APEX version of libraries. Useful
88 // for building binaries that are started before APEXes are activated.
89 Bootstrap *bool
90
Ivan Lozano1921e802021-05-20 13:39:16 -040091 // Used by vendor snapshot to record dependencies from snapshot modules.
92 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090093 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040094 SnapshotRlibs []string `blueprint:"mutated"`
95 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040096
Matthew Maurerc6868382021-07-13 14:12:37 -070097 // Make this module available when building for ramdisk.
98 // On device without a dedicated recovery partition, the module is only
99 // available after switching root into
100 // /first_stage_ramdisk. To expose the module before switching root, install
101 // the recovery variant instead.
102 Ramdisk_available *bool
103
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500104 // Make this module available when building for vendor ramdisk.
105 // On device without a dedicated recovery partition, the module is only
106 // available after switching root into
107 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800108 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500109 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400110
Ivan Lozano1921e802021-05-20 13:39:16 -0400111 // Normally Soong uses the directory structure to decide which modules
112 // should be included (framework) or excluded (non-framework) from the
113 // different snapshots (vendor, recovery, etc.), but this property
114 // allows a partner to exclude a module normally thought of as a
115 // framework module from the vendor snapshot.
116 Exclude_from_vendor_snapshot *bool
117
118 // Normally Soong uses the directory structure to decide which modules
119 // should be included (framework) or excluded (non-framework) from the
120 // different snapshots (vendor, recovery, etc.), but this property
121 // allows a partner to exclude a module normally thought of as a
122 // framework module from the recovery snapshot.
123 Exclude_from_recovery_snapshot *bool
124
Matthew Maurer460ee942021-02-11 12:31:46 -0800125 // Make this module available when building for recovery
126 Recovery_available *bool
127
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500128 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
129 Min_sdk_version *string
130
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900131 HideFromMake bool `blueprint:"mutated"`
132 PreventInstall bool `blueprint:"mutated"`
133
134 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135}
136
137type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700138 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139
Ivan Lozano6a884432020-12-02 09:15:16 -0500140 VendorProperties cc.VendorProperties
141
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142 Properties BaseProperties
143
144 hod android.HostOrDeviceSupported
145 multilib android.Multilib
146
Ivan Lozano6a884432020-12-02 09:15:16 -0500147 makeLinkType string
148
Yi Kong46c6e592022-01-20 22:55:00 +0800149 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400151 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200152 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500153 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400155 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700156 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400158 // Output file to be installed, may be stripped or unstripped.
159 outputFile android.OptionalPath
160
Sasha Smundaka76acba2022-04-18 20:12:56 -0700161 // Cross-reference input file
162 kytheFiles android.Paths
163
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400164 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900165
166 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500167
168 // For apex variants, this is set as apex.min_sdk_version
169 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170}
171
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500172func (mod *Module) Header() bool {
173 //TODO: If Rust libraries provide header variants, this needs to be updated.
174 return false
175}
176
177func (mod *Module) SetPreventInstall() {
178 mod.Properties.PreventInstall = true
179}
180
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500181func (mod *Module) SetHideFromMake() {
182 mod.Properties.HideFromMake = true
183}
184
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900185func (mod *Module) HiddenFromMake() bool {
186 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400187}
188
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500189func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500190 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
191 // nil since we need compiler to actually sanitize.
192 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500193}
194
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500195func (mod *Module) IsPrebuilt() bool {
196 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
197 return true
198 }
199 return false
200}
201
Ivan Lozano43845682020-07-09 21:03:28 -0400202func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
203 switch tag {
204 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700205 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400206 return mod.sourceProvider.Srcs(), nil
207 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900208 if mod.OutputFile().Valid() {
209 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400210 }
211 return android.Paths{}, nil
212 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500213 case "unstripped":
214 if mod.compiler != nil {
215 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
216 }
217 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400218 default:
219 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
220 }
221}
222
Ivan Lozano52767be2019-10-18 14:49:46 -0700223func (mod *Module) SelectedStl() string {
224 return ""
225}
226
Ivan Lozano2b262972019-11-21 12:30:50 -0800227func (mod *Module) NonCcVariants() bool {
228 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400229 if _, ok := mod.compiler.(libraryInterface); ok {
230 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800231 }
232 }
233 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
234}
235
Ivan Lozano52767be2019-10-18 14:49:46 -0700236func (mod *Module) Static() bool {
237 if mod.compiler != nil {
238 if library, ok := mod.compiler.(libraryInterface); ok {
239 return library.static()
240 }
241 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400242 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700243}
244
245func (mod *Module) Shared() bool {
246 if mod.compiler != nil {
247 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400248 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700249 }
250 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400251 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700252}
253
Ivan Lozanod7586b62021-04-01 09:49:36 -0400254func (mod *Module) Dylib() bool {
255 if mod.compiler != nil {
256 if library, ok := mod.compiler.(libraryInterface); ok {
257 return library.dylib()
258 }
259 }
260 return false
261}
262
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400263func (mod *Module) RlibStd() bool {
264 if mod.compiler != nil {
265 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
266 return library.rlibStd()
267 }
268 }
269 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
270}
271
Ivan Lozanod7586b62021-04-01 09:49:36 -0400272func (mod *Module) Rlib() bool {
273 if mod.compiler != nil {
274 if library, ok := mod.compiler.(libraryInterface); ok {
275 return library.rlib()
276 }
277 }
278 return false
279}
280
281func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400282 if binary, ok := mod.compiler.(binaryInterface); ok {
283 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400284 }
285 return false
286}
287
Justin Yun5e035862021-06-29 20:50:37 +0900288func (mod *Module) StaticExecutable() bool {
289 if !mod.Binary() {
290 return false
291 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400292 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900293}
294
Ivan Lozanod7586b62021-04-01 09:49:36 -0400295func (mod *Module) Object() bool {
296 // Rust has no modules which produce only object files.
297 return false
298}
299
Ivan Lozano52767be2019-10-18 14:49:46 -0700300func (mod *Module) Toc() android.OptionalPath {
301 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400302 if lib, ok := mod.compiler.(libraryInterface); ok {
303 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700304 }
305 }
306 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
307}
308
Colin Crossc511bc52020-04-07 16:50:32 +0000309func (mod *Module) UseSdk() bool {
310 return false
311}
312
Ivan Lozanod7586b62021-04-01 09:49:36 -0400313func (mod *Module) RelativeInstallPath() string {
314 if mod.compiler != nil {
315 return mod.compiler.relativeInstallPath()
316 }
317 return ""
318}
319
Ivan Lozano52767be2019-10-18 14:49:46 -0700320func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500321 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700322}
323
Jiyong Park7d55b612021-06-11 17:22:09 +0900324func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400325 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900326}
327
Ivan Lozano52767be2019-10-18 14:49:46 -0700328func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400329 return true
330}
331
332func (mod *Module) SubName() string {
333 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700334}
335
336func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500337 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700338 return false
339}
340
Ivan Lozanof9e21722020-12-02 09:00:51 -0500341func (mod *Module) IsVndkExt() bool {
342 return false
343}
344
Ivan Lozanod7586b62021-04-01 09:49:36 -0400345func (mod *Module) IsVndkSp() bool {
346 return false
347}
348
Ivan Lozanof1868af2022-04-12 13:08:36 -0400349func (mod *Module) IsVndkPrebuiltLibrary() bool {
350 // Rust modules do not provide VNDK prebuilts
351 return false
352}
353
354func (mod *Module) IsVendorPublicLibrary() bool {
355 return mod.VendorProperties.IsVendorPublicLibrary
356}
357
358func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
359 // Rust modules to not provide Sdk variants
360 return false
361}
362
Colin Cross127bb8b2020-12-16 16:46:01 -0800363func (c *Module) IsVndkPrivate() bool {
364 return false
365}
366
367func (c *Module) IsLlndk() bool {
368 return false
369}
370
371func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500372 return false
373}
374
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400375func (mod *Module) KernelHeadersDecorator() bool {
376 return false
377}
378
Colin Cross1f3f1302021-04-26 18:37:44 -0700379func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400380 return false
381}
382
Colin Cross5271fea2021-04-27 13:06:04 -0700383func (m *Module) NeedsVendorPublicLibraryVariants() bool {
384 return false
385}
386
Ivan Lozanod7586b62021-04-01 09:49:36 -0400387func (mod *Module) HasLlndkStubs() bool {
388 return false
389}
390
391func (mod *Module) StubsVersion() string {
392 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
393}
394
Ivan Lozano52767be2019-10-18 14:49:46 -0700395func (mod *Module) SdkVersion() string {
396 return ""
397}
398
Colin Crossc511bc52020-04-07 16:50:32 +0000399func (mod *Module) AlwaysSdk() bool {
400 return false
401}
402
Jiyong Park2286afd2020-06-16 21:58:53 +0900403func (mod *Module) IsSdkVariant() bool {
404 return false
405}
406
Colin Cross1348ce32020-10-01 13:37:16 -0700407func (mod *Module) SplitPerApiLevel() bool {
408 return false
409}
410
Sasha Smundaka76acba2022-04-18 20:12:56 -0700411func (mod *Module) XrefRustFiles() android.Paths {
412 return mod.kytheFiles
413}
414
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400416 Dylibs []string
417 Rlibs []string
418 Rustlibs []string
419 Stdlibs []string
420 ProcMacros []string
421 SharedLibs []string
422 StaticLibs []string
423 WholeStaticLibs []string
424 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400426 // Used for data dependencies adjacent to tests
427 DataLibs []string
428 DataBins []string
429
Colin Crossfe605e12022-01-23 20:46:16 -0800430 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431}
432
433type PathDeps struct {
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700434 DyLibs RustLibraries
435 RLibs RustLibraries
436 LibDeps android.Paths
437 WholeStaticLibs android.Paths
438 ProcMacros RustLibraries
439 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500440
441 // depFlags and depLinkFlags are rustc and linker (clang) flags.
442 depFlags []string
443 depLinkFlags []string
444
445 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
446 // Both of these are exported and propagate to dependencies.
447 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700448 linkObjects android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700449
Ivan Lozano45901ed2020-07-24 16:05:01 -0400450 // Used by bindgen modules which call clang
451 depClangFlags []string
452 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400453 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400454 depSystemIncludePaths android.Paths
455
Colin Crossfe605e12022-01-23 20:46:16 -0800456 CrtBegin android.Paths
457 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700458
459 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500460 SrcDeps android.Paths
461 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700462}
463
464type RustLibraries []RustLibrary
465
466type RustLibrary struct {
467 Path android.Path
468 CrateName string
469}
470
471type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100472 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700473 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400474 cfgFlags(ctx ModuleContext, flags Flags) Flags
475 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700477 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700478 compilerDeps(ctx DepsContext, deps Deps) Deps
479 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700480 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100482 // Output directory in which source-generated code from dependencies is
483 // copied. This is equivalent to Cargo's OUT_DIR variable.
484 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700485
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400486 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
487 CargoPkgVersion() string
488
489 // CargoEnvCompat returns whether Cargo environment variables should be used.
490 CargoEnvCompat() bool
491
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800492 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200493 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400495 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400496
497 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400498
499 Disabled() bool
500 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400501
Ivan Lozanodd055472020-09-28 13:22:45 -0400502 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500503 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900504
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400505 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900506 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400507}
508
Matthew Maurerbb3add12020-06-25 09:34:12 -0700509type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700510 exportLinkDirs(...string)
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700511 exportLinkObjects(...android.Path)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700512}
513
Sasha Smundaka76acba2022-04-18 20:12:56 -0700514type xref interface {
515 XrefRustFiles() android.Paths
516}
517
Matthew Maurerbb3add12020-06-25 09:34:12 -0700518type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400519 linkDirs []string
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700520 linkObjects android.Paths
521 libDeps android.Paths
Matthew Maurerbb3add12020-06-25 09:34:12 -0700522}
523
Matthew Maurerbb3add12020-06-25 09:34:12 -0700524func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
525 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
526}
527
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700528func (flagExporter *flagExporter) exportLinkObjects(flags ...android.Path) {
529 flagExporter.linkObjects = android.FirstUniquePaths(append(flagExporter.linkObjects, flags...))
530}
531
532func (flagExporter *flagExporter) exportLibDeps(paths ...android.Path) {
533 flagExporter.libDeps = android.FirstUniquePaths(append(flagExporter.libDeps, paths...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400534}
535
Colin Cross0de8a1e2020-09-18 14:15:30 -0700536func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
537 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700538 LinkDirs: flagExporter.linkDirs,
539 LinkObjects: flagExporter.linkObjects,
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700540 LibDeps: flagExporter.libDeps,
Colin Cross0de8a1e2020-09-18 14:15:30 -0700541 })
542}
543
Matthew Maurerbb3add12020-06-25 09:34:12 -0700544var _ exportedFlagsProducer = (*flagExporter)(nil)
545
546func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700547 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700548}
549
Colin Cross0de8a1e2020-09-18 14:15:30 -0700550type FlagExporterInfo struct {
551 Flags []string
552 LinkDirs []string // TODO: this should be android.Paths
Peter Collingbournee7c71c32023-03-31 20:21:19 -0700553 LinkObjects android.Paths
554 LibDeps android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700555}
556
557var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
558
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559func (mod *Module) isCoverageVariant() bool {
560 return mod.coverage.Properties.IsCoverageVariant
561}
562
563var _ cc.Coverage = (*Module)(nil)
564
565func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
566 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
567}
568
Ivan Lozanod7586b62021-04-01 09:49:36 -0400569func (mod *Module) VndkVersion() string {
570 return mod.Properties.VndkVersion
571}
572
573func (mod *Module) PreventInstall() bool {
574 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400575}
576
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400577func (mod *Module) MarkAsCoverageVariant(coverage bool) {
578 mod.coverage.Properties.IsCoverageVariant = coverage
579}
580
581func (mod *Module) EnableCoverageIfNeeded() {
582 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700583}
584
585func defaultsFactory() android.Module {
586 return DefaultsFactory()
587}
588
589type Defaults struct {
590 android.ModuleBase
591 android.DefaultsModuleBase
592}
593
594func DefaultsFactory(props ...interface{}) android.Module {
595 module := &Defaults{}
596
597 module.AddProperties(props...)
598 module.AddProperties(
599 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800600 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500601 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100602 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400603 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700604 &BaseCompilerProperties{},
605 &BinaryCompilerProperties{},
606 &LibraryCompilerProperties{},
607 &ProcMacroCompilerProperties{},
608 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400609 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700610 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400611 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400612 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200613 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500614 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615 )
616
617 android.InitDefaultsModule(module)
618 return module
619}
620
621func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700622 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700623}
624
Ivan Lozano183a3212019-10-18 14:18:45 -0700625func (mod *Module) CcLibrary() bool {
626 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500627 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700628 return true
629 }
630 }
631 return false
632}
633
634func (mod *Module) CcLibraryInterface() bool {
635 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400636 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
637 // VariantIs{Static,Shared} is set.
638 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700639 return true
640 }
641 }
642 return false
643}
644
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400645func (mod *Module) RustLibraryInterface() bool {
646 if mod.compiler != nil {
647 if _, ok := mod.compiler.(libraryInterface); ok {
648 return true
649 }
650 }
651 return false
652}
653
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500654func (mod *Module) IsFuzzModule() bool {
655 if _, ok := mod.compiler.(*fuzzDecorator); ok {
656 return true
657 }
658 return false
659}
660
661func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
662 return mod.FuzzModule
663}
664
665func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
666 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
667 return fuzzer.fuzzPackagedModule
668 }
669 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
670}
671
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000672func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500673 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
674 return fuzzer.sharedLibraries
675 }
676 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
677}
678
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400679func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400680 if mod.compiler != nil {
681 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400682 }
683 return nil
684}
685
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800686func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700687 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700688 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800689 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700690 }
691 }
692 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
693}
694
695func (mod *Module) SetStatic() {
696 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700697 if library, ok := mod.compiler.(libraryInterface); ok {
698 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700699 return
700 }
701 }
702 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
703}
704
705func (mod *Module) SetShared() {
706 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700707 if library, ok := mod.compiler.(libraryInterface); ok {
708 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700709 return
710 }
711 }
712 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
713}
714
Ivan Lozano183a3212019-10-18 14:18:45 -0700715func (mod *Module) BuildStaticVariant() bool {
716 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700717 if library, ok := mod.compiler.(libraryInterface); ok {
718 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700719 }
720 }
721 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
722}
723
724func (mod *Module) BuildSharedVariant() bool {
725 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700726 if library, ok := mod.compiler.(libraryInterface); ok {
727 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700728 }
729 }
730 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
731}
732
Ivan Lozano183a3212019-10-18 14:18:45 -0700733func (mod *Module) Module() android.Module {
734 return mod
735}
736
Ivan Lozano183a3212019-10-18 14:18:45 -0700737func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400738 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700739}
740
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400741func (mod *Module) CoverageFiles() android.Paths {
742 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800743 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400744 }
745 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
746}
747
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400748// Rust does not produce gcno files, and therefore does not produce a coverage archive.
749func (mod *Module) CoverageOutputFile() android.OptionalPath {
750 return android.OptionalPath{}
751}
752
753func (mod *Module) IsNdk(config android.Config) bool {
754 return false
755}
756
757func (mod *Module) IsStubs() bool {
758 return false
759}
760
Jiyong Park459feca2020-12-15 11:02:21 +0900761func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900762 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900763 return false
764 }
765
Jiyong Park459feca2020-12-15 11:02:21 +0900766 // The apex variant is not installable because it is included in the APEX and won't appear
767 // in the system partition as a standalone file.
768 if !apexInfo.IsForPlatform() {
769 return false
770 }
771
Jiyong Parke54f07e2021-04-07 15:08:04 +0900772 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900773}
774
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500775func (ctx moduleContext) apexVariationName() string {
776 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
777}
778
Ivan Lozano183a3212019-10-18 14:18:45 -0700779var _ cc.LinkableInterface = (*Module)(nil)
780
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781func (mod *Module) Init() android.Module {
782 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500783 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784
Yi Kong46c6e592022-01-20 22:55:00 +0800785 if mod.afdo != nil {
786 mod.AddProperties(mod.afdo.props()...)
787 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 if mod.compiler != nil {
789 mod.AddProperties(mod.compiler.compilerProps()...)
790 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400791 if mod.coverage != nil {
792 mod.AddProperties(mod.coverage.props()...)
793 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200794 if mod.clippy != nil {
795 mod.AddProperties(mod.clippy.props()...)
796 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400797 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700798 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400799 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500800 if mod.sanitize != nil {
801 mod.AddProperties(mod.sanitize.props()...)
802 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400803
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900805 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806
807 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808 return mod
809}
810
811func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
812 return &Module{
813 hod: hod,
814 multilib: multilib,
815 }
816}
817func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
818 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800819 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400820 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200821 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500822 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 return module
824}
825
826type ModuleContext interface {
827 android.ModuleContext
828 ModuleContextIntf
829}
830
831type BaseModuleContext interface {
832 android.BaseModuleContext
833 ModuleContextIntf
834}
835
836type DepsContext interface {
837 android.BottomUpMutatorContext
838 ModuleContextIntf
839}
840
841type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200842 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700843 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844}
845
846type depsContext struct {
847 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700848}
849
850type moduleContext struct {
851 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852}
853
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200854type baseModuleContext struct {
855 android.BaseModuleContext
856}
857
858func (ctx *moduleContext) RustModule() *Module {
859 return ctx.Module().(*Module)
860}
861
862func (ctx *moduleContext) toolchain() config.Toolchain {
863 return ctx.RustModule().toolchain(ctx)
864}
865
866func (ctx *depsContext) RustModule() *Module {
867 return ctx.Module().(*Module)
868}
869
870func (ctx *depsContext) toolchain() config.Toolchain {
871 return ctx.RustModule().toolchain(ctx)
872}
873
874func (ctx *baseModuleContext) RustModule() *Module {
875 return ctx.Module().(*Module)
876}
877
878func (ctx *baseModuleContext) toolchain() config.Toolchain {
879 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400880}
881
882func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700883 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
884 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
885 return false
886 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400887 return mod.compiler != nil && mod.compiler.nativeCoverage()
888}
889
Ivan Lozanod7586b62021-04-01 09:49:36 -0400890func (mod *Module) EverInstallable() bool {
891 return mod.compiler != nil &&
892 // Check to see whether the module is actually ever installable.
893 mod.compiler.everInstallable()
894}
895
896func (mod *Module) Installable() *bool {
897 return mod.Properties.Installable
898}
899
Ivan Lozano872d5792022-03-23 17:31:39 -0400900func (mod *Module) ProcMacro() bool {
901 if pm, ok := mod.compiler.(procMacroInterface); ok {
902 return pm.ProcMacro()
903 }
904 return false
905}
906
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
908 if mod.cachedToolchain == nil {
909 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
910 }
911 return mod.cachedToolchain
912}
913
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200914func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
915 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
916}
917
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
919}
920
921func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
922 ctx := &moduleContext{
923 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925
Jiyong Park99644e92020-11-17 22:21:02 +0900926 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
927 if !apexInfo.IsForPlatform() {
928 mod.hideApexVariantFromMake = true
929 }
930
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500932 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
933
Ivan Lozanof1868af2022-04-12 13:08:36 -0400934 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700935
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936 if !toolchain.Supported() {
937 // This toolchain's unsupported, there's nothing to do for this mod.
938 return
939 }
940
941 deps := mod.depsToPaths(ctx)
942 flags := Flags{
943 Toolchain: toolchain,
944 }
945
Ivan Lozano67eada32021-09-23 11:50:33 -0400946 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800947 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500948 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800949 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950 if mod.compiler != nil {
951 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400952 flags = mod.compiler.cfgFlags(ctx, flags)
953 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400954 }
955 if mod.coverage != nil {
956 flags, deps = mod.coverage.flags(ctx, flags, deps)
957 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200958 if mod.clippy != nil {
959 flags, deps = mod.clippy.flags(ctx, flags, deps)
960 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500961 if mod.sanitize != nil {
962 flags, deps = mod.sanitize.flags(ctx, flags, deps)
963 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400964
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200965 // SourceProvider needs to call GenerateSource() before compiler calls
966 // compile() so it can provide the source. A SourceProvider has
967 // multiple variants (e.g. source, rlib, dylib). Only the "source"
968 // variant is responsible for effectively generating the source. The
969 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400970 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200971 if mod.compiler.(libraryInterface).source() {
972 mod.sourceProvider.GenerateSource(ctx, deps)
973 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
974 } else {
975 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
976 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700977 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200978 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400979 }
980
981 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100982 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700983 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400984 if ctx.Failed() {
985 return
986 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700987 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
988 if buildOutput.kytheFile != nil {
989 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
990 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400991 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900992
Dan Albert06feee92021-03-19 15:06:02 -0700993 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000994 if mod.docTimestampFile.Valid() {
995 ctx.CheckbuildFile(mod.docTimestampFile.Path())
996 }
Dan Albert06feee92021-03-19 15:06:02 -0700997
Ivan Lozano1921e802021-05-20 13:39:16 -0400998 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
999 // RECOVERY_SNAPSHOT_VERSION is current.
1000 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1001 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1002 lib.collectHeadersForSnapshot(ctx, deps)
1003 }
1004 }
1005
Jiyong Park459feca2020-12-15 11:02:21 +09001006 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001007 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001008 // If the module has been specifically configure to not be installed then
1009 // hide from make as otherwise it will break when running inside make as the
1010 // output path to install will not be specified. Not all uninstallable
1011 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001012 // side dependencies. In particular, proc-macros need to be captured in the
1013 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001014 mod.HideFromMake()
1015 } else if !mod.installable(apexInfo) {
1016 mod.SkipInstall()
1017 }
1018
1019 // Still call install though, the installs will be stored as PackageSpecs to allow
1020 // using the outputs in a genrule.
1021 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001022 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001023 if ctx.Failed() {
1024 return
1025 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001026 }
Chris Wailes74be7642021-07-22 16:20:28 -07001027
1028 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 }
1030}
1031
1032func (mod *Module) deps(ctx DepsContext) Deps {
1033 deps := Deps{}
1034
1035 if mod.compiler != nil {
1036 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001037 }
1038 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001039 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 }
1041
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001042 if mod.coverage != nil {
1043 deps = mod.coverage.deps(ctx, deps)
1044 }
1045
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001046 if mod.sanitize != nil {
1047 deps = mod.sanitize.deps(ctx, deps)
1048 }
1049
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1051 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001052 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1054 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1055 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001056 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001057 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001058 return deps
1059
1060}
1061
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062type dependencyTag struct {
1063 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001064 name string
1065 library bool
1066 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001067 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001068}
1069
Jiyong Park65b62242020-11-25 12:44:59 +09001070// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1071// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1072func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001073 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001074}
1075
1076var _ android.InstallNeededDependencyTag = dependencyTag{}
1077
Colin Cross65cb3142021-12-10 23:05:02 +00001078func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1079 if d.library && d.dynamic {
1080 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1081 }
1082 return nil
1083}
1084
1085var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1086
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001088 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1089 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001090 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001091 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001092 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001093 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001094 dataLibDepTag = dependencyTag{name: "data lib"}
1095 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001096)
1097
Jiyong Park99644e92020-11-17 22:21:02 +09001098func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1099 tag, ok := depTag.(dependencyTag)
1100 return ok && tag == dylibDepTag
1101}
1102
Jiyong Park94e22fd2021-04-08 18:19:15 +09001103func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1104 tag, ok := depTag.(dependencyTag)
1105 return ok && tag == rlibDepTag
1106}
1107
Matthew Maurer0f003b12020-06-29 14:34:06 -07001108type autoDep struct {
1109 variation string
1110 depTag dependencyTag
1111}
1112
1113var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001114 rlibVariation = "rlib"
1115 dylibVariation = "dylib"
1116 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1117 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001118)
1119
1120type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001121 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001122}
1123
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001124func (mod *Module) begin(ctx BaseModuleContext) {
1125 if mod.coverage != nil {
1126 mod.coverage.begin(ctx)
1127 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001128 if mod.sanitize != nil {
1129 mod.sanitize.begin(ctx)
1130 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001131}
1132
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001133func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001134 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001135 return p.prebuilt()
1136 }
1137 return nil
1138}
1139
Justin Yun24b246a2023-03-16 10:36:16 +09001140func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1141 if rustDep, ok := dep.(*Module); ok {
1142 // Use base module name for snapshots when exporting to Makefile.
1143 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1144 baseName := rustDep.BaseModuleName()
1145 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1146 }
1147 }
1148 return cc.MakeLibName(ctx, c, dep, depName)
1149}
1150
Ivan Lozanoffee3342019-08-27 12:03:00 -07001151func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1152 var depPaths PathDeps
1153
1154 directRlibDeps := []*Module{}
1155 directDylibDeps := []*Module{}
1156 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001157 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001158 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001159 directSrcProvidersDeps := []*Module{}
1160 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001161
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001162 // For the dependency from platform to apex, use the latest stubs
1163 mod.apexSdkVersion = android.FutureApiLevel
1164 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1165 if !apexInfo.IsForPlatform() {
1166 mod.apexSdkVersion = apexInfo.MinSdkVersion
1167 }
1168
1169 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1170 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1171 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1172 // (b/144430859)
1173 mod.apexSdkVersion = android.FutureApiLevel
1174 }
1175
Spandan Das604f3762023-03-16 22:51:40 +00001176 skipModuleList := map[string]bool{}
1177
1178 var apiImportInfo multitree.ApiImportInfo
1179 hasApiImportInfo := false
1180
1181 ctx.VisitDirectDeps(func(dep android.Module) {
1182 if dep.Name() == "api_imports" {
1183 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1184 hasApiImportInfo = true
1185 }
1186 })
1187
1188 if hasApiImportInfo {
1189 targetStubModuleList := map[string]string{}
1190 targetOrigModuleList := map[string]string{}
1191
1192 // Search for dependency which both original module and API imported library with APEX stub exists
1193 ctx.VisitDirectDeps(func(dep android.Module) {
1194 depName := ctx.OtherModuleName(dep)
1195 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1196 targetStubModuleList[apiLibrary] = depName
1197 }
1198 })
1199 ctx.VisitDirectDeps(func(dep android.Module) {
1200 depName := ctx.OtherModuleName(dep)
1201 if origLibrary, ok := targetStubModuleList[depName]; ok {
1202 targetOrigModuleList[origLibrary] = depName
1203 }
1204 })
1205
1206 // Decide which library should be used between original and API imported library
1207 ctx.VisitDirectDeps(func(dep android.Module) {
1208 depName := ctx.OtherModuleName(dep)
1209 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1210 if cc.ShouldUseStubForApex(ctx, dep) {
1211 skipModuleList[depName] = true
1212 } else {
1213 skipModuleList[apiLibrary] = true
1214 }
1215 }
1216 })
1217 }
1218
Ivan Lozanoffee3342019-08-27 12:03:00 -07001219 ctx.VisitDirectDeps(func(dep android.Module) {
1220 depName := ctx.OtherModuleName(dep)
1221 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001222
Spandan Das604f3762023-03-16 22:51:40 +00001223 if _, exists := skipModuleList[depName]; exists {
1224 return
1225 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001226 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001227 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001228 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001229
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230 switch depTag {
1231 case dylibDepTag:
1232 dylib, ok := rustDep.compiler.(libraryInterface)
1233 if !ok || !dylib.dylib() {
1234 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1235 return
1236 }
1237 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001238 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001239 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1240
Ivan Lozanoffee3342019-08-27 12:03:00 -07001241 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001242
Ivan Lozanoffee3342019-08-27 12:03:00 -07001243 rlib, ok := rustDep.compiler.(libraryInterface)
1244 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001245 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 return
1247 }
1248 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001249 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001250 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1251
Ivan Lozanoffee3342019-08-27 12:03:00 -07001252 case procMacroDepTag:
1253 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001254 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001255 }
1256
1257 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001258 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1259 // OS/Arch variant is used.
1260 var helper string
1261 if ctx.Host() {
1262 helper = "missing 'host_supported'?"
1263 } else {
1264 helper = "device module defined?"
1265 }
1266
1267 if dep.Target().Os != ctx.Os() {
1268 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1269 return
1270 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1271 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1272 return
1273 }
1274 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275 }
1276
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001277 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001278 if depTag != procMacroDepTag {
1279 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1280 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1281 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1282 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001283 depPaths.LibDeps = append(depPaths.LibDeps, exportedInfo.LibDeps...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001284 }
1285
Ivan Lozanoffee3342019-08-27 12:03:00 -07001286 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001287 linkFile := rustDep.UnstrippedOutputFile()
1288 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001289 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1290 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001291 }
1292 }
1293
Ivan Lozano89435d12020-07-31 11:01:18 -04001294 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001295 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001296 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001297 if _, ok := ccDep.(*Module); !ok {
1298 if ccDep.Module().Target().Os != ctx.Os() {
1299 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1300 return
1301 }
1302 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1303 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1304 return
1305 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001306 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001307 linkObject := ccDep.OutputFile()
1308 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001309
Ivan Lozano2093af22020-08-25 12:48:19 -04001310 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001311 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1312 }
1313
1314 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001315 switch {
1316 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001317 if cc.IsWholeStaticLib(depTag) {
1318 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1319 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001320 if mod.Binary() {
1321 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1322 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1323 // final linkage, pass the args directly to the linker to handle these cases.
1324 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1325 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001326 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001327 depPaths.WholeStaticLibs = append(depPaths.WholeStaticLibs, linkObject.Path())
Ivan Lozano63bb7682021-03-23 15:53:44 -04001328 } else {
1329 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 -05001330 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001331 }
1332
1333 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1334 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001335 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001336 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1337
Colin Cross0de8a1e2020-09-18 14:15:30 -07001338 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1339 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1340 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1341 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1342 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001343 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001344
1345 // Record baseLibName for snapshots.
1346 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1347
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001348 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001349 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001350 // For the shared lib dependencies, we may link to the stub variant
1351 // of the dependency depending on the context (e.g. if this
1352 // dependency crosses the APEX boundaries).
1353 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1354
1355 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1356 // object (either from stub or non-stub) to use.
1357 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1358 linkPath = linkPathFromFilePath(linkObject.Path())
1359
Ivan Lozanoffee3342019-08-27 12:03:00 -07001360 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001361 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.AsPaths()...)
Colin Cross0de8a1e2020-09-18 14:15:30 -07001362 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1363 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1364 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1365 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001366 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001367
1368 // Record baseLibName for snapshots.
1369 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1370
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001371 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001372 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001373 case cc.IsHeaderDepTag(depTag):
1374 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1375 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1376 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1377 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001378 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001379 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001380 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001381 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001382 }
1383
1384 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001385 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1386 lib.exportLinkDirs(linkPath)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001387 if linkObject.Valid() {
1388 lib.exportLinkObjects(linkObject.Path())
1389 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001390 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001391 } else {
1392 switch {
1393 case depTag == cc.CrtBeginDepTag:
1394 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1395 case depTag == cc.CrtEndDepTag:
1396 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1397 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001398 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001399
1400 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001401 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001402 // These are usually genrules which don't have per-target variants.
1403 directSrcDeps = append(directSrcDeps, srcDep)
1404 }
1405 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001406 })
1407
1408 var rlibDepFiles RustLibraries
1409 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001410 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001411 }
1412 var dylibDepFiles RustLibraries
1413 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001414 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001415 }
1416 var procMacroDepFiles RustLibraries
1417 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001418 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001419 }
1420
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001421 var libDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001422 for _, dep := range directStaticLibDeps {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001423 libDepFiles = append(libDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001424 }
1425
Ivan Lozanoffee3342019-08-27 12:03:00 -07001426 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001427 if dep.TableOfContents.Valid() {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001428 libDepFiles = append(libDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001429 } else {
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001430 libDepFiles = append(libDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001431 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001432 }
1433
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001434 var srcProviderDepFiles android.Paths
1435 for _, dep := range directSrcProvidersDeps {
1436 srcs, _ := dep.OutputFiles("")
1437 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1438 }
1439 for _, dep := range directSrcDeps {
1440 srcs := dep.Srcs()
1441 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1442 }
1443
Ivan Lozanoffee3342019-08-27 12:03:00 -07001444 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1445 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001446 depPaths.LibDeps = append(depPaths.LibDeps, libDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001448 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001449
1450 // Dedup exported flags from dependencies
1451 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Peter Collingbournee7c71c32023-03-31 20:21:19 -07001452 depPaths.linkObjects = android.FirstUniquePaths(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001453 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001454 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1455 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1456 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001457
1458 return depPaths
1459}
1460
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001461func (mod *Module) InstallInData() bool {
1462 if mod.compiler == nil {
1463 return false
1464 }
1465 return mod.compiler.inData()
1466}
1467
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001468func (mod *Module) InstallInRamdisk() bool {
1469 return mod.InRamdisk()
1470}
1471
1472func (mod *Module) InstallInVendorRamdisk() bool {
1473 return mod.InVendorRamdisk()
1474}
1475
1476func (mod *Module) InstallInRecovery() bool {
1477 return mod.InRecovery()
1478}
1479
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480func linkPathFromFilePath(filepath android.Path) string {
1481 return strings.Split(filepath.String(), filepath.Base())[0]
1482}
Ivan Lozanod648c432020-02-06 12:05:10 -05001483
Spandan Das604f3762023-03-16 22:51:40 +00001484// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1485func (r *Module) usePublicApi() bool {
1486 return r.Device() && r.UseSdk()
1487}
1488
1489// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1490func (r *Module) useVendorApi() bool {
1491 return r.Device() && (r.InVendor() || r.InProduct())
1492}
1493
Ivan Lozanoffee3342019-08-27 12:03:00 -07001494func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1495 ctx := &depsContext{
1496 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001497 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001498
1499 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001500 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001501 var snapshotInfo *cc.SnapshotInfo
1502
Kiyoung Kim487689e2022-07-26 09:48:22 +09001503 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001504 if mod.usePublicApi() || mod.useVendorApi() {
1505 for idx, lib := range deps.SharedLibs {
1506 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1507 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001508 }
1509
Ivan Lozano1921e802021-05-20 13:39:16 -04001510 if ctx.Os() == android.Android {
1511 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1512 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001513
Ivan Lozano2b081132020-09-08 12:46:52 -04001514 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001515 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001516 stdLinkage = "rlib-std"
1517 }
1518
1519 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001520
Ivan Lozano2b081132020-09-08 12:46:52 -04001521 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1522 rlibDepVariations = append(rlibDepVariations,
1523 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1524 }
1525
Ivan Lozano1921e802021-05-20 13:39:16 -04001526 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001527 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001528 for _, lib := range deps.Rlibs {
1529 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001530 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001531
Ivan Lozano2d407632022-04-07 12:59:11 -04001532 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001533 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001534
1535 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001536 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1537 for _, lib := range deps.Dylibs {
1538 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1539 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001540
Ivan Lozano1921e802021-05-20 13:39:16 -04001541 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001542 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1543 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001544 for _, lib := range deps.Rustlibs {
1545 if autoDep.depTag == rlibDepTag {
1546 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001547 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001548 } else {
1549 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1550 // Check for the existence of the dylib deptag variant. Select it if available,
1551 // otherwise select the rlib variant.
1552 autoDepVariations := append(commonDepVariations,
1553 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001554
1555 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1556
1557 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1558 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
Ivan Lozano2d407632022-04-07 12:59:11 -04001559 } else {
1560 // If there's no dylib dependency available, try to add the rlib dependency instead.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001561 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozano2d407632022-04-07 12:59:11 -04001562 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001563 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001564 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001565 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001566 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001567 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001568 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001569 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001570 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001571 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001572 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001573 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001574 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001575 for _, lib := range deps.Stdlibs {
1576 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1577 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001578 }
1579 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001580
1581 for _, lib := range deps.SharedLibs {
1582 depTag := cc.SharedDepTag()
1583 name, version := cc.StubsLibNameAndVersion(lib)
1584
1585 variations := []blueprint.Variation{
1586 {Mutator: "link", Variation: "shared"},
1587 }
Spandan Das604f3762023-03-16 22:51:40 +00001588 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1589 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1590 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1591 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1592 }
1593
1594 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1595 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1596 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001597 }
1598
1599 for _, lib := range deps.WholeStaticLibs {
1600 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001601 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001602
1603 actx.AddVariationDependencies([]blueprint.Variation{
1604 {Mutator: "link", Variation: "static"},
1605 }, depTag, lib)
1606 }
1607
1608 for _, lib := range deps.StaticLibs {
1609 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001610 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001611
1612 actx.AddVariationDependencies([]blueprint.Variation{
1613 {Mutator: "link", Variation: "static"},
1614 }, depTag, lib)
1615 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001616
Zach Johnson3df4e632020-11-06 11:56:27 -08001617 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1618
Colin Cross565cafd2020-09-25 18:47:38 -07001619 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001620 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001621 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001622 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001623 }
Colin Crossfe605e12022-01-23 20:46:16 -08001624 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001625 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001626 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001627 }
1628
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001629 if mod.sourceProvider != nil {
1630 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1631 bindgen.Properties.Custom_bindgen != "" {
1632 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1633 bindgen.Properties.Custom_bindgen)
1634 }
1635 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001636
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001637 actx.AddVariationDependencies([]blueprint.Variation{
1638 {Mutator: "link", Variation: "shared"},
1639 }, dataLibDepTag, deps.DataLibs...)
1640
1641 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1642
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001643 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001644 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001645
1646 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001647}
1648
Ivan Lozano2d407632022-04-07 12:59:11 -04001649// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001650func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1651 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001652 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1653}
1654
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001655func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1656 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1657 actx.AddVariationDependencies(variations, depTag, lib)
1658}
1659
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001660func BeginMutator(ctx android.BottomUpMutatorContext) {
1661 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1662 mod.beginMutator(ctx)
1663 }
1664}
1665
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001666func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1667 ctx := &baseModuleContext{
1668 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001669 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001670
1671 mod.begin(ctx)
1672}
1673
Ivan Lozanoffee3342019-08-27 12:03:00 -07001674func (mod *Module) Name() string {
1675 name := mod.ModuleBase.Name()
1676 if p, ok := mod.compiler.(interface {
1677 Name(string) string
1678 }); ok {
1679 name = p.Name(name)
1680 }
1681 return name
1682}
1683
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001684func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001685 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001686 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001687 }
1688}
1689
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001690var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001691var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001692
1693func (mod *Module) HostToolPath() android.OptionalPath {
1694 if !mod.Host() {
1695 return android.OptionalPath{}
1696 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001697 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1698 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001699 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1700 // Even though proc-macros aren't strictly "tools", since they target the compiler
1701 // and act as compiler plugins, we treat them similarly.
1702 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001703 }
1704 return android.OptionalPath{}
1705}
1706
Jiyong Park99644e92020-11-17 22:21:02 +09001707var _ android.ApexModule = (*Module)(nil)
1708
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001709func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001710 return String(mod.Properties.Min_sdk_version)
1711}
1712
Jiyong Park45bf82e2020-12-15 22:29:02 +09001713// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001714func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001715 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001716 if minSdkVersion == "apex_inherit" {
1717 return nil
1718 }
1719 if minSdkVersion == "" {
1720 return fmt.Errorf("min_sdk_version is not specificed")
1721 }
1722
1723 // Not using nativeApiLevelFromUser because the context here is not
1724 // necessarily a native context.
1725 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1726 if err != nil {
1727 return err
1728 }
1729
1730 if ver.GreaterThan(sdkVersion) {
1731 return fmt.Errorf("newer SDK(%v)", ver)
1732 }
Jiyong Park99644e92020-11-17 22:21:02 +09001733 return nil
1734}
1735
Jiyong Park45bf82e2020-12-15 22:29:02 +09001736// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001737func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1738 depTag := ctx.OtherModuleDependencyTag(dep)
1739
1740 if ccm, ok := dep.(*cc.Module); ok {
1741 if ccm.HasStubsVariants() {
1742 if cc.IsSharedDepTag(depTag) {
1743 // dynamic dep to a stubs lib crosses APEX boundary
1744 return false
1745 }
1746 if cc.IsRuntimeDepTag(depTag) {
1747 // runtime dep to a stubs lib also crosses APEX boundary
1748 return false
1749 }
1750
1751 if cc.IsHeaderDepTag(depTag) {
1752 return false
1753 }
1754 }
1755 if mod.Static() && cc.IsSharedDepTag(depTag) {
1756 // shared_lib dependency from a static lib is considered as crossing
1757 // the APEX boundary because the dependency doesn't actually is
1758 // linked; the dependency is used only during the compilation phase.
1759 return false
1760 }
1761 }
1762
Matthew Maurer581b6d82022-09-29 16:46:25 -07001763 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001764 return false
1765 }
1766
1767 return true
1768}
1769
1770// Overrides ApexModule.IsInstallabeToApex()
1771func (mod *Module) IsInstallableToApex() bool {
1772 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001773 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001774 return true
1775 }
1776 if _, ok := mod.compiler.(*binaryDecorator); ok {
1777 return true
1778 }
1779 }
1780 return false
1781}
1782
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001783// If a library file has a "lib" prefix, extract the library name without the prefix.
1784func libNameFromFilePath(filepath android.Path) (string, bool) {
1785 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1786 if strings.HasPrefix(libName, "lib") {
1787 libName = libName[3:]
1788 return libName, true
1789 }
1790 return "", false
1791}
1792
Sasha Smundaka76acba2022-04-18 20:12:56 -07001793func kytheExtractRustFactory() android.Singleton {
1794 return &kytheExtractRustSingleton{}
1795}
1796
1797type kytheExtractRustSingleton struct {
1798}
1799
1800func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1801 var xrefTargets android.Paths
1802 ctx.VisitAllModules(func(module android.Module) {
1803 if rustModule, ok := module.(xref); ok {
1804 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1805 }
1806 })
1807 if len(xrefTargets) > 0 {
1808 ctx.Phony("xref_rust", xrefTargets...)
1809 }
1810}
1811
Jihoon Kangf78a8902022-09-01 22:47:07 +00001812func (c *Module) Partition() string {
1813 return ""
1814}
1815
Ivan Lozanoffee3342019-08-27 12:03:00 -07001816var Bool = proptools.Bool
1817var BoolDefault = proptools.BoolDefault
1818var String = proptools.String
1819var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001820
1821var _ android.OutputFileProducer = (*Module)(nil)