blob: a97fc91cdb2b4db41c5755c820d213878370de47 [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
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400393 // Used for data dependencies adjacent to tests
394 DataLibs []string
395 DataBins []string
396
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397 CrtBegin, CrtEnd string
398}
399
400type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500401 DyLibs RustLibraries
402 RLibs RustLibraries
403 SharedLibs android.Paths
404 SharedLibDeps android.Paths
405 StaticLibs android.Paths
406 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500407
408 // depFlags and depLinkFlags are rustc and linker (clang) flags.
409 depFlags []string
410 depLinkFlags []string
411
412 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
413 // Both of these are exported and propagate to dependencies.
414 linkDirs []string
415 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700416
Ivan Lozano45901ed2020-07-24 16:05:01 -0400417 // Used by bindgen modules which call clang
418 depClangFlags []string
419 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400420 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400421 depSystemIncludePaths android.Paths
422
Ivan Lozanof1c84332019-09-20 11:00:37 -0700423 CrtBegin android.OptionalPath
424 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700425
426 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500427 SrcDeps android.Paths
428 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429}
430
431type RustLibraries []RustLibrary
432
433type RustLibrary struct {
434 Path android.Path
435 CrateName string
436}
437
438type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100439 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400441 cfgFlags(ctx ModuleContext, flags Flags) Flags
442 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443 compilerProps() []interface{}
444 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
445 compilerDeps(ctx DepsContext, deps Deps) Deps
446 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700447 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700448
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100449 // Output directory in which source-generated code from dependencies is
450 // copied. This is equivalent to Cargo's OUT_DIR variable.
451 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700452
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400453 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
454 CargoPkgVersion() string
455
456 // CargoEnvCompat returns whether Cargo environment variables should be used.
457 CargoEnvCompat() bool
458
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800459 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200460 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700461 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400462 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400463
464 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400465
466 Disabled() bool
467 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400468
Ivan Lozanodd055472020-09-28 13:22:45 -0400469 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900470
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400471 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900472 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400473}
474
Matthew Maurerbb3add12020-06-25 09:34:12 -0700475type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700476 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400477 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700478}
479
480type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400481 linkDirs []string
482 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700483}
484
Matthew Maurerbb3add12020-06-25 09:34:12 -0700485func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
486 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
487}
488
Ivan Lozano2093af22020-08-25 12:48:19 -0400489func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
490 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
491}
492
Colin Cross0de8a1e2020-09-18 14:15:30 -0700493func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
494 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700495 LinkDirs: flagExporter.linkDirs,
496 LinkObjects: flagExporter.linkObjects,
497 })
498}
499
Matthew Maurerbb3add12020-06-25 09:34:12 -0700500var _ exportedFlagsProducer = (*flagExporter)(nil)
501
502func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700503 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700504}
505
Colin Cross0de8a1e2020-09-18 14:15:30 -0700506type FlagExporterInfo struct {
507 Flags []string
508 LinkDirs []string // TODO: this should be android.Paths
509 LinkObjects []string // TODO: this should be android.Paths
510}
511
512var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
513
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400514func (mod *Module) isCoverageVariant() bool {
515 return mod.coverage.Properties.IsCoverageVariant
516}
517
518var _ cc.Coverage = (*Module)(nil)
519
520func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
521 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
522}
523
Ivan Lozanod7586b62021-04-01 09:49:36 -0400524func (mod *Module) VndkVersion() string {
525 return mod.Properties.VndkVersion
526}
527
528func (mod *Module) PreventInstall() bool {
529 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400530}
531
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400532func (mod *Module) MarkAsCoverageVariant(coverage bool) {
533 mod.coverage.Properties.IsCoverageVariant = coverage
534}
535
536func (mod *Module) EnableCoverageIfNeeded() {
537 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700538}
539
540func defaultsFactory() android.Module {
541 return DefaultsFactory()
542}
543
544type Defaults struct {
545 android.ModuleBase
546 android.DefaultsModuleBase
547}
548
549func DefaultsFactory(props ...interface{}) android.Module {
550 module := &Defaults{}
551
552 module.AddProperties(props...)
553 module.AddProperties(
554 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500555 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100556 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400557 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558 &BaseCompilerProperties{},
559 &BinaryCompilerProperties{},
560 &LibraryCompilerProperties{},
561 &ProcMacroCompilerProperties{},
562 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400563 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700564 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400565 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400566 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200567 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500568 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 )
570
571 android.InitDefaultsModule(module)
572 return module
573}
574
575func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700576 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700577}
578
Ivan Lozano183a3212019-10-18 14:18:45 -0700579func (mod *Module) CcLibrary() bool {
580 if mod.compiler != nil {
581 if _, ok := mod.compiler.(*libraryDecorator); ok {
582 return true
583 }
584 }
585 return false
586}
587
588func (mod *Module) CcLibraryInterface() bool {
589 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400590 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
591 // VariantIs{Static,Shared} is set.
592 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700593 return true
594 }
595 }
596 return false
597}
598
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400599func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400600 if mod.compiler != nil {
601 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400602 }
603 return nil
604}
605
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800606func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700607 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700608 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800609 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700610 }
611 }
612 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
613}
614
615func (mod *Module) SetStatic() {
616 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700617 if library, ok := mod.compiler.(libraryInterface); ok {
618 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700619 return
620 }
621 }
622 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
623}
624
625func (mod *Module) SetShared() {
626 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700627 if library, ok := mod.compiler.(libraryInterface); ok {
628 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700629 return
630 }
631 }
632 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
633}
634
Ivan Lozano183a3212019-10-18 14:18:45 -0700635func (mod *Module) BuildStaticVariant() bool {
636 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700637 if library, ok := mod.compiler.(libraryInterface); ok {
638 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700639 }
640 }
641 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
642}
643
644func (mod *Module) BuildSharedVariant() bool {
645 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700646 if library, ok := mod.compiler.(libraryInterface); ok {
647 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700648 }
649 }
650 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
651}
652
Ivan Lozano183a3212019-10-18 14:18:45 -0700653func (mod *Module) Module() android.Module {
654 return mod
655}
656
Ivan Lozano183a3212019-10-18 14:18:45 -0700657func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400658 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700659}
660
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400661func (mod *Module) CoverageFiles() android.Paths {
662 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800663 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400664 }
665 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
666}
667
Jiyong Park459feca2020-12-15 11:02:21 +0900668func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900669 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900670 return false
671 }
672
Jiyong Park459feca2020-12-15 11:02:21 +0900673 // The apex variant is not installable because it is included in the APEX and won't appear
674 // in the system partition as a standalone file.
675 if !apexInfo.IsForPlatform() {
676 return false
677 }
678
Jiyong Parke54f07e2021-04-07 15:08:04 +0900679 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900680}
681
Ivan Lozano183a3212019-10-18 14:18:45 -0700682var _ cc.LinkableInterface = (*Module)(nil)
683
Ivan Lozanoffee3342019-08-27 12:03:00 -0700684func (mod *Module) Init() android.Module {
685 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500686 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700687
688 if mod.compiler != nil {
689 mod.AddProperties(mod.compiler.compilerProps()...)
690 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400691 if mod.coverage != nil {
692 mod.AddProperties(mod.coverage.props()...)
693 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200694 if mod.clippy != nil {
695 mod.AddProperties(mod.clippy.props()...)
696 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400697 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700698 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400699 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500700 if mod.sanitize != nil {
701 mod.AddProperties(mod.sanitize.props()...)
702 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400703
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900705 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706
707 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708 return mod
709}
710
711func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
712 return &Module{
713 hod: hod,
714 multilib: multilib,
715 }
716}
717func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
718 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400719 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200720 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500721 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700722 return module
723}
724
725type ModuleContext interface {
726 android.ModuleContext
727 ModuleContextIntf
728}
729
730type BaseModuleContext interface {
731 android.BaseModuleContext
732 ModuleContextIntf
733}
734
735type DepsContext interface {
736 android.BottomUpMutatorContext
737 ModuleContextIntf
738}
739
740type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200741 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700743}
744
745type depsContext struct {
746 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700747}
748
749type moduleContext struct {
750 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751}
752
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200753type baseModuleContext struct {
754 android.BaseModuleContext
755}
756
757func (ctx *moduleContext) RustModule() *Module {
758 return ctx.Module().(*Module)
759}
760
761func (ctx *moduleContext) toolchain() config.Toolchain {
762 return ctx.RustModule().toolchain(ctx)
763}
764
765func (ctx *depsContext) RustModule() *Module {
766 return ctx.Module().(*Module)
767}
768
769func (ctx *depsContext) toolchain() config.Toolchain {
770 return ctx.RustModule().toolchain(ctx)
771}
772
773func (ctx *baseModuleContext) RustModule() *Module {
774 return ctx.Module().(*Module)
775}
776
777func (ctx *baseModuleContext) toolchain() config.Toolchain {
778 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400779}
780
781func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700782 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
783 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
784 return false
785 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400786 return mod.compiler != nil && mod.compiler.nativeCoverage()
787}
788
Ivan Lozanod7586b62021-04-01 09:49:36 -0400789func (mod *Module) EverInstallable() bool {
790 return mod.compiler != nil &&
791 // Check to see whether the module is actually ever installable.
792 mod.compiler.everInstallable()
793}
794
795func (mod *Module) Installable() *bool {
796 return mod.Properties.Installable
797}
798
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
800 if mod.cachedToolchain == nil {
801 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
802 }
803 return mod.cachedToolchain
804}
805
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200806func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
807 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
808}
809
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
811}
812
813func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
814 ctx := &moduleContext{
815 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817
Jiyong Park99644e92020-11-17 22:21:02 +0900818 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
819 if !apexInfo.IsForPlatform() {
820 mod.hideApexVariantFromMake = true
821 }
822
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500824 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
825
826 // Differentiate static libraries that are vendor available
827 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700828 if mod.InProduct() && !mod.OnlyInProduct() {
829 mod.Properties.SubName += cc.ProductSuffix
830 } else {
831 mod.Properties.SubName += cc.VendorSuffix
832 }
Matthew Maurerc6868382021-07-13 14:12:37 -0700833 } else if mod.InRamdisk() && !mod.OnlyInRamdisk() {
834 mod.Properties.SubName += cc.RamdiskSuffix
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500835 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
836 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800837 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
838 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500839 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840
Matthew Maurera61e31f2021-05-27 11:09:11 -0700841 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
842 mod.Properties.SubName += cc.NativeBridgeSuffix
843 }
844
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 if !toolchain.Supported() {
846 // This toolchain's unsupported, there's nothing to do for this mod.
847 return
848 }
849
850 deps := mod.depsToPaths(ctx)
851 flags := Flags{
852 Toolchain: toolchain,
853 }
854
Ivan Lozano67eada32021-09-23 11:50:33 -0400855 // Calculate rustc flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700856 if mod.compiler != nil {
857 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400858 flags = mod.compiler.cfgFlags(ctx, flags)
859 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400860 }
861 if mod.coverage != nil {
862 flags, deps = mod.coverage.flags(ctx, flags, deps)
863 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200864 if mod.clippy != nil {
865 flags, deps = mod.clippy.flags(ctx, flags, deps)
866 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500867 if mod.sanitize != nil {
868 flags, deps = mod.sanitize.flags(ctx, flags, deps)
869 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400870
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200871 // SourceProvider needs to call GenerateSource() before compiler calls
872 // compile() so it can provide the source. A SourceProvider has
873 // multiple variants (e.g. source, rlib, dylib). Only the "source"
874 // variant is responsible for effectively generating the source. The
875 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400876 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200877 if mod.compiler.(libraryInterface).source() {
878 mod.sourceProvider.GenerateSource(ctx, deps)
879 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
880 } else {
881 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
882 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700883 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200884 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400885 }
886
887 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100888 mod.compiler.initialize(ctx)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400889 outputFile := mod.compiler.compile(ctx, flags, deps)
890 if ctx.Failed() {
891 return
892 }
893 mod.outputFile = android.OptionalPathForPath(outputFile)
894 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900895
Dan Albert06feee92021-03-19 15:06:02 -0700896 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
897
Ivan Lozano1921e802021-05-20 13:39:16 -0400898 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
899 // RECOVERY_SNAPSHOT_VERSION is current.
900 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
901 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
902 lib.collectHeadersForSnapshot(ctx, deps)
903 }
904 }
905
Jiyong Park459feca2020-12-15 11:02:21 +0900906 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900907 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
908 // If the module has been specifically configure to not be installed then
909 // hide from make as otherwise it will break when running inside make as the
910 // output path to install will not be specified. Not all uninstallable
911 // modules can be hidden from make as some are needed for resolving make
912 // side dependencies.
913 mod.HideFromMake()
914 } else if !mod.installable(apexInfo) {
915 mod.SkipInstall()
916 }
917
918 // Still call install though, the installs will be stored as PackageSpecs to allow
919 // using the outputs in a genrule.
920 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200921 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900922 if ctx.Failed() {
923 return
924 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400925 }
Chris Wailes74be7642021-07-22 16:20:28 -0700926
927 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700928 }
929}
930
931func (mod *Module) deps(ctx DepsContext) Deps {
932 deps := Deps{}
933
934 if mod.compiler != nil {
935 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400936 }
937 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700938 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939 }
940
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400941 if mod.coverage != nil {
942 deps = mod.coverage.deps(ctx, deps)
943 }
944
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500945 if mod.sanitize != nil {
946 deps = mod.sanitize.deps(ctx, deps)
947 }
948
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
950 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700951 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700952 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
953 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
954 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400955 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956 return deps
957
958}
959
Ivan Lozanoffee3342019-08-27 12:03:00 -0700960type dependencyTag struct {
961 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800962 name string
963 library bool
964 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700965}
966
Jiyong Park65b62242020-11-25 12:44:59 +0900967// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
968// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
969func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800970 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900971}
972
973var _ android.InstallNeededDependencyTag = dependencyTag{}
974
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400976 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
977 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
978 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800979 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400980 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200981 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400982 dataLibDepTag = dependencyTag{name: "data lib"}
983 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700984)
985
Jiyong Park99644e92020-11-17 22:21:02 +0900986func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
987 tag, ok := depTag.(dependencyTag)
988 return ok && tag == dylibDepTag
989}
990
Jiyong Park94e22fd2021-04-08 18:19:15 +0900991func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
992 tag, ok := depTag.(dependencyTag)
993 return ok && tag == rlibDepTag
994}
995
Matthew Maurer0f003b12020-06-29 14:34:06 -0700996type autoDep struct {
997 variation string
998 depTag dependencyTag
999}
1000
1001var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001002 rlibVariation = "rlib"
1003 dylibVariation = "dylib"
1004 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1005 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001006)
1007
1008type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001009 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001010}
1011
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001012func (mod *Module) begin(ctx BaseModuleContext) {
1013 if mod.coverage != nil {
1014 mod.coverage.begin(ctx)
1015 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001016 if mod.sanitize != nil {
1017 mod.sanitize.begin(ctx)
1018 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001019}
1020
Ivan Lozanoffee3342019-08-27 12:03:00 -07001021func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1022 var depPaths PathDeps
1023
1024 directRlibDeps := []*Module{}
1025 directDylibDeps := []*Module{}
1026 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001027 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001028 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001029 directSrcProvidersDeps := []*Module{}
1030 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031
1032 ctx.VisitDirectDeps(func(dep android.Module) {
1033 depName := ctx.OtherModuleName(dep)
1034 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001035
Ivan Lozano89435d12020-07-31 11:01:18 -04001036 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001037 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001038 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001039
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 switch depTag {
1041 case dylibDepTag:
1042 dylib, ok := rustDep.compiler.(libraryInterface)
1043 if !ok || !dylib.dylib() {
1044 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1045 return
1046 }
1047 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001048 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001049 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001050
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051 rlib, ok := rustDep.compiler.(libraryInterface)
1052 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001053 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054 return
1055 }
1056 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001057 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001058 case procMacroDepTag:
1059 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001060 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001061 }
1062
1063 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001064 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1065 // OS/Arch variant is used.
1066 var helper string
1067 if ctx.Host() {
1068 helper = "missing 'host_supported'?"
1069 } else {
1070 helper = "device module defined?"
1071 }
1072
1073 if dep.Target().Os != ctx.Os() {
1074 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1075 return
1076 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1077 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1078 return
1079 }
1080 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001081 }
1082
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001083 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001084 if depTag != procMacroDepTag {
1085 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1086 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1087 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1088 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089 }
1090
Ivan Lozanoffee3342019-08-27 12:03:00 -07001091 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001092 linkFile := rustDep.UnstrippedOutputFile()
1093 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001094 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1095 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001096 }
1097 }
1098
Ivan Lozano89435d12020-07-31 11:01:18 -04001099 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001100 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001101 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001102 if _, ok := ccDep.(*Module); !ok {
1103 if ccDep.Module().Target().Os != ctx.Os() {
1104 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1105 return
1106 }
1107 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1108 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1109 return
1110 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001111 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001112 linkObject := ccDep.OutputFile()
1113 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001114
Ivan Lozano2093af22020-08-25 12:48:19 -04001115 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001116 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1117 }
1118
1119 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001120 switch {
1121 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001122 if cc.IsWholeStaticLib(depTag) {
1123 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1124 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001125 if mod.Binary() {
1126 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1127 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1128 // final linkage, pass the args directly to the linker to handle these cases.
1129 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1130 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001131 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001132 } else {
1133 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 -05001134 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001135 }
1136
1137 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1138 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001139 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001140 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1141
Colin Cross0de8a1e2020-09-18 14:15:30 -07001142 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1143 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1144 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1145 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1146 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001147 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001148 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001149 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001150 // For the shared lib dependencies, we may link to the stub variant
1151 // of the dependency depending on the context (e.g. if this
1152 // dependency crosses the APEX boundaries).
1153 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1154
1155 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1156 // object (either from stub or non-stub) to use.
1157 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1158 linkPath = linkPathFromFilePath(linkObject.Path())
1159
Ivan Lozanoffee3342019-08-27 12:03:00 -07001160 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001161 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001162 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1163 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1164 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1165 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001166 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001167
1168 // Record baseLibName for snapshots.
1169 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001170 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001171
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001172 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001173 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001174 case cc.IsHeaderDepTag(depTag):
1175 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1176 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1177 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1178 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001179 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001180 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001181 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001182 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001183 }
1184
1185 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001186 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1187 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001188 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001189 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001190 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001191
1192 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001193 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001194 // These are usually genrules which don't have per-target variants.
1195 directSrcDeps = append(directSrcDeps, srcDep)
1196 }
1197 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001198 })
1199
1200 var rlibDepFiles RustLibraries
1201 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001202 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001203 }
1204 var dylibDepFiles RustLibraries
1205 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001206 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207 }
1208 var procMacroDepFiles RustLibraries
1209 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001210 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001211 }
1212
1213 var staticLibDepFiles android.Paths
1214 for _, dep := range directStaticLibDeps {
1215 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1216 }
1217
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001218 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001219 var sharedLibDepFiles android.Paths
1220 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001221 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1222 if dep.TableOfContents.Valid() {
1223 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001224 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001225 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001226 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001227 }
1228
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001229 var srcProviderDepFiles android.Paths
1230 for _, dep := range directSrcProvidersDeps {
1231 srcs, _ := dep.OutputFiles("")
1232 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1233 }
1234 for _, dep := range directSrcDeps {
1235 srcs := dep.Srcs()
1236 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1237 }
1238
Ivan Lozanoffee3342019-08-27 12:03:00 -07001239 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1240 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1241 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001242 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001243 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1244 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001245 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001246
1247 // Dedup exported flags from dependencies
1248 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001249 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001250 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001251 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1252 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1253 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001254
1255 return depPaths
1256}
1257
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001258func (mod *Module) InstallInData() bool {
1259 if mod.compiler == nil {
1260 return false
1261 }
1262 return mod.compiler.inData()
1263}
1264
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001265func (mod *Module) InstallInRamdisk() bool {
1266 return mod.InRamdisk()
1267}
1268
1269func (mod *Module) InstallInVendorRamdisk() bool {
1270 return mod.InVendorRamdisk()
1271}
1272
1273func (mod *Module) InstallInRecovery() bool {
1274 return mod.InRecovery()
1275}
1276
Colin Cross77435572021-11-08 12:37:01 -08001277func (mod *Module) InstallBypassMake() bool {
1278 return true
1279}
1280
Ivan Lozanoffee3342019-08-27 12:03:00 -07001281func linkPathFromFilePath(filepath android.Path) string {
1282 return strings.Split(filepath.String(), filepath.Base())[0]
1283}
Ivan Lozanod648c432020-02-06 12:05:10 -05001284
Ivan Lozanoffee3342019-08-27 12:03:00 -07001285func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1286 ctx := &depsContext{
1287 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001288 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001289
1290 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001291 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001292 var snapshotInfo *cc.SnapshotInfo
1293
1294 if ctx.Os() == android.Android {
1295 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1296 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001297
Ivan Lozano2b081132020-09-08 12:46:52 -04001298 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001299 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001300 stdLinkage = "rlib-std"
1301 }
1302
1303 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001304
Ivan Lozano2b081132020-09-08 12:46:52 -04001305 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1306 rlibDepVariations = append(rlibDepVariations,
1307 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1308 }
1309
Ivan Lozano1921e802021-05-20 13:39:16 -04001310 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001311 for _, lib := range deps.Rlibs {
1312 depTag := rlibDepTag
1313 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1314
1315 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1316 {Mutator: "rust_libraries", Variation: rlibVariation},
1317 }...), depTag, lib)
1318 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001319
1320 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001321 actx.AddVariationDependencies(
1322 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001323 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001324 dylibDepTag, deps.Dylibs...)
1325
Ivan Lozano1921e802021-05-20 13:39:16 -04001326 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001327 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1328 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001329 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001330 for _, lib := range deps.Rustlibs {
1331 depTag := autoDep.depTag
1332 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1333 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1334 {Mutator: "rust_libraries", Variation: autoDep.variation},
1335 }...), depTag, lib)
1336 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001337 } else {
1338 actx.AddVariationDependencies(
1339 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1340 autoDep.depTag, deps.Rustlibs...)
1341 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001342 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001343
1344 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001345 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001346 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001347 for _, lib := range deps.Stdlibs {
1348 depTag := rlibDepTag
1349 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1350
1351 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1352 depTag, lib)
1353 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001354 } else {
1355 actx.AddVariationDependencies(
1356 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1357 dylibDepTag, deps.Stdlibs...)
1358 }
1359 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001360
1361 for _, lib := range deps.SharedLibs {
1362 depTag := cc.SharedDepTag()
1363 name, version := cc.StubsLibNameAndVersion(lib)
1364
1365 variations := []blueprint.Variation{
1366 {Mutator: "link", Variation: "shared"},
1367 }
1368 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1369 }
1370
1371 for _, lib := range deps.WholeStaticLibs {
1372 depTag := cc.StaticDepTag(true)
1373 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1374
1375 actx.AddVariationDependencies([]blueprint.Variation{
1376 {Mutator: "link", Variation: "static"},
1377 }, depTag, lib)
1378 }
1379
1380 for _, lib := range deps.StaticLibs {
1381 depTag := cc.StaticDepTag(false)
1382 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1383
1384 actx.AddVariationDependencies([]blueprint.Variation{
1385 {Mutator: "link", Variation: "static"},
1386 }, depTag, lib)
1387 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001388
Zach Johnson3df4e632020-11-06 11:56:27 -08001389 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1390
Colin Cross565cafd2020-09-25 18:47:38 -07001391 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001392 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001393 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1394 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001395 }
1396 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001397 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1398 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001399 }
1400
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001401 if mod.sourceProvider != nil {
1402 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1403 bindgen.Properties.Custom_bindgen != "" {
1404 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1405 bindgen.Properties.Custom_bindgen)
1406 }
1407 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001408
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001409 actx.AddVariationDependencies([]blueprint.Variation{
1410 {Mutator: "link", Variation: "shared"},
1411 }, dataLibDepTag, deps.DataLibs...)
1412
1413 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1414
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001415 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001416 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001417}
1418
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001419func BeginMutator(ctx android.BottomUpMutatorContext) {
1420 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1421 mod.beginMutator(ctx)
1422 }
1423}
1424
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001425func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1426 ctx := &baseModuleContext{
1427 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001428 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001429
1430 mod.begin(ctx)
1431}
1432
Ivan Lozanoffee3342019-08-27 12:03:00 -07001433func (mod *Module) Name() string {
1434 name := mod.ModuleBase.Name()
1435 if p, ok := mod.compiler.(interface {
1436 Name(string) string
1437 }); ok {
1438 name = p.Name(name)
1439 }
1440 return name
1441}
1442
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001443func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001444 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001445 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001446 }
1447}
1448
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001449var _ android.HostToolProvider = (*Module)(nil)
1450
1451func (mod *Module) HostToolPath() android.OptionalPath {
1452 if !mod.Host() {
1453 return android.OptionalPath{}
1454 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001455 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1456 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001457 }
1458 return android.OptionalPath{}
1459}
1460
Jiyong Park99644e92020-11-17 22:21:02 +09001461var _ android.ApexModule = (*Module)(nil)
1462
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001463func (mod *Module) minSdkVersion() string {
1464 return String(mod.Properties.Min_sdk_version)
1465}
1466
Jiyong Park45bf82e2020-12-15 22:29:02 +09001467var _ android.ApexModule = (*Module)(nil)
1468
1469// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001470func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001471 minSdkVersion := mod.minSdkVersion()
1472 if minSdkVersion == "apex_inherit" {
1473 return nil
1474 }
1475 if minSdkVersion == "" {
1476 return fmt.Errorf("min_sdk_version is not specificed")
1477 }
1478
1479 // Not using nativeApiLevelFromUser because the context here is not
1480 // necessarily a native context.
1481 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1482 if err != nil {
1483 return err
1484 }
1485
1486 if ver.GreaterThan(sdkVersion) {
1487 return fmt.Errorf("newer SDK(%v)", ver)
1488 }
Jiyong Park99644e92020-11-17 22:21:02 +09001489 return nil
1490}
1491
Jiyong Park45bf82e2020-12-15 22:29:02 +09001492// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001493func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1494 depTag := ctx.OtherModuleDependencyTag(dep)
1495
1496 if ccm, ok := dep.(*cc.Module); ok {
1497 if ccm.HasStubsVariants() {
1498 if cc.IsSharedDepTag(depTag) {
1499 // dynamic dep to a stubs lib crosses APEX boundary
1500 return false
1501 }
1502 if cc.IsRuntimeDepTag(depTag) {
1503 // runtime dep to a stubs lib also crosses APEX boundary
1504 return false
1505 }
1506
1507 if cc.IsHeaderDepTag(depTag) {
1508 return false
1509 }
1510 }
1511 if mod.Static() && cc.IsSharedDepTag(depTag) {
1512 // shared_lib dependency from a static lib is considered as crossing
1513 // the APEX boundary because the dependency doesn't actually is
1514 // linked; the dependency is used only during the compilation phase.
1515 return false
1516 }
1517 }
1518
1519 if depTag == procMacroDepTag {
1520 return false
1521 }
1522
1523 return true
1524}
1525
1526// Overrides ApexModule.IsInstallabeToApex()
1527func (mod *Module) IsInstallableToApex() bool {
1528 if mod.compiler != nil {
1529 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1530 return true
1531 }
1532 if _, ok := mod.compiler.(*binaryDecorator); ok {
1533 return true
1534 }
1535 }
1536 return false
1537}
1538
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001539// If a library file has a "lib" prefix, extract the library name without the prefix.
1540func libNameFromFilePath(filepath android.Path) (string, bool) {
1541 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1542 if strings.HasPrefix(libName, "lib") {
1543 libName = libName[3:]
1544 return libName, true
1545 }
1546 return "", false
1547}
1548
Ivan Lozanoffee3342019-08-27 12:03:00 -07001549var Bool = proptools.Bool
1550var BoolDefault = proptools.BoolDefault
1551var String = proptools.String
1552var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001553
1554var _ android.OutputFileProducer = (*Module)(nil)