blob: 19c5230b47a0ff9a612071f0f4a5f05674e505a9 [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"
Vinh Tranbcb5f572023-08-24 11:10:01 -040023 "android/soong/ui/metrics/bp2build_metrics_proto"
Ivan Lozanoffee3342019-08-27 12:03:00 -070024
25 "github.com/google/blueprint"
26 "github.com/google/blueprint/proptools"
27
28 "android/soong/android"
29 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020030 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070031 "android/soong/fuzz"
Spandan Das604f3762023-03-16 22:51:40 +000032 "android/soong/multitree"
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040034 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070035)
36
37var pctx = android.NewPackageContext("android/soong/rust")
38
39func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070040 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050045 })
46 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
47 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070048 })
Inseob Kim3b244062023-07-11 13:31:36 +090049 pctx.Import("android/soong/android")
Ivan Lozanoffee3342019-08-27 12:03:00 -070050 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020051 pctx.ImportAs("cc_config", "android/soong/cc/config")
LaMont Jones0c10e4d2023-05-16 00:58:37 +000052 android.InitRegistrationContext.RegisterParallelSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
55type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040056 GlobalRustFlags []string // Flags that apply globally to rust
57 GlobalLinkFlags []string // Flags that apply globally to linker
58 RustFlags []string // Flags that apply to rust
59 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070061 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070062 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020064 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070065 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050069 AndroidMkRlibs []string `blueprint:"mutated"`
70 AndroidMkDylibs []string `blueprint:"mutated"`
71 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
Liz Kammer884fe9e2023-02-28 14:29:13 -050072 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040073
Ivan Lozano6a884432020-12-02 09:15:16 -050074 ImageVariationPrefix string `blueprint:"mutated"`
75 VndkVersion string `blueprint:"mutated"`
76 SubName string `blueprint:"mutated"`
77
Ivan Lozanoc08897c2021-04-02 12:41:32 -040078 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
79 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
80 // appended before SubName.
81 RustSubName string `blueprint:"mutated"`
82
Ivan Lozano6a884432020-12-02 09:15:16 -050083 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050084 CoreVariantNeeded bool `blueprint:"mutated"`
85 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070086 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080087 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050088 ExtraVariants []string `blueprint:"mutated"`
89
Ivan Lozanoa2268632021-07-22 10:52:06 -040090 // Allows this module to use non-APEX version of libraries. Useful
91 // for building binaries that are started before APEXes are activated.
92 Bootstrap *bool
93
Ivan Lozano1921e802021-05-20 13:39:16 -040094 // Used by vendor snapshot to record dependencies from snapshot modules.
95 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090096 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozanoadd122a2023-07-13 11:01:41 -040097 SnapshotRlibs []string `blueprint:"mutated"`
98 SnapshotDylibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040099
Matthew Maurerc6868382021-07-13 14:12:37 -0700100 // Make this module available when building for ramdisk.
101 // On device without a dedicated recovery partition, the module is only
102 // available after switching root into
103 // /first_stage_ramdisk. To expose the module before switching root, install
104 // the recovery variant instead.
105 Ramdisk_available *bool
106
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500107 // Make this module available when building for vendor ramdisk.
108 // On device without a dedicated recovery partition, the module is only
109 // available after switching root into
110 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800111 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500112 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400113
Ivan Lozano1921e802021-05-20 13:39:16 -0400114 // Normally Soong uses the directory structure to decide which modules
115 // should be included (framework) or excluded (non-framework) from the
116 // different snapshots (vendor, recovery, etc.), but this property
117 // allows a partner to exclude a module normally thought of as a
118 // framework module from the vendor snapshot.
119 Exclude_from_vendor_snapshot *bool
120
121 // Normally Soong uses the directory structure to decide which modules
122 // should be included (framework) or excluded (non-framework) from the
123 // different snapshots (vendor, recovery, etc.), but this property
124 // allows a partner to exclude a module normally thought of as a
125 // framework module from the recovery snapshot.
126 Exclude_from_recovery_snapshot *bool
127
Matthew Maurer460ee942021-02-11 12:31:46 -0800128 // Make this module available when building for recovery
129 Recovery_available *bool
130
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500131 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
132 Min_sdk_version *string
133
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900134 HideFromMake bool `blueprint:"mutated"`
135 PreventInstall bool `blueprint:"mutated"`
136
137 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138}
139
140type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700141 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142
Ivan Lozano6a884432020-12-02 09:15:16 -0500143 VendorProperties cc.VendorProperties
144
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 Properties BaseProperties
146
147 hod android.HostOrDeviceSupported
148 multilib android.Multilib
149
Ivan Lozano6a884432020-12-02 09:15:16 -0500150 makeLinkType string
151
Yi Kong46c6e592022-01-20 22:55:00 +0800152 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400154 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200155 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500156 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700159 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400160
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400161 // Output file to be installed, may be stripped or unstripped.
162 outputFile android.OptionalPath
163
Sasha Smundaka76acba2022-04-18 20:12:56 -0700164 // Cross-reference input file
165 kytheFiles android.Paths
166
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400167 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900168
169 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500170
171 // For apex variants, this is set as apex.min_sdk_version
172 apexSdkVersion android.ApiLevel
Cole Faustb6e6f992023-08-17 17:42:26 -0700173
174 transitiveAndroidMkSharedLibs *android.DepSet[string]
Vinh Tranbcb5f572023-08-24 11:10:01 -0400175
176 android.BazelModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177}
178
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179func (mod *Module) Header() bool {
180 //TODO: If Rust libraries provide header variants, this needs to be updated.
181 return false
182}
183
184func (mod *Module) SetPreventInstall() {
185 mod.Properties.PreventInstall = true
186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SetHideFromMake() {
189 mod.Properties.HideFromMake = true
190}
191
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900192func (mod *Module) HiddenFromMake() bool {
193 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400194}
195
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500196func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500197 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
198 // nil since we need compiler to actually sanitize.
199 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500200}
201
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500202func (mod *Module) IsPrebuilt() bool {
203 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
204 return true
205 }
206 return false
207}
208
Ivan Lozano43845682020-07-09 21:03:28 -0400209func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
210 switch tag {
211 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700212 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400213 return mod.sourceProvider.Srcs(), nil
214 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900215 if mod.OutputFile().Valid() {
216 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400217 }
218 return android.Paths{}, nil
219 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500220 case "unstripped":
221 if mod.compiler != nil {
222 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
223 }
224 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400225 default:
226 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
227 }
228}
229
Ivan Lozano52767be2019-10-18 14:49:46 -0700230func (mod *Module) SelectedStl() string {
231 return ""
232}
233
Ivan Lozano2b262972019-11-21 12:30:50 -0800234func (mod *Module) NonCcVariants() bool {
235 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400236 if _, ok := mod.compiler.(libraryInterface); ok {
237 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800238 }
239 }
240 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
241}
242
Ivan Lozano52767be2019-10-18 14:49:46 -0700243func (mod *Module) Static() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
246 return library.static()
247 }
248 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700250}
251
252func (mod *Module) Shared() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400255 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700256 }
257 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400258 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700259}
260
Ivan Lozanod7586b62021-04-01 09:49:36 -0400261func (mod *Module) Dylib() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok {
264 return library.dylib()
265 }
266 }
267 return false
268}
269
Ivan Lozanod106efe2023-09-21 23:30:26 -0400270func (mod *Module) Source() bool {
271 if mod.compiler != nil {
272 if library, ok := mod.compiler.(libraryInterface); ok && mod.sourceProvider != nil {
273 return library.source()
274 }
275 }
276 return false
277}
278
Ivan Lozanoadd122a2023-07-13 11:01:41 -0400279func (mod *Module) RlibStd() bool {
280 if mod.compiler != nil {
281 if library, ok := mod.compiler.(libraryInterface); ok && library.rlib() {
282 return library.rlibStd()
283 }
284 }
285 panic(fmt.Errorf("RlibStd() called on non-rlib module: %q", mod.BaseModuleName()))
286}
287
Ivan Lozanod7586b62021-04-01 09:49:36 -0400288func (mod *Module) Rlib() bool {
289 if mod.compiler != nil {
290 if library, ok := mod.compiler.(libraryInterface); ok {
291 return library.rlib()
292 }
293 }
294 return false
295}
296
297func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400298 if binary, ok := mod.compiler.(binaryInterface); ok {
299 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400300 }
301 return false
302}
303
Justin Yun5e035862021-06-29 20:50:37 +0900304func (mod *Module) StaticExecutable() bool {
305 if !mod.Binary() {
306 return false
307 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400308 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900309}
310
Ivan Lozanod7586b62021-04-01 09:49:36 -0400311func (mod *Module) Object() bool {
312 // Rust has no modules which produce only object files.
313 return false
314}
315
Ivan Lozano52767be2019-10-18 14:49:46 -0700316func (mod *Module) Toc() android.OptionalPath {
317 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400318 if lib, ok := mod.compiler.(libraryInterface); ok {
319 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700320 }
321 }
322 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
323}
324
Colin Crossc511bc52020-04-07 16:50:32 +0000325func (mod *Module) UseSdk() bool {
326 return false
327}
328
Ivan Lozanod7586b62021-04-01 09:49:36 -0400329func (mod *Module) RelativeInstallPath() string {
330 if mod.compiler != nil {
331 return mod.compiler.relativeInstallPath()
332 }
333 return ""
334}
335
Ivan Lozano52767be2019-10-18 14:49:46 -0700336func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500337 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700338}
339
Jiyong Park7d55b612021-06-11 17:22:09 +0900340func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400341 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900342}
343
Ivan Lozano52767be2019-10-18 14:49:46 -0700344func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400345 return true
346}
347
348func (mod *Module) SubName() string {
349 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700350}
351
352func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500353 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700354 return false
355}
356
Ivan Lozanof9e21722020-12-02 09:00:51 -0500357func (mod *Module) IsVndkExt() bool {
358 return false
359}
360
Ivan Lozanod7586b62021-04-01 09:49:36 -0400361func (mod *Module) IsVndkSp() bool {
362 return false
363}
364
Ivan Lozanof1868af2022-04-12 13:08:36 -0400365func (mod *Module) IsVndkPrebuiltLibrary() bool {
366 // Rust modules do not provide VNDK prebuilts
367 return false
368}
369
370func (mod *Module) IsVendorPublicLibrary() bool {
371 return mod.VendorProperties.IsVendorPublicLibrary
372}
373
374func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
375 // Rust modules to not provide Sdk variants
376 return false
377}
378
Colin Cross127bb8b2020-12-16 16:46:01 -0800379func (c *Module) IsVndkPrivate() bool {
380 return false
381}
382
383func (c *Module) IsLlndk() bool {
384 return false
385}
386
387func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500388 return false
389}
390
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400391func (mod *Module) KernelHeadersDecorator() bool {
392 return false
393}
394
Colin Cross1f3f1302021-04-26 18:37:44 -0700395func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400396 return false
397}
398
Colin Cross5271fea2021-04-27 13:06:04 -0700399func (m *Module) NeedsVendorPublicLibraryVariants() bool {
400 return false
401}
402
Ivan Lozanod7586b62021-04-01 09:49:36 -0400403func (mod *Module) HasLlndkStubs() bool {
404 return false
405}
406
407func (mod *Module) StubsVersion() string {
408 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
409}
410
Ivan Lozano52767be2019-10-18 14:49:46 -0700411func (mod *Module) SdkVersion() string {
412 return ""
413}
414
Colin Crossc511bc52020-04-07 16:50:32 +0000415func (mod *Module) AlwaysSdk() bool {
416 return false
417}
418
Jiyong Park2286afd2020-06-16 21:58:53 +0900419func (mod *Module) IsSdkVariant() bool {
420 return false
421}
422
Colin Cross1348ce32020-10-01 13:37:16 -0700423func (mod *Module) SplitPerApiLevel() bool {
424 return false
425}
426
Sasha Smundaka76acba2022-04-18 20:12:56 -0700427func (mod *Module) XrefRustFiles() android.Paths {
428 return mod.kytheFiles
429}
430
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400432 Dylibs []string
433 Rlibs []string
434 Rustlibs []string
435 Stdlibs []string
436 ProcMacros []string
437 SharedLibs []string
438 StaticLibs []string
439 WholeStaticLibs []string
440 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700441
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400442 // Used for data dependencies adjacent to tests
443 DataLibs []string
444 DataBins []string
445
Colin Crossfe605e12022-01-23 20:46:16 -0800446 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447}
448
449type PathDeps struct {
Colin Cross004bd3f2023-10-02 11:39:17 -0700450 DyLibs RustLibraries
451 RLibs RustLibraries
452 SharedLibs android.Paths
453 SharedLibDeps android.Paths
454 StaticLibs android.Paths
455 ProcMacros RustLibraries
456 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500457
458 // depFlags and depLinkFlags are rustc and linker (clang) flags.
459 depFlags []string
460 depLinkFlags []string
461
462 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
463 // Both of these are exported and propagate to dependencies.
Wen-yi Chu41326c12023-09-22 03:58:59 +0000464 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700465 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700466
Ivan Lozano45901ed2020-07-24 16:05:01 -0400467 // Used by bindgen modules which call clang
468 depClangFlags []string
469 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400470 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400471 depSystemIncludePaths android.Paths
472
Colin Crossfe605e12022-01-23 20:46:16 -0800473 CrtBegin android.Paths
474 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700475
476 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500477 SrcDeps android.Paths
478 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479}
480
481type RustLibraries []RustLibrary
482
483type RustLibrary struct {
484 Path android.Path
485 CrateName string
486}
487
488type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100489 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700490 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400491 cfgFlags(ctx ModuleContext, flags Flags) Flags
492 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700493 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700494 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495 compilerDeps(ctx DepsContext, deps Deps) Deps
496 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700497 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700498
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100499 // Output directory in which source-generated code from dependencies is
500 // copied. This is equivalent to Cargo's OUT_DIR variable.
501 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700502
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400503 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
504 CargoPkgVersion() string
505
506 // CargoEnvCompat returns whether Cargo environment variables should be used.
507 CargoEnvCompat() bool
508
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800509 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200510 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700511 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400512 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513
514 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400515
516 Disabled() bool
517 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400518
Ivan Lozanodd055472020-09-28 13:22:45 -0400519 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500520 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900521
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400522 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900523 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400524}
525
Matthew Maurerbb3add12020-06-25 09:34:12 -0700526type exportedFlagsProducer interface {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000527 exportLinkDirs(...string)
Colin Cross004bd3f2023-10-02 11:39:17 -0700528 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700529}
530
Sasha Smundaka76acba2022-04-18 20:12:56 -0700531type xref interface {
532 XrefRustFiles() android.Paths
533}
534
Matthew Maurerbb3add12020-06-25 09:34:12 -0700535type flagExporter struct {
Wen-yi Chu41326c12023-09-22 03:58:59 +0000536 linkDirs []string
Colin Cross004bd3f2023-10-02 11:39:17 -0700537 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700538}
539
Wen-yi Chu41326c12023-09-22 03:58:59 +0000540func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
541 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
Matthew Maurerbb3add12020-06-25 09:34:12 -0700542}
543
Colin Cross004bd3f2023-10-02 11:39:17 -0700544func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
545 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
Ivan Lozano2093af22020-08-25 12:48:19 -0400546}
547
Colin Cross0de8a1e2020-09-18 14:15:30 -0700548func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
549 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700550 LinkDirs: flagExporter.linkDirs,
551 LinkObjects: flagExporter.linkObjects,
552 })
553}
554
Matthew Maurerbb3add12020-06-25 09:34:12 -0700555var _ exportedFlagsProducer = (*flagExporter)(nil)
556
557func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700558 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700559}
560
Colin Cross0de8a1e2020-09-18 14:15:30 -0700561type FlagExporterInfo struct {
562 Flags []string
Wen-yi Chu41326c12023-09-22 03:58:59 +0000563 LinkDirs []string // TODO: this should be android.Paths
Colin Cross004bd3f2023-10-02 11:39:17 -0700564 LinkObjects []string // TODO: this should be android.Paths
Colin Cross0de8a1e2020-09-18 14:15:30 -0700565}
566
567var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
568
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400569func (mod *Module) isCoverageVariant() bool {
570 return mod.coverage.Properties.IsCoverageVariant
571}
572
573var _ cc.Coverage = (*Module)(nil)
574
575func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
576 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
577}
578
Ivan Lozanod7586b62021-04-01 09:49:36 -0400579func (mod *Module) VndkVersion() string {
580 return mod.Properties.VndkVersion
581}
582
583func (mod *Module) PreventInstall() bool {
584 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400585}
586
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400587func (mod *Module) MarkAsCoverageVariant(coverage bool) {
588 mod.coverage.Properties.IsCoverageVariant = coverage
589}
590
591func (mod *Module) EnableCoverageIfNeeded() {
592 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593}
594
595func defaultsFactory() android.Module {
596 return DefaultsFactory()
597}
598
599type Defaults struct {
600 android.ModuleBase
601 android.DefaultsModuleBase
602}
603
604func DefaultsFactory(props ...interface{}) android.Module {
605 module := &Defaults{}
606
607 module.AddProperties(props...)
608 module.AddProperties(
609 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800610 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500611 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100612 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400613 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614 &BaseCompilerProperties{},
615 &BinaryCompilerProperties{},
616 &LibraryCompilerProperties{},
617 &ProcMacroCompilerProperties{},
618 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400619 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700620 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400621 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400622 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200623 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500624 &SanitizeProperties{},
Pawan Waghccb75582023-08-16 23:58:25 +0000625 &fuzz.FuzzProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700626 )
627
628 android.InitDefaultsModule(module)
629 return module
630}
631
632func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700633 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700634}
635
Ivan Lozano183a3212019-10-18 14:18:45 -0700636func (mod *Module) CcLibrary() bool {
637 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500638 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700639 return true
640 }
641 }
642 return false
643}
644
645func (mod *Module) CcLibraryInterface() bool {
646 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400647 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
648 // VariantIs{Static,Shared} is set.
649 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700650 return true
651 }
652 }
653 return false
654}
655
Ivan Lozano61c02cc2023-06-09 14:06:44 -0400656func (mod *Module) RustLibraryInterface() bool {
657 if mod.compiler != nil {
658 if _, ok := mod.compiler.(libraryInterface); ok {
659 return true
660 }
661 }
662 return false
663}
664
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500665func (mod *Module) IsFuzzModule() bool {
666 if _, ok := mod.compiler.(*fuzzDecorator); ok {
667 return true
668 }
669 return false
670}
671
672func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
673 return mod.FuzzModule
674}
675
676func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
677 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
678 return fuzzer.fuzzPackagedModule
679 }
680 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
681}
682
Hamzeh Zawawy38917492023-04-05 22:08:46 +0000683func (mod *Module) FuzzSharedLibraries() android.RuleBuilderInstalls {
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500684 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
685 return fuzzer.sharedLibraries
686 }
687 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
688}
689
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400690func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400691 if mod.compiler != nil {
692 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400693 }
694 return nil
695}
696
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800697func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700698 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700699 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800700 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700701 }
702 }
703 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
704}
705
706func (mod *Module) SetStatic() {
707 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700708 if library, ok := mod.compiler.(libraryInterface); ok {
709 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700710 return
711 }
712 }
713 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
714}
715
716func (mod *Module) SetShared() {
717 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700718 if library, ok := mod.compiler.(libraryInterface); ok {
719 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700720 return
721 }
722 }
723 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
724}
725
Ivan Lozano183a3212019-10-18 14:18:45 -0700726func (mod *Module) BuildStaticVariant() bool {
727 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700728 if library, ok := mod.compiler.(libraryInterface); ok {
729 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700730 }
731 }
732 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
733}
734
735func (mod *Module) BuildSharedVariant() bool {
736 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700737 if library, ok := mod.compiler.(libraryInterface); ok {
738 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700739 }
740 }
741 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
742}
743
Ivan Lozano183a3212019-10-18 14:18:45 -0700744func (mod *Module) Module() android.Module {
745 return mod
746}
747
Ivan Lozano183a3212019-10-18 14:18:45 -0700748func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400749 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700750}
751
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400752func (mod *Module) CoverageFiles() android.Paths {
753 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800754 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400755 }
756 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
757}
758
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400759// Rust does not produce gcno files, and therefore does not produce a coverage archive.
760func (mod *Module) CoverageOutputFile() android.OptionalPath {
761 return android.OptionalPath{}
762}
763
764func (mod *Module) IsNdk(config android.Config) bool {
765 return false
766}
767
768func (mod *Module) IsStubs() bool {
769 return false
770}
771
Jiyong Park459feca2020-12-15 11:02:21 +0900772func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900773 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900774 return false
775 }
776
Jiyong Park459feca2020-12-15 11:02:21 +0900777 // The apex variant is not installable because it is included in the APEX and won't appear
778 // in the system partition as a standalone file.
779 if !apexInfo.IsForPlatform() {
780 return false
781 }
782
Jiyong Parke54f07e2021-04-07 15:08:04 +0900783 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900784}
785
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500786func (ctx moduleContext) apexVariationName() string {
787 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
788}
789
Ivan Lozano183a3212019-10-18 14:18:45 -0700790var _ cc.LinkableInterface = (*Module)(nil)
791
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792func (mod *Module) Init() android.Module {
793 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500794 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795
Yi Kong46c6e592022-01-20 22:55:00 +0800796 if mod.afdo != nil {
797 mod.AddProperties(mod.afdo.props()...)
798 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 if mod.compiler != nil {
800 mod.AddProperties(mod.compiler.compilerProps()...)
801 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400802 if mod.coverage != nil {
803 mod.AddProperties(mod.coverage.props()...)
804 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200805 if mod.clippy != nil {
806 mod.AddProperties(mod.clippy.props()...)
807 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400808 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700809 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400810 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500811 if mod.sanitize != nil {
812 mod.AddProperties(mod.sanitize.props()...)
813 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400814
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900816 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817
818 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 return mod
820}
821
822func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
823 return &Module{
824 hod: hod,
825 multilib: multilib,
826 }
827}
828func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
829 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800830 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400831 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200832 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500833 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834 return module
835}
836
837type ModuleContext interface {
838 android.ModuleContext
839 ModuleContextIntf
840}
841
842type BaseModuleContext interface {
843 android.BaseModuleContext
844 ModuleContextIntf
845}
846
847type DepsContext interface {
848 android.BottomUpMutatorContext
849 ModuleContextIntf
850}
851
852type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200853 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855}
856
857type depsContext struct {
858 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700859}
860
861type moduleContext struct {
862 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863}
864
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200865type baseModuleContext struct {
866 android.BaseModuleContext
867}
868
869func (ctx *moduleContext) RustModule() *Module {
870 return ctx.Module().(*Module)
871}
872
873func (ctx *moduleContext) toolchain() config.Toolchain {
874 return ctx.RustModule().toolchain(ctx)
875}
876
877func (ctx *depsContext) RustModule() *Module {
878 return ctx.Module().(*Module)
879}
880
881func (ctx *depsContext) toolchain() config.Toolchain {
882 return ctx.RustModule().toolchain(ctx)
883}
884
885func (ctx *baseModuleContext) RustModule() *Module {
886 return ctx.Module().(*Module)
887}
888
889func (ctx *baseModuleContext) toolchain() config.Toolchain {
890 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400891}
892
893func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700894 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
895 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
896 return false
897 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400898 return mod.compiler != nil && mod.compiler.nativeCoverage()
899}
900
Ivan Lozanod7586b62021-04-01 09:49:36 -0400901func (mod *Module) EverInstallable() bool {
902 return mod.compiler != nil &&
903 // Check to see whether the module is actually ever installable.
904 mod.compiler.everInstallable()
905}
906
907func (mod *Module) Installable() *bool {
908 return mod.Properties.Installable
909}
910
Ivan Lozano872d5792022-03-23 17:31:39 -0400911func (mod *Module) ProcMacro() bool {
912 if pm, ok := mod.compiler.(procMacroInterface); ok {
913 return pm.ProcMacro()
914 }
915 return false
916}
917
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
919 if mod.cachedToolchain == nil {
920 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
921 }
922 return mod.cachedToolchain
923}
924
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200925func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
926 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
927}
928
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
930}
931
932func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
933 ctx := &moduleContext{
934 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936
Jiyong Park99644e92020-11-17 22:21:02 +0900937 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
938 if !apexInfo.IsForPlatform() {
939 mod.hideApexVariantFromMake = true
940 }
941
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500943 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
944
Ivan Lozanof1868af2022-04-12 13:08:36 -0400945 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700946
Ivan Lozanoffee3342019-08-27 12:03:00 -0700947 if !toolchain.Supported() {
948 // This toolchain's unsupported, there's nothing to do for this mod.
949 return
950 }
951
952 deps := mod.depsToPaths(ctx)
953 flags := Flags{
954 Toolchain: toolchain,
955 }
956
Ivan Lozano67eada32021-09-23 11:50:33 -0400957 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800958 if mod.afdo != nil {
Vinh Trancde10162023-03-09 22:07:19 -0500959 flags, deps = mod.afdo.flags(actx, flags, deps)
Yi Kong46c6e592022-01-20 22:55:00 +0800960 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961 if mod.compiler != nil {
962 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400963 flags = mod.compiler.cfgFlags(ctx, flags)
964 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400965 }
966 if mod.coverage != nil {
967 flags, deps = mod.coverage.flags(ctx, flags, deps)
968 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200969 if mod.clippy != nil {
970 flags, deps = mod.clippy.flags(ctx, flags, deps)
971 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500972 if mod.sanitize != nil {
973 flags, deps = mod.sanitize.flags(ctx, flags, deps)
974 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400975
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200976 // SourceProvider needs to call GenerateSource() before compiler calls
977 // compile() so it can provide the source. A SourceProvider has
978 // multiple variants (e.g. source, rlib, dylib). Only the "source"
979 // variant is responsible for effectively generating the source. The
980 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400981 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200982 if mod.compiler.(libraryInterface).source() {
983 mod.sourceProvider.GenerateSource(ctx, deps)
984 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
985 } else {
986 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
987 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700988 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200989 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400990 }
991
992 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100993 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700994 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400995 if ctx.Failed() {
996 return
997 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700998 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
999 if buildOutput.kytheFile != nil {
1000 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
1001 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001002 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +09001003
Dan Albert06feee92021-03-19 15:06:02 -07001004 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +00001005 if mod.docTimestampFile.Valid() {
1006 ctx.CheckbuildFile(mod.docTimestampFile.Path())
1007 }
Dan Albert06feee92021-03-19 15:06:02 -07001008
Ivan Lozano1921e802021-05-20 13:39:16 -04001009 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
1010 // RECOVERY_SNAPSHOT_VERSION is current.
1011 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
1012 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
1013 lib.collectHeadersForSnapshot(ctx, deps)
1014 }
1015 }
1016
Jiyong Park459feca2020-12-15 11:02:21 +09001017 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -04001018 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001019 // If the module has been specifically configure to not be installed then
1020 // hide from make as otherwise it will break when running inside make as the
1021 // output path to install will not be specified. Not all uninstallable
1022 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -04001023 // side dependencies. In particular, proc-macros need to be captured in the
1024 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001025 mod.HideFromMake()
1026 } else if !mod.installable(apexInfo) {
1027 mod.SkipInstall()
1028 }
1029
1030 // Still call install though, the installs will be stored as PackageSpecs to allow
1031 // using the outputs in a genrule.
1032 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +02001033 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +09001034 if ctx.Failed() {
1035 return
1036 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001037 }
Chris Wailes74be7642021-07-22 16:20:28 -07001038
1039 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 }
1041}
1042
1043func (mod *Module) deps(ctx DepsContext) Deps {
1044 deps := Deps{}
1045
1046 if mod.compiler != nil {
1047 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001048 }
1049 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001050 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051 }
1052
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001053 if mod.coverage != nil {
1054 deps = mod.coverage.deps(ctx, deps)
1055 }
1056
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001057 if mod.sanitize != nil {
1058 deps = mod.sanitize.deps(ctx, deps)
1059 }
1060
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1062 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001063 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001064 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1065 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1066 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001067 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001068 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069 return deps
1070
1071}
1072
Ivan Lozanoffee3342019-08-27 12:03:00 -07001073type dependencyTag struct {
1074 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001075 name string
1076 library bool
1077 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001078 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001079}
1080
Jiyong Park65b62242020-11-25 12:44:59 +09001081// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1082// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1083func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001084 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001085}
1086
1087var _ android.InstallNeededDependencyTag = dependencyTag{}
1088
Colin Cross65cb3142021-12-10 23:05:02 +00001089func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1090 if d.library && d.dynamic {
1091 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1092 }
1093 return nil
1094}
1095
1096var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1097
Ivan Lozanoffee3342019-08-27 12:03:00 -07001098var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001099 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1100 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001101 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001102 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001103 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001104 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001105 dataLibDepTag = dependencyTag{name: "data lib"}
1106 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001107)
1108
Jiyong Park99644e92020-11-17 22:21:02 +09001109func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1110 tag, ok := depTag.(dependencyTag)
1111 return ok && tag == dylibDepTag
1112}
1113
Jiyong Park94e22fd2021-04-08 18:19:15 +09001114func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1115 tag, ok := depTag.(dependencyTag)
1116 return ok && tag == rlibDepTag
1117}
1118
Matthew Maurer0f003b12020-06-29 14:34:06 -07001119type autoDep struct {
1120 variation string
1121 depTag dependencyTag
1122}
1123
1124var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001125 rlibVariation = "rlib"
1126 dylibVariation = "dylib"
1127 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1128 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001129)
1130
1131type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001132 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001133}
1134
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001135func (mod *Module) begin(ctx BaseModuleContext) {
1136 if mod.coverage != nil {
1137 mod.coverage.begin(ctx)
1138 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001139 if mod.sanitize != nil {
1140 mod.sanitize.begin(ctx)
1141 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001142}
1143
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001144func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001145 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001146 return p.prebuilt()
1147 }
1148 return nil
1149}
1150
Justin Yun24b246a2023-03-16 10:36:16 +09001151func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1152 if rustDep, ok := dep.(*Module); ok {
1153 // Use base module name for snapshots when exporting to Makefile.
1154 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1155 baseName := rustDep.BaseModuleName()
1156 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1157 }
1158 }
1159 return cc.MakeLibName(ctx, c, dep, depName)
1160}
1161
Ivan Lozanod106efe2023-09-21 23:30:26 -04001162func collectIncludedProtos(mod *Module, dep *Module) {
1163 if protoMod, ok := mod.sourceProvider.(*protobufDecorator); ok {
1164 if _, ok := dep.sourceProvider.(*protobufDecorator); ok {
1165 protoMod.additionalCrates = append(protoMod.additionalCrates, dep.CrateName())
1166 }
1167 }
1168}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001169func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1170 var depPaths PathDeps
1171
1172 directRlibDeps := []*Module{}
1173 directDylibDeps := []*Module{}
1174 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001175 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001176 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001177 directSrcProvidersDeps := []*Module{}
1178 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001179
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001180 // For the dependency from platform to apex, use the latest stubs
1181 mod.apexSdkVersion = android.FutureApiLevel
1182 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1183 if !apexInfo.IsForPlatform() {
1184 mod.apexSdkVersion = apexInfo.MinSdkVersion
1185 }
1186
1187 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1188 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1189 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1190 // (b/144430859)
1191 mod.apexSdkVersion = android.FutureApiLevel
1192 }
1193
Spandan Das604f3762023-03-16 22:51:40 +00001194 skipModuleList := map[string]bool{}
1195
1196 var apiImportInfo multitree.ApiImportInfo
1197 hasApiImportInfo := false
1198
1199 ctx.VisitDirectDeps(func(dep android.Module) {
1200 if dep.Name() == "api_imports" {
1201 apiImportInfo = ctx.OtherModuleProvider(dep, multitree.ApiImportsProvider).(multitree.ApiImportInfo)
1202 hasApiImportInfo = true
1203 }
1204 })
1205
1206 if hasApiImportInfo {
1207 targetStubModuleList := map[string]string{}
1208 targetOrigModuleList := map[string]string{}
1209
1210 // Search for dependency which both original module and API imported library with APEX stub exists
1211 ctx.VisitDirectDeps(func(dep android.Module) {
1212 depName := ctx.OtherModuleName(dep)
1213 if apiLibrary, ok := apiImportInfo.ApexSharedLibs[depName]; ok {
1214 targetStubModuleList[apiLibrary] = depName
1215 }
1216 })
1217 ctx.VisitDirectDeps(func(dep android.Module) {
1218 depName := ctx.OtherModuleName(dep)
1219 if origLibrary, ok := targetStubModuleList[depName]; ok {
1220 targetOrigModuleList[origLibrary] = depName
1221 }
1222 })
1223
1224 // Decide which library should be used between original and API imported library
1225 ctx.VisitDirectDeps(func(dep android.Module) {
1226 depName := ctx.OtherModuleName(dep)
1227 if apiLibrary, ok := targetOrigModuleList[depName]; ok {
1228 if cc.ShouldUseStubForApex(ctx, dep) {
1229 skipModuleList[depName] = true
1230 } else {
1231 skipModuleList[apiLibrary] = true
1232 }
1233 }
1234 })
1235 }
1236
Cole Faustb6e6f992023-08-17 17:42:26 -07001237 var transitiveAndroidMkSharedLibs []*android.DepSet[string]
1238 var directAndroidMkSharedLibs []string
Wen-yi Chu41326c12023-09-22 03:58:59 +00001239
Ivan Lozanoffee3342019-08-27 12:03:00 -07001240 ctx.VisitDirectDeps(func(dep android.Module) {
1241 depName := ctx.OtherModuleName(dep)
1242 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001243
Spandan Das604f3762023-03-16 22:51:40 +00001244 if _, exists := skipModuleList[depName]; exists {
1245 return
1246 }
A. Cody Schuffelenc183e3a2023-08-14 21:09:47 -07001247
1248 if depTag == android.DarwinUniversalVariantTag {
1249 return
1250 }
1251
Ivan Lozano89435d12020-07-31 11:01:18 -04001252 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001253 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001254 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001255
Ivan Lozanoffee3342019-08-27 12:03:00 -07001256 switch depTag {
1257 case dylibDepTag:
1258 dylib, ok := rustDep.compiler.(libraryInterface)
1259 if !ok || !dylib.dylib() {
1260 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1261 return
1262 }
1263 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001264 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001265 mod.Properties.SnapshotDylibs = append(mod.Properties.SnapshotDylibs, cc.BaseLibName(depName))
1266
Ivan Lozanoffee3342019-08-27 12:03:00 -07001267 case rlibDepTag:
Wen-yi Chu41326c12023-09-22 03:58:59 +00001268
Ivan Lozanoffee3342019-08-27 12:03:00 -07001269 rlib, ok := rustDep.compiler.(libraryInterface)
1270 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001271 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001272 return
1273 }
1274 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001275 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001276 mod.Properties.SnapshotRlibs = append(mod.Properties.SnapshotRlibs, cc.BaseLibName(depName))
1277
Ivan Lozanoffee3342019-08-27 12:03:00 -07001278 case procMacroDepTag:
1279 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001280 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001281
1282 case sourceDepTag:
1283 if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1284 collectIncludedProtos(mod, rustDep)
1285 }
Paul Duffind5cf92e2021-07-09 17:38:55 +01001286 }
1287
Cole Faustb6e6f992023-08-17 17:42:26 -07001288 transitiveAndroidMkSharedLibs = append(transitiveAndroidMkSharedLibs, rustDep.transitiveAndroidMkSharedLibs)
1289
Paul Duffind5cf92e2021-07-09 17:38:55 +01001290 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001291 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1292 // OS/Arch variant is used.
1293 var helper string
1294 if ctx.Host() {
1295 helper = "missing 'host_supported'?"
1296 } else {
1297 helper = "device module defined?"
1298 }
1299
1300 if dep.Target().Os != ctx.Os() {
1301 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1302 return
1303 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1304 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1305 return
1306 }
1307 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001308 }
1309
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001310 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001311 if depTag != procMacroDepTag {
1312 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1313 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1314 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1315 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001316 }
1317
Ivan Lozanoffee3342019-08-27 12:03:00 -07001318 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001319 linkFile := rustDep.UnstrippedOutputFile()
Wen-yi Chu41326c12023-09-22 03:58:59 +00001320 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001321 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
Wen-yi Chu41326c12023-09-22 03:58:59 +00001322 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001323 }
1324 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001325 if depTag == sourceDepTag {
1326 if _, ok := mod.sourceProvider.(*protobufDecorator); ok && mod.Source() {
1327 if _, ok := rustDep.sourceProvider.(*protobufDecorator); ok {
1328 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1329 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1330 }
1331 }
1332 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001333 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001334 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001335 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001336 if _, ok := ccDep.(*Module); !ok {
1337 if ccDep.Module().Target().Os != ctx.Os() {
1338 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1339 return
1340 }
1341 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1342 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1343 return
1344 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001345 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001346 linkObject := ccDep.OutputFile()
Ivan Lozano2093af22020-08-25 12:48:19 -04001347 if !linkObject.Valid() {
Colin Crossa86ea0e2023-08-01 09:57:22 -07001348 if !ctx.Config().AllowMissingDependencies() {
1349 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1350 } else {
1351 ctx.AddMissingDependencies([]string{depName})
1352 }
1353 return
Ivan Lozanoffee3342019-08-27 12:03:00 -07001354 }
1355
Wen-yi Chu41326c12023-09-22 03:58:59 +00001356 linkPath := linkPathFromFilePath(linkObject.Path())
Colin Crossa86ea0e2023-08-01 09:57:22 -07001357
Ivan Lozanoffee3342019-08-27 12:03:00 -07001358 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001359 switch {
1360 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001361 if cc.IsWholeStaticLib(depTag) {
1362 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1363 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001364 if mod.Binary() {
1365 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1366 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1367 // final linkage, pass the args directly to the linker to handle these cases.
1368 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1369 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001370 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001371 } else {
1372 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 -05001373 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001374 }
1375
1376 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1377 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Colin Cross004bd3f2023-10-02 11:39:17 -07001378 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001379 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1380
Colin Cross0de8a1e2020-09-18 14:15:30 -07001381 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1382 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1383 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1384 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1385 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001386 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001387
1388 // Record baseLibName for snapshots.
1389 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1390
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001391 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001392 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001393 // For the shared lib dependencies, we may link to the stub variant
1394 // of the dependency depending on the context (e.g. if this
1395 // dependency crosses the APEX boundaries).
1396 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1397
1398 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1399 // object (either from stub or non-stub) to use.
1400 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
Colin Crossa86ea0e2023-08-01 09:57:22 -07001401 if !linkObject.Valid() {
1402 if !ctx.Config().AllowMissingDependencies() {
1403 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1404 } else {
1405 ctx.AddMissingDependencies([]string{depName})
1406 }
1407 return
1408 }
Wen-yi Chu41326c12023-09-22 03:58:59 +00001409 linkPath = linkPathFromFilePath(linkObject.Path())
Jiyong Park7d55b612021-06-11 17:22:09 +09001410
Ivan Lozanoffee3342019-08-27 12:03:00 -07001411 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001412 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001413 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1414 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1415 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1416 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001417 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001418
1419 // Record baseLibName for snapshots.
1420 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1421
Cole Faustb6e6f992023-08-17 17:42:26 -07001422 directAndroidMkSharedLibs = append(directAndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001423 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001424 case cc.IsHeaderDepTag(depTag):
1425 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1426 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1427 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1428 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001429 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001430 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001431 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001432 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001433 }
1434
1435 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001436 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1437 lib.exportLinkDirs(linkPath)
Colin Cross004bd3f2023-10-02 11:39:17 -07001438 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001439 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001440 } else {
1441 switch {
1442 case depTag == cc.CrtBeginDepTag:
1443 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1444 case depTag == cc.CrtEndDepTag:
1445 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1446 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001447 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001448
1449 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001450 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001451 // These are usually genrules which don't have per-target variants.
1452 directSrcDeps = append(directSrcDeps, srcDep)
1453 }
1454 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455 })
1456
Wen-yi Chu41326c12023-09-22 03:58:59 +00001457 mod.transitiveAndroidMkSharedLibs = android.NewDepSet[string](android.PREORDER, directAndroidMkSharedLibs, transitiveAndroidMkSharedLibs)
1458
1459 var rlibDepFiles RustLibraries
1460 for _, dep := range directRlibDeps {
1461 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1462 }
1463 var dylibDepFiles RustLibraries
1464 for _, dep := range directDylibDeps {
1465 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1466 }
1467 var procMacroDepFiles RustLibraries
1468 for _, dep := range directProcMacroDeps {
1469 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
1470 }
1471
Colin Cross004bd3f2023-10-02 11:39:17 -07001472 var staticLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001473 for _, dep := range directStaticLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001474 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001475 }
1476
Colin Cross004bd3f2023-10-02 11:39:17 -07001477 var sharedLibFiles android.Paths
1478 var sharedLibDepFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001479 for _, dep := range directSharedLibDeps {
Colin Cross004bd3f2023-10-02 11:39:17 -07001480 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
Jiyong Park7d55b612021-06-11 17:22:09 +09001481 if dep.TableOfContents.Valid() {
Colin Cross004bd3f2023-10-02 11:39:17 -07001482 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001483 } else {
Colin Cross004bd3f2023-10-02 11:39:17 -07001484 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001485 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001486 }
1487
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001488 var srcProviderDepFiles android.Paths
1489 for _, dep := range directSrcProvidersDeps {
1490 srcs, _ := dep.OutputFiles("")
1491 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1492 }
1493 for _, dep := range directSrcDeps {
1494 srcs := dep.Srcs()
1495 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1496 }
1497
Wen-yi Chu41326c12023-09-22 03:58:59 +00001498 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1499 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
Colin Cross004bd3f2023-10-02 11:39:17 -07001500 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibFiles...)
1501 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
1502 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
Wen-yi Chu41326c12023-09-22 03:58:59 +00001503 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001504 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001505
1506 // Dedup exported flags from dependencies
Wen-yi Chu41326c12023-09-22 03:58:59 +00001507 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Colin Cross004bd3f2023-10-02 11:39:17 -07001508 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001509 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001510 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1511 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1512 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001513
1514 return depPaths
1515}
1516
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001517func (mod *Module) InstallInData() bool {
1518 if mod.compiler == nil {
1519 return false
1520 }
1521 return mod.compiler.inData()
1522}
1523
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001524func (mod *Module) InstallInRamdisk() bool {
1525 return mod.InRamdisk()
1526}
1527
1528func (mod *Module) InstallInVendorRamdisk() bool {
1529 return mod.InVendorRamdisk()
1530}
1531
1532func (mod *Module) InstallInRecovery() bool {
1533 return mod.InRecovery()
1534}
1535
Wen-yi Chu41326c12023-09-22 03:58:59 +00001536func linkPathFromFilePath(filepath android.Path) string {
1537 return strings.Split(filepath.String(), filepath.Base())[0]
1538}
1539
Spandan Das604f3762023-03-16 22:51:40 +00001540// usePublicApi returns true if the rust variant should link against NDK (publicapi)
1541func (r *Module) usePublicApi() bool {
1542 return r.Device() && r.UseSdk()
1543}
1544
1545// useVendorApi returns true if the rust variant should link against LLNDK (vendorapi)
1546func (r *Module) useVendorApi() bool {
1547 return r.Device() && (r.InVendor() || r.InProduct())
1548}
1549
Ivan Lozanoffee3342019-08-27 12:03:00 -07001550func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1551 ctx := &depsContext{
1552 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001553 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001554
1555 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001556 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001557 var snapshotInfo *cc.SnapshotInfo
1558
Kiyoung Kim487689e2022-07-26 09:48:22 +09001559 apiImportInfo := cc.GetApiImports(mod, actx)
Spandan Das604f3762023-03-16 22:51:40 +00001560 if mod.usePublicApi() || mod.useVendorApi() {
1561 for idx, lib := range deps.SharedLibs {
1562 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1563 }
Kiyoung Kim487689e2022-07-26 09:48:22 +09001564 }
1565
Ivan Lozano1921e802021-05-20 13:39:16 -04001566 if ctx.Os() == android.Android {
1567 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1568 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001569
Ivan Lozano2b081132020-09-08 12:46:52 -04001570 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001571 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001572 stdLinkage = "rlib-std"
1573 }
1574
1575 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001576
Ivan Lozano2b081132020-09-08 12:46:52 -04001577 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1578 rlibDepVariations = append(rlibDepVariations,
1579 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1580 }
1581
Ivan Lozano1921e802021-05-20 13:39:16 -04001582 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001583 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001584 for _, lib := range deps.Rlibs {
1585 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001586 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001587
Ivan Lozano2d407632022-04-07 12:59:11 -04001588 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001589 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001590
1591 // dylibs
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001592 dylibDepVariations := append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: dylibVariation})
1593 for _, lib := range deps.Dylibs {
1594 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1595 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001596
Ivan Lozano1921e802021-05-20 13:39:16 -04001597 // rustlibs
Ivan Lozanod106efe2023-09-21 23:30:26 -04001598 if deps.Rustlibs != nil {
1599 if !mod.compiler.Disabled() {
1600 for _, lib := range deps.Rustlibs {
1601 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
1602 if autoDep.depTag == rlibDepTag {
1603 // Handle the rlib deptag case
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001604 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
Ivan Lozanod106efe2023-09-21 23:30:26 -04001605 } else {
1606 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1607 // Check for the existence of the dylib deptag variant. Select it if available,
1608 // otherwise select the rlib variant.
1609 autoDepVariations := append(commonDepVariations,
1610 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1611
1612 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1613
1614 if actx.OtherModuleDependencyVariantExists(autoDepVariations, replacementLib) {
1615 addDylibDependency(actx, lib, mod, &snapshotInfo, autoDepVariations, autoDep.depTag)
1616 } else {
1617 // If there's no dylib dependency available, try to add the rlib dependency instead.
1618 addRlibDependency(actx, lib, mod, &snapshotInfo, rlibDepVariations)
1619 }
1620 }
1621 }
1622 } else if _, ok := mod.sourceProvider.(*protobufDecorator); ok {
1623 for _, lib := range deps.Rustlibs {
1624 replacementLib := cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Dylibs)
1625 srcProviderVariations := append(commonDepVariations,
1626 blueprint.Variation{Mutator: "rust_libraries", Variation: "source"})
1627
1628 if actx.OtherModuleDependencyVariantExists(srcProviderVariations, replacementLib) {
1629 actx.AddVariationDependencies(srcProviderVariations, sourceDepTag, lib)
Ivan Lozano2d407632022-04-07 12:59:11 -04001630 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001631 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001632 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001633 }
Ivan Lozanod106efe2023-09-21 23:30:26 -04001634
Ivan Lozano1921e802021-05-20 13:39:16 -04001635 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001636 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001637 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001638 for _, lib := range deps.Stdlibs {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001639 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001640 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001641 rlibDepTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001642 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001643 } else {
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001644 for _, lib := range deps.Stdlibs {
1645 addDylibDependency(actx, lib, mod, &snapshotInfo, dylibDepVariations, dylibDepTag)
1646 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001647 }
1648 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001649
1650 for _, lib := range deps.SharedLibs {
1651 depTag := cc.SharedDepTag()
1652 name, version := cc.StubsLibNameAndVersion(lib)
1653
1654 variations := []blueprint.Variation{
1655 {Mutator: "link", Variation: "shared"},
1656 }
Spandan Das604f3762023-03-16 22:51:40 +00001657 // For core variant, add a dep on the implementation (if it exists) and its .apiimport (if it exists)
1658 // GenerateAndroidBuildActions will pick the correct impl/stub based on the api_domain boundary
1659 if _, ok := apiImportInfo.ApexSharedLibs[name]; !ok || ctx.OtherModuleExists(name) {
1660 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1661 }
1662
1663 if apiLibraryName, ok := apiImportInfo.ApexSharedLibs[name]; ok {
1664 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, apiLibraryName, version, false)
1665 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001666 }
1667
1668 for _, lib := range deps.WholeStaticLibs {
1669 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001670 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001671
1672 actx.AddVariationDependencies([]blueprint.Variation{
1673 {Mutator: "link", Variation: "static"},
1674 }, depTag, lib)
1675 }
1676
1677 for _, lib := range deps.StaticLibs {
1678 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001679 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001680
1681 actx.AddVariationDependencies([]blueprint.Variation{
1682 {Mutator: "link", Variation: "static"},
1683 }, depTag, lib)
1684 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001685
Zach Johnson3df4e632020-11-06 11:56:27 -08001686 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1687
Colin Cross565cafd2020-09-25 18:47:38 -07001688 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001689 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001690 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001691 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001692 }
Colin Crossfe605e12022-01-23 20:46:16 -08001693 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001694 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001695 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001696 }
1697
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001698 if mod.sourceProvider != nil {
1699 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1700 bindgen.Properties.Custom_bindgen != "" {
1701 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1702 bindgen.Properties.Custom_bindgen)
1703 }
1704 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001705
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001706 actx.AddVariationDependencies([]blueprint.Variation{
1707 {Mutator: "link", Variation: "shared"},
1708 }, dataLibDepTag, deps.DataLibs...)
1709
1710 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1711
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001712 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001713 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Vinh Trancde10162023-03-09 22:07:19 -05001714
1715 mod.afdo.addDep(ctx, actx)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001716}
1717
Ivan Lozano2d407632022-04-07 12:59:11 -04001718// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
Inseob Kimcd2b46a2023-03-31 18:04:12 +09001719func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation) {
1720 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001721 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1722}
1723
Ivan Lozanoadd122a2023-07-13 11:01:41 -04001724func addDylibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo **cc.SnapshotInfo, variations []blueprint.Variation, depTag dependencyTag) {
1725 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, snapshotInfo, actx).Dylibs)
1726 actx.AddVariationDependencies(variations, depTag, lib)
1727}
1728
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001729func BeginMutator(ctx android.BottomUpMutatorContext) {
1730 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1731 mod.beginMutator(ctx)
1732 }
1733}
1734
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001735func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1736 ctx := &baseModuleContext{
1737 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001738 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001739
1740 mod.begin(ctx)
1741}
1742
Ivan Lozanoffee3342019-08-27 12:03:00 -07001743func (mod *Module) Name() string {
1744 name := mod.ModuleBase.Name()
1745 if p, ok := mod.compiler.(interface {
1746 Name(string) string
1747 }); ok {
1748 name = p.Name(name)
1749 }
1750 return name
1751}
1752
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001753func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001754 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001755 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001756 }
1757}
1758
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001759var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001760var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001761
1762func (mod *Module) HostToolPath() android.OptionalPath {
1763 if !mod.Host() {
1764 return android.OptionalPath{}
1765 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001766 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1767 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001768 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1769 // Even though proc-macros aren't strictly "tools", since they target the compiler
1770 // and act as compiler plugins, we treat them similarly.
1771 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001772 }
1773 return android.OptionalPath{}
1774}
1775
Jiyong Park99644e92020-11-17 22:21:02 +09001776var _ android.ApexModule = (*Module)(nil)
1777
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001778func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001779 return String(mod.Properties.Min_sdk_version)
1780}
1781
Jiyong Park45bf82e2020-12-15 22:29:02 +09001782// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001783func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001784 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001785 if minSdkVersion == "apex_inherit" {
1786 return nil
1787 }
1788 if minSdkVersion == "" {
1789 return fmt.Errorf("min_sdk_version is not specificed")
1790 }
1791
1792 // Not using nativeApiLevelFromUser because the context here is not
1793 // necessarily a native context.
1794 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1795 if err != nil {
1796 return err
1797 }
1798
1799 if ver.GreaterThan(sdkVersion) {
1800 return fmt.Errorf("newer SDK(%v)", ver)
1801 }
Jiyong Park99644e92020-11-17 22:21:02 +09001802 return nil
1803}
1804
Jiyong Park45bf82e2020-12-15 22:29:02 +09001805// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001806func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1807 depTag := ctx.OtherModuleDependencyTag(dep)
1808
1809 if ccm, ok := dep.(*cc.Module); ok {
1810 if ccm.HasStubsVariants() {
1811 if cc.IsSharedDepTag(depTag) {
1812 // dynamic dep to a stubs lib crosses APEX boundary
1813 return false
1814 }
1815 if cc.IsRuntimeDepTag(depTag) {
1816 // runtime dep to a stubs lib also crosses APEX boundary
1817 return false
1818 }
1819
1820 if cc.IsHeaderDepTag(depTag) {
1821 return false
1822 }
1823 }
1824 if mod.Static() && cc.IsSharedDepTag(depTag) {
1825 // shared_lib dependency from a static lib is considered as crossing
1826 // the APEX boundary because the dependency doesn't actually is
1827 // linked; the dependency is used only during the compilation phase.
1828 return false
1829 }
1830 }
1831
Matthew Maurer581b6d82022-09-29 16:46:25 -07001832 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001833 return false
1834 }
1835
1836 return true
1837}
1838
1839// Overrides ApexModule.IsInstallabeToApex()
1840func (mod *Module) IsInstallableToApex() bool {
1841 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001842 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001843 return true
1844 }
1845 if _, ok := mod.compiler.(*binaryDecorator); ok {
1846 return true
1847 }
1848 }
1849 return false
1850}
1851
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001852// If a library file has a "lib" prefix, extract the library name without the prefix.
1853func libNameFromFilePath(filepath android.Path) (string, bool) {
1854 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1855 if strings.HasPrefix(libName, "lib") {
1856 libName = libName[3:]
1857 return libName, true
1858 }
1859 return "", false
1860}
1861
Sasha Smundaka76acba2022-04-18 20:12:56 -07001862func kytheExtractRustFactory() android.Singleton {
1863 return &kytheExtractRustSingleton{}
1864}
1865
1866type kytheExtractRustSingleton struct {
1867}
1868
1869func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1870 var xrefTargets android.Paths
1871 ctx.VisitAllModules(func(module android.Module) {
1872 if rustModule, ok := module.(xref); ok {
1873 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1874 }
1875 })
1876 if len(xrefTargets) > 0 {
1877 ctx.Phony("xref_rust", xrefTargets...)
1878 }
1879}
1880
Jihoon Kangf78a8902022-09-01 22:47:07 +00001881func (c *Module) Partition() string {
1882 return ""
1883}
1884
Chris Parsons637458d2023-09-19 20:09:00 +00001885func (m *Module) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Tranbcb5f572023-08-24 11:10:01 -04001886 if ctx.ModuleType() == "rust_library_host" || ctx.ModuleType() == "rust_library" {
1887 libraryBp2build(ctx, m)
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001888 } else if ctx.ModuleType() == "rust_proc_macro" {
1889 procMacroBp2build(ctx, m)
Vinh Tran093a57e2023-08-24 12:10:49 -04001890 } else if ctx.ModuleType() == "rust_binary_host" {
1891 binaryBp2build(ctx, m)
Vinh Tran47faaad2023-09-25 14:47:19 -04001892 } else if ctx.ModuleType() == "rust_protobuf_host" || ctx.ModuleType() == "rust_protobuf" {
Vinh Tran0c4b9ec2023-08-24 11:10:01 -04001893 protoLibraryBp2build(ctx, m)
Vinh Tran75159942023-10-17 16:28:04 -04001894 } else if ctx.ModuleType() == "rust_ffi_static" {
1895 ffiStaticBp2build(ctx, m)
Vinh Tranbcb5f572023-08-24 11:10:01 -04001896 } else {
1897 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
1898 }
1899}
1900
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001901// This is a workaround by assuming the conventions that rust crate repos are structured
Vinh Tranc1a6cee2023-08-24 12:10:49 -04001902// while waiting for the sandboxing work to complete.
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001903// TODO(b/297344471): When crate_root prop is set which enforces inputs sandboxing,
1904// always use `srcs` and `compile_data` props to generate `srcs` and `compile_data` attributes
1905// instead of using globs.
Chris Parsons637458d2023-09-19 20:09:00 +00001906func srcsAndCompileDataAttrs(ctx android.Bp2buildMutatorContext, c baseCompiler) (bazel.LabelList, bazel.LabelList) {
Vinh Tranb4bb20f2023-08-24 11:10:01 -04001907 var srcs bazel.LabelList
1908 var compileData bazel.LabelList
1909
1910 if c.Properties.Srcs[0] == "src/lib.rs" {
1911 srcs = android.BazelLabelForModuleSrc(ctx, []string{"src/**/*.rs"})
1912 compileData = android.BazelLabelForModuleSrc(
1913 ctx,
1914 []string{
1915 "src/**/*.proto",
1916 "examples/**/*.rs",
1917 "**/*.md",
1918 },
1919 )
1920 } else {
1921 srcs = android.BazelLabelForModuleSrc(ctx, c.Properties.Srcs)
1922 }
1923
1924 return srcs, compileData
1925}
1926
Ivan Lozanoffee3342019-08-27 12:03:00 -07001927var Bool = proptools.Bool
1928var BoolDefault = proptools.BoolDefault
1929var String = proptools.String
1930var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001931
1932var _ android.OutputFileProducer = (*Module)(nil)