blob: 0f7b76823a509c9f1013a6b590b4b4919758fb36 [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
Yi Kong46c6e592022-01-20 22:55:00 +0800151 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400153 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200154 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500155 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700158 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400159
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400160 // Output file to be installed, may be stripped or unstripped.
161 outputFile android.OptionalPath
162
163 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900164
165 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500166
167 // For apex variants, this is set as apex.min_sdk_version
168 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700169}
170
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171func (mod *Module) Header() bool {
172 //TODO: If Rust libraries provide header variants, this needs to be updated.
173 return false
174}
175
176func (mod *Module) SetPreventInstall() {
177 mod.Properties.PreventInstall = true
178}
179
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500180func (mod *Module) SetHideFromMake() {
181 mod.Properties.HideFromMake = true
182}
183
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900184func (mod *Module) HiddenFromMake() bool {
185 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400186}
187
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500189 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
190 // nil since we need compiler to actually sanitize.
191 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500192}
193
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500194func (mod *Module) IsPrebuilt() bool {
195 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
196 return true
197 }
198 return false
199}
200
Ivan Lozano43845682020-07-09 21:03:28 -0400201func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
202 switch tag {
203 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700204 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400205 return mod.sourceProvider.Srcs(), nil
206 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900207 if mod.OutputFile().Valid() {
208 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400209 }
210 return android.Paths{}, nil
211 }
212 default:
213 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
214 }
215}
216
Ivan Lozano52767be2019-10-18 14:49:46 -0700217func (mod *Module) SelectedStl() string {
218 return ""
219}
220
Ivan Lozano2b262972019-11-21 12:30:50 -0800221func (mod *Module) NonCcVariants() bool {
222 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400223 if _, ok := mod.compiler.(libraryInterface); ok {
224 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800225 }
226 }
227 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
228}
229
Ivan Lozano52767be2019-10-18 14:49:46 -0700230func (mod *Module) Static() bool {
231 if mod.compiler != nil {
232 if library, ok := mod.compiler.(libraryInterface); ok {
233 return library.static()
234 }
235 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400236 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700237}
238
239func (mod *Module) Shared() bool {
240 if mod.compiler != nil {
241 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400242 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700243 }
244 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400245 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700246}
247
Ivan Lozanod7586b62021-04-01 09:49:36 -0400248func (mod *Module) Dylib() bool {
249 if mod.compiler != nil {
250 if library, ok := mod.compiler.(libraryInterface); ok {
251 return library.dylib()
252 }
253 }
254 return false
255}
256
257func (mod *Module) Rlib() bool {
258 if mod.compiler != nil {
259 if library, ok := mod.compiler.(libraryInterface); ok {
260 return library.rlib()
261 }
262 }
263 return false
264}
265
266func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400267 if binary, ok := mod.compiler.(binaryInterface); ok {
268 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400269 }
270 return false
271}
272
Justin Yun5e035862021-06-29 20:50:37 +0900273func (mod *Module) StaticExecutable() bool {
274 if !mod.Binary() {
275 return false
276 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400277 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900278}
279
Ivan Lozanod7586b62021-04-01 09:49:36 -0400280func (mod *Module) Object() bool {
281 // Rust has no modules which produce only object files.
282 return false
283}
284
Ivan Lozano52767be2019-10-18 14:49:46 -0700285func (mod *Module) Toc() android.OptionalPath {
286 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400287 if lib, ok := mod.compiler.(libraryInterface); ok {
288 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700289 }
290 }
291 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
292}
293
Colin Crossc511bc52020-04-07 16:50:32 +0000294func (mod *Module) UseSdk() bool {
295 return false
296}
297
Ivan Lozanod7586b62021-04-01 09:49:36 -0400298func (mod *Module) RelativeInstallPath() string {
299 if mod.compiler != nil {
300 return mod.compiler.relativeInstallPath()
301 }
302 return ""
303}
304
Ivan Lozano52767be2019-10-18 14:49:46 -0700305func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500306 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700307}
308
Jiyong Park7d55b612021-06-11 17:22:09 +0900309func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400310 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900311}
312
Ivan Lozano52767be2019-10-18 14:49:46 -0700313func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400314 return true
315}
316
317func (mod *Module) SubName() string {
318 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700319}
320
321func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500322 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700323 return false
324}
325
Ivan Lozanof9e21722020-12-02 09:00:51 -0500326func (mod *Module) IsVndkExt() bool {
327 return false
328}
329
Ivan Lozanod7586b62021-04-01 09:49:36 -0400330func (mod *Module) IsVndkSp() bool {
331 return false
332}
333
Colin Cross127bb8b2020-12-16 16:46:01 -0800334func (c *Module) IsVndkPrivate() bool {
335 return false
336}
337
338func (c *Module) IsLlndk() bool {
339 return false
340}
341
342func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500343 return false
344}
345
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400346func (mod *Module) KernelHeadersDecorator() bool {
347 return false
348}
349
Colin Cross1f3f1302021-04-26 18:37:44 -0700350func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400351 return false
352}
353
Colin Cross5271fea2021-04-27 13:06:04 -0700354func (m *Module) NeedsVendorPublicLibraryVariants() bool {
355 return false
356}
357
Ivan Lozanod7586b62021-04-01 09:49:36 -0400358func (mod *Module) HasLlndkStubs() bool {
359 return false
360}
361
362func (mod *Module) StubsVersion() string {
363 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
364}
365
Ivan Lozano52767be2019-10-18 14:49:46 -0700366func (mod *Module) SdkVersion() string {
367 return ""
368}
369
Colin Crossc511bc52020-04-07 16:50:32 +0000370func (mod *Module) AlwaysSdk() bool {
371 return false
372}
373
Jiyong Park2286afd2020-06-16 21:58:53 +0900374func (mod *Module) IsSdkVariant() bool {
375 return false
376}
377
Colin Cross1348ce32020-10-01 13:37:16 -0700378func (mod *Module) SplitPerApiLevel() bool {
379 return false
380}
381
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400383 Dylibs []string
384 Rlibs []string
385 Rustlibs []string
386 Stdlibs []string
387 ProcMacros []string
388 SharedLibs []string
389 StaticLibs []string
390 WholeStaticLibs []string
391 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700392
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400393 // Used for data dependencies adjacent to tests
394 DataLibs []string
395 DataBins []string
396
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397 CrtBegin, CrtEnd string
398}
399
400type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500401 DyLibs RustLibraries
402 RLibs RustLibraries
403 SharedLibs android.Paths
404 SharedLibDeps android.Paths
405 StaticLibs android.Paths
406 ProcMacros RustLibraries
Yi Kong46c6e592022-01-20 22:55:00 +0800407 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500408
409 // depFlags and depLinkFlags are rustc and linker (clang) flags.
410 depFlags []string
411 depLinkFlags []string
412
413 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
414 // Both of these are exported and propagate to dependencies.
415 linkDirs []string
416 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700417
Ivan Lozano45901ed2020-07-24 16:05:01 -0400418 // Used by bindgen modules which call clang
419 depClangFlags []string
420 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400421 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400422 depSystemIncludePaths android.Paths
423
Ivan Lozanof1c84332019-09-20 11:00:37 -0700424 CrtBegin android.OptionalPath
425 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700426
427 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500428 SrcDeps android.Paths
429 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430}
431
432type RustLibraries []RustLibrary
433
434type RustLibrary struct {
435 Path android.Path
436 CrateName string
437}
438
439type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100440 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700441 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400442 cfgFlags(ctx ModuleContext, flags Flags) Flags
443 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444 compilerProps() []interface{}
445 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
446 compilerDeps(ctx DepsContext, deps Deps) Deps
447 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700448 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100450 // Output directory in which source-generated code from dependencies is
451 // copied. This is equivalent to Cargo's OUT_DIR variable.
452 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700453
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400454 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
455 CargoPkgVersion() string
456
457 // CargoEnvCompat returns whether Cargo environment variables should be used.
458 CargoEnvCompat() bool
459
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800460 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200461 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700462 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400463 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400464
465 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400466
467 Disabled() bool
468 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400469
Ivan Lozanodd055472020-09-28 13:22:45 -0400470 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900471
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400472 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900473 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400474}
475
Matthew Maurerbb3add12020-06-25 09:34:12 -0700476type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700477 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400478 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700479}
480
481type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400482 linkDirs []string
483 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700484}
485
Matthew Maurerbb3add12020-06-25 09:34:12 -0700486func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
487 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
488}
489
Ivan Lozano2093af22020-08-25 12:48:19 -0400490func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
491 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
492}
493
Colin Cross0de8a1e2020-09-18 14:15:30 -0700494func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
495 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700496 LinkDirs: flagExporter.linkDirs,
497 LinkObjects: flagExporter.linkObjects,
498 })
499}
500
Matthew Maurerbb3add12020-06-25 09:34:12 -0700501var _ exportedFlagsProducer = (*flagExporter)(nil)
502
503func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700504 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700505}
506
Colin Cross0de8a1e2020-09-18 14:15:30 -0700507type FlagExporterInfo struct {
508 Flags []string
509 LinkDirs []string // TODO: this should be android.Paths
510 LinkObjects []string // TODO: this should be android.Paths
511}
512
513var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
514
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400515func (mod *Module) isCoverageVariant() bool {
516 return mod.coverage.Properties.IsCoverageVariant
517}
518
519var _ cc.Coverage = (*Module)(nil)
520
521func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
522 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
523}
524
Ivan Lozanod7586b62021-04-01 09:49:36 -0400525func (mod *Module) VndkVersion() string {
526 return mod.Properties.VndkVersion
527}
528
529func (mod *Module) PreventInstall() bool {
530 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531}
532
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533func (mod *Module) MarkAsCoverageVariant(coverage bool) {
534 mod.coverage.Properties.IsCoverageVariant = coverage
535}
536
537func (mod *Module) EnableCoverageIfNeeded() {
538 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539}
540
541func defaultsFactory() android.Module {
542 return DefaultsFactory()
543}
544
545type Defaults struct {
546 android.ModuleBase
547 android.DefaultsModuleBase
548}
549
550func DefaultsFactory(props ...interface{}) android.Module {
551 module := &Defaults{}
552
553 module.AddProperties(props...)
554 module.AddProperties(
555 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800556 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500557 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100558 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400559 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560 &BaseCompilerProperties{},
561 &BinaryCompilerProperties{},
562 &LibraryCompilerProperties{},
563 &ProcMacroCompilerProperties{},
564 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400565 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700566 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400567 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400568 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200569 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500570 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571 )
572
573 android.InitDefaultsModule(module)
574 return module
575}
576
577func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700578 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579}
580
Ivan Lozano183a3212019-10-18 14:18:45 -0700581func (mod *Module) CcLibrary() bool {
582 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500583 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700584 return true
585 }
586 }
587 return false
588}
589
590func (mod *Module) CcLibraryInterface() bool {
591 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400592 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
593 // VariantIs{Static,Shared} is set.
594 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700595 return true
596 }
597 }
598 return false
599}
600
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400601func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400602 if mod.compiler != nil {
603 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400604 }
605 return nil
606}
607
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800608func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700609 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700610 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800611 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700612 }
613 }
614 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
615}
616
617func (mod *Module) SetStatic() {
618 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700619 if library, ok := mod.compiler.(libraryInterface); ok {
620 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700621 return
622 }
623 }
624 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
625}
626
627func (mod *Module) SetShared() {
628 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700629 if library, ok := mod.compiler.(libraryInterface); ok {
630 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700631 return
632 }
633 }
634 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
635}
636
Ivan Lozano183a3212019-10-18 14:18:45 -0700637func (mod *Module) BuildStaticVariant() bool {
638 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700639 if library, ok := mod.compiler.(libraryInterface); ok {
640 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700641 }
642 }
643 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
644}
645
646func (mod *Module) BuildSharedVariant() bool {
647 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700648 if library, ok := mod.compiler.(libraryInterface); ok {
649 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700650 }
651 }
652 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
653}
654
Ivan Lozano183a3212019-10-18 14:18:45 -0700655func (mod *Module) Module() android.Module {
656 return mod
657}
658
Ivan Lozano183a3212019-10-18 14:18:45 -0700659func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400660 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700661}
662
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400663func (mod *Module) CoverageFiles() android.Paths {
664 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800665 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400666 }
667 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
668}
669
Jiyong Park459feca2020-12-15 11:02:21 +0900670func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900671 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900672 return false
673 }
674
Jiyong Park459feca2020-12-15 11:02:21 +0900675 // The apex variant is not installable because it is included in the APEX and won't appear
676 // in the system partition as a standalone file.
677 if !apexInfo.IsForPlatform() {
678 return false
679 }
680
Jiyong Parke54f07e2021-04-07 15:08:04 +0900681 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900682}
683
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500684func (ctx moduleContext) apexVariationName() string {
685 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
686}
687
Ivan Lozano183a3212019-10-18 14:18:45 -0700688var _ cc.LinkableInterface = (*Module)(nil)
689
Ivan Lozanoffee3342019-08-27 12:03:00 -0700690func (mod *Module) Init() android.Module {
691 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500692 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700693
Yi Kong46c6e592022-01-20 22:55:00 +0800694 if mod.afdo != nil {
695 mod.AddProperties(mod.afdo.props()...)
696 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700697 if mod.compiler != nil {
698 mod.AddProperties(mod.compiler.compilerProps()...)
699 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400700 if mod.coverage != nil {
701 mod.AddProperties(mod.coverage.props()...)
702 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200703 if mod.clippy != nil {
704 mod.AddProperties(mod.clippy.props()...)
705 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400706 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700707 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400708 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500709 if mod.sanitize != nil {
710 mod.AddProperties(mod.sanitize.props()...)
711 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400712
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900714 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715
716 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717 return mod
718}
719
720func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
721 return &Module{
722 hod: hod,
723 multilib: multilib,
724 }
725}
726func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
727 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800728 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400729 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200730 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500731 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700732 return module
733}
734
735type ModuleContext interface {
736 android.ModuleContext
737 ModuleContextIntf
738}
739
740type BaseModuleContext interface {
741 android.BaseModuleContext
742 ModuleContextIntf
743}
744
745type DepsContext interface {
746 android.BottomUpMutatorContext
747 ModuleContextIntf
748}
749
750type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200751 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753}
754
755type depsContext struct {
756 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757}
758
759type moduleContext struct {
760 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761}
762
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200763type baseModuleContext struct {
764 android.BaseModuleContext
765}
766
767func (ctx *moduleContext) RustModule() *Module {
768 return ctx.Module().(*Module)
769}
770
771func (ctx *moduleContext) toolchain() config.Toolchain {
772 return ctx.RustModule().toolchain(ctx)
773}
774
775func (ctx *depsContext) RustModule() *Module {
776 return ctx.Module().(*Module)
777}
778
779func (ctx *depsContext) toolchain() config.Toolchain {
780 return ctx.RustModule().toolchain(ctx)
781}
782
783func (ctx *baseModuleContext) RustModule() *Module {
784 return ctx.Module().(*Module)
785}
786
787func (ctx *baseModuleContext) toolchain() config.Toolchain {
788 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400789}
790
791func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700792 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
793 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
794 return false
795 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400796 return mod.compiler != nil && mod.compiler.nativeCoverage()
797}
798
Ivan Lozanod7586b62021-04-01 09:49:36 -0400799func (mod *Module) EverInstallable() bool {
800 return mod.compiler != nil &&
801 // Check to see whether the module is actually ever installable.
802 mod.compiler.everInstallable()
803}
804
805func (mod *Module) Installable() *bool {
806 return mod.Properties.Installable
807}
808
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
810 if mod.cachedToolchain == nil {
811 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
812 }
813 return mod.cachedToolchain
814}
815
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200816func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
817 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
818}
819
Ivan Lozanoffee3342019-08-27 12:03:00 -0700820func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
821}
822
823func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
824 ctx := &moduleContext{
825 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827
Jiyong Park99644e92020-11-17 22:21:02 +0900828 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
829 if !apexInfo.IsForPlatform() {
830 mod.hideApexVariantFromMake = true
831 }
832
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500834 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
835
836 // Differentiate static libraries that are vendor available
837 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700838 if mod.InProduct() && !mod.OnlyInProduct() {
839 mod.Properties.SubName += cc.ProductSuffix
840 } else {
841 mod.Properties.SubName += cc.VendorSuffix
842 }
Matthew Maurerc6868382021-07-13 14:12:37 -0700843 } else if mod.InRamdisk() && !mod.OnlyInRamdisk() {
844 mod.Properties.SubName += cc.RamdiskSuffix
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500845 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
846 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800847 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
848 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500849 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850
Matthew Maurera61e31f2021-05-27 11:09:11 -0700851 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
852 mod.Properties.SubName += cc.NativeBridgeSuffix
853 }
854
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 if !toolchain.Supported() {
856 // This toolchain's unsupported, there's nothing to do for this mod.
857 return
858 }
859
860 deps := mod.depsToPaths(ctx)
861 flags := Flags{
862 Toolchain: toolchain,
863 }
864
Ivan Lozano67eada32021-09-23 11:50:33 -0400865 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800866 if mod.afdo != nil {
867 flags, deps = mod.afdo.flags(ctx, flags, deps)
868 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 if mod.compiler != nil {
870 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400871 flags = mod.compiler.cfgFlags(ctx, flags)
872 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400873 }
874 if mod.coverage != nil {
875 flags, deps = mod.coverage.flags(ctx, flags, deps)
876 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200877 if mod.clippy != nil {
878 flags, deps = mod.clippy.flags(ctx, flags, deps)
879 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500880 if mod.sanitize != nil {
881 flags, deps = mod.sanitize.flags(ctx, flags, deps)
882 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400883
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200884 // SourceProvider needs to call GenerateSource() before compiler calls
885 // compile() so it can provide the source. A SourceProvider has
886 // multiple variants (e.g. source, rlib, dylib). Only the "source"
887 // variant is responsible for effectively generating the source. The
888 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400889 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200890 if mod.compiler.(libraryInterface).source() {
891 mod.sourceProvider.GenerateSource(ctx, deps)
892 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
893 } else {
894 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
895 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700896 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200897 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400898 }
899
900 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100901 mod.compiler.initialize(ctx)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400902 outputFile := mod.compiler.compile(ctx, flags, deps)
903 if ctx.Failed() {
904 return
905 }
906 mod.outputFile = android.OptionalPathForPath(outputFile)
907 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900908
Dan Albert06feee92021-03-19 15:06:02 -0700909 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000910 if mod.docTimestampFile.Valid() {
911 ctx.CheckbuildFile(mod.docTimestampFile.Path())
912 }
Dan Albert06feee92021-03-19 15:06:02 -0700913
Ivan Lozano1921e802021-05-20 13:39:16 -0400914 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
915 // RECOVERY_SNAPSHOT_VERSION is current.
916 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
917 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
918 lib.collectHeadersForSnapshot(ctx, deps)
919 }
920 }
921
Jiyong Park459feca2020-12-15 11:02:21 +0900922 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900923 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
924 // If the module has been specifically configure to not be installed then
925 // hide from make as otherwise it will break when running inside make as the
926 // output path to install will not be specified. Not all uninstallable
927 // modules can be hidden from make as some are needed for resolving make
928 // side dependencies.
929 mod.HideFromMake()
930 } else if !mod.installable(apexInfo) {
931 mod.SkipInstall()
932 }
933
934 // Still call install though, the installs will be stored as PackageSpecs to allow
935 // using the outputs in a genrule.
936 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200937 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900938 if ctx.Failed() {
939 return
940 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400941 }
Chris Wailes74be7642021-07-22 16:20:28 -0700942
943 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944 }
945}
946
947func (mod *Module) deps(ctx DepsContext) Deps {
948 deps := Deps{}
949
950 if mod.compiler != nil {
951 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400952 }
953 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700954 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955 }
956
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400957 if mod.coverage != nil {
958 deps = mod.coverage.deps(ctx, deps)
959 }
960
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500961 if mod.sanitize != nil {
962 deps = mod.sanitize.deps(ctx, deps)
963 }
964
Ivan Lozanoffee3342019-08-27 12:03:00 -0700965 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
966 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700967 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700968 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
969 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
970 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400971 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700972 return deps
973
974}
975
Ivan Lozanoffee3342019-08-27 12:03:00 -0700976type dependencyTag struct {
977 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800978 name string
979 library bool
980 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +0000981 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982}
983
Jiyong Park65b62242020-11-25 12:44:59 +0900984// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
985// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
986func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800987 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900988}
989
990var _ android.InstallNeededDependencyTag = dependencyTag{}
991
Colin Cross65cb3142021-12-10 23:05:02 +0000992func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
993 if d.library && d.dynamic {
994 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
995 }
996 return nil
997}
998
999var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1000
Ivan Lozanoffee3342019-08-27 12:03:00 -07001001var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001002 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1003 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001004 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001005 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001006 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001007 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001008 dataLibDepTag = dependencyTag{name: "data lib"}
1009 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001010)
1011
Jiyong Park99644e92020-11-17 22:21:02 +09001012func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1013 tag, ok := depTag.(dependencyTag)
1014 return ok && tag == dylibDepTag
1015}
1016
Jiyong Park94e22fd2021-04-08 18:19:15 +09001017func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1018 tag, ok := depTag.(dependencyTag)
1019 return ok && tag == rlibDepTag
1020}
1021
Matthew Maurer0f003b12020-06-29 14:34:06 -07001022type autoDep struct {
1023 variation string
1024 depTag dependencyTag
1025}
1026
1027var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001028 rlibVariation = "rlib"
1029 dylibVariation = "dylib"
1030 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1031 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001032)
1033
1034type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001035 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001036}
1037
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001038func (mod *Module) begin(ctx BaseModuleContext) {
1039 if mod.coverage != nil {
1040 mod.coverage.begin(ctx)
1041 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001042 if mod.sanitize != nil {
1043 mod.sanitize.begin(ctx)
1044 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001045}
1046
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001047func (mod *Module) Prebuilt() *android.Prebuilt {
1048 if p, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
1049 return p.prebuilt()
1050 }
1051 return nil
1052}
1053
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1055 var depPaths PathDeps
1056
1057 directRlibDeps := []*Module{}
1058 directDylibDeps := []*Module{}
1059 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001060 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001061 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001062 directSrcProvidersDeps := []*Module{}
1063 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001064
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001065 // For the dependency from platform to apex, use the latest stubs
1066 mod.apexSdkVersion = android.FutureApiLevel
1067 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1068 if !apexInfo.IsForPlatform() {
1069 mod.apexSdkVersion = apexInfo.MinSdkVersion
1070 }
1071
1072 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1073 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1074 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1075 // (b/144430859)
1076 mod.apexSdkVersion = android.FutureApiLevel
1077 }
1078
Ivan Lozanoffee3342019-08-27 12:03:00 -07001079 ctx.VisitDirectDeps(func(dep android.Module) {
1080 depName := ctx.OtherModuleName(dep)
1081 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001082
Ivan Lozano89435d12020-07-31 11:01:18 -04001083 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001084 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001085 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001086
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087 switch depTag {
1088 case dylibDepTag:
1089 dylib, ok := rustDep.compiler.(libraryInterface)
1090 if !ok || !dylib.dylib() {
1091 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1092 return
1093 }
1094 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001095 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001096 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001097
Ivan Lozanoffee3342019-08-27 12:03:00 -07001098 rlib, ok := rustDep.compiler.(libraryInterface)
1099 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001100 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001101 return
1102 }
1103 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001104 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001105 case procMacroDepTag:
1106 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001107 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001108 }
1109
1110 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001111 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1112 // OS/Arch variant is used.
1113 var helper string
1114 if ctx.Host() {
1115 helper = "missing 'host_supported'?"
1116 } else {
1117 helper = "device module defined?"
1118 }
1119
1120 if dep.Target().Os != ctx.Os() {
1121 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1122 return
1123 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1124 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1125 return
1126 }
1127 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001128 }
1129
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001130 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001131 if depTag != procMacroDepTag {
1132 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1133 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1134 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1135 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136 }
1137
Ivan Lozanoffee3342019-08-27 12:03:00 -07001138 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001139 linkFile := rustDep.UnstrippedOutputFile()
1140 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001141 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1142 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001143 }
1144 }
1145
Ivan Lozano89435d12020-07-31 11:01:18 -04001146 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001147 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001148 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001149 if _, ok := ccDep.(*Module); !ok {
1150 if ccDep.Module().Target().Os != ctx.Os() {
1151 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1152 return
1153 }
1154 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1155 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1156 return
1157 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001158 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001159 linkObject := ccDep.OutputFile()
1160 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001161
Ivan Lozano2093af22020-08-25 12:48:19 -04001162 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001163 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1164 }
1165
1166 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001167 switch {
1168 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001169 if cc.IsWholeStaticLib(depTag) {
1170 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1171 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001172 if mod.Binary() {
1173 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1174 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1175 // final linkage, pass the args directly to the linker to handle these cases.
1176 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1177 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001178 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001179 } else {
1180 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 -05001181 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001182 }
1183
1184 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1185 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001186 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001187 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1188
Colin Cross0de8a1e2020-09-18 14:15:30 -07001189 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1190 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1191 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1192 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1193 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001194 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001195 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001196 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001197 // For the shared lib dependencies, we may link to the stub variant
1198 // of the dependency depending on the context (e.g. if this
1199 // dependency crosses the APEX boundaries).
1200 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1201
1202 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1203 // object (either from stub or non-stub) to use.
1204 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1205 linkPath = linkPathFromFilePath(linkObject.Path())
1206
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001208 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001209 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1210 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1211 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1212 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001213 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001214
1215 // Record baseLibName for snapshots.
1216 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001217 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001218
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001219 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001220 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001221 case cc.IsHeaderDepTag(depTag):
1222 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1223 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1224 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1225 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001226 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001227 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001228 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001229 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230 }
1231
1232 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001233 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1234 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001235 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001236 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001238
1239 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001240 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001241 // These are usually genrules which don't have per-target variants.
1242 directSrcDeps = append(directSrcDeps, srcDep)
1243 }
1244 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 })
1246
1247 var rlibDepFiles RustLibraries
1248 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001249 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001250 }
1251 var dylibDepFiles RustLibraries
1252 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001253 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001254 }
1255 var procMacroDepFiles RustLibraries
1256 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001257 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001258 }
1259
1260 var staticLibDepFiles android.Paths
1261 for _, dep := range directStaticLibDeps {
1262 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1263 }
1264
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001265 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001266 var sharedLibDepFiles android.Paths
1267 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001268 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1269 if dep.TableOfContents.Valid() {
1270 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001271 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001272 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001273 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001274 }
1275
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001276 var srcProviderDepFiles android.Paths
1277 for _, dep := range directSrcProvidersDeps {
1278 srcs, _ := dep.OutputFiles("")
1279 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1280 }
1281 for _, dep := range directSrcDeps {
1282 srcs := dep.Srcs()
1283 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1284 }
1285
Ivan Lozanoffee3342019-08-27 12:03:00 -07001286 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1287 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1288 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001289 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001290 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1291 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001292 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001293
1294 // Dedup exported flags from dependencies
1295 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001296 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001297 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001298 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1299 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1300 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001301
1302 return depPaths
1303}
1304
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001305func (mod *Module) InstallInData() bool {
1306 if mod.compiler == nil {
1307 return false
1308 }
1309 return mod.compiler.inData()
1310}
1311
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001312func (mod *Module) InstallInRamdisk() bool {
1313 return mod.InRamdisk()
1314}
1315
1316func (mod *Module) InstallInVendorRamdisk() bool {
1317 return mod.InVendorRamdisk()
1318}
1319
1320func (mod *Module) InstallInRecovery() bool {
1321 return mod.InRecovery()
1322}
1323
Ivan Lozanoffee3342019-08-27 12:03:00 -07001324func linkPathFromFilePath(filepath android.Path) string {
1325 return strings.Split(filepath.String(), filepath.Base())[0]
1326}
Ivan Lozanod648c432020-02-06 12:05:10 -05001327
Ivan Lozanoffee3342019-08-27 12:03:00 -07001328func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1329 ctx := &depsContext{
1330 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001331 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001332
1333 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001334 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001335 var snapshotInfo *cc.SnapshotInfo
1336
1337 if ctx.Os() == android.Android {
1338 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1339 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001340
Ivan Lozano2b081132020-09-08 12:46:52 -04001341 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001342 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001343 stdLinkage = "rlib-std"
1344 }
1345
1346 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001347
Ivan Lozano2b081132020-09-08 12:46:52 -04001348 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1349 rlibDepVariations = append(rlibDepVariations,
1350 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1351 }
1352
Ivan Lozano1921e802021-05-20 13:39:16 -04001353 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001354 for _, lib := range deps.Rlibs {
1355 depTag := rlibDepTag
1356 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1357
1358 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1359 {Mutator: "rust_libraries", Variation: rlibVariation},
1360 }...), depTag, lib)
1361 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001362
1363 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001364 actx.AddVariationDependencies(
1365 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001366 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001367 dylibDepTag, deps.Dylibs...)
1368
Ivan Lozano1921e802021-05-20 13:39:16 -04001369 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001370 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1371 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001372 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001373 for _, lib := range deps.Rustlibs {
1374 depTag := autoDep.depTag
1375 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1376 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1377 {Mutator: "rust_libraries", Variation: autoDep.variation},
1378 }...), depTag, lib)
1379 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001380 } else {
1381 actx.AddVariationDependencies(
1382 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1383 autoDep.depTag, deps.Rustlibs...)
1384 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001385 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001386
1387 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001388 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001389 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001390 for _, lib := range deps.Stdlibs {
1391 depTag := rlibDepTag
1392 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1393
1394 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1395 depTag, lib)
1396 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001397 } else {
1398 actx.AddVariationDependencies(
1399 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1400 dylibDepTag, deps.Stdlibs...)
1401 }
1402 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001403
1404 for _, lib := range deps.SharedLibs {
1405 depTag := cc.SharedDepTag()
1406 name, version := cc.StubsLibNameAndVersion(lib)
1407
1408 variations := []blueprint.Variation{
1409 {Mutator: "link", Variation: "shared"},
1410 }
1411 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1412 }
1413
1414 for _, lib := range deps.WholeStaticLibs {
1415 depTag := cc.StaticDepTag(true)
1416 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1417
1418 actx.AddVariationDependencies([]blueprint.Variation{
1419 {Mutator: "link", Variation: "static"},
1420 }, depTag, lib)
1421 }
1422
1423 for _, lib := range deps.StaticLibs {
1424 depTag := cc.StaticDepTag(false)
1425 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1426
1427 actx.AddVariationDependencies([]blueprint.Variation{
1428 {Mutator: "link", Variation: "static"},
1429 }, depTag, lib)
1430 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001431
Zach Johnson3df4e632020-11-06 11:56:27 -08001432 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1433
Colin Cross565cafd2020-09-25 18:47:38 -07001434 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001435 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001436 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1437 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001438 }
1439 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001440 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1441 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001442 }
1443
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001444 if mod.sourceProvider != nil {
1445 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1446 bindgen.Properties.Custom_bindgen != "" {
1447 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1448 bindgen.Properties.Custom_bindgen)
1449 }
1450 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001451
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001452 actx.AddVariationDependencies([]blueprint.Variation{
1453 {Mutator: "link", Variation: "shared"},
1454 }, dataLibDepTag, deps.DataLibs...)
1455
1456 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1457
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001458 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001459 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001460}
1461
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001462func BeginMutator(ctx android.BottomUpMutatorContext) {
1463 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1464 mod.beginMutator(ctx)
1465 }
1466}
1467
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001468func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1469 ctx := &baseModuleContext{
1470 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001471 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001472
1473 mod.begin(ctx)
1474}
1475
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476func (mod *Module) Name() string {
1477 name := mod.ModuleBase.Name()
1478 if p, ok := mod.compiler.(interface {
1479 Name(string) string
1480 }); ok {
1481 name = p.Name(name)
1482 }
1483 return name
1484}
1485
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001486func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001487 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001488 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001489 }
1490}
1491
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001492var _ android.HostToolProvider = (*Module)(nil)
1493
1494func (mod *Module) HostToolPath() android.OptionalPath {
1495 if !mod.Host() {
1496 return android.OptionalPath{}
1497 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001498 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1499 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001500 }
1501 return android.OptionalPath{}
1502}
1503
Jiyong Park99644e92020-11-17 22:21:02 +09001504var _ android.ApexModule = (*Module)(nil)
1505
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001506func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001507 return String(mod.Properties.Min_sdk_version)
1508}
1509
Jiyong Park45bf82e2020-12-15 22:29:02 +09001510// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001511func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001512 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001513 if minSdkVersion == "apex_inherit" {
1514 return nil
1515 }
1516 if minSdkVersion == "" {
1517 return fmt.Errorf("min_sdk_version is not specificed")
1518 }
1519
1520 // Not using nativeApiLevelFromUser because the context here is not
1521 // necessarily a native context.
1522 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1523 if err != nil {
1524 return err
1525 }
1526
1527 if ver.GreaterThan(sdkVersion) {
1528 return fmt.Errorf("newer SDK(%v)", ver)
1529 }
Jiyong Park99644e92020-11-17 22:21:02 +09001530 return nil
1531}
1532
Jiyong Park45bf82e2020-12-15 22:29:02 +09001533// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001534func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1535 depTag := ctx.OtherModuleDependencyTag(dep)
1536
1537 if ccm, ok := dep.(*cc.Module); ok {
1538 if ccm.HasStubsVariants() {
1539 if cc.IsSharedDepTag(depTag) {
1540 // dynamic dep to a stubs lib crosses APEX boundary
1541 return false
1542 }
1543 if cc.IsRuntimeDepTag(depTag) {
1544 // runtime dep to a stubs lib also crosses APEX boundary
1545 return false
1546 }
1547
1548 if cc.IsHeaderDepTag(depTag) {
1549 return false
1550 }
1551 }
1552 if mod.Static() && cc.IsSharedDepTag(depTag) {
1553 // shared_lib dependency from a static lib is considered as crossing
1554 // the APEX boundary because the dependency doesn't actually is
1555 // linked; the dependency is used only during the compilation phase.
1556 return false
1557 }
1558 }
1559
1560 if depTag == procMacroDepTag {
1561 return false
1562 }
1563
1564 return true
1565}
1566
1567// Overrides ApexModule.IsInstallabeToApex()
1568func (mod *Module) IsInstallableToApex() bool {
1569 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001570 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001571 return true
1572 }
1573 if _, ok := mod.compiler.(*binaryDecorator); ok {
1574 return true
1575 }
1576 }
1577 return false
1578}
1579
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001580// If a library file has a "lib" prefix, extract the library name without the prefix.
1581func libNameFromFilePath(filepath android.Path) (string, bool) {
1582 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1583 if strings.HasPrefix(libName, "lib") {
1584 libName = libName[3:]
1585 return libName, true
1586 }
1587 return "", false
1588}
1589
Ivan Lozanoffee3342019-08-27 12:03:00 -07001590var Bool = proptools.Bool
1591var BoolDefault = proptools.BoolDefault
1592var String = proptools.String
1593var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001594
1595var _ android.OutputFileProducer = (*Module)(nil)