blob: 3d51a1373580a9a2d5652386415a037139ce50c4 [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
Vinh Tranb4bb20f2023-08-24 11:10:01 -040021 "android/soong/bazel"
Sasha Smundaka76acba2022-04-18 20:12:56 -070022 "android/soong/bloaty"
Aditya Choudhary87b2ab22023-11-17 15:27:06 +000023 "android/soong/testing"
Vinh Tranbcb5f572023-08-24 11:10:01 -040024 "android/soong/ui/metrics/bp2build_metrics_proto"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025
26 "github.com/google/blueprint"
27 "github.com/google/blueprint/proptools"
28
29 "android/soong/android"
30 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020031 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070032 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000033 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070034 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040035 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070036)
37
38var pctx = android.NewPackageContext("android/soong/rust")
39
40func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070041 android.RegisterModuleType("rust_defaults", defaultsFactory)
42 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
43 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040044 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040045 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050046 })
47 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
48 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 })
Inseob Kim3b244062023-07-11 13:31:36 +090050 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070051 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020052 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000053 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070054}
55
56type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040057 GlobalRustFlags []string // Flags that apply globally to rust
58 GlobalLinkFlags []string // Flags that apply globally to linker
59 RustFlags []string // Flags that apply to rust
60 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070062 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070063 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040064 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020065 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070066 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070067}
68
69type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050070 AndroidMkRlibs []string `blueprint:"mutated"`
71 AndroidMkDylibs []string `blueprint:"mutated"`
72 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050073 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040074
Ivan Lozano6a884432020-12-02 09:15:16 -050075 ImageVariationPrefix string `blueprint:"mutated"`
76 VndkVersion string `blueprint:"mutated"`
77 SubName string `blueprint:"mutated"`
78
Ivan Lozanoc08897c2021-04-02 12:41:32 -040079 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
80 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
81 // appended before SubName.
82 RustSubName string `blueprint:"mutated"`
83
Ivan Lozano6a884432020-12-02 09:15:16 -050084 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 CoreVariantNeeded bool `blueprint:"mutated"`
86 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070087 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080088 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050089 ExtraVariants []string `blueprint:"mutated"`
90
Ivan Lozanoa2268632021-07-22 10:52:06 -040091 // Allows this module to use non-APEX version of libraries. Useful
92 // for building binaries that are started before APEXes are activated.
93 Bootstrap *bool
94
Ivan Lozano1921e802021-05-20 13:39:16 -040095 // Used by vendor snapshot to record dependencies from snapshot modules.
96 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090097 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040098 SnapshotRlibs []string `blueprint:"mutated"`
99 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -0400100
Matthew Maurerc6868382021-07-13 14:12:37 -0700101 // Make this module available when building for ramdisk.
102 // On device without a dedicated recovery partition, the module is only
103 // available after switching root into
104 // /first_stage_ramdisk. To expose the module before switching root, install
105 // the recovery variant instead.
106 Ramdisk_available *bool
107
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500108 // Make this module available when building for vendor ramdisk.
109 // On device without a dedicated recovery partition, the module is only
110 // available after switching root into
111 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800112 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500113 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400114
Ivan Lozano1921e802021-05-20 13:39:16 -0400115 // Normally Soong uses the directory structure to decide which modules
116 // should be included (framework) or excluded (non-framework) from the
117 // different snapshots (vendor, recovery, etc.), but this property
118 // allows a partner to exclude a module normally thought of as a
119 // framework module from the vendor snapshot.
120 Exclude_from_vendor_snapshot *bool
121
122 // Normally Soong uses the directory structure to decide which modules
123 // should be included (framework) or excluded (non-framework) from the
124 // different snapshots (vendor, recovery, etc.), but this property
125 // allows a partner to exclude a module normally thought of as a
126 // framework module from the recovery snapshot.
127 Exclude_from_recovery_snapshot *bool
128
Matthew Maurer460ee942021-02-11 12:31:46 -0800129 // Make this module available when building for recovery
130 Recovery_available *bool
131
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500132 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
133 Min_sdk_version *string
134
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900135 HideFromMake bool `blueprint:"mutated"`
136 PreventInstall bool `blueprint:"mutated"`
137
138 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139}
140
141type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700142 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143
Ivan Lozano6a884432020-12-02 09:15:16 -0500144 VendorProperties cc.VendorProperties
145
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 Properties BaseProperties
147
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000148 hod android.HostOrDeviceSupported
149 multilib android.Multilib
150 testModule bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151
Ivan Lozano6a884432020-12-02 09:15:16 -0500152 makeLinkType string
153
Yi Kong46c6e592022-01-20 22:55:00 +0800154 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400156 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200157 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500158 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400160 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700161 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400162
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400163 // Output file to be installed, may be stripped or unstripped.
164 outputFile android.OptionalPath
165
Sasha Smundaka76acba2022-04-18 20:12:56 -0700166 // Cross-reference input file
167 kytheFiles android.Paths
168
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400169 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900170
171 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500172
173 // For apex variants, this is set as apex.min_sdk_version
174 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700175
176 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400177
178 android.BazelModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179}
180
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500181func (mod *Module) Header() bool {
182 //TODO: If Rust libraries provide header variants, this needs to be updated.
183 return false
184}
185
186func (mod *Module) SetPreventInstall() {
187 mod.Properties.PreventInstall = true
188}
189
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500190func (mod *Module) SetHideFromMake() {
191 mod.Properties.HideFromMake = true
192}
193
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900194func (mod *Module) HiddenFromMake() bool {
195 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400196}
197
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500198func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500199 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
200 // nil since we need compiler to actually sanitize.
201 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202}
203
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500204func (mod *Module) IsPrebuilt() bool {
205 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
206 return true
207 }
208 return false
209}
210
Ivan Lozano43845682020-07-09 21:03:28 -0400211func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
212 switch tag {
213 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700214 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400215 return mod.sourceProvider.Srcs(), nil
216 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900217 if mod.OutputFile().Valid() {
218 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400219 }
220 return android.Paths{}, nil
221 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500222 case "unstripped":
223 if mod.compiler != nil {
224 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
225 }
226 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400227 default:
228 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
229 }
230}
231
Ivan Lozano52767be2019-10-18 14:49:46 -0700232func (mod *Module) SelectedStl() string {
233 return ""
234}
235
Ivan Lozano2b262972019-11-21 12:30:50 -0800236func (mod *Module) NonCcVariants() bool {
237 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400238 if _, ok := mod.compiler.(libraryInterface); ok {
239 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800240 }
241 }
242 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
243}
244
Ivan Lozano52767be2019-10-18 14:49:46 -0700245func (mod *Module) Static() bool {
246 if mod.compiler != nil {
247 if library, ok := mod.compiler.(libraryInterface); ok {
248 return library.static()
249 }
250 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400251 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700252}
253
254func (mod *Module) Shared() bool {
255 if mod.compiler != nil {
256 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400257 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700258 }
259 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400260 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700261}
262
Ivan Lozanod7586b62021-04-01 09:49:36 -0400263func (mod *Module) Dylib() bool {
264 if mod.compiler != nil {
265 if library, ok := mod.compiler.(libraryInterface); ok {
266 return library.dylib()
267 }
268 }
269 return false
270}
271
Ivan Lozanod106efe2023-09-21 23:30:26 -0400272func (mod *Module) Source() bool {
273 if mod.compiler != nil {
274 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
275 return library.source()
276 }
277 }
278 return false
279}
280
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400281func (mod *Module) RlibStd() bool {
282 if mod.compiler != nil {
283 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
284 return library.rlibStd()
285 }
286 }
287 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
288}
289
Ivan Lozanod7586b62021-04-01 09:49:36 -0400290func (mod *Module) Rlib() bool {
291 if mod.compiler != nil {
292 if library, ok := mod.compiler.(libraryInterface); ok {
293 return library.rlib()
294 }
295 }
296 return false
297}
298
299func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400300 if binary, ok := mod.compiler.(binaryInterface); ok {
301 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302 }
303 return false
304}
305
Justin Yun5e035862021-06-29 20:50:37 +0900306func (mod *Module) StaticExecutable() bool {
307 if !mod.Binary() {
308 return false
309 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400310 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900311}
312
Ivan Lozanod7586b62021-04-01 09:49:36 -0400313func (mod *Module) Object() bool {
314 // Rust has no modules which produce only object files.
315 return false
316}
317
Ivan Lozano52767be2019-10-18 14:49:46 -0700318func (mod *Module) Toc() android.OptionalPath {
319 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400320 if lib, ok := mod.compiler.(libraryInterface); ok {
321 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 }
323 }
324 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
325}
326
Colin Crossc511bc52020-04-07 16:50:32 +0000327func (mod *Module) UseSdk() bool {
328 return false
329}
330
Ivan Lozanod7586b62021-04-01 09:49:36 -0400331func (mod *Module) RelativeInstallPath() string {
332 if mod.compiler != nil {
333 return mod.compiler.relativeInstallPath()
334 }
335 return ""
336}
337
Ivan Lozano52767be2019-10-18 14:49:46 -0700338func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500339 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700340}
341
Jiyong Park7d55b612021-06-11 17:22:09 +0900342func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400343 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900344}
345
Ivan Lozano52767be2019-10-18 14:49:46 -0700346func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400347 return true
348}
349
350func (mod *Module) SubName() string {
351 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700352}
353
354func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500355 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700356 return false
357}
358
Ivan Lozanof9e21722020-12-02 09:00:51 -0500359func (mod *Module) IsVndkExt() bool {
360 return false
361}
362
Ivan Lozanod7586b62021-04-01 09:49:36 -0400363func (mod *Module) IsVndkSp() bool {
364 return false
365}
366
Ivan Lozanof1868af2022-04-12 13:08:36 -0400367func (mod *Module) IsVndkPrebuiltLibrary() bool {
368 // Rust modules do not provide VNDK prebuilts
369 return false
370}
371
372func (mod *Module) IsVendorPublicLibrary() bool {
373 return mod.VendorProperties.IsVendorPublicLibrary
374}
375
376func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
377 // Rust modules to not provide Sdk variants
378 return false
379}
380
Colin Cross127bb8b2020-12-16 16:46:01 -0800381func (c *Module) IsVndkPrivate() bool {
382 return false
383}
384
385func (c *Module) IsLlndk() bool {
386 return false
387}
388
389func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500390 return false
391}
392
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400393func (mod *Module) KernelHeadersDecorator() bool {
394 return false
395}
396
Colin Cross1f3f1302021-04-26 18:37:44 -0700397func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400398 return false
399}
400
Colin Cross5271fea2021-04-27 13:06:04 -0700401func (m *Module) NeedsVendorPublicLibraryVariants() bool {
402 return false
403}
404
Ivan Lozanod7586b62021-04-01 09:49:36 -0400405func (mod *Module) HasLlndkStubs() bool {
406 return false
407}
408
409func (mod *Module) StubsVersion() string {
410 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
411}
412
Ivan Lozano52767be2019-10-18 14:49:46 -0700413func (mod *Module) SdkVersion() string {
414 return ""
415}
416
Colin Crossc511bc52020-04-07 16:50:32 +0000417func (mod *Module) AlwaysSdk() bool {
418 return false
419}
420
Jiyong Park2286afd2020-06-16 21:58:53 +0900421func (mod *Module) IsSdkVariant() bool {
422 return false
423}
424
Colin Cross1348ce32020-10-01 13:37:16 -0700425func (mod *Module) SplitPerApiLevel() bool {
426 return false
427}
428
Sasha Smundaka76acba2022-04-18 20:12:56 -0700429func (mod *Module) XrefRustFiles() android.Paths {
430 return mod.kytheFiles
431}
432
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400434 Dylibs []string
435 Rlibs []string
436 Rustlibs []string
437 Stdlibs []string
438 ProcMacros []string
439 SharedLibs []string
440 StaticLibs []string
441 WholeStaticLibs []string
442 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400444 // Used for data dependencies adjacent to tests
445 DataLibs []string
446 DataBins []string
447
Colin Crossfe605e12022-01-23 20:46:16 -0800448 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449}
450
451type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700452 DyLibs RustLibraries
453 RLibs RustLibraries
454 SharedLibs android.Paths
455 SharedLibDeps android.Paths
456 StaticLibs android.Paths
457 ProcMacros RustLibraries
458 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500459
460 // depFlags and depLinkFlags are rustc and linker (clang) flags.
461 depFlags []string
462 depLinkFlags []string
463
464 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
465 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000466 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700467 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700468
Ivan Lozano45901ed2020-07-24 16:05:01 -0400469 // Used by bindgen modules which call clang
470 depClangFlags []string
471 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400472 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400473 depSystemIncludePaths android.Paths
474
Colin Crossfe605e12022-01-23 20:46:16 -0800475 CrtBegin android.Paths
476 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700477
478 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500479 SrcDeps android.Paths
480 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481}
482
483type RustLibraries []RustLibrary
484
485type RustLibrary struct {
486 Path android.Path
487 CrateName string
488}
489
Matthew Maurerbb3add12020-06-25 09:34:12 -0700490type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000491 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700492 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700493}
494
Sasha Smundaka76acba2022-04-18 20:12:56 -0700495type xref interface {
496 XrefRustFiles() android.Paths
497}
498
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000500 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700501 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700502}
503
Wen-yi Chu41326c12023-09-22 03:58:59 +0000504func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
505 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700506}
507
Colin Cross004bd3f2023-10-02 11:39:17 -0700508func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
509 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400510}
511
Colin Cross0de8a1e2020-09-18 14:15:30 -0700512func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
513 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700514 LinkDirs: flagExporter.linkDirs,
515 LinkObjects: flagExporter.linkObjects,
516 })
517}
518
Matthew Maurerbb3add12020-06-25 09:34:12 -0700519var _ exportedFlagsProducer = (*flagExporter)(nil)
520
521func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700522 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700523}
524
Colin Cross0de8a1e2020-09-18 14:15:30 -0700525type FlagExporterInfo struct {
526 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000527 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700528 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700529}
530
531var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
532
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533func (mod *Module) isCoverageVariant() bool {
534 return mod.coverage.Properties.IsCoverageVariant
535}
536
537var _ cc.Coverage = (*Module)(nil)
538
539func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
540 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
541}
542
Ivan Lozanod7586b62021-04-01 09:49:36 -0400543func (mod *Module) VndkVersion() string {
544 return mod.Properties.VndkVersion
545}
546
547func (mod *Module) PreventInstall() bool {
548 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400549}
550
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400551func (mod *Module) MarkAsCoverageVariant(coverage bool) {
552 mod.coverage.Properties.IsCoverageVariant = coverage
553}
554
555func (mod *Module) EnableCoverageIfNeeded() {
556 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557}
558
559func defaultsFactory() android.Module {
560 return DefaultsFactory()
561}
562
563type Defaults struct {
564 android.ModuleBase
565 android.DefaultsModuleBase
566}
567
568func DefaultsFactory(props ...interface{}) android.Module {
569 module := &Defaults{}
570
571 module.AddProperties(props...)
572 module.AddProperties(
573 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800574 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500575 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100576 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400577 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578 &BaseCompilerProperties{},
579 &BinaryCompilerProperties{},
580 &LibraryCompilerProperties{},
581 &ProcMacroCompilerProperties{},
582 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400583 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700584 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400585 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400586 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200587 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500588 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000589 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590 )
591
592 android.InitDefaultsModule(module)
593 return module
594}
595
596func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700597 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598}
599
Ivan Lozano183a3212019-10-18 14:18:45 -0700600func (mod *Module) CcLibrary() bool {
601 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500602 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700603 return true
604 }
605 }
606 return false
607}
608
609func (mod *Module) CcLibraryInterface() bool {
610 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400611 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
612 // VariantIs{Static,Shared} is set.
613 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700614 return true
615 }
616 }
617 return false
618}
619
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400620func (mod *Module) RustLibraryInterface() bool {
621 if mod.compiler != nil {
622 if _, ok := mod.compiler.(libraryInterface); ok {
623 return true
624 }
625 }
626 return false
627}
628
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500629func (mod *Module) IsFuzzModule() bool {
630 if _, ok := mod.compiler.(*fuzzDecorator); ok {
631 return true
632 }
633 return false
634}
635
636func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
637 return mod.FuzzModule
638}
639
640func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
641 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
642 return fuzzer.fuzzPackagedModule
643 }
644 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
645}
646
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000647func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500648 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
649 return fuzzer.sharedLibraries
650 }
651 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
652}
653
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400654func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400655 if mod.compiler != nil {
656 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400657 }
658 return nil
659}
660
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800661func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700662 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700663 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800664 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700665 }
666 }
667 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
668}
669
670func (mod *Module) SetStatic() {
671 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700672 if library, ok := mod.compiler.(libraryInterface); ok {
673 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700674 return
675 }
676 }
677 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
678}
679
680func (mod *Module) SetShared() {
681 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700682 if library, ok := mod.compiler.(libraryInterface); ok {
683 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700684 return
685 }
686 }
687 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
688}
689
Ivan Lozano183a3212019-10-18 14:18:45 -0700690func (mod *Module) BuildStaticVariant() bool {
691 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700692 if library, ok := mod.compiler.(libraryInterface); ok {
693 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700694 }
695 }
696 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
697}
698
699func (mod *Module) BuildSharedVariant() bool {
700 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700701 if library, ok := mod.compiler.(libraryInterface); ok {
702 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700703 }
704 }
705 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
706}
707
Ivan Lozano183a3212019-10-18 14:18:45 -0700708func (mod *Module) Module() android.Module {
709 return mod
710}
711
Ivan Lozano183a3212019-10-18 14:18:45 -0700712func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400713 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700714}
715
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400716func (mod *Module) CoverageFiles() android.Paths {
717 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800718 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400719 }
720 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
721}
722
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400723// Rust does not produce gcno files, and therefore does not produce a coverage archive.
724func (mod *Module) CoverageOutputFile() android.OptionalPath {
725 return android.OptionalPath{}
726}
727
728func (mod *Module) IsNdk(config android.Config) bool {
729 return false
730}
731
732func (mod *Module) IsStubs() bool {
733 return false
734}
735
Jiyong Park459feca2020-12-15 11:02:21 +0900736func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900737 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900738 return false
739 }
740
Jiyong Park459feca2020-12-15 11:02:21 +0900741 // The apex variant is not installable because it is included in the APEX and won't appear
742 // in the system partition as a standalone file.
743 if !apexInfo.IsForPlatform() {
744 return false
745 }
746
Jiyong Parke54f07e2021-04-07 15:08:04 +0900747 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900748}
749
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500750func (ctx moduleContext) apexVariationName() string {
751 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
752}
753
Ivan Lozano183a3212019-10-18 14:18:45 -0700754var _ cc.LinkableInterface = (*Module)(nil)
755
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756func (mod *Module) Init() android.Module {
757 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500758 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759
Yi Kong46c6e592022-01-20 22:55:00 +0800760 if mod.afdo != nil {
761 mod.AddProperties(mod.afdo.props()...)
762 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700763 if mod.compiler != nil {
764 mod.AddProperties(mod.compiler.compilerProps()...)
765 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766 if mod.coverage != nil {
767 mod.AddProperties(mod.coverage.props()...)
768 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200769 if mod.clippy != nil {
770 mod.AddProperties(mod.clippy.props()...)
771 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400772 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700773 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400774 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500775 if mod.sanitize != nil {
776 mod.AddProperties(mod.sanitize.props()...)
777 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778
Ivan Lozanoffee3342019-08-27 12:03:00 -0700779 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900780 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781
782 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 return mod
784}
785
786func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
787 return &Module{
788 hod: hod,
789 multilib: multilib,
790 }
791}
792func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
793 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800794 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400795 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200796 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500797 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798 return module
799}
800
801type ModuleContext interface {
802 android.ModuleContext
803 ModuleContextIntf
804}
805
806type BaseModuleContext interface {
807 android.BaseModuleContext
808 ModuleContextIntf
809}
810
811type DepsContext interface {
812 android.BottomUpMutatorContext
813 ModuleContextIntf
814}
815
816type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200817 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819}
820
821type depsContext struct {
822 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823}
824
825type moduleContext struct {
826 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827}
828
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200829type baseModuleContext struct {
830 android.BaseModuleContext
831}
832
833func (ctx *moduleContext) RustModule() *Module {
834 return ctx.Module().(*Module)
835}
836
837func (ctx *moduleContext) toolchain() config.Toolchain {
838 return ctx.RustModule().toolchain(ctx)
839}
840
841func (ctx *depsContext) RustModule() *Module {
842 return ctx.Module().(*Module)
843}
844
845func (ctx *depsContext) toolchain() config.Toolchain {
846 return ctx.RustModule().toolchain(ctx)
847}
848
849func (ctx *baseModuleContext) RustModule() *Module {
850 return ctx.Module().(*Module)
851}
852
853func (ctx *baseModuleContext) toolchain() config.Toolchain {
854 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400855}
856
857func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700858 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
859 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
860 return false
861 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400862 return mod.compiler != nil && mod.compiler.nativeCoverage()
863}
864
Ivan Lozanod7586b62021-04-01 09:49:36 -0400865func (mod *Module) EverInstallable() bool {
866 return mod.compiler != nil &&
867 // Check to see whether the module is actually ever installable.
868 mod.compiler.everInstallable()
869}
870
871func (mod *Module) Installable() *bool {
872 return mod.Properties.Installable
873}
874
Ivan Lozano872d5792022-03-23 17:31:39 -0400875func (mod *Module) ProcMacro() bool {
876 if pm, ok := mod.compiler.(procMacroInterface); ok {
877 return pm.ProcMacro()
878 }
879 return false
880}
881
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
883 if mod.cachedToolchain == nil {
884 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
885 }
886 return mod.cachedToolchain
887}
888
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200889func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
890 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
891}
892
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
894}
895
896func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
897 ctx := &moduleContext{
898 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900
Jiyong Park99644e92020-11-17 22:21:02 +0900901 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
902 if !apexInfo.IsForPlatform() {
903 mod.hideApexVariantFromMake = true
904 }
905
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500907 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
908
Ivan Lozanof1868af2022-04-12 13:08:36 -0400909 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 if !toolchain.Supported() {
912 // This toolchain's unsupported, there's nothing to do for this mod.
913 return
914 }
915
916 deps := mod.depsToPaths(ctx)
917 flags := Flags{
918 Toolchain: toolchain,
919 }
920
Ivan Lozano67eada32021-09-23 11:50:33 -0400921 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800922 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500923 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800924 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 if mod.compiler != nil {
926 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400927 flags = mod.compiler.cfgFlags(ctx, flags)
928 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400929 }
930 if mod.coverage != nil {
931 flags, deps = mod.coverage.flags(ctx, flags, deps)
932 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200933 if mod.clippy != nil {
934 flags, deps = mod.clippy.flags(ctx, flags, deps)
935 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500936 if mod.sanitize != nil {
937 flags, deps = mod.sanitize.flags(ctx, flags, deps)
938 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400939
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200940 // SourceProvider needs to call GenerateSource() before compiler calls
941 // compile() so it can provide the source. A SourceProvider has
942 // multiple variants (e.g. source, rlib, dylib). Only the "source"
943 // variant is responsible for effectively generating the source. The
944 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400945 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200946 if mod.compiler.(libraryInterface).source() {
947 mod.sourceProvider.GenerateSource(ctx, deps)
948 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
949 } else {
950 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
951 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700952 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200953 }
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000954 ctx.SetProvider(blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: mod.sourceProvider.Srcs().Strings()})
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400955 }
956
957 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100958 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700959 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400960 if ctx.Failed() {
961 return
962 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700963 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
964 if buildOutput.kytheFile != nil {
965 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
966 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400967 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900968
Dan Albert06feee92021-03-19 15:06:02 -0700969 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000970 if mod.docTimestampFile.Valid() {
971 ctx.CheckbuildFile(mod.docTimestampFile.Path())
972 }
Dan Albert06feee92021-03-19 15:06:02 -0700973
Ivan Lozano1921e802021-05-20 13:39:16 -0400974 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
975 // RECOVERY_SNAPSHOT_VERSION is current.
976 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
977 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
978 lib.collectHeadersForSnapshot(ctx, deps)
979 }
980 }
981
Jiyong Park459feca2020-12-15 11:02:21 +0900982 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400983 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900984 // If the module has been specifically configure to not be installed then
985 // hide from make as otherwise it will break when running inside make as the
986 // output path to install will not be specified. Not all uninstallable
987 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400988 // side dependencies. In particular, proc-macros need to be captured in the
989 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900990 mod.HideFromMake()
991 } else if !mod.installable(apexInfo) {
992 mod.SkipInstall()
993 }
994
995 // Still call install though, the installs will be stored as PackageSpecs to allow
996 // using the outputs in a genrule.
997 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200998 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900999 if ctx.Failed() {
1000 return
1001 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001002 }
Chris Wailes74be7642021-07-22 16:20:28 -07001003
1004 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001005 }
Aditya Choudhary87b2ab22023-11-17 15:27:06 +00001006 if mod.testModule {
1007 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
1008 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001009}
1010
1011func (mod *Module) deps(ctx DepsContext) Deps {
1012 deps := Deps{}
1013
1014 if mod.compiler != nil {
1015 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001016 }
1017 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001018 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019 }
1020
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001021 if mod.coverage != nil {
1022 deps = mod.coverage.deps(ctx, deps)
1023 }
1024
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001025 if mod.sanitize != nil {
1026 deps = mod.sanitize.deps(ctx, deps)
1027 }
1028
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1030 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001031 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1033 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1034 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001035 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001036 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001037 return deps
1038
1039}
1040
Ivan Lozanoffee3342019-08-27 12:03:00 -07001041type dependencyTag struct {
1042 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001043 name string
1044 library bool
1045 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001046 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047}
1048
Jiyong Park65b62242020-11-25 12:44:59 +09001049// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1050// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1051func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001052 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001053}
1054
1055var _ android.InstallNeededDependencyTag = dependencyTag{}
1056
Colin Cross65cb3142021-12-10 23:05:02 +00001057func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1058 if d.library && d.dynamic {
1059 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1060 }
1061 return nil
1062}
1063
1064var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1065
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001067 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1068 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001069 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001070 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001071 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001072 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001073 dataLibDepTag = dependencyTag{name: "data lib"}
1074 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075)
1076
Jiyong Park99644e92020-11-17 22:21:02 +09001077func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1078 tag, ok := depTag.(dependencyTag)
1079 return ok && tag == dylibDepTag
1080}
1081
Jiyong Park94e22fd2021-04-08 18:19:15 +09001082func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1083 tag, ok := depTag.(dependencyTag)
1084 return ok && tag == rlibDepTag
1085}
1086
Matthew Maurer0f003b12020-06-29 14:34:06 -07001087type autoDep struct {
1088 variation string
1089 depTag dependencyTag
1090}
1091
1092var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001093 rlibVariation = "rlib"
1094 dylibVariation = "dylib"
1095 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1096 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001097)
1098
1099type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001100 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001101}
1102
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001103func (mod *Module) begin(ctx BaseModuleContext) {
1104 if mod.coverage != nil {
1105 mod.coverage.begin(ctx)
1106 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001107 if mod.sanitize != nil {
1108 mod.sanitize.begin(ctx)
1109 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001110}
1111
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001112func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001113 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001114 return p.prebuilt()
1115 }
1116 return nil
1117}
1118
Justin Yun24b246a2023-03-16 10:36:16 +09001119func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1120 if rustDep, ok := dep.(*Module); ok {
1121 // Use base module name for snapshots when exporting to Makefile.
1122 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1123 baseName := rustDep.BaseModuleName()
1124 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1125 }
1126 }
1127 return cc.MakeLibName(ctx, c, dep, depName)
1128}
1129
Ivan Lozanod106efe2023-09-21 23:30:26 -04001130func collectIncludedProtos(mod *Module, dep *Module) {
1131 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1132 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1133 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1134 }
1135 }
1136}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001137func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1138 var depPaths PathDeps
1139
1140 directRlibDeps := []*Module{}
1141 directDylibDeps := []*Module{}
1142 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001143 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001144 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001145 directSrcProvidersDeps := []*Module{}
1146 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001147
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001148 // For the dependency from platform to apex, use the latest stubs
1149 mod.apexSdkVersion = android.FutureApiLevel
1150 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1151 if !apexInfo.IsForPlatform() {
1152 mod.apexSdkVersion = apexInfo.MinSdkVersion
1153 }
1154
1155 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1156 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1157 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1158 // (b/144430859)
1159 mod.apexSdkVersion = android.FutureApiLevel
1160 }
1161
Spandan Das604f3762023-03-16 22:51:40 +00001162 skipModuleList := map[string]bool{}
1163
1164 var apiImportInfo multitree.ApiImportInfo
1165 hasApiImportInfo := false
1166
1167 ctx.VisitDirectDeps(func(dep android.Module) {
1168 if dep.Name() == "api_imports" {
1169 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1170 hasApiImportInfo = true
1171 }
1172 })
1173
1174 if hasApiImportInfo {
1175 targetStubModuleList := map[string]string{}
1176 targetOrigModuleList := map[string]string{}
1177
1178 // Search for dependency which both original module and API imported library with APEX stub exists
1179 ctx.VisitDirectDeps(func(dep android.Module) {
1180 depName := ctx.OtherModuleName(dep)
1181 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1182 targetStubModuleList[apiLibrary] = depName
1183 }
1184 })
1185 ctx.VisitDirectDeps(func(dep android.Module) {
1186 depName := ctx.OtherModuleName(dep)
1187 if origLibrary, ok := targetStubModuleList[depName]; ok {
1188 targetOrigModuleList[origLibrary] = depName
1189 }
1190 })
1191
1192 // Decide which library should be used between original and API imported library
1193 ctx.VisitDirectDeps(func(dep android.Module) {
1194 depName := ctx.OtherModuleName(dep)
1195 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1196 if cc.ShouldUseStubForApex(ctx, dep) {
1197 skipModuleList[depName] = true
1198 } else {
1199 skipModuleList[apiLibrary] = true
1200 }
1201 }
1202 })
1203 }
1204
Cole Faustb6e6f992023-08-17 17:42:26 -07001205 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1206 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001207
Ivan Lozanoffee3342019-08-27 12:03:00 -07001208 ctx.VisitDirectDeps(func(dep android.Module) {
1209 depName := ctx.OtherModuleName(dep)
1210 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001211
Spandan Das604f3762023-03-16 22:51:40 +00001212 if _, exists := skipModuleList[depName]; exists {
1213 return
1214 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001215
1216 if depTag == android.DarwinUniversalVariantTag {
1217 return
1218 }
1219
Ivan Lozano89435d12020-07-31 11:01:18 -04001220 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001221 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001222 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001223
Ivan Lozanoffee3342019-08-27 12:03:00 -07001224 switch depTag {
1225 case dylibDepTag:
1226 dylib, ok := rustDep.compiler.(libraryInterface)
1227 if !ok || !dylib.dylib() {
1228 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1229 return
1230 }
1231 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001232 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001233 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1234
Ivan Lozanoffee3342019-08-27 12:03:00 -07001235 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001236
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237 rlib, ok := rustDep.compiler.(libraryInterface)
1238 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001239 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001240 return
1241 }
1242 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001243 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001244 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1245
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 case procMacroDepTag:
1247 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001248 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001249
1250 case sourceDepTag:
1251 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1252 collectIncludedProtos(mod, rustDep)
1253 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001254 }
1255
Cole Faustb6e6f992023-08-17 17:42:26 -07001256 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1257
Paul Duffind5cf92e2021-07-09 17:38:55 +01001258 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001259 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1260 // OS/Arch variant is used.
1261 var helper string
1262 if ctx.Host() {
1263 helper = "missing 'host_supported'?"
1264 } else {
1265 helper = "device module defined?"
1266 }
1267
1268 if dep.Target().Os != ctx.Os() {
1269 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1270 return
1271 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1272 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1273 return
1274 }
1275 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001276 }
1277
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001278 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001279 if depTag != procMacroDepTag {
1280 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1281 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1282 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1283 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
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()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001288 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001289 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001290 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001291 }
1292 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001293 if depTag == sourceDepTag {
1294 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1295 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
1296 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1297 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1298 }
1299 }
1300 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001301 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001302 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001303 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001304 if _, ok := ccDep.(*Module); !ok {
1305 if ccDep.Module().Target().Os != ctx.Os() {
1306 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1307 return
1308 }
1309 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1310 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1311 return
1312 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001313 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001314 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001315 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001316 if !ctx.Config().AllowMissingDependencies() {
1317 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1318 } else {
1319 ctx.AddMissingDependencies([]string{depName})
1320 }
1321 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001322 }
1323
Wen-yi Chu41326c12023-09-22 03:58:59 +00001324 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001325
Ivan Lozanoffee3342019-08-27 12:03:00 -07001326 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001327 switch {
1328 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001329 if cc.IsWholeStaticLib(depTag) {
1330 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1331 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001332 if mod.Binary() {
1333 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1334 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1335 // final linkage, pass the args directly to the linker to handle these cases.
1336 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1337 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001338 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001339 } else {
1340 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 -05001341 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001342 }
1343
1344 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1345 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001346 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001347 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1348
Colin Cross0de8a1e2020-09-18 14:15:30 -07001349 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1350 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1351 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1352 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1353 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001354 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001355
1356 // Record baseLibName for snapshots.
1357 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1358
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001359 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001360 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001361 // For the shared lib dependencies, we may link to the stub variant
1362 // of the dependency depending on the context (e.g. if this
1363 // dependency crosses the APEX boundaries).
1364 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1365
1366 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1367 // object (either from stub or non-stub) to use.
1368 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001369 if !linkObject.Valid() {
1370 if !ctx.Config().AllowMissingDependencies() {
1371 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1372 } else {
1373 ctx.AddMissingDependencies([]string{depName})
1374 }
1375 return
1376 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001377 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001378
Ivan Lozanoffee3342019-08-27 12:03:00 -07001379 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001380 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001381 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1382 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1383 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1384 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001385 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001386
1387 // Record baseLibName for snapshots.
1388 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1389
Cole Faustb6e6f992023-08-17 17:42:26 -07001390 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001391 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001392 case cc.IsHeaderDepTag(depTag):
1393 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1394 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1395 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1396 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001397 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001398 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001399 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001400 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001401 }
1402
1403 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001404 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1405 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001406 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001407 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001408 } else {
1409 switch {
1410 case depTag == cc.CrtBeginDepTag:
1411 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1412 case depTag == cc.CrtEndDepTag:
1413 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1414 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001415 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001416
1417 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001418 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001419 // These are usually genrules which don't have per-target variants.
1420 directSrcDeps = append(directSrcDeps, srcDep)
1421 }
1422 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001423 })
1424
Wen-yi Chu41326c12023-09-22 03:58:59 +00001425 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1426
1427 var rlibDepFiles RustLibraries
1428 for _, dep := range directRlibDeps {
1429 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1430 }
1431 var dylibDepFiles RustLibraries
1432 for _, dep := range directDylibDeps {
1433 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1434 }
1435 var procMacroDepFiles RustLibraries
1436 for _, dep := range directProcMacroDeps {
1437 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1438 }
1439
Colin Cross004bd3f2023-10-02 11:39:17 -07001440 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001441 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001442 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001443 }
1444
Colin Cross004bd3f2023-10-02 11:39:17 -07001445 var sharedLibFiles android.Paths
1446 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001448 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001449 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001450 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001451 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001452 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001453 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001454 }
1455
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001456 var srcProviderDepFiles android.Paths
1457 for _, dep := range directSrcProvidersDeps {
1458 srcs, _ := dep.OutputFiles("")
1459 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1460 }
1461 for _, dep := range directSrcDeps {
1462 srcs := dep.Srcs()
1463 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1464 }
1465
Wen-yi Chu41326c12023-09-22 03:58:59 +00001466 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1467 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001468 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1469 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1470 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001471 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001472 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001473
1474 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001475 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001476 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001477 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001478 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1479 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1480 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001481
1482 return depPaths
1483}
1484
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001485func (mod *Module) InstallInData() bool {
1486 if mod.compiler == nil {
1487 return false
1488 }
1489 return mod.compiler.inData()
1490}
1491
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001492func (mod *Module) InstallInRamdisk() bool {
1493 return mod.InRamdisk()
1494}
1495
1496func (mod *Module) InstallInVendorRamdisk() bool {
1497 return mod.InVendorRamdisk()
1498}
1499
1500func (mod *Module) InstallInRecovery() bool {
1501 return mod.InRecovery()
1502}
1503
Wen-yi Chu41326c12023-09-22 03:58:59 +00001504func linkPathFromFilePath(filepath android.Path) string {
1505 return strings.Split(filepath.String(), filepath.Base())[0]
1506}
1507
Spandan Das604f3762023-03-16 22:51:40 +00001508// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1509func (r *Module) usePublicApi() bool {
1510 return r.Device() && r.UseSdk()
1511}
1512
1513// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1514func (r *Module) useVendorApi() bool {
1515 return r.Device() && (r.InVendor() || r.InProduct())
1516}
1517
Ivan Lozanoffee3342019-08-27 12:03:00 -07001518func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1519 ctx := &depsContext{
1520 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001521 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001522
1523 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001524 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001525 var snapshotInfo *cc.SnapshotInfo
1526
Kiyoung Kim487689e2022-07-26 09:48:22 +09001527 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001528 if mod.usePublicApi() || mod.useVendorApi() {
1529 for idx, lib := range deps.SharedLibs {
1530 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1531 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001532 }
1533
Ivan Lozano1921e802021-05-20 13:39:16 -04001534 if ctx.Os() == android.Android {
1535 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1536 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001537
Ivan Lozano2b081132020-09-08 12:46:52 -04001538 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001539 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001540 stdLinkage = "rlib-std"
1541 }
1542
1543 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001544
Ivan Lozano2b081132020-09-08 12:46:52 -04001545 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1546 rlibDepVariations = append(rlibDepVariations,
1547 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1548 }
1549
Ivan Lozano1921e802021-05-20 13:39:16 -04001550 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001551 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001552 for _, lib := range deps.Rlibs {
1553 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001554 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001555
Ivan Lozano2d407632022-04-07 12:59:11 -04001556 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001557 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001558
1559 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001560 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1561 for _, lib := range deps.Dylibs {
1562 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1563 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001564
Ivan Lozano1921e802021-05-20 13:39:16 -04001565 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001566 if deps.Rustlibs != nil {
1567 if !mod.compiler.Disabled() {
1568 for _, lib := range deps.Rustlibs {
1569 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1570 if autoDep.depTag == rlibDepTag {
1571 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001572 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001573 } else {
1574 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1575 // Check for the existence of the dylib deptag variant. Select it if available,
1576 // otherwise select the rlib variant.
1577 autoDepVariations := append(commonDepVariations,
1578 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1579
1580 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1581
1582 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1583 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1584 } else {
1585 // If there's no dylib dependency available, try to add the rlib dependency instead.
1586 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1587 }
1588 }
1589 }
1590 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1591 for _, lib := range deps.Rustlibs {
1592 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1593 srcProviderVariations := append(commonDepVariations,
1594 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1595
1596 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1597 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001598 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001599 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001600 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001601 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001602
Ivan Lozano1921e802021-05-20 13:39:16 -04001603 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001604 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001605 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001606 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001607 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001608 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001609 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001610 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001611 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001612 for _, lib := range deps.Stdlibs {
1613 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1614 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001615 }
1616 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001617
1618 for _, lib := range deps.SharedLibs {
1619 depTag := cc.SharedDepTag()
1620 name, version := cc.StubsLibNameAndVersion(lib)
1621
1622 variations := []blueprint.Variation{
1623 {Mutator: "link", Variation: "shared"},
1624 }
Spandan Das604f3762023-03-16 22:51:40 +00001625 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1626 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1627 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1628 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1629 }
1630
1631 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1632 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1633 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001634 }
1635
1636 for _, lib := range deps.WholeStaticLibs {
1637 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001638 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001639
1640 actx.AddVariationDependencies([]blueprint.Variation{
1641 {Mutator: "link", Variation: "static"},
1642 }, depTag, lib)
1643 }
1644
1645 for _, lib := range deps.StaticLibs {
1646 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001647 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001648
1649 actx.AddVariationDependencies([]blueprint.Variation{
1650 {Mutator: "link", Variation: "static"},
1651 }, depTag, lib)
1652 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001653
Zach Johnson3df4e632020-11-06 11:56:27 -08001654 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1655
Colin Cross565cafd2020-09-25 18:47:38 -07001656 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001657 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001658 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001659 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001660 }
Colin Crossfe605e12022-01-23 20:46:16 -08001661 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001662 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001663 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001664 }
1665
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001666 if mod.sourceProvider != nil {
1667 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1668 bindgen.Properties.Custom_bindgen != "" {
1669 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1670 bindgen.Properties.Custom_bindgen)
1671 }
1672 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001673
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001674 actx.AddVariationDependencies([]blueprint.Variation{
1675 {Mutator: "link", Variation: "shared"},
1676 }, dataLibDepTag, deps.DataLibs...)
1677
1678 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1679
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001680 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001681 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001682
1683 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001684}
1685
Ivan Lozano2d407632022-04-07 12:59:11 -04001686// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001687func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1688 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001689 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1690}
1691
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001692func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1693 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1694 actx.AddVariationDependencies(variations, depTag, lib)
1695}
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)
Ivan Lozano872d5792022-03-23 17:31:39 -04001728var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001729
1730func (mod *Module) HostToolPath() android.OptionalPath {
1731 if !mod.Host() {
1732 return android.OptionalPath{}
1733 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001734 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1735 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001736 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1737 // Even though proc-macros aren't strictly "tools", since they target the compiler
1738 // and act as compiler plugins, we treat them similarly.
1739 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001740 }
1741 return android.OptionalPath{}
1742}
1743
Jiyong Park99644e92020-11-17 22:21:02 +09001744var _ android.ApexModule = (*Module)(nil)
1745
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001746func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001747 return String(mod.Properties.Min_sdk_version)
1748}
1749
Jiyong Park45bf82e2020-12-15 22:29:02 +09001750// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001751func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001752 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001753 if minSdkVersion == "apex_inherit" {
1754 return nil
1755 }
1756 if minSdkVersion == "" {
1757 return fmt.Errorf("min_sdk_version is not specificed")
1758 }
1759
1760 // Not using nativeApiLevelFromUser because the context here is not
1761 // necessarily a native context.
1762 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1763 if err != nil {
1764 return err
1765 }
1766
1767 if ver.GreaterThan(sdkVersion) {
1768 return fmt.Errorf("newer SDK(%v)", ver)
1769 }
Jiyong Park99644e92020-11-17 22:21:02 +09001770 return nil
1771}
1772
Jiyong Park45bf82e2020-12-15 22:29:02 +09001773// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001774func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1775 depTag := ctx.OtherModuleDependencyTag(dep)
1776
1777 if ccm, ok := dep.(*cc.Module); ok {
1778 if ccm.HasStubsVariants() {
1779 if cc.IsSharedDepTag(depTag) {
1780 // dynamic dep to a stubs lib crosses APEX boundary
1781 return false
1782 }
1783 if cc.IsRuntimeDepTag(depTag) {
1784 // runtime dep to a stubs lib also crosses APEX boundary
1785 return false
1786 }
1787
1788 if cc.IsHeaderDepTag(depTag) {
1789 return false
1790 }
1791 }
1792 if mod.Static() && cc.IsSharedDepTag(depTag) {
1793 // shared_lib dependency from a static lib is considered as crossing
1794 // the APEX boundary because the dependency doesn't actually is
1795 // linked; the dependency is used only during the compilation phase.
1796 return false
1797 }
1798 }
1799
Matthew Maurer581b6d82022-09-29 16:46:25 -07001800 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001801 return false
1802 }
1803
1804 return true
1805}
1806
1807// Overrides ApexModule.IsInstallabeToApex()
1808func (mod *Module) IsInstallableToApex() bool {
1809 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001810 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001811 return true
1812 }
1813 if _, ok := mod.compiler.(*binaryDecorator); ok {
1814 return true
1815 }
1816 }
1817 return false
1818}
1819
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001820// If a library file has a "lib" prefix, extract the library name without the prefix.
1821func libNameFromFilePath(filepath android.Path) (string, bool) {
1822 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1823 if strings.HasPrefix(libName, "lib") {
1824 libName = libName[3:]
1825 return libName, true
1826 }
1827 return "", false
1828}
1829
Sasha Smundaka76acba2022-04-18 20:12:56 -07001830func kytheExtractRustFactory() android.Singleton {
1831 return &kytheExtractRustSingleton{}
1832}
1833
1834type kytheExtractRustSingleton struct {
1835}
1836
1837func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1838 var xrefTargets android.Paths
1839 ctx.VisitAllModules(func(module android.Module) {
1840 if rustModule, ok := module.(xref); ok {
1841 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1842 }
1843 })
1844 if len(xrefTargets) > 0 {
1845 ctx.Phony("xref_rust", xrefTargets...)
1846 }
1847}
1848
Jihoon Kangf78a8902022-09-01 22:47:07 +00001849func (c *Module) Partition() string {
1850 return ""
1851}
1852
Chris Parsons637458d2023-09-19 20:09:00 +00001853func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001854 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1855 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001856 } else if ctx.ModuleType() == "rust_proc_macro" {
1857 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001858 } else if ctx.ModuleType() == "rust_binary_host" {
1859 binaryBp2build(ctx, m)
Vinh Tran47faaad2023-09-25 14:47:19 -04001860 } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" {
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001861 protoLibraryBp2build(ctx, m)
Vinh Tran75159942023-10-17 16:28:04 -04001862 } else if ctx.ModuleType() == "rust_ffi_static" {
1863 ffiStaticBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001864 } else {
1865 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1866 }
1867}
1868
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001869// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001870// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001871// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1872// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1873// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001874func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001875 var srcs bazel.LabelList
1876 var compileData bazel.LabelList
1877
1878 if c.Properties.Srcs[0] == "src/lib.rs" {
1879 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1880 compileData = android.BazelLabelForModuleSrc(
1881 ctx,
1882 []string{
1883 "src/**/*.proto",
1884 "examples/**/*.rs",
1885 "**/*.md",
1886 },
1887 )
1888 } else {
1889 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1890 }
1891
1892 return srcs, compileData
1893}
1894
Ivan Lozanoffee3342019-08-27 12:03:00 -07001895var Bool = proptools.Bool
1896var BoolDefault = proptools.BoolDefault
1897var String = proptools.String
1898var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001899
1900var _ android.OutputFileProducer = (*Module)(nil)