blob: 0cd299dc1effc8b6acb760cce9539542520e37c4 [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
Ivan Lozano43845682020-07-09 21:03:28 -0400133 PreventInstall bool
134 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400135 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136}
137
138type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700139 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140
Ivan Lozano6a884432020-12-02 09:15:16 -0500141 VendorProperties cc.VendorProperties
142
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143 Properties BaseProperties
144
145 hod android.HostOrDeviceSupported
146 multilib android.Multilib
147
Ivan Lozano6a884432020-12-02 09:15:16 -0500148 makeLinkType string
149
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400151 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200152 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500153 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400155 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700156 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400157
Jiyong Parke54f07e2021-04-07 15:08:04 +0900158 // Unstripped output. This is usually used when this module is linked to another module
159 // as a library. The stripped output which is used for installation can be found via
160 // compiler.strippedOutputFile if it exists.
161 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700162 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
Ivan Lozanod7586b62021-04-01 09:49:36 -0400180func (c *Module) HiddenFromMake() bool {
181 return c.Properties.HideFromMake
182}
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 {
263 if mod.compiler != nil {
264 if _, ok := mod.compiler.(*binaryDecorator); ok {
265 return true
266 }
267 }
268 return false
269}
270
Justin Yun5e035862021-06-29 20:50:37 +0900271func (mod *Module) StaticExecutable() bool {
272 if !mod.Binary() {
273 return false
274 }
275 return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
276}
277
Ivan Lozanod7586b62021-04-01 09:49:36 -0400278func (mod *Module) Object() bool {
279 // Rust has no modules which produce only object files.
280 return false
281}
282
Ivan Lozano52767be2019-10-18 14:49:46 -0700283func (mod *Module) Toc() android.OptionalPath {
284 if mod.compiler != nil {
285 if _, ok := mod.compiler.(libraryInterface); ok {
286 return android.OptionalPath{}
287 }
288 }
289 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
290}
291
Colin Crossc511bc52020-04-07 16:50:32 +0000292func (mod *Module) UseSdk() bool {
293 return false
294}
295
Ivan Lozanod7586b62021-04-01 09:49:36 -0400296func (mod *Module) RelativeInstallPath() string {
297 if mod.compiler != nil {
298 return mod.compiler.relativeInstallPath()
299 }
300 return ""
301}
302
Ivan Lozano52767be2019-10-18 14:49:46 -0700303func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500304 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700305}
306
Jiyong Park7d55b612021-06-11 17:22:09 +0900307func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400308 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900309}
310
Ivan Lozano52767be2019-10-18 14:49:46 -0700311func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400312 return true
313}
314
315func (mod *Module) SubName() string {
316 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700317}
318
319func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500320 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700321 return false
322}
323
Ivan Lozanof9e21722020-12-02 09:00:51 -0500324func (mod *Module) IsVndkExt() bool {
325 return false
326}
327
Ivan Lozanod7586b62021-04-01 09:49:36 -0400328func (mod *Module) IsVndkSp() bool {
329 return false
330}
331
Colin Cross127bb8b2020-12-16 16:46:01 -0800332func (c *Module) IsVndkPrivate() bool {
333 return false
334}
335
336func (c *Module) IsLlndk() bool {
337 return false
338}
339
340func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500341 return false
342}
343
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400344func (mod *Module) KernelHeadersDecorator() bool {
345 return false
346}
347
Colin Cross1f3f1302021-04-26 18:37:44 -0700348func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400349 return false
350}
351
Colin Cross5271fea2021-04-27 13:06:04 -0700352func (m *Module) NeedsVendorPublicLibraryVariants() bool {
353 return false
354}
355
Ivan Lozanod7586b62021-04-01 09:49:36 -0400356func (mod *Module) HasLlndkStubs() bool {
357 return false
358}
359
360func (mod *Module) StubsVersion() string {
361 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
362}
363
Ivan Lozano52767be2019-10-18 14:49:46 -0700364func (mod *Module) SdkVersion() string {
365 return ""
366}
367
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900368func (mod *Module) MinSdkVersion() string {
369 return ""
370}
371
Colin Crossc511bc52020-04-07 16:50:32 +0000372func (mod *Module) AlwaysSdk() bool {
373 return false
374}
375
Jiyong Park2286afd2020-06-16 21:58:53 +0900376func (mod *Module) IsSdkVariant() bool {
377 return false
378}
379
Colin Cross1348ce32020-10-01 13:37:16 -0700380func (mod *Module) SplitPerApiLevel() bool {
381 return false
382}
383
Ivan Lozanoffee3342019-08-27 12:03:00 -0700384type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400385 Dylibs []string
386 Rlibs []string
387 Rustlibs []string
388 Stdlibs []string
389 ProcMacros []string
390 SharedLibs []string
391 StaticLibs []string
392 WholeStaticLibs []string
393 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394
395 CrtBegin, CrtEnd string
396}
397
398type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500399 DyLibs RustLibraries
400 RLibs RustLibraries
401 SharedLibs android.Paths
402 SharedLibDeps android.Paths
403 StaticLibs android.Paths
404 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500405
406 // depFlags and depLinkFlags are rustc and linker (clang) flags.
407 depFlags []string
408 depLinkFlags []string
409
410 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
411 // Both of these are exported and propagate to dependencies.
412 linkDirs []string
413 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700414
Ivan Lozano45901ed2020-07-24 16:05:01 -0400415 // Used by bindgen modules which call clang
416 depClangFlags []string
417 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400418 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400419 depSystemIncludePaths android.Paths
420
Ivan Lozanof1c84332019-09-20 11:00:37 -0700421 CrtBegin android.OptionalPath
422 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700423
424 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500425 SrcDeps android.Paths
426 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427}
428
429type RustLibraries []RustLibrary
430
431type RustLibrary struct {
432 Path android.Path
433 CrateName string
434}
435
436type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100437 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438 compilerFlags(ctx ModuleContext, flags Flags) Flags
439 compilerProps() []interface{}
440 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
441 compilerDeps(ctx DepsContext, deps Deps) Deps
442 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700443 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100445 // Output directory in which source-generated code from dependencies is
446 // copied. This is equivalent to Cargo's OUT_DIR variable.
447 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700448
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400449 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
450 CargoPkgVersion() string
451
452 // CargoEnvCompat returns whether Cargo environment variables should be used.
453 CargoEnvCompat() bool
454
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800455 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200456 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700457 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400458 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400459
460 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400461
462 Disabled() bool
463 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400464
Ivan Lozanodd055472020-09-28 13:22:45 -0400465 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900466
467 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400468}
469
Matthew Maurerbb3add12020-06-25 09:34:12 -0700470type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700471 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400472 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700473}
474
475type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400476 linkDirs []string
477 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700478}
479
Matthew Maurerbb3add12020-06-25 09:34:12 -0700480func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
481 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
482}
483
Ivan Lozano2093af22020-08-25 12:48:19 -0400484func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
485 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
486}
487
Colin Cross0de8a1e2020-09-18 14:15:30 -0700488func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
489 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700490 LinkDirs: flagExporter.linkDirs,
491 LinkObjects: flagExporter.linkObjects,
492 })
493}
494
Matthew Maurerbb3add12020-06-25 09:34:12 -0700495var _ exportedFlagsProducer = (*flagExporter)(nil)
496
497func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700498 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499}
500
Colin Cross0de8a1e2020-09-18 14:15:30 -0700501type FlagExporterInfo struct {
502 Flags []string
503 LinkDirs []string // TODO: this should be android.Paths
504 LinkObjects []string // TODO: this should be android.Paths
505}
506
507var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
508
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400509func (mod *Module) isCoverageVariant() bool {
510 return mod.coverage.Properties.IsCoverageVariant
511}
512
513var _ cc.Coverage = (*Module)(nil)
514
515func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
516 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
517}
518
Ivan Lozanod7586b62021-04-01 09:49:36 -0400519func (mod *Module) VndkVersion() string {
520 return mod.Properties.VndkVersion
521}
522
523func (mod *Module) PreventInstall() bool {
524 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400525}
526
527func (mod *Module) HideFromMake() {
528 mod.Properties.HideFromMake = true
529}
530
531func (mod *Module) MarkAsCoverageVariant(coverage bool) {
532 mod.coverage.Properties.IsCoverageVariant = coverage
533}
534
535func (mod *Module) EnableCoverageIfNeeded() {
536 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700537}
538
539func defaultsFactory() android.Module {
540 return DefaultsFactory()
541}
542
543type Defaults struct {
544 android.ModuleBase
545 android.DefaultsModuleBase
546}
547
548func DefaultsFactory(props ...interface{}) android.Module {
549 module := &Defaults{}
550
551 module.AddProperties(props...)
552 module.AddProperties(
553 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500554 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100555 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400556 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557 &BaseCompilerProperties{},
558 &BinaryCompilerProperties{},
559 &LibraryCompilerProperties{},
560 &ProcMacroCompilerProperties{},
561 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400562 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700563 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400564 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400565 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200566 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500567 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568 )
569
570 android.InitDefaultsModule(module)
571 return module
572}
573
574func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700575 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576}
577
Ivan Lozano183a3212019-10-18 14:18:45 -0700578func (mod *Module) CcLibrary() bool {
579 if mod.compiler != nil {
580 if _, ok := mod.compiler.(*libraryDecorator); ok {
581 return true
582 }
583 }
584 return false
585}
586
587func (mod *Module) CcLibraryInterface() bool {
588 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400589 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
590 // VariantIs{Static,Shared} is set.
591 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700592 return true
593 }
594 }
595 return false
596}
597
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800598func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700599 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700600 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800601 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700602 }
603 }
604 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
605}
606
607func (mod *Module) SetStatic() {
608 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700609 if library, ok := mod.compiler.(libraryInterface); ok {
610 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700611 return
612 }
613 }
614 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
615}
616
617func (mod *Module) SetShared() {
618 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700619 if library, ok := mod.compiler.(libraryInterface); ok {
620 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700621 return
622 }
623 }
624 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
625}
626
Ivan Lozano183a3212019-10-18 14:18:45 -0700627func (mod *Module) BuildStaticVariant() bool {
628 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700629 if library, ok := mod.compiler.(libraryInterface); ok {
630 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700631 }
632 }
633 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
634}
635
636func (mod *Module) BuildSharedVariant() bool {
637 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700638 if library, ok := mod.compiler.(libraryInterface); ok {
639 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700640 }
641 }
642 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
643}
644
Ivan Lozano183a3212019-10-18 14:18:45 -0700645func (mod *Module) Module() android.Module {
646 return mod
647}
648
Ivan Lozano183a3212019-10-18 14:18:45 -0700649func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900650 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
651 return mod.compiler.strippedOutputFilePath()
652 }
653 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700654}
655
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400656func (mod *Module) CoverageFiles() android.Paths {
657 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800658 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400659 }
660 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
661}
662
Jiyong Park459feca2020-12-15 11:02:21 +0900663func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900664 if !mod.EverInstallable() {
665 return false
666 }
667
Jiyong Park459feca2020-12-15 11:02:21 +0900668 // The apex variant is not installable because it is included in the APEX and won't appear
669 // in the system partition as a standalone file.
670 if !apexInfo.IsForPlatform() {
671 return false
672 }
673
Jiyong Parke54f07e2021-04-07 15:08:04 +0900674 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900675}
676
Ivan Lozano183a3212019-10-18 14:18:45 -0700677var _ cc.LinkableInterface = (*Module)(nil)
678
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679func (mod *Module) Init() android.Module {
680 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500681 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682
683 if mod.compiler != nil {
684 mod.AddProperties(mod.compiler.compilerProps()...)
685 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400686 if mod.coverage != nil {
687 mod.AddProperties(mod.coverage.props()...)
688 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200689 if mod.clippy != nil {
690 mod.AddProperties(mod.clippy.props()...)
691 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400692 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700693 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400694 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500695 if mod.sanitize != nil {
696 mod.AddProperties(mod.sanitize.props()...)
697 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400698
Ivan Lozanoffee3342019-08-27 12:03:00 -0700699 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900700 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700701
702 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700703 return mod
704}
705
706func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
707 return &Module{
708 hod: hod,
709 multilib: multilib,
710 }
711}
712func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
713 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400714 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200715 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500716 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717 return module
718}
719
720type ModuleContext interface {
721 android.ModuleContext
722 ModuleContextIntf
723}
724
725type BaseModuleContext interface {
726 android.BaseModuleContext
727 ModuleContextIntf
728}
729
730type DepsContext interface {
731 android.BottomUpMutatorContext
732 ModuleContextIntf
733}
734
735type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200736 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738}
739
740type depsContext struct {
741 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742}
743
744type moduleContext struct {
745 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746}
747
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200748type baseModuleContext struct {
749 android.BaseModuleContext
750}
751
752func (ctx *moduleContext) RustModule() *Module {
753 return ctx.Module().(*Module)
754}
755
756func (ctx *moduleContext) toolchain() config.Toolchain {
757 return ctx.RustModule().toolchain(ctx)
758}
759
760func (ctx *depsContext) RustModule() *Module {
761 return ctx.Module().(*Module)
762}
763
764func (ctx *depsContext) toolchain() config.Toolchain {
765 return ctx.RustModule().toolchain(ctx)
766}
767
768func (ctx *baseModuleContext) RustModule() *Module {
769 return ctx.Module().(*Module)
770}
771
772func (ctx *baseModuleContext) toolchain() config.Toolchain {
773 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400774}
775
776func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700777 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
778 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
779 return false
780 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400781 return mod.compiler != nil && mod.compiler.nativeCoverage()
782}
783
Ivan Lozanod7586b62021-04-01 09:49:36 -0400784func (mod *Module) EverInstallable() bool {
785 return mod.compiler != nil &&
786 // Check to see whether the module is actually ever installable.
787 mod.compiler.everInstallable()
788}
789
790func (mod *Module) Installable() *bool {
791 return mod.Properties.Installable
792}
793
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
795 if mod.cachedToolchain == nil {
796 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
797 }
798 return mod.cachedToolchain
799}
800
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200801func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
802 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
803}
804
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
806}
807
808func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
809 ctx := &moduleContext{
810 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812
Jiyong Park99644e92020-11-17 22:21:02 +0900813 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
814 if !apexInfo.IsForPlatform() {
815 mod.hideApexVariantFromMake = true
816 }
817
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500819 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
820
821 // Differentiate static libraries that are vendor available
822 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700823 if mod.InProduct() && !mod.OnlyInProduct() {
824 mod.Properties.SubName += cc.ProductSuffix
825 } else {
826 mod.Properties.SubName += cc.VendorSuffix
827 }
Matthew Maurerc6868382021-07-13 14:12:37 -0700828 } else if mod.InRamdisk() && !mod.OnlyInRamdisk() {
829 mod.Properties.SubName += cc.RamdiskSuffix
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500830 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
831 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800832 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
833 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500834 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835
Matthew Maurera61e31f2021-05-27 11:09:11 -0700836 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
837 mod.Properties.SubName += cc.NativeBridgeSuffix
838 }
839
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 if !toolchain.Supported() {
841 // This toolchain's unsupported, there's nothing to do for this mod.
842 return
843 }
844
845 deps := mod.depsToPaths(ctx)
846 flags := Flags{
847 Toolchain: toolchain,
848 }
849
850 if mod.compiler != nil {
851 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400852 }
853 if mod.coverage != nil {
854 flags, deps = mod.coverage.flags(ctx, flags, deps)
855 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200856 if mod.clippy != nil {
857 flags, deps = mod.clippy.flags(ctx, flags, deps)
858 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500859 if mod.sanitize != nil {
860 flags, deps = mod.sanitize.flags(ctx, flags, deps)
861 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400862
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200863 // SourceProvider needs to call GenerateSource() before compiler calls
864 // compile() so it can provide the source. A SourceProvider has
865 // multiple variants (e.g. source, rlib, dylib). Only the "source"
866 // variant is responsible for effectively generating the source. The
867 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400868 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200869 if mod.compiler.(libraryInterface).source() {
870 mod.sourceProvider.GenerateSource(ctx, deps)
871 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
872 } else {
873 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
874 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700875 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200876 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400877 }
878
879 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100880 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900881 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900882 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200883 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900884
Dan Albert06feee92021-03-19 15:06:02 -0700885 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
886
Ivan Lozano1921e802021-05-20 13:39:16 -0400887 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
888 // RECOVERY_SNAPSHOT_VERSION is current.
889 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
890 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
891 lib.collectHeadersForSnapshot(ctx, deps)
892 }
893 }
894
Jiyong Park459feca2020-12-15 11:02:21 +0900895 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
896 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200897 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400898 }
Chris Wailes74be7642021-07-22 16:20:28 -0700899
900 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901 }
902}
903
904func (mod *Module) deps(ctx DepsContext) Deps {
905 deps := Deps{}
906
907 if mod.compiler != nil {
908 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400909 }
910 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700911 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 }
913
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400914 if mod.coverage != nil {
915 deps = mod.coverage.deps(ctx, deps)
916 }
917
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500918 if mod.sanitize != nil {
919 deps = mod.sanitize.deps(ctx, deps)
920 }
921
Ivan Lozanoffee3342019-08-27 12:03:00 -0700922 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
923 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700924 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
926 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
927 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400928 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700929 return deps
930
931}
932
Ivan Lozanoffee3342019-08-27 12:03:00 -0700933type dependencyTag struct {
934 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800935 name string
936 library bool
937 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938}
939
Jiyong Park65b62242020-11-25 12:44:59 +0900940// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
941// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
942func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800943 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900944}
945
946var _ android.InstallNeededDependencyTag = dependencyTag{}
947
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400949 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
950 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
951 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800952 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400953 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200954 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700955)
956
Jiyong Park99644e92020-11-17 22:21:02 +0900957func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
958 tag, ok := depTag.(dependencyTag)
959 return ok && tag == dylibDepTag
960}
961
Jiyong Park94e22fd2021-04-08 18:19:15 +0900962func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
963 tag, ok := depTag.(dependencyTag)
964 return ok && tag == rlibDepTag
965}
966
Matthew Maurer0f003b12020-06-29 14:34:06 -0700967type autoDep struct {
968 variation string
969 depTag dependencyTag
970}
971
972var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200973 rlibVariation = "rlib"
974 dylibVariation = "dylib"
975 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
976 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700977)
978
979type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500980 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700981}
982
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400983func (mod *Module) begin(ctx BaseModuleContext) {
984 if mod.coverage != nil {
985 mod.coverage.begin(ctx)
986 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500987 if mod.sanitize != nil {
988 mod.sanitize.begin(ctx)
989 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400990}
991
Ivan Lozanoffee3342019-08-27 12:03:00 -0700992func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
993 var depPaths PathDeps
994
995 directRlibDeps := []*Module{}
996 directDylibDeps := []*Module{}
997 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900998 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700999 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001000 directSrcProvidersDeps := []*Module{}
1001 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002
1003 ctx.VisitDirectDeps(func(dep android.Module) {
1004 depName := ctx.OtherModuleName(dep)
1005 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001006
Ivan Lozano89435d12020-07-31 11:01:18 -04001007 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001008 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001009 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001010
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011 switch depTag {
1012 case dylibDepTag:
1013 dylib, ok := rustDep.compiler.(libraryInterface)
1014 if !ok || !dylib.dylib() {
1015 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1016 return
1017 }
1018 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001019 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001021
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022 rlib, ok := rustDep.compiler.(libraryInterface)
1023 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001024 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 return
1026 }
1027 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001028 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029 case procMacroDepTag:
1030 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001031 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001032 }
1033
1034 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001035 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1036 // OS/Arch variant is used.
1037 var helper string
1038 if ctx.Host() {
1039 helper = "missing 'host_supported'?"
1040 } else {
1041 helper = "device module defined?"
1042 }
1043
1044 if dep.Target().Os != ctx.Os() {
1045 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1046 return
1047 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1048 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1049 return
1050 }
1051 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052 }
1053
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001054 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001055 if depTag != procMacroDepTag {
1056 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1057 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1058 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1059 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001060 }
1061
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001063 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001064 if !linkFile.Valid() {
1065 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1066 depName, ctx.ModuleName())
1067 return
1068 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001070 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1071 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001072 }
1073 }
1074
Ivan Lozano89435d12020-07-31 11:01:18 -04001075 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001076 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001077 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001078 if _, ok := ccDep.(*Module); !ok {
1079 if ccDep.Module().Target().Os != ctx.Os() {
1080 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1081 return
1082 }
1083 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1084 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1085 return
1086 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001087 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001088 linkObject := ccDep.OutputFile()
1089 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001090
Ivan Lozano2093af22020-08-25 12:48:19 -04001091 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001092 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1093 }
1094
1095 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001096 switch {
1097 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001098 if cc.IsWholeStaticLib(depTag) {
1099 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1100 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001101 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1102 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001103 } else {
1104 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 -05001105 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001106 }
1107
1108 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1109 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001110 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001111 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1112
Colin Cross0de8a1e2020-09-18 14:15:30 -07001113 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1114 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1115 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1116 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1117 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001118 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001119 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001120 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001121 // For the shared lib dependencies, we may link to the stub variant
1122 // of the dependency depending on the context (e.g. if this
1123 // dependency crosses the APEX boundaries).
1124 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1125
1126 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1127 // object (either from stub or non-stub) to use.
1128 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1129 linkPath = linkPathFromFilePath(linkObject.Path())
1130
Ivan Lozanoffee3342019-08-27 12:03:00 -07001131 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001132 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001133 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1134 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1135 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1136 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001137 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001138
1139 // Record baseLibName for snapshots.
1140 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001141 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001142
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001143 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001144 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001145 case cc.IsHeaderDepTag(depTag):
1146 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1147 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1148 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1149 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001150 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001151 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001152 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001153 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001154 }
1155
1156 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001157 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1158 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001159 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001160 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001161 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001162
1163 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001164 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001165 // These are usually genrules which don't have per-target variants.
1166 directSrcDeps = append(directSrcDeps, srcDep)
1167 }
1168 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001169 })
1170
1171 var rlibDepFiles RustLibraries
1172 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001173 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001174 }
1175 var dylibDepFiles RustLibraries
1176 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001177 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001178 }
1179 var procMacroDepFiles RustLibraries
1180 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001181 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182 }
1183
1184 var staticLibDepFiles android.Paths
1185 for _, dep := range directStaticLibDeps {
1186 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1187 }
1188
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001189 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001190 var sharedLibDepFiles android.Paths
1191 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001192 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1193 if dep.TableOfContents.Valid() {
1194 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001195 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001196 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001197 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001198 }
1199
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001200 var srcProviderDepFiles android.Paths
1201 for _, dep := range directSrcProvidersDeps {
1202 srcs, _ := dep.OutputFiles("")
1203 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1204 }
1205 for _, dep := range directSrcDeps {
1206 srcs := dep.Srcs()
1207 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1208 }
1209
Ivan Lozanoffee3342019-08-27 12:03:00 -07001210 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1211 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1212 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001213 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001214 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1215 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001216 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001217
1218 // Dedup exported flags from dependencies
1219 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001220 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001221 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001222 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1223 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1224 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001225
1226 return depPaths
1227}
1228
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001229func (mod *Module) InstallInData() bool {
1230 if mod.compiler == nil {
1231 return false
1232 }
1233 return mod.compiler.inData()
1234}
1235
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001236func (mod *Module) InstallInRamdisk() bool {
1237 return mod.InRamdisk()
1238}
1239
1240func (mod *Module) InstallInVendorRamdisk() bool {
1241 return mod.InVendorRamdisk()
1242}
1243
1244func (mod *Module) InstallInRecovery() bool {
1245 return mod.InRecovery()
1246}
1247
Ivan Lozanoffee3342019-08-27 12:03:00 -07001248func linkPathFromFilePath(filepath android.Path) string {
1249 return strings.Split(filepath.String(), filepath.Base())[0]
1250}
Ivan Lozanod648c432020-02-06 12:05:10 -05001251
Ivan Lozanoffee3342019-08-27 12:03:00 -07001252func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1253 ctx := &depsContext{
1254 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001255 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001256
1257 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001258 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001259 var snapshotInfo *cc.SnapshotInfo
1260
1261 if ctx.Os() == android.Android {
1262 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1263 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001264
Ivan Lozano2b081132020-09-08 12:46:52 -04001265 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001266 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001267 stdLinkage = "rlib-std"
1268 }
1269
1270 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001271
Ivan Lozano2b081132020-09-08 12:46:52 -04001272 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1273 rlibDepVariations = append(rlibDepVariations,
1274 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1275 }
1276
Ivan Lozano1921e802021-05-20 13:39:16 -04001277 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001278 for _, lib := range deps.Rlibs {
1279 depTag := rlibDepTag
1280 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1281
1282 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1283 {Mutator: "rust_libraries", Variation: rlibVariation},
1284 }...), depTag, lib)
1285 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001286
1287 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001288 actx.AddVariationDependencies(
1289 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001290 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001291 dylibDepTag, deps.Dylibs...)
1292
Ivan Lozano1921e802021-05-20 13:39:16 -04001293 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001294 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1295 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001296 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001297 for _, lib := range deps.Rustlibs {
1298 depTag := autoDep.depTag
1299 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1300 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1301 {Mutator: "rust_libraries", Variation: autoDep.variation},
1302 }...), depTag, lib)
1303 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001304 } else {
1305 actx.AddVariationDependencies(
1306 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1307 autoDep.depTag, deps.Rustlibs...)
1308 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001309 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001310
1311 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001312 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001313 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001314 for _, lib := range deps.Stdlibs {
1315 depTag := rlibDepTag
1316 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1317
1318 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1319 depTag, lib)
1320 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001321 } else {
1322 actx.AddVariationDependencies(
1323 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1324 dylibDepTag, deps.Stdlibs...)
1325 }
1326 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001327
1328 for _, lib := range deps.SharedLibs {
1329 depTag := cc.SharedDepTag()
1330 name, version := cc.StubsLibNameAndVersion(lib)
1331
1332 variations := []blueprint.Variation{
1333 {Mutator: "link", Variation: "shared"},
1334 }
1335 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1336 }
1337
1338 for _, lib := range deps.WholeStaticLibs {
1339 depTag := cc.StaticDepTag(true)
1340 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1341
1342 actx.AddVariationDependencies([]blueprint.Variation{
1343 {Mutator: "link", Variation: "static"},
1344 }, depTag, lib)
1345 }
1346
1347 for _, lib := range deps.StaticLibs {
1348 depTag := cc.StaticDepTag(false)
1349 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1350
1351 actx.AddVariationDependencies([]blueprint.Variation{
1352 {Mutator: "link", Variation: "static"},
1353 }, depTag, lib)
1354 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001355
Zach Johnson3df4e632020-11-06 11:56:27 -08001356 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1357
Colin Cross565cafd2020-09-25 18:47:38 -07001358 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001359 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001360 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1361 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001362 }
1363 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001364 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1365 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001366 }
1367
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001368 if mod.sourceProvider != nil {
1369 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1370 bindgen.Properties.Custom_bindgen != "" {
1371 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1372 bindgen.Properties.Custom_bindgen)
1373 }
1374 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001375
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001376 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001377 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001378}
1379
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001380func BeginMutator(ctx android.BottomUpMutatorContext) {
1381 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1382 mod.beginMutator(ctx)
1383 }
1384}
1385
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001386func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1387 ctx := &baseModuleContext{
1388 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001389 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001390
1391 mod.begin(ctx)
1392}
1393
Ivan Lozanoffee3342019-08-27 12:03:00 -07001394func (mod *Module) Name() string {
1395 name := mod.ModuleBase.Name()
1396 if p, ok := mod.compiler.(interface {
1397 Name(string) string
1398 }); ok {
1399 name = p.Name(name)
1400 }
1401 return name
1402}
1403
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001404func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001405 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001406 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001407 }
1408}
1409
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001410var _ android.HostToolProvider = (*Module)(nil)
1411
1412func (mod *Module) HostToolPath() android.OptionalPath {
1413 if !mod.Host() {
1414 return android.OptionalPath{}
1415 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001416 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1417 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001418 }
1419 return android.OptionalPath{}
1420}
1421
Jiyong Park99644e92020-11-17 22:21:02 +09001422var _ android.ApexModule = (*Module)(nil)
1423
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001424func (mod *Module) minSdkVersion() string {
1425 return String(mod.Properties.Min_sdk_version)
1426}
1427
Jiyong Park45bf82e2020-12-15 22:29:02 +09001428var _ android.ApexModule = (*Module)(nil)
1429
1430// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001431func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001432 minSdkVersion := mod.minSdkVersion()
1433 if minSdkVersion == "apex_inherit" {
1434 return nil
1435 }
1436 if minSdkVersion == "" {
1437 return fmt.Errorf("min_sdk_version is not specificed")
1438 }
1439
1440 // Not using nativeApiLevelFromUser because the context here is not
1441 // necessarily a native context.
1442 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1443 if err != nil {
1444 return err
1445 }
1446
1447 if ver.GreaterThan(sdkVersion) {
1448 return fmt.Errorf("newer SDK(%v)", ver)
1449 }
Jiyong Park99644e92020-11-17 22:21:02 +09001450 return nil
1451}
1452
Jiyong Park45bf82e2020-12-15 22:29:02 +09001453// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001454func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1455 depTag := ctx.OtherModuleDependencyTag(dep)
1456
1457 if ccm, ok := dep.(*cc.Module); ok {
1458 if ccm.HasStubsVariants() {
1459 if cc.IsSharedDepTag(depTag) {
1460 // dynamic dep to a stubs lib crosses APEX boundary
1461 return false
1462 }
1463 if cc.IsRuntimeDepTag(depTag) {
1464 // runtime dep to a stubs lib also crosses APEX boundary
1465 return false
1466 }
1467
1468 if cc.IsHeaderDepTag(depTag) {
1469 return false
1470 }
1471 }
1472 if mod.Static() && cc.IsSharedDepTag(depTag) {
1473 // shared_lib dependency from a static lib is considered as crossing
1474 // the APEX boundary because the dependency doesn't actually is
1475 // linked; the dependency is used only during the compilation phase.
1476 return false
1477 }
1478 }
1479
1480 if depTag == procMacroDepTag {
1481 return false
1482 }
1483
1484 return true
1485}
1486
1487// Overrides ApexModule.IsInstallabeToApex()
1488func (mod *Module) IsInstallableToApex() bool {
1489 if mod.compiler != nil {
1490 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1491 return true
1492 }
1493 if _, ok := mod.compiler.(*binaryDecorator); ok {
1494 return true
1495 }
1496 }
1497 return false
1498}
1499
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001500// If a library file has a "lib" prefix, extract the library name without the prefix.
1501func libNameFromFilePath(filepath android.Path) (string, bool) {
1502 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1503 if strings.HasPrefix(libName, "lib") {
1504 libName = libName[3:]
1505 return libName, true
1506 }
1507 return "", false
1508}
1509
Ivan Lozanoffee3342019-08-27 12:03:00 -07001510var Bool = proptools.Bool
1511var BoolDefault = proptools.BoolDefault
1512var String = proptools.String
1513var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001514
1515var _ android.OutputFileProducer = (*Module)(nil)