blob: c4fd14859ee666d2f50851bb85656a8ac19c2e36 [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 (
Ivan Lozano183a3212019-10-18 14:18:45 -070018 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +020025 "android/soong/bloaty"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020027 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070028 "android/soong/fuzz"
Ivan Lozanoffee3342019-08-27 12:03:00 -070029 "android/soong/rust/config"
Ivan Lozano872d5792022-03-23 17:31:39 -040030 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070031)
32
33var pctx = android.NewPackageContext("android/soong/rust")
34
35func init() {
36 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070037
38 android.AddNeverAllowRules(
39 android.NeverAllow().
Ivan Lozano03a94c42021-06-28 11:27:11 -040040 NotIn(append(config.RustAllowedPaths, config.DownstreamRustAllowedPaths...)...).
Ivan Lozanoe169ad72019-09-18 08:42:54 -070041 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070042
43 android.RegisterModuleType("rust_defaults", defaultsFactory)
44 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
45 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040046 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040047 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050048
49 })
50 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
51 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070052 })
53 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020054 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070055}
56
57type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040058 GlobalRustFlags []string // Flags that apply globally to rust
59 GlobalLinkFlags []string // Flags that apply globally to linker
60 RustFlags []string // Flags that apply to rust
61 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070063 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070064 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040065 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020066 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070067}
68
69type BaseProperties struct {
70 AndroidMkRlibs []string
71 AndroidMkDylibs []string
72 AndroidMkProcMacroLibs []string
73 AndroidMkSharedLibs []string
74 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040075
Ivan Lozano6a884432020-12-02 09:15:16 -050076 ImageVariationPrefix string `blueprint:"mutated"`
77 VndkVersion string `blueprint:"mutated"`
78 SubName string `blueprint:"mutated"`
79
Ivan Lozanoc08897c2021-04-02 12:41:32 -040080 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
81 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
82 // appended before SubName.
83 RustSubName string `blueprint:"mutated"`
84
Ivan Lozano6a884432020-12-02 09:15:16 -050085 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050086 CoreVariantNeeded bool `blueprint:"mutated"`
87 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070088 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080089 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050090 ExtraVariants []string `blueprint:"mutated"`
91
Ivan Lozanoa2268632021-07-22 10:52:06 -040092 // Allows this module to use non-APEX version of libraries. Useful
93 // for building binaries that are started before APEXes are activated.
94 Bootstrap *bool
95
Ivan Lozano1921e802021-05-20 13:39:16 -040096 // Used by vendor snapshot to record dependencies from snapshot modules.
97 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090098 SnapshotStaticLibs []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
164 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900165
166 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500167
168 // For apex variants, this is set as apex.min_sdk_version
169 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170}
171
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500172func (mod *Module) Header() bool {
173 //TODO: If Rust libraries provide header variants, this needs to be updated.
174 return false
175}
176
177func (mod *Module) SetPreventInstall() {
178 mod.Properties.PreventInstall = true
179}
180
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500181func (mod *Module) SetHideFromMake() {
182 mod.Properties.HideFromMake = true
183}
184
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900185func (mod *Module) HiddenFromMake() bool {
186 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400187}
188
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500189func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500190 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
191 // nil since we need compiler to actually sanitize.
192 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500193}
194
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500195func (mod *Module) IsPrebuilt() bool {
196 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
197 return true
198 }
199 return false
200}
201
Ivan Lozano43845682020-07-09 21:03:28 -0400202func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
203 switch tag {
204 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700205 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400206 return mod.sourceProvider.Srcs(), nil
207 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900208 if mod.OutputFile().Valid() {
209 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400210 }
211 return android.Paths{}, nil
212 }
213 default:
214 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
215 }
216}
217
Ivan Lozano52767be2019-10-18 14:49:46 -0700218func (mod *Module) SelectedStl() string {
219 return ""
220}
221
Ivan Lozano2b262972019-11-21 12:30:50 -0800222func (mod *Module) NonCcVariants() bool {
223 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400224 if _, ok := mod.compiler.(libraryInterface); ok {
225 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800226 }
227 }
228 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
229}
230
Ivan Lozano52767be2019-10-18 14:49:46 -0700231func (mod *Module) Static() bool {
232 if mod.compiler != nil {
233 if library, ok := mod.compiler.(libraryInterface); ok {
234 return library.static()
235 }
236 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400237 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700238}
239
240func (mod *Module) Shared() bool {
241 if mod.compiler != nil {
242 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400243 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700244 }
245 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400246 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700247}
248
Ivan Lozanod7586b62021-04-01 09:49:36 -0400249func (mod *Module) Dylib() bool {
250 if mod.compiler != nil {
251 if library, ok := mod.compiler.(libraryInterface); ok {
252 return library.dylib()
253 }
254 }
255 return false
256}
257
258func (mod *Module) Rlib() bool {
259 if mod.compiler != nil {
260 if library, ok := mod.compiler.(libraryInterface); ok {
261 return library.rlib()
262 }
263 }
264 return false
265}
266
267func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400268 if binary, ok := mod.compiler.(binaryInterface); ok {
269 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400270 }
271 return false
272}
273
Justin Yun5e035862021-06-29 20:50:37 +0900274func (mod *Module) StaticExecutable() bool {
275 if !mod.Binary() {
276 return false
277 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400278 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900279}
280
Ivan Lozanod7586b62021-04-01 09:49:36 -0400281func (mod *Module) Object() bool {
282 // Rust has no modules which produce only object files.
283 return false
284}
285
Ivan Lozano52767be2019-10-18 14:49:46 -0700286func (mod *Module) Toc() android.OptionalPath {
287 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400288 if lib, ok := mod.compiler.(libraryInterface); ok {
289 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700290 }
291 }
292 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
293}
294
Colin Crossc511bc52020-04-07 16:50:32 +0000295func (mod *Module) UseSdk() bool {
296 return false
297}
298
Ivan Lozanod7586b62021-04-01 09:49:36 -0400299func (mod *Module) RelativeInstallPath() string {
300 if mod.compiler != nil {
301 return mod.compiler.relativeInstallPath()
302 }
303 return ""
304}
305
Ivan Lozano52767be2019-10-18 14:49:46 -0700306func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500307 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700308}
309
Jiyong Park7d55b612021-06-11 17:22:09 +0900310func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400311 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900312}
313
Ivan Lozano52767be2019-10-18 14:49:46 -0700314func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400315 return true
316}
317
318func (mod *Module) SubName() string {
319 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700320}
321
322func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500323 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700324 return false
325}
326
Ivan Lozanof9e21722020-12-02 09:00:51 -0500327func (mod *Module) IsVndkExt() bool {
328 return false
329}
330
Ivan Lozanod7586b62021-04-01 09:49:36 -0400331func (mod *Module) IsVndkSp() bool {
332 return false
333}
334
Ivan Lozanof1868af2022-04-12 13:08:36 -0400335func (mod *Module) IsVndkPrebuiltLibrary() bool {
336 // Rust modules do not provide VNDK prebuilts
337 return false
338}
339
340func (mod *Module) IsVendorPublicLibrary() bool {
341 return mod.VendorProperties.IsVendorPublicLibrary
342}
343
344func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
345 // Rust modules to not provide Sdk variants
346 return false
347}
348
Colin Cross127bb8b2020-12-16 16:46:01 -0800349func (c *Module) IsVndkPrivate() bool {
350 return false
351}
352
353func (c *Module) IsLlndk() bool {
354 return false
355}
356
357func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500358 return false
359}
360
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400361func (mod *Module) KernelHeadersDecorator() bool {
362 return false
363}
364
Colin Cross1f3f1302021-04-26 18:37:44 -0700365func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400366 return false
367}
368
Colin Cross5271fea2021-04-27 13:06:04 -0700369func (m *Module) NeedsVendorPublicLibraryVariants() bool {
370 return false
371}
372
Ivan Lozanod7586b62021-04-01 09:49:36 -0400373func (mod *Module) HasLlndkStubs() bool {
374 return false
375}
376
377func (mod *Module) StubsVersion() string {
378 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
379}
380
Ivan Lozano52767be2019-10-18 14:49:46 -0700381func (mod *Module) SdkVersion() string {
382 return ""
383}
384
Colin Crossc511bc52020-04-07 16:50:32 +0000385func (mod *Module) AlwaysSdk() bool {
386 return false
387}
388
Jiyong Park2286afd2020-06-16 21:58:53 +0900389func (mod *Module) IsSdkVariant() bool {
390 return false
391}
392
Colin Cross1348ce32020-10-01 13:37:16 -0700393func (mod *Module) SplitPerApiLevel() bool {
394 return false
395}
396
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400398 Dylibs []string
399 Rlibs []string
400 Rustlibs []string
401 Stdlibs []string
402 ProcMacros []string
403 SharedLibs []string
404 StaticLibs []string
405 WholeStaticLibs []string
406 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400408 // Used for data dependencies adjacent to tests
409 DataLibs []string
410 DataBins []string
411
Colin Crossfe605e12022-01-23 20:46:16 -0800412 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700413}
414
415type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500416 DyLibs RustLibraries
417 RLibs RustLibraries
418 SharedLibs android.Paths
419 SharedLibDeps android.Paths
420 StaticLibs android.Paths
421 ProcMacros RustLibraries
Yi Kong46c6e592022-01-20 22:55:00 +0800422 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500423
424 // depFlags and depLinkFlags are rustc and linker (clang) flags.
425 depFlags []string
426 depLinkFlags []string
427
428 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
429 // Both of these are exported and propagate to dependencies.
430 linkDirs []string
431 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700432
Ivan Lozano45901ed2020-07-24 16:05:01 -0400433 // Used by bindgen modules which call clang
434 depClangFlags []string
435 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400436 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400437 depSystemIncludePaths android.Paths
438
Colin Crossfe605e12022-01-23 20:46:16 -0800439 CrtBegin android.Paths
440 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700441
442 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500443 SrcDeps android.Paths
444 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445}
446
447type RustLibraries []RustLibrary
448
449type RustLibrary struct {
450 Path android.Path
451 CrateName string
452}
453
454type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100455 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700456 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400457 cfgFlags(ctx ModuleContext, flags Flags) Flags
458 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700459 compilerProps() []interface{}
460 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
461 compilerDeps(ctx DepsContext, deps Deps) Deps
462 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700463 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700464
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100465 // Output directory in which source-generated code from dependencies is
466 // copied. This is equivalent to Cargo's OUT_DIR variable.
467 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700468
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400469 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
470 CargoPkgVersion() string
471
472 // CargoEnvCompat returns whether Cargo environment variables should be used.
473 CargoEnvCompat() bool
474
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800475 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200476 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700477 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400478 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400479
480 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400481
482 Disabled() bool
483 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400484
Ivan Lozanodd055472020-09-28 13:22:45 -0400485 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900486
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400487 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900488 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400489}
490
Matthew Maurerbb3add12020-06-25 09:34:12 -0700491type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700492 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400493 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700494}
495
496type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400497 linkDirs []string
498 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499}
500
Matthew Maurerbb3add12020-06-25 09:34:12 -0700501func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
502 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
503}
504
Ivan Lozano2093af22020-08-25 12:48:19 -0400505func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
506 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
507}
508
Colin Cross0de8a1e2020-09-18 14:15:30 -0700509func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
510 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700511 LinkDirs: flagExporter.linkDirs,
512 LinkObjects: flagExporter.linkObjects,
513 })
514}
515
Matthew Maurerbb3add12020-06-25 09:34:12 -0700516var _ exportedFlagsProducer = (*flagExporter)(nil)
517
518func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700519 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700520}
521
Colin Cross0de8a1e2020-09-18 14:15:30 -0700522type FlagExporterInfo struct {
523 Flags []string
524 LinkDirs []string // TODO: this should be android.Paths
525 LinkObjects []string // TODO: this should be android.Paths
526}
527
528var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
529
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400530func (mod *Module) isCoverageVariant() bool {
531 return mod.coverage.Properties.IsCoverageVariant
532}
533
534var _ cc.Coverage = (*Module)(nil)
535
536func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
537 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
538}
539
Ivan Lozanod7586b62021-04-01 09:49:36 -0400540func (mod *Module) VndkVersion() string {
541 return mod.Properties.VndkVersion
542}
543
544func (mod *Module) PreventInstall() bool {
545 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400546}
547
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400548func (mod *Module) MarkAsCoverageVariant(coverage bool) {
549 mod.coverage.Properties.IsCoverageVariant = coverage
550}
551
552func (mod *Module) EnableCoverageIfNeeded() {
553 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554}
555
556func defaultsFactory() android.Module {
557 return DefaultsFactory()
558}
559
560type Defaults struct {
561 android.ModuleBase
562 android.DefaultsModuleBase
563}
564
565func DefaultsFactory(props ...interface{}) android.Module {
566 module := &Defaults{}
567
568 module.AddProperties(props...)
569 module.AddProperties(
570 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800571 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500572 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100573 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400574 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700575 &BaseCompilerProperties{},
576 &BinaryCompilerProperties{},
577 &LibraryCompilerProperties{},
578 &ProcMacroCompilerProperties{},
579 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400580 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700581 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400582 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400583 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200584 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500585 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700586 )
587
588 android.InitDefaultsModule(module)
589 return module
590}
591
592func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700593 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594}
595
Ivan Lozano183a3212019-10-18 14:18:45 -0700596func (mod *Module) CcLibrary() bool {
597 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500598 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700599 return true
600 }
601 }
602 return false
603}
604
605func (mod *Module) CcLibraryInterface() bool {
606 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400607 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
608 // VariantIs{Static,Shared} is set.
609 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700610 return true
611 }
612 }
613 return false
614}
615
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400616func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400617 if mod.compiler != nil {
618 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400619 }
620 return nil
621}
622
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800623func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700624 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700625 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800626 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700627 }
628 }
629 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
630}
631
632func (mod *Module) SetStatic() {
633 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700634 if library, ok := mod.compiler.(libraryInterface); ok {
635 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700636 return
637 }
638 }
639 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
640}
641
642func (mod *Module) SetShared() {
643 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700644 if library, ok := mod.compiler.(libraryInterface); ok {
645 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700646 return
647 }
648 }
649 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
650}
651
Ivan Lozano183a3212019-10-18 14:18:45 -0700652func (mod *Module) BuildStaticVariant() bool {
653 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700654 if library, ok := mod.compiler.(libraryInterface); ok {
655 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700656 }
657 }
658 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
659}
660
661func (mod *Module) BuildSharedVariant() bool {
662 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700663 if library, ok := mod.compiler.(libraryInterface); ok {
664 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700665 }
666 }
667 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
668}
669
Ivan Lozano183a3212019-10-18 14:18:45 -0700670func (mod *Module) Module() android.Module {
671 return mod
672}
673
Ivan Lozano183a3212019-10-18 14:18:45 -0700674func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400675 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700676}
677
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400678func (mod *Module) CoverageFiles() android.Paths {
679 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800680 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400681 }
682 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
683}
684
Jiyong Park459feca2020-12-15 11:02:21 +0900685func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900686 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900687 return false
688 }
689
Jiyong Park459feca2020-12-15 11:02:21 +0900690 // The apex variant is not installable because it is included in the APEX and won't appear
691 // in the system partition as a standalone file.
692 if !apexInfo.IsForPlatform() {
693 return false
694 }
695
Jiyong Parke54f07e2021-04-07 15:08:04 +0900696 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900697}
698
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500699func (ctx moduleContext) apexVariationName() string {
700 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
701}
702
Ivan Lozano183a3212019-10-18 14:18:45 -0700703var _ cc.LinkableInterface = (*Module)(nil)
704
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705func (mod *Module) Init() android.Module {
706 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500707 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708
Yi Kong46c6e592022-01-20 22:55:00 +0800709 if mod.afdo != nil {
710 mod.AddProperties(mod.afdo.props()...)
711 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700712 if mod.compiler != nil {
713 mod.AddProperties(mod.compiler.compilerProps()...)
714 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400715 if mod.coverage != nil {
716 mod.AddProperties(mod.coverage.props()...)
717 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200718 if mod.clippy != nil {
719 mod.AddProperties(mod.clippy.props()...)
720 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400721 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700722 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400723 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500724 if mod.sanitize != nil {
725 mod.AddProperties(mod.sanitize.props()...)
726 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400727
Ivan Lozanoffee3342019-08-27 12:03:00 -0700728 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900729 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730
731 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732 return mod
733}
734
735func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
736 return &Module{
737 hod: hod,
738 multilib: multilib,
739 }
740}
741func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
742 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800743 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400744 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200745 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500746 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747 return module
748}
749
750type ModuleContext interface {
751 android.ModuleContext
752 ModuleContextIntf
753}
754
755type BaseModuleContext interface {
756 android.BaseModuleContext
757 ModuleContextIntf
758}
759
760type DepsContext interface {
761 android.BottomUpMutatorContext
762 ModuleContextIntf
763}
764
765type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200766 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768}
769
770type depsContext struct {
771 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772}
773
774type moduleContext struct {
775 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700776}
777
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200778type baseModuleContext struct {
779 android.BaseModuleContext
780}
781
782func (ctx *moduleContext) RustModule() *Module {
783 return ctx.Module().(*Module)
784}
785
786func (ctx *moduleContext) toolchain() config.Toolchain {
787 return ctx.RustModule().toolchain(ctx)
788}
789
790func (ctx *depsContext) RustModule() *Module {
791 return ctx.Module().(*Module)
792}
793
794func (ctx *depsContext) toolchain() config.Toolchain {
795 return ctx.RustModule().toolchain(ctx)
796}
797
798func (ctx *baseModuleContext) RustModule() *Module {
799 return ctx.Module().(*Module)
800}
801
802func (ctx *baseModuleContext) toolchain() config.Toolchain {
803 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400804}
805
806func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700807 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
808 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
809 return false
810 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400811 return mod.compiler != nil && mod.compiler.nativeCoverage()
812}
813
Ivan Lozanod7586b62021-04-01 09:49:36 -0400814func (mod *Module) EverInstallable() bool {
815 return mod.compiler != nil &&
816 // Check to see whether the module is actually ever installable.
817 mod.compiler.everInstallable()
818}
819
820func (mod *Module) Installable() *bool {
821 return mod.Properties.Installable
822}
823
Ivan Lozano872d5792022-03-23 17:31:39 -0400824func (mod *Module) ProcMacro() bool {
825 if pm, ok := mod.compiler.(procMacroInterface); ok {
826 return pm.ProcMacro()
827 }
828 return false
829}
830
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
832 if mod.cachedToolchain == nil {
833 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
834 }
835 return mod.cachedToolchain
836}
837
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200838func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
839 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
840}
841
Ivan Lozanoffee3342019-08-27 12:03:00 -0700842func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
843}
844
845func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
846 ctx := &moduleContext{
847 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700848 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849
Jiyong Park99644e92020-11-17 22:21:02 +0900850 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
851 if !apexInfo.IsForPlatform() {
852 mod.hideApexVariantFromMake = true
853 }
854
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500856 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
857
Ivan Lozanof1868af2022-04-12 13:08:36 -0400858 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700859
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860 if !toolchain.Supported() {
861 // This toolchain's unsupported, there's nothing to do for this mod.
862 return
863 }
864
865 deps := mod.depsToPaths(ctx)
866 flags := Flags{
867 Toolchain: toolchain,
868 }
869
Ivan Lozano67eada32021-09-23 11:50:33 -0400870 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800871 if mod.afdo != nil {
872 flags, deps = mod.afdo.flags(ctx, flags, deps)
873 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700874 if mod.compiler != nil {
875 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400876 flags = mod.compiler.cfgFlags(ctx, flags)
877 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400878 }
879 if mod.coverage != nil {
880 flags, deps = mod.coverage.flags(ctx, flags, deps)
881 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200882 if mod.clippy != nil {
883 flags, deps = mod.clippy.flags(ctx, flags, deps)
884 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500885 if mod.sanitize != nil {
886 flags, deps = mod.sanitize.flags(ctx, flags, deps)
887 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400888
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200889 // SourceProvider needs to call GenerateSource() before compiler calls
890 // compile() so it can provide the source. A SourceProvider has
891 // multiple variants (e.g. source, rlib, dylib). Only the "source"
892 // variant is responsible for effectively generating the source. The
893 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400894 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200895 if mod.compiler.(libraryInterface).source() {
896 mod.sourceProvider.GenerateSource(ctx, deps)
897 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
898 } else {
899 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
900 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700901 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200902 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400903 }
904
905 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100906 mod.compiler.initialize(ctx)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400907 outputFile := mod.compiler.compile(ctx, flags, deps)
908 if ctx.Failed() {
909 return
910 }
911 mod.outputFile = android.OptionalPathForPath(outputFile)
912 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900913
Dan Albert06feee92021-03-19 15:06:02 -0700914 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000915 if mod.docTimestampFile.Valid() {
916 ctx.CheckbuildFile(mod.docTimestampFile.Path())
917 }
Dan Albert06feee92021-03-19 15:06:02 -0700918
Ivan Lozano1921e802021-05-20 13:39:16 -0400919 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
920 // RECOVERY_SNAPSHOT_VERSION is current.
921 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
922 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
923 lib.collectHeadersForSnapshot(ctx, deps)
924 }
925 }
926
Jiyong Park459feca2020-12-15 11:02:21 +0900927 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400928 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900929 // If the module has been specifically configure to not be installed then
930 // hide from make as otherwise it will break when running inside make as the
931 // output path to install will not be specified. Not all uninstallable
932 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400933 // side dependencies. In particular, proc-macros need to be captured in the
934 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900935 mod.HideFromMake()
936 } else if !mod.installable(apexInfo) {
937 mod.SkipInstall()
938 }
939
940 // Still call install though, the installs will be stored as PackageSpecs to allow
941 // using the outputs in a genrule.
942 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200943 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900944 if ctx.Failed() {
945 return
946 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400947 }
Chris Wailes74be7642021-07-22 16:20:28 -0700948
949 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950 }
951}
952
953func (mod *Module) deps(ctx DepsContext) Deps {
954 deps := Deps{}
955
956 if mod.compiler != nil {
957 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400958 }
959 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700960 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961 }
962
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400963 if mod.coverage != nil {
964 deps = mod.coverage.deps(ctx, deps)
965 }
966
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500967 if mod.sanitize != nil {
968 deps = mod.sanitize.deps(ctx, deps)
969 }
970
Ivan Lozanoffee3342019-08-27 12:03:00 -0700971 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
972 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700973 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
975 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
976 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000977 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400978 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979 return deps
980
981}
982
Ivan Lozanoffee3342019-08-27 12:03:00 -0700983type dependencyTag struct {
984 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800985 name string
986 library bool
987 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +0000988 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700989}
990
Jiyong Park65b62242020-11-25 12:44:59 +0900991// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
992// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
993func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800994 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900995}
996
997var _ android.InstallNeededDependencyTag = dependencyTag{}
998
Colin Cross65cb3142021-12-10 23:05:02 +0000999func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1000 if d.library && d.dynamic {
1001 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1002 }
1003 return nil
1004}
1005
1006var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1007
Ivan Lozanoffee3342019-08-27 12:03:00 -07001008var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001009 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1010 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001011 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001012 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001013 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001014 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001015 dataLibDepTag = dependencyTag{name: "data lib"}
1016 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017)
1018
Jiyong Park99644e92020-11-17 22:21:02 +09001019func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1020 tag, ok := depTag.(dependencyTag)
1021 return ok && tag == dylibDepTag
1022}
1023
Jiyong Park94e22fd2021-04-08 18:19:15 +09001024func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1025 tag, ok := depTag.(dependencyTag)
1026 return ok && tag == rlibDepTag
1027}
1028
Matthew Maurer0f003b12020-06-29 14:34:06 -07001029type autoDep struct {
1030 variation string
1031 depTag dependencyTag
1032}
1033
1034var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001035 rlibVariation = "rlib"
1036 dylibVariation = "dylib"
1037 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1038 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001039)
1040
1041type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001042 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001043}
1044
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001045func (mod *Module) begin(ctx BaseModuleContext) {
1046 if mod.coverage != nil {
1047 mod.coverage.begin(ctx)
1048 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001049 if mod.sanitize != nil {
1050 mod.sanitize.begin(ctx)
1051 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001052}
1053
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001054func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001055 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001056 return p.prebuilt()
1057 }
1058 return nil
1059}
1060
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1062 var depPaths PathDeps
1063
1064 directRlibDeps := []*Module{}
1065 directDylibDeps := []*Module{}
1066 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001067 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001068 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001069 directSrcProvidersDeps := []*Module{}
1070 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001071
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001072 // For the dependency from platform to apex, use the latest stubs
1073 mod.apexSdkVersion = android.FutureApiLevel
1074 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1075 if !apexInfo.IsForPlatform() {
1076 mod.apexSdkVersion = apexInfo.MinSdkVersion
1077 }
1078
1079 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1080 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1081 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1082 // (b/144430859)
1083 mod.apexSdkVersion = android.FutureApiLevel
1084 }
1085
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086 ctx.VisitDirectDeps(func(dep android.Module) {
1087 depName := ctx.OtherModuleName(dep)
1088 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001089
Ivan Lozano89435d12020-07-31 11:01:18 -04001090 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001091 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001092 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001093
Ivan Lozanoffee3342019-08-27 12:03:00 -07001094 switch depTag {
1095 case dylibDepTag:
1096 dylib, ok := rustDep.compiler.(libraryInterface)
1097 if !ok || !dylib.dylib() {
1098 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1099 return
1100 }
1101 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001102 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001103 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001104
Ivan Lozanoffee3342019-08-27 12:03:00 -07001105 rlib, ok := rustDep.compiler.(libraryInterface)
1106 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001107 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001108 return
1109 }
1110 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001111 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001112 case procMacroDepTag:
1113 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001114 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001115 }
1116
1117 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001118 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1119 // OS/Arch variant is used.
1120 var helper string
1121 if ctx.Host() {
1122 helper = "missing 'host_supported'?"
1123 } else {
1124 helper = "device module defined?"
1125 }
1126
1127 if dep.Target().Os != ctx.Os() {
1128 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1129 return
1130 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1131 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1132 return
1133 }
1134 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001135 }
1136
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001137 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001138 if depTag != procMacroDepTag {
1139 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1140 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1141 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1142 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001143 }
1144
Ivan Lozanoffee3342019-08-27 12:03:00 -07001145 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001146 linkFile := rustDep.UnstrippedOutputFile()
1147 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001148 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1149 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001150 }
1151 }
1152
Ivan Lozano89435d12020-07-31 11:01:18 -04001153 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001154 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001155 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001156 if _, ok := ccDep.(*Module); !ok {
1157 if ccDep.Module().Target().Os != ctx.Os() {
1158 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1159 return
1160 }
1161 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1162 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1163 return
1164 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001165 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001166 linkObject := ccDep.OutputFile()
1167 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001168
Ivan Lozano2093af22020-08-25 12:48:19 -04001169 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001170 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1171 }
1172
1173 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001174 switch {
1175 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001176 if cc.IsWholeStaticLib(depTag) {
1177 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1178 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001179 if mod.Binary() {
1180 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1181 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1182 // final linkage, pass the args directly to the linker to handle these cases.
1183 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1184 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001185 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001186 } else {
1187 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 -05001188 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001189 }
1190
1191 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1192 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001193 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001194 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1195
Colin Cross0de8a1e2020-09-18 14:15:30 -07001196 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1197 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1198 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1199 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1200 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001201 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001202
1203 // Record baseLibName for snapshots.
1204 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1205
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001206 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001207 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001208 // For the shared lib dependencies, we may link to the stub variant
1209 // of the dependency depending on the context (e.g. if this
1210 // dependency crosses the APEX boundaries).
1211 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1212
1213 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1214 // object (either from stub or non-stub) to use.
1215 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1216 linkPath = linkPathFromFilePath(linkObject.Path())
1217
Ivan Lozanoffee3342019-08-27 12:03:00 -07001218 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001219 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001220 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1221 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1222 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1223 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001224 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001225
1226 // Record baseLibName for snapshots.
1227 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1228
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001229 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001231 case cc.IsHeaderDepTag(depTag):
1232 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1233 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1234 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1235 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001236 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001237 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001238 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001239 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001240 }
1241
1242 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001243 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1244 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001245 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001247 } else {
1248 switch {
1249 case depTag == cc.CrtBeginDepTag:
1250 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1251 case depTag == cc.CrtEndDepTag:
1252 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1253 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001254 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001255
1256 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001257 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001258 // These are usually genrules which don't have per-target variants.
1259 directSrcDeps = append(directSrcDeps, srcDep)
1260 }
1261 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001262 })
1263
1264 var rlibDepFiles RustLibraries
1265 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001266 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001267 }
1268 var dylibDepFiles RustLibraries
1269 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001270 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001271 }
1272 var procMacroDepFiles RustLibraries
1273 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001274 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275 }
1276
1277 var staticLibDepFiles android.Paths
1278 for _, dep := range directStaticLibDeps {
1279 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1280 }
1281
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001282 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 var sharedLibDepFiles android.Paths
1284 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001285 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1286 if dep.TableOfContents.Valid() {
1287 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001288 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001289 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001290 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001291 }
1292
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001293 var srcProviderDepFiles android.Paths
1294 for _, dep := range directSrcProvidersDeps {
1295 srcs, _ := dep.OutputFiles("")
1296 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1297 }
1298 for _, dep := range directSrcDeps {
1299 srcs := dep.Srcs()
1300 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1301 }
1302
Ivan Lozanoffee3342019-08-27 12:03:00 -07001303 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1304 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1305 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001306 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001307 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1308 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001309 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001310
1311 // Dedup exported flags from dependencies
1312 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001313 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001314 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001315 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1316 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1317 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001318
1319 return depPaths
1320}
1321
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001322func (mod *Module) InstallInData() bool {
1323 if mod.compiler == nil {
1324 return false
1325 }
1326 return mod.compiler.inData()
1327}
1328
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001329func (mod *Module) InstallInRamdisk() bool {
1330 return mod.InRamdisk()
1331}
1332
1333func (mod *Module) InstallInVendorRamdisk() bool {
1334 return mod.InVendorRamdisk()
1335}
1336
1337func (mod *Module) InstallInRecovery() bool {
1338 return mod.InRecovery()
1339}
1340
Ivan Lozanoffee3342019-08-27 12:03:00 -07001341func linkPathFromFilePath(filepath android.Path) string {
1342 return strings.Split(filepath.String(), filepath.Base())[0]
1343}
Ivan Lozanod648c432020-02-06 12:05:10 -05001344
Ivan Lozanoffee3342019-08-27 12:03:00 -07001345func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1346 ctx := &depsContext{
1347 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001348 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001349
1350 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001351 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001352 var snapshotInfo *cc.SnapshotInfo
1353
1354 if ctx.Os() == android.Android {
1355 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1356 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001357
Ivan Lozano2b081132020-09-08 12:46:52 -04001358 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001359 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001360 stdLinkage = "rlib-std"
1361 }
1362
1363 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001364
Ivan Lozano2b081132020-09-08 12:46:52 -04001365 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1366 rlibDepVariations = append(rlibDepVariations,
1367 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1368 }
1369
Ivan Lozano1921e802021-05-20 13:39:16 -04001370 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001371 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001372 for _, lib := range deps.Rlibs {
1373 depTag := rlibDepTag
1374 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1375
Ivan Lozano2d407632022-04-07 12:59:11 -04001376 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001377 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001378
1379 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001380 actx.AddVariationDependencies(
1381 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001382 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001383 dylibDepTag, deps.Dylibs...)
1384
Ivan Lozano1921e802021-05-20 13:39:16 -04001385 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001386 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1387 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001388 for _, lib := range deps.Rustlibs {
1389 if autoDep.depTag == rlibDepTag {
1390 // Handle the rlib deptag case
1391 addRlibDependency(actx, lib, mod, snapshotInfo, rlibDepVariations)
1392 } else {
1393 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1394 // Check for the existence of the dylib deptag variant. Select it if available,
1395 // otherwise select the rlib variant.
1396 autoDepVariations := append(commonDepVariations,
1397 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1398 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1399 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
1400 } else {
1401 // If there's no dylib dependency available, try to add the rlib dependency instead.
1402 addRlibDependency(actx, lib, mod, snapshotInfo, rlibDepVariations)
1403 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001404 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001405 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001406 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001407 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001408 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001409 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001410 for _, lib := range deps.Stdlibs {
1411 depTag := rlibDepTag
1412 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1413
1414 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1415 depTag, lib)
1416 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001417 } else {
1418 actx.AddVariationDependencies(
1419 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1420 dylibDepTag, deps.Stdlibs...)
1421 }
1422 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001423
1424 for _, lib := range deps.SharedLibs {
1425 depTag := cc.SharedDepTag()
1426 name, version := cc.StubsLibNameAndVersion(lib)
1427
1428 variations := []blueprint.Variation{
1429 {Mutator: "link", Variation: "shared"},
1430 }
1431 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1432 }
1433
1434 for _, lib := range deps.WholeStaticLibs {
1435 depTag := cc.StaticDepTag(true)
1436 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1437
1438 actx.AddVariationDependencies([]blueprint.Variation{
1439 {Mutator: "link", Variation: "static"},
1440 }, depTag, lib)
1441 }
1442
1443 for _, lib := range deps.StaticLibs {
1444 depTag := cc.StaticDepTag(false)
1445 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1446
1447 actx.AddVariationDependencies([]blueprint.Variation{
1448 {Mutator: "link", Variation: "static"},
1449 }, depTag, lib)
1450 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001451
Zach Johnson3df4e632020-11-06 11:56:27 -08001452 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1453
Colin Cross565cafd2020-09-25 18:47:38 -07001454 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001455 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001456 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Colin Crossfe605e12022-01-23 20:46:16 -08001457 cc.RewriteSnapshotLib(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001458 }
Colin Crossfe605e12022-01-23 20:46:16 -08001459 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001460 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Colin Crossfe605e12022-01-23 20:46:16 -08001461 cc.RewriteSnapshotLib(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001462 }
1463
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001464 if mod.sourceProvider != nil {
1465 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1466 bindgen.Properties.Custom_bindgen != "" {
1467 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1468 bindgen.Properties.Custom_bindgen)
1469 }
1470 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001471
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001472 actx.AddVariationDependencies([]blueprint.Variation{
1473 {Mutator: "link", Variation: "shared"},
1474 }, dataLibDepTag, deps.DataLibs...)
1475
1476 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1477
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001478 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001479 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480}
1481
Ivan Lozano2d407632022-04-07 12:59:11 -04001482// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
1483func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo *cc.SnapshotInfo, variations []blueprint.Variation) {
1484 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1485 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1486}
1487
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001488func BeginMutator(ctx android.BottomUpMutatorContext) {
1489 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1490 mod.beginMutator(ctx)
1491 }
1492}
1493
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001494func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1495 ctx := &baseModuleContext{
1496 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001497 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001498
1499 mod.begin(ctx)
1500}
1501
Ivan Lozanoffee3342019-08-27 12:03:00 -07001502func (mod *Module) Name() string {
1503 name := mod.ModuleBase.Name()
1504 if p, ok := mod.compiler.(interface {
1505 Name(string) string
1506 }); ok {
1507 name = p.Name(name)
1508 }
1509 return name
1510}
1511
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001512func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001513 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001514 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001515 }
1516}
1517
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001518var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001519var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001520
1521func (mod *Module) HostToolPath() android.OptionalPath {
1522 if !mod.Host() {
1523 return android.OptionalPath{}
1524 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001525 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1526 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001527 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1528 // Even though proc-macros aren't strictly "tools", since they target the compiler
1529 // and act as compiler plugins, we treat them similarly.
1530 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001531 }
1532 return android.OptionalPath{}
1533}
1534
Jiyong Park99644e92020-11-17 22:21:02 +09001535var _ android.ApexModule = (*Module)(nil)
1536
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001537func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001538 return String(mod.Properties.Min_sdk_version)
1539}
1540
Jiyong Park45bf82e2020-12-15 22:29:02 +09001541// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001542func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001543 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001544 if minSdkVersion == "apex_inherit" {
1545 return nil
1546 }
1547 if minSdkVersion == "" {
1548 return fmt.Errorf("min_sdk_version is not specificed")
1549 }
1550
1551 // Not using nativeApiLevelFromUser because the context here is not
1552 // necessarily a native context.
1553 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1554 if err != nil {
1555 return err
1556 }
1557
1558 if ver.GreaterThan(sdkVersion) {
1559 return fmt.Errorf("newer SDK(%v)", ver)
1560 }
Jiyong Park99644e92020-11-17 22:21:02 +09001561 return nil
1562}
1563
Jiyong Park45bf82e2020-12-15 22:29:02 +09001564// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001565func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1566 depTag := ctx.OtherModuleDependencyTag(dep)
1567
1568 if ccm, ok := dep.(*cc.Module); ok {
1569 if ccm.HasStubsVariants() {
1570 if cc.IsSharedDepTag(depTag) {
1571 // dynamic dep to a stubs lib crosses APEX boundary
1572 return false
1573 }
1574 if cc.IsRuntimeDepTag(depTag) {
1575 // runtime dep to a stubs lib also crosses APEX boundary
1576 return false
1577 }
1578
1579 if cc.IsHeaderDepTag(depTag) {
1580 return false
1581 }
1582 }
1583 if mod.Static() && cc.IsSharedDepTag(depTag) {
1584 // shared_lib dependency from a static lib is considered as crossing
1585 // the APEX boundary because the dependency doesn't actually is
1586 // linked; the dependency is used only during the compilation phase.
1587 return false
1588 }
1589 }
1590
1591 if depTag == procMacroDepTag {
1592 return false
1593 }
1594
1595 return true
1596}
1597
1598// Overrides ApexModule.IsInstallabeToApex()
1599func (mod *Module) IsInstallableToApex() bool {
1600 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001601 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001602 return true
1603 }
1604 if _, ok := mod.compiler.(*binaryDecorator); ok {
1605 return true
1606 }
1607 }
1608 return false
1609}
1610
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001611// If a library file has a "lib" prefix, extract the library name without the prefix.
1612func libNameFromFilePath(filepath android.Path) (string, bool) {
1613 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1614 if strings.HasPrefix(libName, "lib") {
1615 libName = libName[3:]
1616 return libName, true
1617 }
1618 return "", false
1619}
1620
Ivan Lozanoffee3342019-08-27 12:03:00 -07001621var Bool = proptools.Bool
1622var BoolDefault = proptools.BoolDefault
1623var String = proptools.String
1624var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001625
1626var _ android.OutputFileProducer = (*Module)(nil)