blob: 93dbd001003dee4ad27117b99e1a521eb4fddb33 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Ivan Lozano183a3212019-10-18 14:18:45 -070018 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +020025 "android/soong/bloaty"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020027 cc_config "android/soong/cc/config"
hamzehc0a671f2021-07-22 12:05:08 -070028 "android/soong/fuzz"
Ivan Lozanoffee3342019-08-27 12:03:00 -070029 "android/soong/rust/config"
30)
31
32var pctx = android.NewPackageContext("android/soong/rust")
33
34func init() {
35 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070036
37 android.AddNeverAllowRules(
38 android.NeverAllow().
Ivan Lozano03a94c42021-06-28 11:27:11 -040039 NotIn(append(config.RustAllowedPaths, config.DownstreamRustAllowedPaths...)...).
Ivan Lozanoe169ad72019-09-18 08:42:54 -070040 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070041
42 android.RegisterModuleType("rust_defaults", defaultsFactory)
43 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040045 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040046 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050047
48 })
49 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
50 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070051 })
52 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020053 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070054}
55
56type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040057 GlobalRustFlags []string // Flags that apply globally to rust
58 GlobalLinkFlags []string // Flags that apply globally to linker
59 RustFlags []string // Flags that apply to rust
60 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070062 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070063 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040064 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020065 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type BaseProperties struct {
69 AndroidMkRlibs []string
70 AndroidMkDylibs []string
71 AndroidMkProcMacroLibs []string
72 AndroidMkSharedLibs []string
73 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040074
Ivan Lozano6a884432020-12-02 09:15:16 -050075 ImageVariationPrefix string `blueprint:"mutated"`
76 VndkVersion string `blueprint:"mutated"`
77 SubName string `blueprint:"mutated"`
78
Ivan Lozanoc08897c2021-04-02 12:41:32 -040079 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
80 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
81 // appended before SubName.
82 RustSubName string `blueprint:"mutated"`
83
Ivan Lozano6a884432020-12-02 09:15:16 -050084 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 CoreVariantNeeded bool `blueprint:"mutated"`
86 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070087 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080088 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050089 ExtraVariants []string `blueprint:"mutated"`
90
Ivan Lozanoa2268632021-07-22 10:52:06 -040091 // Allows this module to use non-APEX version of libraries. Useful
92 // for building binaries that are started before APEXes are activated.
93 Bootstrap *bool
94
Ivan Lozano1921e802021-05-20 13:39:16 -040095 // Used by vendor snapshot to record dependencies from snapshot modules.
96 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090097 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040098
Matthew Maurerc6868382021-07-13 14:12:37 -070099 // Make this module available when building for ramdisk.
100 // On device without a dedicated recovery partition, the module is only
101 // available after switching root into
102 // /first_stage_ramdisk. To expose the module before switching root, install
103 // the recovery variant instead.
104 Ramdisk_available *bool
105
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500106 // Make this module available when building for vendor ramdisk.
107 // On device without a dedicated recovery partition, the module is only
108 // available after switching root into
109 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800110 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500111 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400112
Ivan Lozano1921e802021-05-20 13:39:16 -0400113 // Normally Soong uses the directory structure to decide which modules
114 // should be included (framework) or excluded (non-framework) from the
115 // different snapshots (vendor, recovery, etc.), but this property
116 // allows a partner to exclude a module normally thought of as a
117 // framework module from the vendor snapshot.
118 Exclude_from_vendor_snapshot *bool
119
120 // Normally Soong uses the directory structure to decide which modules
121 // should be included (framework) or excluded (non-framework) from the
122 // different snapshots (vendor, recovery, etc.), but this property
123 // allows a partner to exclude a module normally thought of as a
124 // framework module from the recovery snapshot.
125 Exclude_from_recovery_snapshot *bool
126
Matthew Maurer460ee942021-02-11 12:31:46 -0800127 // Make this module available when building for recovery
128 Recovery_available *bool
129
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500130 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
131 Min_sdk_version *string
132
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900133 HideFromMake bool `blueprint:"mutated"`
134 PreventInstall bool `blueprint:"mutated"`
135
136 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137}
138
139type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700140 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
Ivan Lozano6a884432020-12-02 09:15:16 -0500142 VendorProperties cc.VendorProperties
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144 Properties BaseProperties
145
146 hod android.HostOrDeviceSupported
147 multilib android.Multilib
148
Ivan Lozano6a884432020-12-02 09:15:16 -0500149 makeLinkType string
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400152 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200153 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500154 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700157 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158
Jiyong Parke54f07e2021-04-07 15:08:04 +0900159 // Unstripped output. This is usually used when this module is linked to another module
160 // as a library. The stripped output which is used for installation can be found via
161 // compiler.strippedOutputFile if it exists.
162 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700163 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900164
165 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700166}
167
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500168func (mod *Module) Header() bool {
169 //TODO: If Rust libraries provide header variants, this needs to be updated.
170 return false
171}
172
173func (mod *Module) SetPreventInstall() {
174 mod.Properties.PreventInstall = true
175}
176
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500177func (mod *Module) SetHideFromMake() {
178 mod.Properties.HideFromMake = true
179}
180
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900181func (mod *Module) HiddenFromMake() bool {
182 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400183}
184
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500185func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500186 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
187 // nil since we need compiler to actually sanitize.
188 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500189}
190
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500191func (mod *Module) IsPrebuilt() bool {
192 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
193 return true
194 }
195 return false
196}
197
Ivan Lozano43845682020-07-09 21:03:28 -0400198func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
199 switch tag {
200 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700201 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400202 return mod.sourceProvider.Srcs(), nil
203 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900204 if mod.OutputFile().Valid() {
205 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400206 }
207 return android.Paths{}, nil
208 }
209 default:
210 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
211 }
212}
213
Ivan Lozano52767be2019-10-18 14:49:46 -0700214func (mod *Module) SelectedStl() string {
215 return ""
216}
217
Ivan Lozano2b262972019-11-21 12:30:50 -0800218func (mod *Module) NonCcVariants() bool {
219 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400220 if _, ok := mod.compiler.(libraryInterface); ok {
221 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800222 }
223 }
224 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
225}
226
Ivan Lozano52767be2019-10-18 14:49:46 -0700227func (mod *Module) Static() bool {
228 if mod.compiler != nil {
229 if library, ok := mod.compiler.(libraryInterface); ok {
230 return library.static()
231 }
232 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400233 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700234}
235
236func (mod *Module) Shared() bool {
237 if mod.compiler != nil {
238 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400239 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700240 }
241 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400242 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700243}
244
Ivan Lozanod7586b62021-04-01 09:49:36 -0400245func (mod *Module) Dylib() bool {
246 if mod.compiler != nil {
247 if library, ok := mod.compiler.(libraryInterface); ok {
248 return library.dylib()
249 }
250 }
251 return false
252}
253
254func (mod *Module) Rlib() bool {
255 if mod.compiler != nil {
256 if library, ok := mod.compiler.(libraryInterface); ok {
257 return library.rlib()
258 }
259 }
260 return false
261}
262
263func (mod *Module) Binary() bool {
264 if mod.compiler != nil {
265 if _, ok := mod.compiler.(*binaryDecorator); ok {
266 return true
267 }
268 }
269 return false
270}
271
Justin Yun5e035862021-06-29 20:50:37 +0900272func (mod *Module) StaticExecutable() bool {
273 if !mod.Binary() {
274 return false
275 }
276 return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
277}
278
Ivan Lozanod7586b62021-04-01 09:49:36 -0400279func (mod *Module) Object() bool {
280 // Rust has no modules which produce only object files.
281 return false
282}
283
Ivan Lozano52767be2019-10-18 14:49:46 -0700284func (mod *Module) Toc() android.OptionalPath {
285 if mod.compiler != nil {
286 if _, ok := mod.compiler.(libraryInterface); ok {
287 return android.OptionalPath{}
288 }
289 }
290 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
291}
292
Colin Crossc511bc52020-04-07 16:50:32 +0000293func (mod *Module) UseSdk() bool {
294 return false
295}
296
Ivan Lozanod7586b62021-04-01 09:49:36 -0400297func (mod *Module) RelativeInstallPath() string {
298 if mod.compiler != nil {
299 return mod.compiler.relativeInstallPath()
300 }
301 return ""
302}
303
Ivan Lozano52767be2019-10-18 14:49:46 -0700304func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500305 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700306}
307
Jiyong Park7d55b612021-06-11 17:22:09 +0900308func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400309 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900310}
311
Ivan Lozano52767be2019-10-18 14:49:46 -0700312func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400313 return true
314}
315
316func (mod *Module) SubName() string {
317 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700318}
319
320func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500321 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 return false
323}
324
Ivan Lozanof9e21722020-12-02 09:00:51 -0500325func (mod *Module) IsVndkExt() bool {
326 return false
327}
328
Ivan Lozanod7586b62021-04-01 09:49:36 -0400329func (mod *Module) IsVndkSp() bool {
330 return false
331}
332
Colin Cross127bb8b2020-12-16 16:46:01 -0800333func (c *Module) IsVndkPrivate() bool {
334 return false
335}
336
337func (c *Module) IsLlndk() bool {
338 return false
339}
340
341func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500342 return false
343}
344
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400345func (mod *Module) KernelHeadersDecorator() bool {
346 return false
347}
348
Colin Cross1f3f1302021-04-26 18:37:44 -0700349func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400350 return false
351}
352
Colin Cross5271fea2021-04-27 13:06:04 -0700353func (m *Module) NeedsVendorPublicLibraryVariants() bool {
354 return false
355}
356
Ivan Lozanod7586b62021-04-01 09:49:36 -0400357func (mod *Module) HasLlndkStubs() bool {
358 return false
359}
360
361func (mod *Module) StubsVersion() string {
362 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
363}
364
Ivan Lozano52767be2019-10-18 14:49:46 -0700365func (mod *Module) SdkVersion() string {
366 return ""
367}
368
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900369func (mod *Module) MinSdkVersion() string {
370 return ""
371}
372
Colin Crossc511bc52020-04-07 16:50:32 +0000373func (mod *Module) AlwaysSdk() bool {
374 return false
375}
376
Jiyong Park2286afd2020-06-16 21:58:53 +0900377func (mod *Module) IsSdkVariant() bool {
378 return false
379}
380
Colin Cross1348ce32020-10-01 13:37:16 -0700381func (mod *Module) SplitPerApiLevel() bool {
382 return false
383}
384
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400386 Dylibs []string
387 Rlibs []string
388 Rustlibs []string
389 Stdlibs []string
390 ProcMacros []string
391 SharedLibs []string
392 StaticLibs []string
393 WholeStaticLibs []string
394 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700395
396 CrtBegin, CrtEnd string
397}
398
399type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500400 DyLibs RustLibraries
401 RLibs RustLibraries
402 SharedLibs android.Paths
403 SharedLibDeps android.Paths
404 StaticLibs android.Paths
405 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500406
407 // depFlags and depLinkFlags are rustc and linker (clang) flags.
408 depFlags []string
409 depLinkFlags []string
410
411 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
412 // Both of these are exported and propagate to dependencies.
413 linkDirs []string
414 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700415
Ivan Lozano45901ed2020-07-24 16:05:01 -0400416 // Used by bindgen modules which call clang
417 depClangFlags []string
418 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400419 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400420 depSystemIncludePaths android.Paths
421
Ivan Lozanof1c84332019-09-20 11:00:37 -0700422 CrtBegin android.OptionalPath
423 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700424
425 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500426 SrcDeps android.Paths
427 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428}
429
430type RustLibraries []RustLibrary
431
432type RustLibrary struct {
433 Path android.Path
434 CrateName string
435}
436
437type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100438 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700439 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400440 cfgFlags(ctx ModuleContext, flags Flags) Flags
441 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442 compilerProps() []interface{}
443 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
444 compilerDeps(ctx DepsContext, deps Deps) Deps
445 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700446 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100448 // Output directory in which source-generated code from dependencies is
449 // copied. This is equivalent to Cargo's OUT_DIR variable.
450 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700451
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400452 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
453 CargoPkgVersion() string
454
455 // CargoEnvCompat returns whether Cargo environment variables should be used.
456 CargoEnvCompat() bool
457
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800458 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200459 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700460 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400461 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400462
463 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400464
465 Disabled() bool
466 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400467
Ivan Lozanodd055472020-09-28 13:22:45 -0400468 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900469
470 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400471}
472
Matthew Maurerbb3add12020-06-25 09:34:12 -0700473type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700474 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400475 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700476}
477
478type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400479 linkDirs []string
480 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700481}
482
Matthew Maurerbb3add12020-06-25 09:34:12 -0700483func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
484 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
485}
486
Ivan Lozano2093af22020-08-25 12:48:19 -0400487func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
488 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
489}
490
Colin Cross0de8a1e2020-09-18 14:15:30 -0700491func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
492 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700493 LinkDirs: flagExporter.linkDirs,
494 LinkObjects: flagExporter.linkObjects,
495 })
496}
497
Matthew Maurerbb3add12020-06-25 09:34:12 -0700498var _ exportedFlagsProducer = (*flagExporter)(nil)
499
500func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700501 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700502}
503
Colin Cross0de8a1e2020-09-18 14:15:30 -0700504type FlagExporterInfo struct {
505 Flags []string
506 LinkDirs []string // TODO: this should be android.Paths
507 LinkObjects []string // TODO: this should be android.Paths
508}
509
510var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
511
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512func (mod *Module) isCoverageVariant() bool {
513 return mod.coverage.Properties.IsCoverageVariant
514}
515
516var _ cc.Coverage = (*Module)(nil)
517
518func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
519 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
520}
521
Ivan Lozanod7586b62021-04-01 09:49:36 -0400522func (mod *Module) VndkVersion() string {
523 return mod.Properties.VndkVersion
524}
525
526func (mod *Module) PreventInstall() bool {
527 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400528}
529
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400530func (mod *Module) MarkAsCoverageVariant(coverage bool) {
531 mod.coverage.Properties.IsCoverageVariant = coverage
532}
533
534func (mod *Module) EnableCoverageIfNeeded() {
535 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700536}
537
538func defaultsFactory() android.Module {
539 return DefaultsFactory()
540}
541
542type Defaults struct {
543 android.ModuleBase
544 android.DefaultsModuleBase
545}
546
547func DefaultsFactory(props ...interface{}) android.Module {
548 module := &Defaults{}
549
550 module.AddProperties(props...)
551 module.AddProperties(
552 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500553 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100554 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400555 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556 &BaseCompilerProperties{},
557 &BinaryCompilerProperties{},
558 &LibraryCompilerProperties{},
559 &ProcMacroCompilerProperties{},
560 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400561 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700562 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400563 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400564 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200565 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500566 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567 )
568
569 android.InitDefaultsModule(module)
570 return module
571}
572
573func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700574 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700575}
576
Ivan Lozano183a3212019-10-18 14:18:45 -0700577func (mod *Module) CcLibrary() bool {
578 if mod.compiler != nil {
579 if _, ok := mod.compiler.(*libraryDecorator); ok {
580 return true
581 }
582 }
583 return false
584}
585
586func (mod *Module) CcLibraryInterface() bool {
587 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400588 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
589 // VariantIs{Static,Shared} is set.
590 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700591 return true
592 }
593 }
594 return false
595}
596
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800597func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700598 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700599 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800600 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700601 }
602 }
603 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
604}
605
606func (mod *Module) SetStatic() {
607 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700608 if library, ok := mod.compiler.(libraryInterface); ok {
609 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700610 return
611 }
612 }
613 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
614}
615
616func (mod *Module) SetShared() {
617 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700618 if library, ok := mod.compiler.(libraryInterface); ok {
619 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700620 return
621 }
622 }
623 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
624}
625
Ivan Lozano183a3212019-10-18 14:18:45 -0700626func (mod *Module) BuildStaticVariant() bool {
627 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700628 if library, ok := mod.compiler.(libraryInterface); ok {
629 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700630 }
631 }
632 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
633}
634
635func (mod *Module) BuildSharedVariant() bool {
636 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700637 if library, ok := mod.compiler.(libraryInterface); ok {
638 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700639 }
640 }
641 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
642}
643
Ivan Lozano183a3212019-10-18 14:18:45 -0700644func (mod *Module) Module() android.Module {
645 return mod
646}
647
Ivan Lozano183a3212019-10-18 14:18:45 -0700648func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900649 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
650 return mod.compiler.strippedOutputFilePath()
651 }
652 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700653}
654
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400655func (mod *Module) CoverageFiles() android.Paths {
656 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800657 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400658 }
659 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
660}
661
Jiyong Park459feca2020-12-15 11:02:21 +0900662func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900663 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900664 return false
665 }
666
Jiyong Park459feca2020-12-15 11:02:21 +0900667 // The apex variant is not installable because it is included in the APEX and won't appear
668 // in the system partition as a standalone file.
669 if !apexInfo.IsForPlatform() {
670 return false
671 }
672
Jiyong Parke54f07e2021-04-07 15:08:04 +0900673 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900674}
675
Ivan Lozano183a3212019-10-18 14:18:45 -0700676var _ cc.LinkableInterface = (*Module)(nil)
677
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678func (mod *Module) Init() android.Module {
679 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500680 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700681
682 if mod.compiler != nil {
683 mod.AddProperties(mod.compiler.compilerProps()...)
684 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400685 if mod.coverage != nil {
686 mod.AddProperties(mod.coverage.props()...)
687 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200688 if mod.clippy != nil {
689 mod.AddProperties(mod.clippy.props()...)
690 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400691 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700692 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400693 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500694 if mod.sanitize != nil {
695 mod.AddProperties(mod.sanitize.props()...)
696 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400697
Ivan Lozanoffee3342019-08-27 12:03:00 -0700698 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900699 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700
701 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700702 return mod
703}
704
705func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
706 return &Module{
707 hod: hod,
708 multilib: multilib,
709 }
710}
711func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
712 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400713 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200714 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500715 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716 return module
717}
718
719type ModuleContext interface {
720 android.ModuleContext
721 ModuleContextIntf
722}
723
724type BaseModuleContext interface {
725 android.BaseModuleContext
726 ModuleContextIntf
727}
728
729type DepsContext interface {
730 android.BottomUpMutatorContext
731 ModuleContextIntf
732}
733
734type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200735 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737}
738
739type depsContext struct {
740 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741}
742
743type moduleContext struct {
744 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745}
746
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200747type baseModuleContext struct {
748 android.BaseModuleContext
749}
750
751func (ctx *moduleContext) RustModule() *Module {
752 return ctx.Module().(*Module)
753}
754
755func (ctx *moduleContext) toolchain() config.Toolchain {
756 return ctx.RustModule().toolchain(ctx)
757}
758
759func (ctx *depsContext) RustModule() *Module {
760 return ctx.Module().(*Module)
761}
762
763func (ctx *depsContext) toolchain() config.Toolchain {
764 return ctx.RustModule().toolchain(ctx)
765}
766
767func (ctx *baseModuleContext) RustModule() *Module {
768 return ctx.Module().(*Module)
769}
770
771func (ctx *baseModuleContext) toolchain() config.Toolchain {
772 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400773}
774
775func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700776 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
777 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
778 return false
779 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400780 return mod.compiler != nil && mod.compiler.nativeCoverage()
781}
782
Ivan Lozanod7586b62021-04-01 09:49:36 -0400783func (mod *Module) EverInstallable() bool {
784 return mod.compiler != nil &&
785 // Check to see whether the module is actually ever installable.
786 mod.compiler.everInstallable()
787}
788
789func (mod *Module) Installable() *bool {
790 return mod.Properties.Installable
791}
792
Ivan Lozanoffee3342019-08-27 12:03:00 -0700793func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
794 if mod.cachedToolchain == nil {
795 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
796 }
797 return mod.cachedToolchain
798}
799
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200800func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
801 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
802}
803
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
805}
806
807func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
808 ctx := &moduleContext{
809 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811
Jiyong Park99644e92020-11-17 22:21:02 +0900812 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
813 if !apexInfo.IsForPlatform() {
814 mod.hideApexVariantFromMake = true
815 }
816
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500818 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
819
820 // Differentiate static libraries that are vendor available
821 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700822 if mod.InProduct() && !mod.OnlyInProduct() {
823 mod.Properties.SubName += cc.ProductSuffix
824 } else {
825 mod.Properties.SubName += cc.VendorSuffix
826 }
Matthew Maurerc6868382021-07-13 14:12:37 -0700827 } else if mod.InRamdisk() && !mod.OnlyInRamdisk() {
828 mod.Properties.SubName += cc.RamdiskSuffix
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500829 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
830 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800831 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
832 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500833 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834
Matthew Maurera61e31f2021-05-27 11:09:11 -0700835 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
836 mod.Properties.SubName += cc.NativeBridgeSuffix
837 }
838
Ivan Lozanoffee3342019-08-27 12:03:00 -0700839 if !toolchain.Supported() {
840 // This toolchain's unsupported, there's nothing to do for this mod.
841 return
842 }
843
844 deps := mod.depsToPaths(ctx)
845 flags := Flags{
846 Toolchain: toolchain,
847 }
848
Ivan Lozano67eada32021-09-23 11:50:33 -0400849 // Calculate rustc flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850 if mod.compiler != nil {
851 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400852 flags = mod.compiler.cfgFlags(ctx, flags)
853 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400854 }
855 if mod.coverage != nil {
856 flags, deps = mod.coverage.flags(ctx, flags, deps)
857 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200858 if mod.clippy != nil {
859 flags, deps = mod.clippy.flags(ctx, flags, deps)
860 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500861 if mod.sanitize != nil {
862 flags, deps = mod.sanitize.flags(ctx, flags, deps)
863 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400864
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200865 // SourceProvider needs to call GenerateSource() before compiler calls
866 // compile() so it can provide the source. A SourceProvider has
867 // multiple variants (e.g. source, rlib, dylib). Only the "source"
868 // variant is responsible for effectively generating the source. The
869 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400870 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200871 if mod.compiler.(libraryInterface).source() {
872 mod.sourceProvider.GenerateSource(ctx, deps)
873 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
874 } else {
875 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
876 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700877 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200878 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400879 }
880
881 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100882 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900883 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900884 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200885 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900886
Dan Albert06feee92021-03-19 15:06:02 -0700887 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
888
Ivan Lozano1921e802021-05-20 13:39:16 -0400889 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
890 // RECOVERY_SNAPSHOT_VERSION is current.
891 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
892 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
893 lib.collectHeadersForSnapshot(ctx, deps)
894 }
895 }
896
Jiyong Park459feca2020-12-15 11:02:21 +0900897 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900898 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
899 // If the module has been specifically configure to not be installed then
900 // hide from make as otherwise it will break when running inside make as the
901 // output path to install will not be specified. Not all uninstallable
902 // modules can be hidden from make as some are needed for resolving make
903 // side dependencies.
904 mod.HideFromMake()
905 } else if !mod.installable(apexInfo) {
906 mod.SkipInstall()
907 }
908
909 // Still call install though, the installs will be stored as PackageSpecs to allow
910 // using the outputs in a genrule.
911 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200912 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900913 if ctx.Failed() {
914 return
915 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400916 }
Chris Wailes74be7642021-07-22 16:20:28 -0700917
918 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919 }
920}
921
922func (mod *Module) deps(ctx DepsContext) Deps {
923 deps := Deps{}
924
925 if mod.compiler != nil {
926 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400927 }
928 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700929 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930 }
931
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400932 if mod.coverage != nil {
933 deps = mod.coverage.deps(ctx, deps)
934 }
935
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500936 if mod.sanitize != nil {
937 deps = mod.sanitize.deps(ctx, deps)
938 }
939
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
941 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700942 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700943 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
944 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
945 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400946 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700947 return deps
948
949}
950
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951type dependencyTag struct {
952 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800953 name string
954 library bool
955 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956}
957
Jiyong Park65b62242020-11-25 12:44:59 +0900958// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
959// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
960func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800961 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900962}
963
964var _ android.InstallNeededDependencyTag = dependencyTag{}
965
Ivan Lozanoffee3342019-08-27 12:03:00 -0700966var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400967 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
968 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
969 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800970 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400971 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200972 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700973)
974
Jiyong Park99644e92020-11-17 22:21:02 +0900975func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
976 tag, ok := depTag.(dependencyTag)
977 return ok && tag == dylibDepTag
978}
979
Jiyong Park94e22fd2021-04-08 18:19:15 +0900980func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
981 tag, ok := depTag.(dependencyTag)
982 return ok && tag == rlibDepTag
983}
984
Matthew Maurer0f003b12020-06-29 14:34:06 -0700985type autoDep struct {
986 variation string
987 depTag dependencyTag
988}
989
990var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200991 rlibVariation = "rlib"
992 dylibVariation = "dylib"
993 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
994 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700995)
996
997type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500998 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700999}
1000
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001001func (mod *Module) begin(ctx BaseModuleContext) {
1002 if mod.coverage != nil {
1003 mod.coverage.begin(ctx)
1004 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001005 if mod.sanitize != nil {
1006 mod.sanitize.begin(ctx)
1007 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001008}
1009
Ivan Lozanoffee3342019-08-27 12:03:00 -07001010func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1011 var depPaths PathDeps
1012
1013 directRlibDeps := []*Module{}
1014 directDylibDeps := []*Module{}
1015 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001016 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001017 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001018 directSrcProvidersDeps := []*Module{}
1019 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020
1021 ctx.VisitDirectDeps(func(dep android.Module) {
1022 depName := ctx.OtherModuleName(dep)
1023 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001024
Ivan Lozano89435d12020-07-31 11:01:18 -04001025 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001027 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001028
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 switch depTag {
1030 case dylibDepTag:
1031 dylib, ok := rustDep.compiler.(libraryInterface)
1032 if !ok || !dylib.dylib() {
1033 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1034 return
1035 }
1036 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001037 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001039
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 rlib, ok := rustDep.compiler.(libraryInterface)
1041 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001042 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 return
1044 }
1045 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001046 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 case procMacroDepTag:
1048 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001049 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001050 }
1051
1052 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001053 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1054 // OS/Arch variant is used.
1055 var helper string
1056 if ctx.Host() {
1057 helper = "missing 'host_supported'?"
1058 } else {
1059 helper = "device module defined?"
1060 }
1061
1062 if dep.Target().Os != ctx.Os() {
1063 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1064 return
1065 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1066 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1067 return
1068 }
1069 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070 }
1071
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001072 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001073 if depTag != procMacroDepTag {
1074 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1075 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1076 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1077 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078 }
1079
Ivan Lozanoffee3342019-08-27 12:03:00 -07001080 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001081 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001082 if !linkFile.Valid() {
1083 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1084 depName, ctx.ModuleName())
1085 return
1086 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087 linkDir := linkPathFromFilePath(linkFile.Path())
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 Lozanofb6f36f2021-02-05 12:27:08 -05001119 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1120 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001121 } else {
1122 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 -05001123 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001124 }
1125
1126 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1127 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001128 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001129 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1130
Colin Cross0de8a1e2020-09-18 14:15:30 -07001131 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1132 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1133 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1134 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1135 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001137 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001138 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001139 // For the shared lib dependencies, we may link to the stub variant
1140 // of the dependency depending on the context (e.g. if this
1141 // dependency crosses the APEX boundaries).
1142 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1143
1144 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1145 // object (either from stub or non-stub) to use.
1146 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1147 linkPath = linkPathFromFilePath(linkObject.Path())
1148
Ivan Lozanoffee3342019-08-27 12:03:00 -07001149 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001150 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001151 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1152 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1153 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1154 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001155 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001156
1157 // Record baseLibName for snapshots.
1158 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001159 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001160
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001161 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001162 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001163 case cc.IsHeaderDepTag(depTag):
1164 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1165 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1166 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1167 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001168 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001169 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001170 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001171 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001172 }
1173
1174 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001175 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1176 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001177 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001178 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001179 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001180
1181 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001182 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001183 // These are usually genrules which don't have per-target variants.
1184 directSrcDeps = append(directSrcDeps, srcDep)
1185 }
1186 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001187 })
1188
1189 var rlibDepFiles RustLibraries
1190 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001191 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001192 }
1193 var dylibDepFiles RustLibraries
1194 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001195 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001196 }
1197 var procMacroDepFiles RustLibraries
1198 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001199 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001200 }
1201
1202 var staticLibDepFiles android.Paths
1203 for _, dep := range directStaticLibDeps {
1204 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1205 }
1206
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001207 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001208 var sharedLibDepFiles android.Paths
1209 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001210 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1211 if dep.TableOfContents.Valid() {
1212 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001213 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001214 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001215 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001216 }
1217
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001218 var srcProviderDepFiles android.Paths
1219 for _, dep := range directSrcProvidersDeps {
1220 srcs, _ := dep.OutputFiles("")
1221 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1222 }
1223 for _, dep := range directSrcDeps {
1224 srcs := dep.Srcs()
1225 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1226 }
1227
Ivan Lozanoffee3342019-08-27 12:03:00 -07001228 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1229 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1230 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001231 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001232 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1233 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001234 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001235
1236 // Dedup exported flags from dependencies
1237 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001238 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001239 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001240 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1241 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1242 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001243
1244 return depPaths
1245}
1246
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001247func (mod *Module) InstallInData() bool {
1248 if mod.compiler == nil {
1249 return false
1250 }
1251 return mod.compiler.inData()
1252}
1253
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001254func (mod *Module) InstallInRamdisk() bool {
1255 return mod.InRamdisk()
1256}
1257
1258func (mod *Module) InstallInVendorRamdisk() bool {
1259 return mod.InVendorRamdisk()
1260}
1261
1262func (mod *Module) InstallInRecovery() bool {
1263 return mod.InRecovery()
1264}
1265
Ivan Lozanoffee3342019-08-27 12:03:00 -07001266func linkPathFromFilePath(filepath android.Path) string {
1267 return strings.Split(filepath.String(), filepath.Base())[0]
1268}
Ivan Lozanod648c432020-02-06 12:05:10 -05001269
Ivan Lozanoffee3342019-08-27 12:03:00 -07001270func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1271 ctx := &depsContext{
1272 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001273 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001274
1275 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001276 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001277 var snapshotInfo *cc.SnapshotInfo
1278
1279 if ctx.Os() == android.Android {
1280 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1281 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001282
Ivan Lozano2b081132020-09-08 12:46:52 -04001283 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001284 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001285 stdLinkage = "rlib-std"
1286 }
1287
1288 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001289
Ivan Lozano2b081132020-09-08 12:46:52 -04001290 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1291 rlibDepVariations = append(rlibDepVariations,
1292 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1293 }
1294
Ivan Lozano1921e802021-05-20 13:39:16 -04001295 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001296 for _, lib := range deps.Rlibs {
1297 depTag := rlibDepTag
1298 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1299
1300 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1301 {Mutator: "rust_libraries", Variation: rlibVariation},
1302 }...), depTag, lib)
1303 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001304
1305 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001306 actx.AddVariationDependencies(
1307 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001308 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001309 dylibDepTag, deps.Dylibs...)
1310
Ivan Lozano1921e802021-05-20 13:39:16 -04001311 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001312 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1313 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001314 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001315 for _, lib := range deps.Rustlibs {
1316 depTag := autoDep.depTag
1317 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1318 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1319 {Mutator: "rust_libraries", Variation: autoDep.variation},
1320 }...), depTag, lib)
1321 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001322 } else {
1323 actx.AddVariationDependencies(
1324 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1325 autoDep.depTag, deps.Rustlibs...)
1326 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001327 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001328
1329 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001330 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001331 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001332 for _, lib := range deps.Stdlibs {
1333 depTag := rlibDepTag
1334 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1335
1336 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1337 depTag, lib)
1338 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001339 } else {
1340 actx.AddVariationDependencies(
1341 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1342 dylibDepTag, deps.Stdlibs...)
1343 }
1344 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001345
1346 for _, lib := range deps.SharedLibs {
1347 depTag := cc.SharedDepTag()
1348 name, version := cc.StubsLibNameAndVersion(lib)
1349
1350 variations := []blueprint.Variation{
1351 {Mutator: "link", Variation: "shared"},
1352 }
1353 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1354 }
1355
1356 for _, lib := range deps.WholeStaticLibs {
1357 depTag := cc.StaticDepTag(true)
1358 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1359
1360 actx.AddVariationDependencies([]blueprint.Variation{
1361 {Mutator: "link", Variation: "static"},
1362 }, depTag, lib)
1363 }
1364
1365 for _, lib := range deps.StaticLibs {
1366 depTag := cc.StaticDepTag(false)
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 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001373
Zach Johnson3df4e632020-11-06 11:56:27 -08001374 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1375
Colin Cross565cafd2020-09-25 18:47:38 -07001376 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001377 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001378 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1379 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001380 }
1381 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001382 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1383 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001384 }
1385
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001386 if mod.sourceProvider != nil {
1387 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1388 bindgen.Properties.Custom_bindgen != "" {
1389 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1390 bindgen.Properties.Custom_bindgen)
1391 }
1392 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001393
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001394 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001395 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001396}
1397
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001398func BeginMutator(ctx android.BottomUpMutatorContext) {
1399 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1400 mod.beginMutator(ctx)
1401 }
1402}
1403
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001404func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1405 ctx := &baseModuleContext{
1406 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001407 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001408
1409 mod.begin(ctx)
1410}
1411
Ivan Lozanoffee3342019-08-27 12:03:00 -07001412func (mod *Module) Name() string {
1413 name := mod.ModuleBase.Name()
1414 if p, ok := mod.compiler.(interface {
1415 Name(string) string
1416 }); ok {
1417 name = p.Name(name)
1418 }
1419 return name
1420}
1421
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001422func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001423 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001424 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001425 }
1426}
1427
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001428var _ android.HostToolProvider = (*Module)(nil)
1429
1430func (mod *Module) HostToolPath() android.OptionalPath {
1431 if !mod.Host() {
1432 return android.OptionalPath{}
1433 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001434 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1435 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001436 }
1437 return android.OptionalPath{}
1438}
1439
Jiyong Park99644e92020-11-17 22:21:02 +09001440var _ android.ApexModule = (*Module)(nil)
1441
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001442func (mod *Module) minSdkVersion() string {
1443 return String(mod.Properties.Min_sdk_version)
1444}
1445
Jiyong Park45bf82e2020-12-15 22:29:02 +09001446var _ android.ApexModule = (*Module)(nil)
1447
1448// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001449func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001450 minSdkVersion := mod.minSdkVersion()
1451 if minSdkVersion == "apex_inherit" {
1452 return nil
1453 }
1454 if minSdkVersion == "" {
1455 return fmt.Errorf("min_sdk_version is not specificed")
1456 }
1457
1458 // Not using nativeApiLevelFromUser because the context here is not
1459 // necessarily a native context.
1460 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1461 if err != nil {
1462 return err
1463 }
1464
1465 if ver.GreaterThan(sdkVersion) {
1466 return fmt.Errorf("newer SDK(%v)", ver)
1467 }
Jiyong Park99644e92020-11-17 22:21:02 +09001468 return nil
1469}
1470
Jiyong Park45bf82e2020-12-15 22:29:02 +09001471// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001472func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1473 depTag := ctx.OtherModuleDependencyTag(dep)
1474
1475 if ccm, ok := dep.(*cc.Module); ok {
1476 if ccm.HasStubsVariants() {
1477 if cc.IsSharedDepTag(depTag) {
1478 // dynamic dep to a stubs lib crosses APEX boundary
1479 return false
1480 }
1481 if cc.IsRuntimeDepTag(depTag) {
1482 // runtime dep to a stubs lib also crosses APEX boundary
1483 return false
1484 }
1485
1486 if cc.IsHeaderDepTag(depTag) {
1487 return false
1488 }
1489 }
1490 if mod.Static() && cc.IsSharedDepTag(depTag) {
1491 // shared_lib dependency from a static lib is considered as crossing
1492 // the APEX boundary because the dependency doesn't actually is
1493 // linked; the dependency is used only during the compilation phase.
1494 return false
1495 }
1496 }
1497
1498 if depTag == procMacroDepTag {
1499 return false
1500 }
1501
1502 return true
1503}
1504
1505// Overrides ApexModule.IsInstallabeToApex()
1506func (mod *Module) IsInstallableToApex() bool {
1507 if mod.compiler != nil {
1508 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1509 return true
1510 }
1511 if _, ok := mod.compiler.(*binaryDecorator); ok {
1512 return true
1513 }
1514 }
1515 return false
1516}
1517
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001518// If a library file has a "lib" prefix, extract the library name without the prefix.
1519func libNameFromFilePath(filepath android.Path) (string, bool) {
1520 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1521 if strings.HasPrefix(libName, "lib") {
1522 libName = libName[3:]
1523 return libName, true
1524 }
1525 return "", false
1526}
1527
Ivan Lozanoffee3342019-08-27 12:03:00 -07001528var Bool = proptools.Bool
1529var BoolDefault = proptools.BoolDefault
1530var String = proptools.String
1531var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001532
1533var _ android.OutputFileProducer = (*Module)(nil)