blob: 7c8e80b5a8cb08d092fcd85b0abf689395a6a8ea [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
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400159 // Output file to be installed, may be stripped or unstripped.
160 outputFile android.OptionalPath
161
162 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900163
164 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165}
166
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500167func (mod *Module) Header() bool {
168 //TODO: If Rust libraries provide header variants, this needs to be updated.
169 return false
170}
171
172func (mod *Module) SetPreventInstall() {
173 mod.Properties.PreventInstall = true
174}
175
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500176func (mod *Module) SetHideFromMake() {
177 mod.Properties.HideFromMake = true
178}
179
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900180func (mod *Module) HiddenFromMake() bool {
181 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400182}
183
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500184func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500185 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
186 // nil since we need compiler to actually sanitize.
187 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500188}
189
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500190func (mod *Module) IsPrebuilt() bool {
191 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
192 return true
193 }
194 return false
195}
196
Ivan Lozano43845682020-07-09 21:03:28 -0400197func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
198 switch tag {
199 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700200 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400201 return mod.sourceProvider.Srcs(), nil
202 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900203 if mod.OutputFile().Valid() {
204 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400205 }
206 return android.Paths{}, nil
207 }
208 default:
209 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
210 }
211}
212
Ivan Lozano52767be2019-10-18 14:49:46 -0700213func (mod *Module) SelectedStl() string {
214 return ""
215}
216
Ivan Lozano2b262972019-11-21 12:30:50 -0800217func (mod *Module) NonCcVariants() bool {
218 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400219 if _, ok := mod.compiler.(libraryInterface); ok {
220 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800221 }
222 }
223 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
224}
225
Ivan Lozano52767be2019-10-18 14:49:46 -0700226func (mod *Module) Static() bool {
227 if mod.compiler != nil {
228 if library, ok := mod.compiler.(libraryInterface); ok {
229 return library.static()
230 }
231 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400232 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700233}
234
235func (mod *Module) Shared() bool {
236 if mod.compiler != nil {
237 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400238 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700239 }
240 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400241 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700242}
243
Ivan Lozanod7586b62021-04-01 09:49:36 -0400244func (mod *Module) Dylib() bool {
245 if mod.compiler != nil {
246 if library, ok := mod.compiler.(libraryInterface); ok {
247 return library.dylib()
248 }
249 }
250 return false
251}
252
253func (mod *Module) Rlib() bool {
254 if mod.compiler != nil {
255 if library, ok := mod.compiler.(libraryInterface); ok {
256 return library.rlib()
257 }
258 }
259 return false
260}
261
262func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400263 if binary, ok := mod.compiler.(binaryInterface); ok {
264 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400265 }
266 return false
267}
268
Justin Yun5e035862021-06-29 20:50:37 +0900269func (mod *Module) StaticExecutable() bool {
270 if !mod.Binary() {
271 return false
272 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400273 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900274}
275
Ivan Lozanod7586b62021-04-01 09:49:36 -0400276func (mod *Module) Object() bool {
277 // Rust has no modules which produce only object files.
278 return false
279}
280
Ivan Lozano52767be2019-10-18 14:49:46 -0700281func (mod *Module) Toc() android.OptionalPath {
282 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400283 if lib, ok := mod.compiler.(libraryInterface); ok {
284 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700285 }
286 }
287 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
288}
289
Colin Crossc511bc52020-04-07 16:50:32 +0000290func (mod *Module) UseSdk() bool {
291 return false
292}
293
Ivan Lozanod7586b62021-04-01 09:49:36 -0400294func (mod *Module) RelativeInstallPath() string {
295 if mod.compiler != nil {
296 return mod.compiler.relativeInstallPath()
297 }
298 return ""
299}
300
Ivan Lozano52767be2019-10-18 14:49:46 -0700301func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500302 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700303}
304
Jiyong Park7d55b612021-06-11 17:22:09 +0900305func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400306 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900307}
308
Ivan Lozano52767be2019-10-18 14:49:46 -0700309func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400310 return true
311}
312
313func (mod *Module) SubName() string {
314 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700315}
316
317func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500318 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700319 return false
320}
321
Ivan Lozanof9e21722020-12-02 09:00:51 -0500322func (mod *Module) IsVndkExt() bool {
323 return false
324}
325
Ivan Lozanod7586b62021-04-01 09:49:36 -0400326func (mod *Module) IsVndkSp() bool {
327 return false
328}
329
Colin Cross127bb8b2020-12-16 16:46:01 -0800330func (c *Module) IsVndkPrivate() bool {
331 return false
332}
333
334func (c *Module) IsLlndk() bool {
335 return false
336}
337
338func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500339 return false
340}
341
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400342func (mod *Module) KernelHeadersDecorator() bool {
343 return false
344}
345
Colin Cross1f3f1302021-04-26 18:37:44 -0700346func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400347 return false
348}
349
Colin Cross5271fea2021-04-27 13:06:04 -0700350func (m *Module) NeedsVendorPublicLibraryVariants() bool {
351 return false
352}
353
Ivan Lozanod7586b62021-04-01 09:49:36 -0400354func (mod *Module) HasLlndkStubs() bool {
355 return false
356}
357
358func (mod *Module) StubsVersion() string {
359 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
360}
361
Ivan Lozano52767be2019-10-18 14:49:46 -0700362func (mod *Module) SdkVersion() string {
363 return ""
364}
365
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900366func (mod *Module) MinSdkVersion() 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
393 CrtBegin, CrtEnd string
394}
395
396type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500397 DyLibs RustLibraries
398 RLibs RustLibraries
399 SharedLibs android.Paths
400 SharedLibDeps android.Paths
401 StaticLibs android.Paths
402 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500403
404 // depFlags and depLinkFlags are rustc and linker (clang) flags.
405 depFlags []string
406 depLinkFlags []string
407
408 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
409 // Both of these are exported and propagate to dependencies.
410 linkDirs []string
411 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700412
Ivan Lozano45901ed2020-07-24 16:05:01 -0400413 // Used by bindgen modules which call clang
414 depClangFlags []string
415 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400416 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400417 depSystemIncludePaths android.Paths
418
Ivan Lozanof1c84332019-09-20 11:00:37 -0700419 CrtBegin android.OptionalPath
420 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700421
422 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500423 SrcDeps android.Paths
424 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425}
426
427type RustLibraries []RustLibrary
428
429type RustLibrary struct {
430 Path android.Path
431 CrateName string
432}
433
434type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100435 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700436 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400437 cfgFlags(ctx ModuleContext, flags Flags) Flags
438 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700439 compilerProps() []interface{}
440 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
441 compilerDeps(ctx DepsContext, deps Deps) Deps
442 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700443 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100445 // Output directory in which source-generated code from dependencies is
446 // copied. This is equivalent to Cargo's OUT_DIR variable.
447 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700448
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400449 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
450 CargoPkgVersion() string
451
452 // CargoEnvCompat returns whether Cargo environment variables should be used.
453 CargoEnvCompat() bool
454
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800455 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200456 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700457 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400458 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400459
460 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400461
462 Disabled() bool
463 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400464
Ivan Lozanodd055472020-09-28 13:22:45 -0400465 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900466
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400467 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900468 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 {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400596 if mod.compiler != nil {
597 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400598 }
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 {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400654 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700655}
656
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400657func (mod *Module) CoverageFiles() android.Paths {
658 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800659 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400660 }
661 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
662}
663
Jiyong Park459feca2020-12-15 11:02:21 +0900664func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900665 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900666 return false
667 }
668
Jiyong Park459feca2020-12-15 11:02:21 +0900669 // The apex variant is not installable because it is included in the APEX and won't appear
670 // in the system partition as a standalone file.
671 if !apexInfo.IsForPlatform() {
672 return false
673 }
674
Jiyong Parke54f07e2021-04-07 15:08:04 +0900675 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900676}
677
Ivan Lozano183a3212019-10-18 14:18:45 -0700678var _ cc.LinkableInterface = (*Module)(nil)
679
Ivan Lozanoffee3342019-08-27 12:03:00 -0700680func (mod *Module) Init() android.Module {
681 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500682 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683
684 if mod.compiler != nil {
685 mod.AddProperties(mod.compiler.compilerProps()...)
686 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400687 if mod.coverage != nil {
688 mod.AddProperties(mod.coverage.props()...)
689 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200690 if mod.clippy != nil {
691 mod.AddProperties(mod.clippy.props()...)
692 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400693 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700694 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400695 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500696 if mod.sanitize != nil {
697 mod.AddProperties(mod.sanitize.props()...)
698 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400699
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900701 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700702
703 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704 return mod
705}
706
707func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
708 return &Module{
709 hod: hod,
710 multilib: multilib,
711 }
712}
713func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
714 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400715 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200716 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500717 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718 return module
719}
720
721type ModuleContext interface {
722 android.ModuleContext
723 ModuleContextIntf
724}
725
726type BaseModuleContext interface {
727 android.BaseModuleContext
728 ModuleContextIntf
729}
730
731type DepsContext interface {
732 android.BottomUpMutatorContext
733 ModuleContextIntf
734}
735
736type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200737 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700739}
740
741type depsContext struct {
742 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743}
744
745type moduleContext struct {
746 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747}
748
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200749type baseModuleContext struct {
750 android.BaseModuleContext
751}
752
753func (ctx *moduleContext) RustModule() *Module {
754 return ctx.Module().(*Module)
755}
756
757func (ctx *moduleContext) toolchain() config.Toolchain {
758 return ctx.RustModule().toolchain(ctx)
759}
760
761func (ctx *depsContext) RustModule() *Module {
762 return ctx.Module().(*Module)
763}
764
765func (ctx *depsContext) toolchain() config.Toolchain {
766 return ctx.RustModule().toolchain(ctx)
767}
768
769func (ctx *baseModuleContext) RustModule() *Module {
770 return ctx.Module().(*Module)
771}
772
773func (ctx *baseModuleContext) toolchain() config.Toolchain {
774 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400775}
776
777func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700778 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
779 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
780 return false
781 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400782 return mod.compiler != nil && mod.compiler.nativeCoverage()
783}
784
Ivan Lozanod7586b62021-04-01 09:49:36 -0400785func (mod *Module) EverInstallable() bool {
786 return mod.compiler != nil &&
787 // Check to see whether the module is actually ever installable.
788 mod.compiler.everInstallable()
789}
790
791func (mod *Module) Installable() *bool {
792 return mod.Properties.Installable
793}
794
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
796 if mod.cachedToolchain == nil {
797 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
798 }
799 return mod.cachedToolchain
800}
801
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200802func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
803 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
804}
805
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
807}
808
809func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
810 ctx := &moduleContext{
811 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700813
Jiyong Park99644e92020-11-17 22:21:02 +0900814 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
815 if !apexInfo.IsForPlatform() {
816 mod.hideApexVariantFromMake = true
817 }
818
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500820 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
821
822 // Differentiate static libraries that are vendor available
823 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700824 if mod.InProduct() && !mod.OnlyInProduct() {
825 mod.Properties.SubName += cc.ProductSuffix
826 } else {
827 mod.Properties.SubName += cc.VendorSuffix
828 }
Matthew Maurerc6868382021-07-13 14:12:37 -0700829 } else if mod.InRamdisk() && !mod.OnlyInRamdisk() {
830 mod.Properties.SubName += cc.RamdiskSuffix
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500831 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
832 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800833 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
834 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500835 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836
Matthew Maurera61e31f2021-05-27 11:09:11 -0700837 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
838 mod.Properties.SubName += cc.NativeBridgeSuffix
839 }
840
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 if !toolchain.Supported() {
842 // This toolchain's unsupported, there's nothing to do for this mod.
843 return
844 }
845
846 deps := mod.depsToPaths(ctx)
847 flags := Flags{
848 Toolchain: toolchain,
849 }
850
Ivan Lozano67eada32021-09-23 11:50:33 -0400851 // Calculate rustc flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 if mod.compiler != nil {
853 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400854 flags = mod.compiler.cfgFlags(ctx, flags)
855 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400856 }
857 if mod.coverage != nil {
858 flags, deps = mod.coverage.flags(ctx, flags, deps)
859 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200860 if mod.clippy != nil {
861 flags, deps = mod.clippy.flags(ctx, flags, deps)
862 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500863 if mod.sanitize != nil {
864 flags, deps = mod.sanitize.flags(ctx, flags, deps)
865 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400866
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200867 // SourceProvider needs to call GenerateSource() before compiler calls
868 // compile() so it can provide the source. A SourceProvider has
869 // multiple variants (e.g. source, rlib, dylib). Only the "source"
870 // variant is responsible for effectively generating the source. The
871 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400872 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200873 if mod.compiler.(libraryInterface).source() {
874 mod.sourceProvider.GenerateSource(ctx, deps)
875 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
876 } else {
877 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
878 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700879 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200880 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400881 }
882
883 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100884 mod.compiler.initialize(ctx)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400885 outputFile := mod.compiler.compile(ctx, flags, deps)
886 if ctx.Failed() {
887 return
888 }
889 mod.outputFile = android.OptionalPathForPath(outputFile)
890 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
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 {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001086 linkFile := rustDep.UnstrippedOutputFile()
1087 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001088 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1089 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001090 }
1091 }
1092
Ivan Lozano89435d12020-07-31 11:01:18 -04001093 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001094 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001095 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001096 if _, ok := ccDep.(*Module); !ok {
1097 if ccDep.Module().Target().Os != ctx.Os() {
1098 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1099 return
1100 }
1101 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1102 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1103 return
1104 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001105 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001106 linkObject := ccDep.OutputFile()
1107 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001108
Ivan Lozano2093af22020-08-25 12:48:19 -04001109 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001110 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1111 }
1112
1113 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001114 switch {
1115 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001116 if cc.IsWholeStaticLib(depTag) {
1117 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1118 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001119 if mod.Binary() {
1120 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1121 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1122 // final linkage, pass the args directly to the linker to handle these cases.
1123 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1124 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001125 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001126 } else {
1127 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 -05001128 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001129 }
1130
1131 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1132 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001133 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001134 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1135
Colin Cross0de8a1e2020-09-18 14:15:30 -07001136 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1137 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1138 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1139 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1140 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001141 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001142 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001143 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001144 // For the shared lib dependencies, we may link to the stub variant
1145 // of the dependency depending on the context (e.g. if this
1146 // dependency crosses the APEX boundaries).
1147 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1148
1149 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1150 // object (either from stub or non-stub) to use.
1151 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1152 linkPath = linkPathFromFilePath(linkObject.Path())
1153
Ivan Lozanoffee3342019-08-27 12:03:00 -07001154 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001155 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001156 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1157 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1158 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1159 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001160 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001161
1162 // Record baseLibName for snapshots.
1163 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001164 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001165
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001166 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001167 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001168 case cc.IsHeaderDepTag(depTag):
1169 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1170 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1171 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1172 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001173 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001174 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001175 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001176 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001177 }
1178
1179 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001180 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1181 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001182 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001183 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001184 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001185
1186 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001187 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001188 // These are usually genrules which don't have per-target variants.
1189 directSrcDeps = append(directSrcDeps, srcDep)
1190 }
1191 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001192 })
1193
1194 var rlibDepFiles RustLibraries
1195 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001196 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001197 }
1198 var dylibDepFiles RustLibraries
1199 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001200 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001201 }
1202 var procMacroDepFiles RustLibraries
1203 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001204 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001205 }
1206
1207 var staticLibDepFiles android.Paths
1208 for _, dep := range directStaticLibDeps {
1209 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1210 }
1211
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001212 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001213 var sharedLibDepFiles android.Paths
1214 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001215 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1216 if dep.TableOfContents.Valid() {
1217 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001218 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001219 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001220 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001221 }
1222
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001223 var srcProviderDepFiles android.Paths
1224 for _, dep := range directSrcProvidersDeps {
1225 srcs, _ := dep.OutputFiles("")
1226 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1227 }
1228 for _, dep := range directSrcDeps {
1229 srcs := dep.Srcs()
1230 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1231 }
1232
Ivan Lozanoffee3342019-08-27 12:03:00 -07001233 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1234 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1235 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001236 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1238 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001239 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001240
1241 // Dedup exported flags from dependencies
1242 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001243 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001244 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001245 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1246 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1247 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001248
1249 return depPaths
1250}
1251
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001252func (mod *Module) InstallInData() bool {
1253 if mod.compiler == nil {
1254 return false
1255 }
1256 return mod.compiler.inData()
1257}
1258
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001259func (mod *Module) InstallInRamdisk() bool {
1260 return mod.InRamdisk()
1261}
1262
1263func (mod *Module) InstallInVendorRamdisk() bool {
1264 return mod.InVendorRamdisk()
1265}
1266
1267func (mod *Module) InstallInRecovery() bool {
1268 return mod.InRecovery()
1269}
1270
Colin Cross77435572021-11-08 12:37:01 -08001271func (mod *Module) InstallBypassMake() bool {
1272 return true
1273}
1274
Ivan Lozanoffee3342019-08-27 12:03:00 -07001275func linkPathFromFilePath(filepath android.Path) string {
1276 return strings.Split(filepath.String(), filepath.Base())[0]
1277}
Ivan Lozanod648c432020-02-06 12:05:10 -05001278
Ivan Lozanoffee3342019-08-27 12:03:00 -07001279func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1280 ctx := &depsContext{
1281 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001282 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001283
1284 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001285 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001286 var snapshotInfo *cc.SnapshotInfo
1287
1288 if ctx.Os() == android.Android {
1289 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1290 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001291
Ivan Lozano2b081132020-09-08 12:46:52 -04001292 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001293 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001294 stdLinkage = "rlib-std"
1295 }
1296
1297 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001298
Ivan Lozano2b081132020-09-08 12:46:52 -04001299 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1300 rlibDepVariations = append(rlibDepVariations,
1301 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1302 }
1303
Ivan Lozano1921e802021-05-20 13:39:16 -04001304 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001305 for _, lib := range deps.Rlibs {
1306 depTag := rlibDepTag
1307 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1308
1309 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1310 {Mutator: "rust_libraries", Variation: rlibVariation},
1311 }...), depTag, lib)
1312 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001313
1314 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001315 actx.AddVariationDependencies(
1316 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001317 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001318 dylibDepTag, deps.Dylibs...)
1319
Ivan Lozano1921e802021-05-20 13:39:16 -04001320 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001321 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1322 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001323 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001324 for _, lib := range deps.Rustlibs {
1325 depTag := autoDep.depTag
1326 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1327 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1328 {Mutator: "rust_libraries", Variation: autoDep.variation},
1329 }...), depTag, lib)
1330 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001331 } else {
1332 actx.AddVariationDependencies(
1333 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1334 autoDep.depTag, deps.Rustlibs...)
1335 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001336 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001337
1338 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001339 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001340 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001341 for _, lib := range deps.Stdlibs {
1342 depTag := rlibDepTag
1343 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1344
1345 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1346 depTag, lib)
1347 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001348 } else {
1349 actx.AddVariationDependencies(
1350 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1351 dylibDepTag, deps.Stdlibs...)
1352 }
1353 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001354
1355 for _, lib := range deps.SharedLibs {
1356 depTag := cc.SharedDepTag()
1357 name, version := cc.StubsLibNameAndVersion(lib)
1358
1359 variations := []blueprint.Variation{
1360 {Mutator: "link", Variation: "shared"},
1361 }
1362 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1363 }
1364
1365 for _, lib := range deps.WholeStaticLibs {
1366 depTag := cc.StaticDepTag(true)
1367 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1368
1369 actx.AddVariationDependencies([]blueprint.Variation{
1370 {Mutator: "link", Variation: "static"},
1371 }, depTag, lib)
1372 }
1373
1374 for _, lib := range deps.StaticLibs {
1375 depTag := cc.StaticDepTag(false)
1376 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1377
1378 actx.AddVariationDependencies([]blueprint.Variation{
1379 {Mutator: "link", Variation: "static"},
1380 }, depTag, lib)
1381 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001382
Zach Johnson3df4e632020-11-06 11:56:27 -08001383 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1384
Colin Cross565cafd2020-09-25 18:47:38 -07001385 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001386 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001387 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1388 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001389 }
1390 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001391 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1392 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001393 }
1394
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001395 if mod.sourceProvider != nil {
1396 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1397 bindgen.Properties.Custom_bindgen != "" {
1398 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1399 bindgen.Properties.Custom_bindgen)
1400 }
1401 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001402
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001403 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001404 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001405}
1406
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001407func BeginMutator(ctx android.BottomUpMutatorContext) {
1408 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1409 mod.beginMutator(ctx)
1410 }
1411}
1412
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001413func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1414 ctx := &baseModuleContext{
1415 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001416 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001417
1418 mod.begin(ctx)
1419}
1420
Ivan Lozanoffee3342019-08-27 12:03:00 -07001421func (mod *Module) Name() string {
1422 name := mod.ModuleBase.Name()
1423 if p, ok := mod.compiler.(interface {
1424 Name(string) string
1425 }); ok {
1426 name = p.Name(name)
1427 }
1428 return name
1429}
1430
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001431func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001432 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001433 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001434 }
1435}
1436
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001437var _ android.HostToolProvider = (*Module)(nil)
1438
1439func (mod *Module) HostToolPath() android.OptionalPath {
1440 if !mod.Host() {
1441 return android.OptionalPath{}
1442 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001443 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1444 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001445 }
1446 return android.OptionalPath{}
1447}
1448
Jiyong Park99644e92020-11-17 22:21:02 +09001449var _ android.ApexModule = (*Module)(nil)
1450
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001451func (mod *Module) minSdkVersion() string {
1452 return String(mod.Properties.Min_sdk_version)
1453}
1454
Jiyong Park45bf82e2020-12-15 22:29:02 +09001455var _ android.ApexModule = (*Module)(nil)
1456
1457// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001458func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001459 minSdkVersion := mod.minSdkVersion()
1460 if minSdkVersion == "apex_inherit" {
1461 return nil
1462 }
1463 if minSdkVersion == "" {
1464 return fmt.Errorf("min_sdk_version is not specificed")
1465 }
1466
1467 // Not using nativeApiLevelFromUser because the context here is not
1468 // necessarily a native context.
1469 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1470 if err != nil {
1471 return err
1472 }
1473
1474 if ver.GreaterThan(sdkVersion) {
1475 return fmt.Errorf("newer SDK(%v)", ver)
1476 }
Jiyong Park99644e92020-11-17 22:21:02 +09001477 return nil
1478}
1479
Jiyong Park45bf82e2020-12-15 22:29:02 +09001480// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001481func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1482 depTag := ctx.OtherModuleDependencyTag(dep)
1483
1484 if ccm, ok := dep.(*cc.Module); ok {
1485 if ccm.HasStubsVariants() {
1486 if cc.IsSharedDepTag(depTag) {
1487 // dynamic dep to a stubs lib crosses APEX boundary
1488 return false
1489 }
1490 if cc.IsRuntimeDepTag(depTag) {
1491 // runtime dep to a stubs lib also crosses APEX boundary
1492 return false
1493 }
1494
1495 if cc.IsHeaderDepTag(depTag) {
1496 return false
1497 }
1498 }
1499 if mod.Static() && cc.IsSharedDepTag(depTag) {
1500 // shared_lib dependency from a static lib is considered as crossing
1501 // the APEX boundary because the dependency doesn't actually is
1502 // linked; the dependency is used only during the compilation phase.
1503 return false
1504 }
1505 }
1506
1507 if depTag == procMacroDepTag {
1508 return false
1509 }
1510
1511 return true
1512}
1513
1514// Overrides ApexModule.IsInstallabeToApex()
1515func (mod *Module) IsInstallableToApex() bool {
1516 if mod.compiler != nil {
1517 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1518 return true
1519 }
1520 if _, ok := mod.compiler.(*binaryDecorator); ok {
1521 return true
1522 }
1523 }
1524 return false
1525}
1526
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001527// If a library file has a "lib" prefix, extract the library name without the prefix.
1528func libNameFromFilePath(filepath android.Path) (string, bool) {
1529 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1530 if strings.HasPrefix(libName, "lib") {
1531 libName = libName[3:]
1532 return libName, true
1533 }
1534 return "", false
1535}
1536
Ivan Lozanoffee3342019-08-27 12:03:00 -07001537var Bool = proptools.Bool
1538var BoolDefault = proptools.BoolDefault
1539var String = proptools.String
1540var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001541
1542var _ android.OutputFileProducer = (*Module)(nil)