blob: c159e4a873daba5c282e60b539ad96c01f63aacd [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"
30)
31
32var pctx = android.NewPackageContext("android/soong/rust")
33
34func init() {
35 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070036
37 android.AddNeverAllowRules(
38 android.NeverAllow().
Ivan Lozano03a94c42021-06-28 11:27:11 -040039 NotIn(append(config.RustAllowedPaths, config.DownstreamRustAllowedPaths...)...).
Ivan Lozanoe169ad72019-09-18 08:42:54 -070040 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070041
42 android.RegisterModuleType("rust_defaults", defaultsFactory)
43 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040045 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040046 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050047
48 })
49 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
50 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070051 })
52 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020053 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070054}
55
56type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040057 GlobalRustFlags []string // Flags that apply globally to rust
58 GlobalLinkFlags []string // Flags that apply globally to linker
59 RustFlags []string // Flags that apply to rust
60 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070062 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070063 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040064 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020065 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type BaseProperties struct {
69 AndroidMkRlibs []string
70 AndroidMkDylibs []string
71 AndroidMkProcMacroLibs []string
72 AndroidMkSharedLibs []string
73 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040074
Ivan Lozano6a884432020-12-02 09:15:16 -050075 ImageVariationPrefix string `blueprint:"mutated"`
76 VndkVersion string `blueprint:"mutated"`
77 SubName string `blueprint:"mutated"`
78
Ivan Lozanoc08897c2021-04-02 12:41:32 -040079 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
80 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
81 // appended before SubName.
82 RustSubName string `blueprint:"mutated"`
83
Ivan Lozano6a884432020-12-02 09:15:16 -050084 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 CoreVariantNeeded bool `blueprint:"mutated"`
86 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070087 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080088 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050089 ExtraVariants []string `blueprint:"mutated"`
90
Ivan Lozanoa2268632021-07-22 10:52:06 -040091 // Allows this module to use non-APEX version of libraries. Useful
92 // for building binaries that are started before APEXes are activated.
93 Bootstrap *bool
94
Ivan Lozano1921e802021-05-20 13:39:16 -040095 // Used by vendor snapshot to record dependencies from snapshot modules.
96 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090097 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040098
Matthew Maurerc6868382021-07-13 14:12:37 -070099 // Make this module available when building for ramdisk.
100 // On device without a dedicated recovery partition, the module is only
101 // available after switching root into
102 // /first_stage_ramdisk. To expose the module before switching root, install
103 // the recovery variant instead.
104 Ramdisk_available *bool
105
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500106 // Make this module available when building for vendor ramdisk.
107 // On device without a dedicated recovery partition, the module is only
108 // available after switching root into
109 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800110 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500111 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400112
Ivan Lozano1921e802021-05-20 13:39:16 -0400113 // Normally Soong uses the directory structure to decide which modules
114 // should be included (framework) or excluded (non-framework) from the
115 // different snapshots (vendor, recovery, etc.), but this property
116 // allows a partner to exclude a module normally thought of as a
117 // framework module from the vendor snapshot.
118 Exclude_from_vendor_snapshot *bool
119
120 // Normally Soong uses the directory structure to decide which modules
121 // should be included (framework) or excluded (non-framework) from the
122 // different snapshots (vendor, recovery, etc.), but this property
123 // allows a partner to exclude a module normally thought of as a
124 // framework module from the recovery snapshot.
125 Exclude_from_recovery_snapshot *bool
126
Matthew Maurer460ee942021-02-11 12:31:46 -0800127 // Make this module available when building for recovery
128 Recovery_available *bool
129
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500130 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
131 Min_sdk_version *string
132
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900133 HideFromMake bool `blueprint:"mutated"`
134 PreventInstall bool `blueprint:"mutated"`
135
136 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137}
138
139type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700140 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
Ivan Lozano6a884432020-12-02 09:15:16 -0500142 VendorProperties cc.VendorProperties
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144 Properties BaseProperties
145
146 hod android.HostOrDeviceSupported
147 multilib android.Multilib
148
Ivan Lozano6a884432020-12-02 09:15:16 -0500149 makeLinkType string
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400152 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200153 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500154 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700157 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158
Jiyong Parke54f07e2021-04-07 15:08:04 +0900159 // Unstripped output. This is usually used when this module is linked to another module
160 // as a library. The stripped output which is used for installation can be found via
161 // compiler.strippedOutputFile if it exists.
162 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700163 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900164
165 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700166}
167
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500168func (mod *Module) Header() bool {
169 //TODO: If Rust libraries provide header variants, this needs to be updated.
170 return false
171}
172
173func (mod *Module) SetPreventInstall() {
174 mod.Properties.PreventInstall = true
175}
176
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500177func (mod *Module) SetHideFromMake() {
178 mod.Properties.HideFromMake = true
179}
180
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900181func (mod *Module) HiddenFromMake() bool {
182 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400183}
184
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500185func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500186 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
187 // nil since we need compiler to actually sanitize.
188 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500189}
190
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500191func (mod *Module) IsPrebuilt() bool {
192 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
193 return true
194 }
195 return false
196}
197
Ivan Lozano43845682020-07-09 21:03:28 -0400198func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
199 switch tag {
200 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700201 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400202 return mod.sourceProvider.Srcs(), nil
203 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900204 if mod.OutputFile().Valid() {
205 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400206 }
207 return android.Paths{}, nil
208 }
209 default:
210 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
211 }
212}
213
Ivan Lozano52767be2019-10-18 14:49:46 -0700214func (mod *Module) SelectedStl() string {
215 return ""
216}
217
Ivan Lozano2b262972019-11-21 12:30:50 -0800218func (mod *Module) NonCcVariants() bool {
219 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400220 if _, ok := mod.compiler.(libraryInterface); ok {
221 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800222 }
223 }
224 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
225}
226
Ivan Lozano52767be2019-10-18 14:49:46 -0700227func (mod *Module) Static() bool {
228 if mod.compiler != nil {
229 if library, ok := mod.compiler.(libraryInterface); ok {
230 return library.static()
231 }
232 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400233 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700234}
235
236func (mod *Module) Shared() bool {
237 if mod.compiler != nil {
238 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400239 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700240 }
241 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400242 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700243}
244
Ivan Lozanod7586b62021-04-01 09:49:36 -0400245func (mod *Module) Dylib() bool {
246 if mod.compiler != nil {
247 if library, ok := mod.compiler.(libraryInterface); ok {
248 return library.dylib()
249 }
250 }
251 return false
252}
253
254func (mod *Module) Rlib() bool {
255 if mod.compiler != nil {
256 if library, ok := mod.compiler.(libraryInterface); ok {
257 return library.rlib()
258 }
259 }
260 return false
261}
262
263func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400264 if binary, ok := mod.compiler.(binaryInterface); ok {
265 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400266 }
267 return false
268}
269
Justin Yun5e035862021-06-29 20:50:37 +0900270func (mod *Module) StaticExecutable() bool {
271 if !mod.Binary() {
272 return false
273 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400274 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900275}
276
Ivan Lozanod7586b62021-04-01 09:49:36 -0400277func (mod *Module) Object() bool {
278 // Rust has no modules which produce only object files.
279 return false
280}
281
Ivan Lozano52767be2019-10-18 14:49:46 -0700282func (mod *Module) Toc() android.OptionalPath {
283 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400284 if lib, ok := mod.compiler.(libraryInterface); ok {
285 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700286 }
287 }
288 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
289}
290
Colin Crossc511bc52020-04-07 16:50:32 +0000291func (mod *Module) UseSdk() bool {
292 return false
293}
294
Ivan Lozanod7586b62021-04-01 09:49:36 -0400295func (mod *Module) RelativeInstallPath() string {
296 if mod.compiler != nil {
297 return mod.compiler.relativeInstallPath()
298 }
299 return ""
300}
301
Ivan Lozano52767be2019-10-18 14:49:46 -0700302func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500303 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700304}
305
Jiyong Park7d55b612021-06-11 17:22:09 +0900306func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400307 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900308}
309
Ivan Lozano52767be2019-10-18 14:49:46 -0700310func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400311 return true
312}
313
314func (mod *Module) SubName() string {
315 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700316}
317
318func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500319 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700320 return false
321}
322
Ivan Lozanof9e21722020-12-02 09:00:51 -0500323func (mod *Module) IsVndkExt() bool {
324 return false
325}
326
Ivan Lozanod7586b62021-04-01 09:49:36 -0400327func (mod *Module) IsVndkSp() bool {
328 return false
329}
330
Colin Cross127bb8b2020-12-16 16:46:01 -0800331func (c *Module) IsVndkPrivate() bool {
332 return false
333}
334
335func (c *Module) IsLlndk() bool {
336 return false
337}
338
339func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500340 return false
341}
342
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400343func (mod *Module) KernelHeadersDecorator() bool {
344 return false
345}
346
Colin Cross1f3f1302021-04-26 18:37:44 -0700347func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400348 return false
349}
350
Colin Cross5271fea2021-04-27 13:06:04 -0700351func (m *Module) NeedsVendorPublicLibraryVariants() bool {
352 return false
353}
354
Ivan Lozanod7586b62021-04-01 09:49:36 -0400355func (mod *Module) HasLlndkStubs() bool {
356 return false
357}
358
359func (mod *Module) StubsVersion() string {
360 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
361}
362
Ivan Lozano52767be2019-10-18 14:49:46 -0700363func (mod *Module) SdkVersion() string {
364 return ""
365}
366
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900367func (mod *Module) MinSdkVersion() string {
368 return ""
369}
370
Colin Crossc511bc52020-04-07 16:50:32 +0000371func (mod *Module) AlwaysSdk() bool {
372 return false
373}
374
Jiyong Park2286afd2020-06-16 21:58:53 +0900375func (mod *Module) IsSdkVariant() bool {
376 return false
377}
378
Colin Cross1348ce32020-10-01 13:37:16 -0700379func (mod *Module) SplitPerApiLevel() bool {
380 return false
381}
382
Ivan Lozanoffee3342019-08-27 12:03:00 -0700383type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400384 Dylibs []string
385 Rlibs []string
386 Rustlibs []string
387 Stdlibs []string
388 ProcMacros []string
389 SharedLibs []string
390 StaticLibs []string
391 WholeStaticLibs []string
392 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393
394 CrtBegin, CrtEnd string
395}
396
397type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500398 DyLibs RustLibraries
399 RLibs RustLibraries
400 SharedLibs android.Paths
401 SharedLibDeps android.Paths
402 StaticLibs android.Paths
403 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500404
405 // depFlags and depLinkFlags are rustc and linker (clang) flags.
406 depFlags []string
407 depLinkFlags []string
408
409 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
410 // Both of these are exported and propagate to dependencies.
411 linkDirs []string
412 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700413
Ivan Lozano45901ed2020-07-24 16:05:01 -0400414 // Used by bindgen modules which call clang
415 depClangFlags []string
416 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400417 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400418 depSystemIncludePaths android.Paths
419
Ivan Lozanof1c84332019-09-20 11:00:37 -0700420 CrtBegin android.OptionalPath
421 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700422
423 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500424 SrcDeps android.Paths
425 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700426}
427
428type RustLibraries []RustLibrary
429
430type RustLibrary struct {
431 Path android.Path
432 CrateName string
433}
434
435type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100436 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400438 cfgFlags(ctx ModuleContext, flags Flags) Flags
439 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440 compilerProps() []interface{}
441 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
442 compilerDeps(ctx DepsContext, deps Deps) Deps
443 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700444 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100446 // Output directory in which source-generated code from dependencies is
447 // copied. This is equivalent to Cargo's OUT_DIR variable.
448 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700449
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400450 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
451 CargoPkgVersion() string
452
453 // CargoEnvCompat returns whether Cargo environment variables should be used.
454 CargoEnvCompat() bool
455
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800456 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200457 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700458 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400459 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400460
461 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400462
463 Disabled() bool
464 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400465
Ivan Lozanodd055472020-09-28 13:22:45 -0400466 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900467
468 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400469}
470
Matthew Maurerbb3add12020-06-25 09:34:12 -0700471type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700472 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400473 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700474}
475
476type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400477 linkDirs []string
478 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700479}
480
Matthew Maurerbb3add12020-06-25 09:34:12 -0700481func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
482 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
483}
484
Ivan Lozano2093af22020-08-25 12:48:19 -0400485func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
486 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
487}
488
Colin Cross0de8a1e2020-09-18 14:15:30 -0700489func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
490 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700491 LinkDirs: flagExporter.linkDirs,
492 LinkObjects: flagExporter.linkObjects,
493 })
494}
495
Matthew Maurerbb3add12020-06-25 09:34:12 -0700496var _ exportedFlagsProducer = (*flagExporter)(nil)
497
498func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700499 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700500}
501
Colin Cross0de8a1e2020-09-18 14:15:30 -0700502type FlagExporterInfo struct {
503 Flags []string
504 LinkDirs []string // TODO: this should be android.Paths
505 LinkObjects []string // TODO: this should be android.Paths
506}
507
508var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
509
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400510func (mod *Module) isCoverageVariant() bool {
511 return mod.coverage.Properties.IsCoverageVariant
512}
513
514var _ cc.Coverage = (*Module)(nil)
515
516func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
517 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
518}
519
Ivan Lozanod7586b62021-04-01 09:49:36 -0400520func (mod *Module) VndkVersion() string {
521 return mod.Properties.VndkVersion
522}
523
524func (mod *Module) PreventInstall() bool {
525 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400526}
527
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400528func (mod *Module) MarkAsCoverageVariant(coverage bool) {
529 mod.coverage.Properties.IsCoverageVariant = coverage
530}
531
532func (mod *Module) EnableCoverageIfNeeded() {
533 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534}
535
536func defaultsFactory() android.Module {
537 return DefaultsFactory()
538}
539
540type Defaults struct {
541 android.ModuleBase
542 android.DefaultsModuleBase
543}
544
545func DefaultsFactory(props ...interface{}) android.Module {
546 module := &Defaults{}
547
548 module.AddProperties(props...)
549 module.AddProperties(
550 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500551 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100552 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400553 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554 &BaseCompilerProperties{},
555 &BinaryCompilerProperties{},
556 &LibraryCompilerProperties{},
557 &ProcMacroCompilerProperties{},
558 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400559 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700560 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400561 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400562 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200563 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500564 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700565 )
566
567 android.InitDefaultsModule(module)
568 return module
569}
570
571func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700572 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700573}
574
Ivan Lozano183a3212019-10-18 14:18:45 -0700575func (mod *Module) CcLibrary() bool {
576 if mod.compiler != nil {
577 if _, ok := mod.compiler.(*libraryDecorator); ok {
578 return true
579 }
580 }
581 return false
582}
583
584func (mod *Module) CcLibraryInterface() bool {
585 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400586 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
587 // VariantIs{Static,Shared} is set.
588 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700589 return true
590 }
591 }
592 return false
593}
594
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400595func (mod *Module) UnstrippedOutputFile() android.Path {
596 if mod.unstrippedOutputFile.Valid() {
597 return mod.unstrippedOutputFile.Path()
598 }
599 return nil
600}
601
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800602func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700603 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700604 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800605 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700606 }
607 }
608 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
609}
610
611func (mod *Module) SetStatic() {
612 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700613 if library, ok := mod.compiler.(libraryInterface); ok {
614 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700615 return
616 }
617 }
618 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
619}
620
621func (mod *Module) SetShared() {
622 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700623 if library, ok := mod.compiler.(libraryInterface); ok {
624 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700625 return
626 }
627 }
628 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
629}
630
Ivan Lozano183a3212019-10-18 14:18:45 -0700631func (mod *Module) BuildStaticVariant() bool {
632 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700633 if library, ok := mod.compiler.(libraryInterface); ok {
634 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700635 }
636 }
637 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
638}
639
640func (mod *Module) BuildSharedVariant() bool {
641 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700642 if library, ok := mod.compiler.(libraryInterface); ok {
643 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700644 }
645 }
646 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
647}
648
Ivan Lozano183a3212019-10-18 14:18:45 -0700649func (mod *Module) Module() android.Module {
650 return mod
651}
652
Ivan Lozano183a3212019-10-18 14:18:45 -0700653func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900654 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
655 return mod.compiler.strippedOutputFilePath()
656 }
657 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700658}
659
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400660func (mod *Module) CoverageFiles() android.Paths {
661 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800662 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400663 }
664 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
665}
666
Jiyong Park459feca2020-12-15 11:02:21 +0900667func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900668 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900669 return false
670 }
671
Jiyong Park459feca2020-12-15 11:02:21 +0900672 // The apex variant is not installable because it is included in the APEX and won't appear
673 // in the system partition as a standalone file.
674 if !apexInfo.IsForPlatform() {
675 return false
676 }
677
Jiyong Parke54f07e2021-04-07 15:08:04 +0900678 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900679}
680
Ivan Lozano183a3212019-10-18 14:18:45 -0700681var _ cc.LinkableInterface = (*Module)(nil)
682
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683func (mod *Module) Init() android.Module {
684 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500685 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700686
687 if mod.compiler != nil {
688 mod.AddProperties(mod.compiler.compilerProps()...)
689 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400690 if mod.coverage != nil {
691 mod.AddProperties(mod.coverage.props()...)
692 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200693 if mod.clippy != nil {
694 mod.AddProperties(mod.clippy.props()...)
695 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400696 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700697 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400698 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500699 if mod.sanitize != nil {
700 mod.AddProperties(mod.sanitize.props()...)
701 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400702
Ivan Lozanoffee3342019-08-27 12:03:00 -0700703 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900704 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705
706 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700707 return mod
708}
709
710func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
711 return &Module{
712 hod: hod,
713 multilib: multilib,
714 }
715}
716func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
717 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400718 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200719 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500720 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721 return module
722}
723
724type ModuleContext interface {
725 android.ModuleContext
726 ModuleContextIntf
727}
728
729type BaseModuleContext interface {
730 android.BaseModuleContext
731 ModuleContextIntf
732}
733
734type DepsContext interface {
735 android.BottomUpMutatorContext
736 ModuleContextIntf
737}
738
739type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200740 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742}
743
744type depsContext struct {
745 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746}
747
748type moduleContext struct {
749 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750}
751
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200752type baseModuleContext struct {
753 android.BaseModuleContext
754}
755
756func (ctx *moduleContext) RustModule() *Module {
757 return ctx.Module().(*Module)
758}
759
760func (ctx *moduleContext) toolchain() config.Toolchain {
761 return ctx.RustModule().toolchain(ctx)
762}
763
764func (ctx *depsContext) RustModule() *Module {
765 return ctx.Module().(*Module)
766}
767
768func (ctx *depsContext) toolchain() config.Toolchain {
769 return ctx.RustModule().toolchain(ctx)
770}
771
772func (ctx *baseModuleContext) RustModule() *Module {
773 return ctx.Module().(*Module)
774}
775
776func (ctx *baseModuleContext) toolchain() config.Toolchain {
777 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400778}
779
780func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700781 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
782 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
783 return false
784 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400785 return mod.compiler != nil && mod.compiler.nativeCoverage()
786}
787
Ivan Lozanod7586b62021-04-01 09:49:36 -0400788func (mod *Module) EverInstallable() bool {
789 return mod.compiler != nil &&
790 // Check to see whether the module is actually ever installable.
791 mod.compiler.everInstallable()
792}
793
794func (mod *Module) Installable() *bool {
795 return mod.Properties.Installable
796}
797
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
799 if mod.cachedToolchain == nil {
800 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
801 }
802 return mod.cachedToolchain
803}
804
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200805func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
806 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
807}
808
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
810}
811
812func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
813 ctx := &moduleContext{
814 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816
Jiyong Park99644e92020-11-17 22:21:02 +0900817 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
818 if !apexInfo.IsForPlatform() {
819 mod.hideApexVariantFromMake = true
820 }
821
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500823 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
824
825 // Differentiate static libraries that are vendor available
826 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700827 if mod.InProduct() && !mod.OnlyInProduct() {
828 mod.Properties.SubName += cc.ProductSuffix
829 } else {
830 mod.Properties.SubName += cc.VendorSuffix
831 }
Matthew Maurerc6868382021-07-13 14:12:37 -0700832 } else if mod.InRamdisk() && !mod.OnlyInRamdisk() {
833 mod.Properties.SubName += cc.RamdiskSuffix
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500834 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
835 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800836 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
837 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500838 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839
Matthew Maurera61e31f2021-05-27 11:09:11 -0700840 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
841 mod.Properties.SubName += cc.NativeBridgeSuffix
842 }
843
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 if !toolchain.Supported() {
845 // This toolchain's unsupported, there's nothing to do for this mod.
846 return
847 }
848
849 deps := mod.depsToPaths(ctx)
850 flags := Flags{
851 Toolchain: toolchain,
852 }
853
Ivan Lozano67eada32021-09-23 11:50:33 -0400854 // Calculate rustc flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 if mod.compiler != nil {
856 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400857 flags = mod.compiler.cfgFlags(ctx, flags)
858 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400859 }
860 if mod.coverage != nil {
861 flags, deps = mod.coverage.flags(ctx, flags, deps)
862 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200863 if mod.clippy != nil {
864 flags, deps = mod.clippy.flags(ctx, flags, deps)
865 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500866 if mod.sanitize != nil {
867 flags, deps = mod.sanitize.flags(ctx, flags, deps)
868 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400869
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200870 // SourceProvider needs to call GenerateSource() before compiler calls
871 // compile() so it can provide the source. A SourceProvider has
872 // multiple variants (e.g. source, rlib, dylib). Only the "source"
873 // variant is responsible for effectively generating the source. The
874 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400875 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200876 if mod.compiler.(libraryInterface).source() {
877 mod.sourceProvider.GenerateSource(ctx, deps)
878 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
879 } else {
880 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
881 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700882 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200883 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400884 }
885
886 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100887 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900888 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900889 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200890 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900891
Dan Albert06feee92021-03-19 15:06:02 -0700892 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
893
Ivan Lozano1921e802021-05-20 13:39:16 -0400894 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
895 // RECOVERY_SNAPSHOT_VERSION is current.
896 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
897 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
898 lib.collectHeadersForSnapshot(ctx, deps)
899 }
900 }
901
Jiyong Park459feca2020-12-15 11:02:21 +0900902 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900903 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
904 // If the module has been specifically configure to not be installed then
905 // hide from make as otherwise it will break when running inside make as the
906 // output path to install will not be specified. Not all uninstallable
907 // modules can be hidden from make as some are needed for resolving make
908 // side dependencies.
909 mod.HideFromMake()
910 } else if !mod.installable(apexInfo) {
911 mod.SkipInstall()
912 }
913
914 // Still call install though, the installs will be stored as PackageSpecs to allow
915 // using the outputs in a genrule.
916 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200917 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900918 if ctx.Failed() {
919 return
920 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400921 }
Chris Wailes74be7642021-07-22 16:20:28 -0700922
923 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924 }
925}
926
927func (mod *Module) deps(ctx DepsContext) Deps {
928 deps := Deps{}
929
930 if mod.compiler != nil {
931 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400932 }
933 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700934 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 }
936
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400937 if mod.coverage != nil {
938 deps = mod.coverage.deps(ctx, deps)
939 }
940
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500941 if mod.sanitize != nil {
942 deps = mod.sanitize.deps(ctx, deps)
943 }
944
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
946 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700947 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
949 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
950 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400951 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700952 return deps
953
954}
955
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956type dependencyTag struct {
957 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800958 name string
959 library bool
960 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961}
962
Jiyong Park65b62242020-11-25 12:44:59 +0900963// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
964// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
965func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800966 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900967}
968
969var _ android.InstallNeededDependencyTag = dependencyTag{}
970
Ivan Lozanoffee3342019-08-27 12:03:00 -0700971var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400972 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
973 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
974 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800975 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400976 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200977 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700978)
979
Jiyong Park99644e92020-11-17 22:21:02 +0900980func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
981 tag, ok := depTag.(dependencyTag)
982 return ok && tag == dylibDepTag
983}
984
Jiyong Park94e22fd2021-04-08 18:19:15 +0900985func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
986 tag, ok := depTag.(dependencyTag)
987 return ok && tag == rlibDepTag
988}
989
Matthew Maurer0f003b12020-06-29 14:34:06 -0700990type autoDep struct {
991 variation string
992 depTag dependencyTag
993}
994
995var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200996 rlibVariation = "rlib"
997 dylibVariation = "dylib"
998 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
999 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001000)
1001
1002type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001003 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001004}
1005
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001006func (mod *Module) begin(ctx BaseModuleContext) {
1007 if mod.coverage != nil {
1008 mod.coverage.begin(ctx)
1009 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001010 if mod.sanitize != nil {
1011 mod.sanitize.begin(ctx)
1012 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001013}
1014
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1016 var depPaths PathDeps
1017
1018 directRlibDeps := []*Module{}
1019 directDylibDeps := []*Module{}
1020 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001021 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001022 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001023 directSrcProvidersDeps := []*Module{}
1024 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025
1026 ctx.VisitDirectDeps(func(dep android.Module) {
1027 depName := ctx.OtherModuleName(dep)
1028 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001029
Ivan Lozano89435d12020-07-31 11:01:18 -04001030 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001032 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001033
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034 switch depTag {
1035 case dylibDepTag:
1036 dylib, ok := rustDep.compiler.(libraryInterface)
1037 if !ok || !dylib.dylib() {
1038 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1039 return
1040 }
1041 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001042 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001044
Ivan Lozanoffee3342019-08-27 12:03:00 -07001045 rlib, ok := rustDep.compiler.(libraryInterface)
1046 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001047 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048 return
1049 }
1050 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001051 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052 case procMacroDepTag:
1053 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001054 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001055 }
1056
1057 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001058 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1059 // OS/Arch variant is used.
1060 var helper string
1061 if ctx.Host() {
1062 helper = "missing 'host_supported'?"
1063 } else {
1064 helper = "device module defined?"
1065 }
1066
1067 if dep.Target().Os != ctx.Os() {
1068 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1069 return
1070 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1071 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1072 return
1073 }
1074 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075 }
1076
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001077 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001078 if depTag != procMacroDepTag {
1079 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1080 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1081 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1082 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001083 }
1084
Ivan Lozanoffee3342019-08-27 12:03:00 -07001085 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001086 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001087 if !linkFile.Valid() {
1088 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1089 depName, ctx.ModuleName())
1090 return
1091 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001092 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001093 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1094 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001095 }
1096 }
1097
Ivan Lozano89435d12020-07-31 11:01:18 -04001098 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001099 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001100 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001101 if _, ok := ccDep.(*Module); !ok {
1102 if ccDep.Module().Target().Os != ctx.Os() {
1103 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1104 return
1105 }
1106 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1107 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1108 return
1109 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001110 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001111 linkObject := ccDep.OutputFile()
1112 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001113
Ivan Lozano2093af22020-08-25 12:48:19 -04001114 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001115 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1116 }
1117
1118 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001119 switch {
1120 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001121 if cc.IsWholeStaticLib(depTag) {
1122 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1123 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001124 if mod.Binary() {
1125 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1126 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1127 // final linkage, pass the args directly to the linker to handle these cases.
1128 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1129 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001130 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001131 } else {
1132 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 -05001133 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001134 }
1135
1136 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1137 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001138 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001139 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1140
Colin Cross0de8a1e2020-09-18 14:15:30 -07001141 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1142 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1143 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1144 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1145 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001146 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001147 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001148 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001149 // For the shared lib dependencies, we may link to the stub variant
1150 // of the dependency depending on the context (e.g. if this
1151 // dependency crosses the APEX boundaries).
1152 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1153
1154 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1155 // object (either from stub or non-stub) to use.
1156 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1157 linkPath = linkPathFromFilePath(linkObject.Path())
1158
Ivan Lozanoffee3342019-08-27 12:03:00 -07001159 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001160 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001161 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1162 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1163 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1164 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001165 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001166
1167 // Record baseLibName for snapshots.
1168 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001169 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001170
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001171 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001172 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001173 case cc.IsHeaderDepTag(depTag):
1174 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1175 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1176 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1177 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001178 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001179 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001180 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001181 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182 }
1183
1184 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001185 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1186 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001187 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001188 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001189 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001190
1191 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001192 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001193 // These are usually genrules which don't have per-target variants.
1194 directSrcDeps = append(directSrcDeps, srcDep)
1195 }
1196 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001197 })
1198
1199 var rlibDepFiles RustLibraries
1200 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001201 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001202 }
1203 var dylibDepFiles RustLibraries
1204 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001205 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001206 }
1207 var procMacroDepFiles RustLibraries
1208 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001209 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001210 }
1211
1212 var staticLibDepFiles android.Paths
1213 for _, dep := range directStaticLibDeps {
1214 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1215 }
1216
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001217 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001218 var sharedLibDepFiles android.Paths
1219 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001220 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1221 if dep.TableOfContents.Valid() {
1222 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001223 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001224 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001225 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001226 }
1227
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001228 var srcProviderDepFiles android.Paths
1229 for _, dep := range directSrcProvidersDeps {
1230 srcs, _ := dep.OutputFiles("")
1231 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1232 }
1233 for _, dep := range directSrcDeps {
1234 srcs := dep.Srcs()
1235 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1236 }
1237
Ivan Lozanoffee3342019-08-27 12:03:00 -07001238 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1239 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1240 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001241 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001242 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1243 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001244 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245
1246 // Dedup exported flags from dependencies
1247 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001248 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001249 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001250 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1251 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1252 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001253
1254 return depPaths
1255}
1256
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001257func (mod *Module) InstallInData() bool {
1258 if mod.compiler == nil {
1259 return false
1260 }
1261 return mod.compiler.inData()
1262}
1263
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001264func (mod *Module) InstallInRamdisk() bool {
1265 return mod.InRamdisk()
1266}
1267
1268func (mod *Module) InstallInVendorRamdisk() bool {
1269 return mod.InVendorRamdisk()
1270}
1271
1272func (mod *Module) InstallInRecovery() bool {
1273 return mod.InRecovery()
1274}
1275
Ivan Lozanoffee3342019-08-27 12:03:00 -07001276func linkPathFromFilePath(filepath android.Path) string {
1277 return strings.Split(filepath.String(), filepath.Base())[0]
1278}
Ivan Lozanod648c432020-02-06 12:05:10 -05001279
Ivan Lozanoffee3342019-08-27 12:03:00 -07001280func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1281 ctx := &depsContext{
1282 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001284
1285 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001286 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001287 var snapshotInfo *cc.SnapshotInfo
1288
1289 if ctx.Os() == android.Android {
1290 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1291 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001292
Ivan Lozano2b081132020-09-08 12:46:52 -04001293 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001294 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001295 stdLinkage = "rlib-std"
1296 }
1297
1298 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001299
Ivan Lozano2b081132020-09-08 12:46:52 -04001300 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1301 rlibDepVariations = append(rlibDepVariations,
1302 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1303 }
1304
Ivan Lozano1921e802021-05-20 13:39:16 -04001305 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001306 for _, lib := range deps.Rlibs {
1307 depTag := rlibDepTag
1308 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1309
1310 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1311 {Mutator: "rust_libraries", Variation: rlibVariation},
1312 }...), depTag, lib)
1313 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001314
1315 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001316 actx.AddVariationDependencies(
1317 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001318 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001319 dylibDepTag, deps.Dylibs...)
1320
Ivan Lozano1921e802021-05-20 13:39:16 -04001321 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001322 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1323 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001324 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001325 for _, lib := range deps.Rustlibs {
1326 depTag := autoDep.depTag
1327 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1328 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1329 {Mutator: "rust_libraries", Variation: autoDep.variation},
1330 }...), depTag, lib)
1331 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001332 } else {
1333 actx.AddVariationDependencies(
1334 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1335 autoDep.depTag, deps.Rustlibs...)
1336 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001337 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001338
1339 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001340 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001341 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001342 for _, lib := range deps.Stdlibs {
1343 depTag := rlibDepTag
1344 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1345
1346 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1347 depTag, lib)
1348 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001349 } else {
1350 actx.AddVariationDependencies(
1351 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1352 dylibDepTag, deps.Stdlibs...)
1353 }
1354 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001355
1356 for _, lib := range deps.SharedLibs {
1357 depTag := cc.SharedDepTag()
1358 name, version := cc.StubsLibNameAndVersion(lib)
1359
1360 variations := []blueprint.Variation{
1361 {Mutator: "link", Variation: "shared"},
1362 }
1363 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1364 }
1365
1366 for _, lib := range deps.WholeStaticLibs {
1367 depTag := cc.StaticDepTag(true)
1368 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1369
1370 actx.AddVariationDependencies([]blueprint.Variation{
1371 {Mutator: "link", Variation: "static"},
1372 }, depTag, lib)
1373 }
1374
1375 for _, lib := range deps.StaticLibs {
1376 depTag := cc.StaticDepTag(false)
1377 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1378
1379 actx.AddVariationDependencies([]blueprint.Variation{
1380 {Mutator: "link", Variation: "static"},
1381 }, depTag, lib)
1382 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001383
Zach Johnson3df4e632020-11-06 11:56:27 -08001384 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1385
Colin Cross565cafd2020-09-25 18:47:38 -07001386 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001387 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001388 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1389 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001390 }
1391 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001392 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1393 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001394 }
1395
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001396 if mod.sourceProvider != nil {
1397 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1398 bindgen.Properties.Custom_bindgen != "" {
1399 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1400 bindgen.Properties.Custom_bindgen)
1401 }
1402 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001403
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001404 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001405 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001406}
1407
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001408func BeginMutator(ctx android.BottomUpMutatorContext) {
1409 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1410 mod.beginMutator(ctx)
1411 }
1412}
1413
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001414func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1415 ctx := &baseModuleContext{
1416 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001417 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001418
1419 mod.begin(ctx)
1420}
1421
Ivan Lozanoffee3342019-08-27 12:03:00 -07001422func (mod *Module) Name() string {
1423 name := mod.ModuleBase.Name()
1424 if p, ok := mod.compiler.(interface {
1425 Name(string) string
1426 }); ok {
1427 name = p.Name(name)
1428 }
1429 return name
1430}
1431
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001432func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001433 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001434 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001435 }
1436}
1437
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001438var _ android.HostToolProvider = (*Module)(nil)
1439
1440func (mod *Module) HostToolPath() android.OptionalPath {
1441 if !mod.Host() {
1442 return android.OptionalPath{}
1443 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001444 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1445 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001446 }
1447 return android.OptionalPath{}
1448}
1449
Jiyong Park99644e92020-11-17 22:21:02 +09001450var _ android.ApexModule = (*Module)(nil)
1451
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001452func (mod *Module) minSdkVersion() string {
1453 return String(mod.Properties.Min_sdk_version)
1454}
1455
Jiyong Park45bf82e2020-12-15 22:29:02 +09001456var _ android.ApexModule = (*Module)(nil)
1457
1458// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001459func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001460 minSdkVersion := mod.minSdkVersion()
1461 if minSdkVersion == "apex_inherit" {
1462 return nil
1463 }
1464 if minSdkVersion == "" {
1465 return fmt.Errorf("min_sdk_version is not specificed")
1466 }
1467
1468 // Not using nativeApiLevelFromUser because the context here is not
1469 // necessarily a native context.
1470 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1471 if err != nil {
1472 return err
1473 }
1474
1475 if ver.GreaterThan(sdkVersion) {
1476 return fmt.Errorf("newer SDK(%v)", ver)
1477 }
Jiyong Park99644e92020-11-17 22:21:02 +09001478 return nil
1479}
1480
Jiyong Park45bf82e2020-12-15 22:29:02 +09001481// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001482func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1483 depTag := ctx.OtherModuleDependencyTag(dep)
1484
1485 if ccm, ok := dep.(*cc.Module); ok {
1486 if ccm.HasStubsVariants() {
1487 if cc.IsSharedDepTag(depTag) {
1488 // dynamic dep to a stubs lib crosses APEX boundary
1489 return false
1490 }
1491 if cc.IsRuntimeDepTag(depTag) {
1492 // runtime dep to a stubs lib also crosses APEX boundary
1493 return false
1494 }
1495
1496 if cc.IsHeaderDepTag(depTag) {
1497 return false
1498 }
1499 }
1500 if mod.Static() && cc.IsSharedDepTag(depTag) {
1501 // shared_lib dependency from a static lib is considered as crossing
1502 // the APEX boundary because the dependency doesn't actually is
1503 // linked; the dependency is used only during the compilation phase.
1504 return false
1505 }
1506 }
1507
1508 if depTag == procMacroDepTag {
1509 return false
1510 }
1511
1512 return true
1513}
1514
1515// Overrides ApexModule.IsInstallabeToApex()
1516func (mod *Module) IsInstallableToApex() bool {
1517 if mod.compiler != nil {
1518 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1519 return true
1520 }
1521 if _, ok := mod.compiler.(*binaryDecorator); ok {
1522 return true
1523 }
1524 }
1525 return false
1526}
1527
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001528// If a library file has a "lib" prefix, extract the library name without the prefix.
1529func libNameFromFilePath(filepath android.Path) (string, bool) {
1530 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1531 if strings.HasPrefix(libName, "lib") {
1532 libName = libName[3:]
1533 return libName, true
1534 }
1535 return "", false
1536}
1537
Ivan Lozanoffee3342019-08-27 12:03:00 -07001538var Bool = proptools.Bool
1539var BoolDefault = proptools.BoolDefault
1540var String = proptools.String
1541var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001542
1543var _ android.OutputFileProducer = (*Module)(nil)