blob: 80be49638b663039fd27a035758cb3e7eb2c96ff [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"
Ivan Lozanoffee3342019-08-27 12:03:00 -070028 "android/soong/rust/config"
29)
30
31var pctx = android.NewPackageContext("android/soong/rust")
32
33func init() {
34 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070035
36 android.AddNeverAllowRules(
37 android.NeverAllow().
Ivan Lozano03a94c42021-06-28 11:27:11 -040038 NotIn(append(config.RustAllowedPaths, config.DownstreamRustAllowedPaths...)...).
Ivan Lozanoe169ad72019-09-18 08:42:54 -070039 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070040
41 android.RegisterModuleType("rust_defaults", defaultsFactory)
42 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
43 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040044 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040045 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050046
47 })
48 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
49 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070050 })
51 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020052 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
55type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040056 GlobalRustFlags []string // Flags that apply globally to rust
57 GlobalLinkFlags []string // Flags that apply globally to linker
58 RustFlags []string // Flags that apply to rust
59 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070061 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070062 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020064 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type BaseProperties struct {
68 AndroidMkRlibs []string
69 AndroidMkDylibs []string
70 AndroidMkProcMacroLibs []string
71 AndroidMkSharedLibs []string
72 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040073
Ivan Lozano6a884432020-12-02 09:15:16 -050074 ImageVariationPrefix string `blueprint:"mutated"`
75 VndkVersion string `blueprint:"mutated"`
76 SubName string `blueprint:"mutated"`
77
Ivan Lozanoc08897c2021-04-02 12:41:32 -040078 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
79 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
80 // appended before SubName.
81 RustSubName string `blueprint:"mutated"`
82
Ivan Lozano6a884432020-12-02 09:15:16 -050083 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050084 CoreVariantNeeded bool `blueprint:"mutated"`
85 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
86 ExtraVariants []string `blueprint:"mutated"`
87
Ivan Lozanoa2268632021-07-22 10:52:06 -040088 // Allows this module to use non-APEX version of libraries. Useful
89 // for building binaries that are started before APEXes are activated.
90 Bootstrap *bool
91
Ivan Lozano1921e802021-05-20 13:39:16 -040092 // Used by vendor snapshot to record dependencies from snapshot modules.
93 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090094 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040095
Ivan Lozanoe6d30982021-02-05 10:57:43 -050096 // Make this module available when building for vendor ramdisk.
97 // On device without a dedicated recovery partition, the module is only
98 // available after switching root into
99 // /first_stage_ramdisk. To expose the module before switching root, install
100 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
101 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400102
Ivan Lozano1921e802021-05-20 13:39:16 -0400103 // Normally Soong uses the directory structure to decide which modules
104 // should be included (framework) or excluded (non-framework) from the
105 // different snapshots (vendor, recovery, etc.), but this property
106 // allows a partner to exclude a module normally thought of as a
107 // framework module from the vendor snapshot.
108 Exclude_from_vendor_snapshot *bool
109
110 // Normally Soong uses the directory structure to decide which modules
111 // should be included (framework) or excluded (non-framework) from the
112 // different snapshots (vendor, recovery, etc.), but this property
113 // allows a partner to exclude a module normally thought of as a
114 // framework module from the recovery snapshot.
115 Exclude_from_recovery_snapshot *bool
116
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500117 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
118 Min_sdk_version *string
119
Ivan Lozano43845682020-07-09 21:03:28 -0400120 PreventInstall bool
121 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400122 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123}
124
125type Module struct {
hamzeh41ad8812021-07-07 14:00:07 -0700126 cc.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700127
Ivan Lozano6a884432020-12-02 09:15:16 -0500128 VendorProperties cc.VendorProperties
129
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130 Properties BaseProperties
131
132 hod android.HostOrDeviceSupported
133 multilib android.Multilib
134
Ivan Lozano6a884432020-12-02 09:15:16 -0500135 makeLinkType string
136
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400138 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200139 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500140 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400142 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700143 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400144
Jiyong Parke54f07e2021-04-07 15:08:04 +0900145 // Unstripped output. This is usually used when this module is linked to another module
146 // as a library. The stripped output which is used for installation can be found via
147 // compiler.strippedOutputFile if it exists.
148 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700149 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900150
151 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152}
153
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500154func (mod *Module) Header() bool {
155 //TODO: If Rust libraries provide header variants, this needs to be updated.
156 return false
157}
158
159func (mod *Module) SetPreventInstall() {
160 mod.Properties.PreventInstall = true
161}
162
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500163func (mod *Module) SetHideFromMake() {
164 mod.Properties.HideFromMake = true
165}
166
Ivan Lozanod7586b62021-04-01 09:49:36 -0400167func (c *Module) HiddenFromMake() bool {
168 return c.Properties.HideFromMake
169}
170
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500172 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
173 // nil since we need compiler to actually sanitize.
174 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500175}
176
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500177func (mod *Module) IsPrebuilt() bool {
178 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
179 return true
180 }
181 return false
182}
183
Ivan Lozano43845682020-07-09 21:03:28 -0400184func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
185 switch tag {
186 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700187 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400188 return mod.sourceProvider.Srcs(), nil
189 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900190 if mod.OutputFile().Valid() {
191 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400192 }
193 return android.Paths{}, nil
194 }
195 default:
196 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
197 }
198}
199
Ivan Lozano52767be2019-10-18 14:49:46 -0700200func (mod *Module) SelectedStl() string {
201 return ""
202}
203
Ivan Lozano2b262972019-11-21 12:30:50 -0800204func (mod *Module) NonCcVariants() bool {
205 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400206 if _, ok := mod.compiler.(libraryInterface); ok {
207 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800208 }
209 }
210 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
211}
212
Ivan Lozano52767be2019-10-18 14:49:46 -0700213func (mod *Module) Static() bool {
214 if mod.compiler != nil {
215 if library, ok := mod.compiler.(libraryInterface); ok {
216 return library.static()
217 }
218 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400219 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700220}
221
222func (mod *Module) Shared() bool {
223 if mod.compiler != nil {
224 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400225 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700226 }
227 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400228 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700229}
230
Ivan Lozanod7586b62021-04-01 09:49:36 -0400231func (mod *Module) Dylib() bool {
232 if mod.compiler != nil {
233 if library, ok := mod.compiler.(libraryInterface); ok {
234 return library.dylib()
235 }
236 }
237 return false
238}
239
240func (mod *Module) Rlib() bool {
241 if mod.compiler != nil {
242 if library, ok := mod.compiler.(libraryInterface); ok {
243 return library.rlib()
244 }
245 }
246 return false
247}
248
249func (mod *Module) Binary() bool {
250 if mod.compiler != nil {
251 if _, ok := mod.compiler.(*binaryDecorator); ok {
252 return true
253 }
254 }
255 return false
256}
257
Justin Yun5e035862021-06-29 20:50:37 +0900258func (mod *Module) StaticExecutable() bool {
259 if !mod.Binary() {
260 return false
261 }
262 return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
263}
264
Ivan Lozanod7586b62021-04-01 09:49:36 -0400265func (mod *Module) Object() bool {
266 // Rust has no modules which produce only object files.
267 return false
268}
269
Ivan Lozano52767be2019-10-18 14:49:46 -0700270func (mod *Module) Toc() android.OptionalPath {
271 if mod.compiler != nil {
272 if _, ok := mod.compiler.(libraryInterface); ok {
273 return android.OptionalPath{}
274 }
275 }
276 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
277}
278
Colin Crossc511bc52020-04-07 16:50:32 +0000279func (mod *Module) UseSdk() bool {
280 return false
281}
282
Ivan Lozanod7586b62021-04-01 09:49:36 -0400283func (mod *Module) RelativeInstallPath() string {
284 if mod.compiler != nil {
285 return mod.compiler.relativeInstallPath()
286 }
287 return ""
288}
289
Ivan Lozano52767be2019-10-18 14:49:46 -0700290func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500291 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700292}
293
Jiyong Park7d55b612021-06-11 17:22:09 +0900294func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400295 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900296}
297
Ivan Lozano52767be2019-10-18 14:49:46 -0700298func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400299 return true
300}
301
302func (mod *Module) SubName() string {
303 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700304}
305
306func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500307 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700308 return false
309}
310
Ivan Lozanof9e21722020-12-02 09:00:51 -0500311func (mod *Module) IsVndkExt() bool {
312 return false
313}
314
Ivan Lozanod7586b62021-04-01 09:49:36 -0400315func (mod *Module) IsVndkSp() bool {
316 return false
317}
318
Colin Cross127bb8b2020-12-16 16:46:01 -0800319func (c *Module) IsVndkPrivate() bool {
320 return false
321}
322
323func (c *Module) IsLlndk() bool {
324 return false
325}
326
327func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500328 return false
329}
330
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400331func (mod *Module) KernelHeadersDecorator() bool {
332 return false
333}
334
Colin Cross1f3f1302021-04-26 18:37:44 -0700335func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400336 return false
337}
338
Colin Cross5271fea2021-04-27 13:06:04 -0700339func (m *Module) NeedsVendorPublicLibraryVariants() bool {
340 return false
341}
342
Ivan Lozanod7586b62021-04-01 09:49:36 -0400343func (mod *Module) HasLlndkStubs() bool {
344 return false
345}
346
347func (mod *Module) StubsVersion() string {
348 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
349}
350
Ivan Lozano52767be2019-10-18 14:49:46 -0700351func (mod *Module) SdkVersion() string {
352 return ""
353}
354
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900355func (mod *Module) MinSdkVersion() string {
356 return ""
357}
358
Colin Crossc511bc52020-04-07 16:50:32 +0000359func (mod *Module) AlwaysSdk() bool {
360 return false
361}
362
Jiyong Park2286afd2020-06-16 21:58:53 +0900363func (mod *Module) IsSdkVariant() bool {
364 return false
365}
366
Colin Cross1348ce32020-10-01 13:37:16 -0700367func (mod *Module) SplitPerApiLevel() bool {
368 return false
369}
370
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400372 Dylibs []string
373 Rlibs []string
374 Rustlibs []string
375 Stdlibs []string
376 ProcMacros []string
377 SharedLibs []string
378 StaticLibs []string
379 WholeStaticLibs []string
380 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700381
382 CrtBegin, CrtEnd string
383}
384
385type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500386 DyLibs RustLibraries
387 RLibs RustLibraries
388 SharedLibs android.Paths
389 SharedLibDeps android.Paths
390 StaticLibs android.Paths
391 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500392
393 // depFlags and depLinkFlags are rustc and linker (clang) flags.
394 depFlags []string
395 depLinkFlags []string
396
397 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
398 // Both of these are exported and propagate to dependencies.
399 linkDirs []string
400 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700401
Ivan Lozano45901ed2020-07-24 16:05:01 -0400402 // Used by bindgen modules which call clang
403 depClangFlags []string
404 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400405 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400406 depSystemIncludePaths android.Paths
407
Ivan Lozanof1c84332019-09-20 11:00:37 -0700408 CrtBegin android.OptionalPath
409 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700410
411 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500412 SrcDeps android.Paths
413 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414}
415
416type RustLibraries []RustLibrary
417
418type RustLibrary struct {
419 Path android.Path
420 CrateName string
421}
422
423type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100424 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425 compilerFlags(ctx ModuleContext, flags Flags) Flags
426 compilerProps() []interface{}
427 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
428 compilerDeps(ctx DepsContext, deps Deps) Deps
429 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700430 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100432 // Output directory in which source-generated code from dependencies is
433 // copied. This is equivalent to Cargo's OUT_DIR variable.
434 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700435
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800436 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200437 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400439 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400440
441 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400442
443 Disabled() bool
444 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400445
Ivan Lozanodd055472020-09-28 13:22:45 -0400446 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900447
448 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400449}
450
Matthew Maurerbb3add12020-06-25 09:34:12 -0700451type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700452 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400453 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700454}
455
456type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400457 linkDirs []string
458 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700459}
460
Matthew Maurerbb3add12020-06-25 09:34:12 -0700461func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
462 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
463}
464
Ivan Lozano2093af22020-08-25 12:48:19 -0400465func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
466 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
467}
468
Colin Cross0de8a1e2020-09-18 14:15:30 -0700469func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
470 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700471 LinkDirs: flagExporter.linkDirs,
472 LinkObjects: flagExporter.linkObjects,
473 })
474}
475
Matthew Maurerbb3add12020-06-25 09:34:12 -0700476var _ exportedFlagsProducer = (*flagExporter)(nil)
477
478func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700479 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700480}
481
Colin Cross0de8a1e2020-09-18 14:15:30 -0700482type FlagExporterInfo struct {
483 Flags []string
484 LinkDirs []string // TODO: this should be android.Paths
485 LinkObjects []string // TODO: this should be android.Paths
486}
487
488var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
489
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400490func (mod *Module) isCoverageVariant() bool {
491 return mod.coverage.Properties.IsCoverageVariant
492}
493
494var _ cc.Coverage = (*Module)(nil)
495
496func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
497 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
498}
499
Ivan Lozanod7586b62021-04-01 09:49:36 -0400500func (mod *Module) VndkVersion() string {
501 return mod.Properties.VndkVersion
502}
503
504func (mod *Module) PreventInstall() bool {
505 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400506}
507
508func (mod *Module) HideFromMake() {
509 mod.Properties.HideFromMake = true
510}
511
512func (mod *Module) MarkAsCoverageVariant(coverage bool) {
513 mod.coverage.Properties.IsCoverageVariant = coverage
514}
515
516func (mod *Module) EnableCoverageIfNeeded() {
517 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518}
519
520func defaultsFactory() android.Module {
521 return DefaultsFactory()
522}
523
524type Defaults struct {
525 android.ModuleBase
526 android.DefaultsModuleBase
527}
528
529func DefaultsFactory(props ...interface{}) android.Module {
530 module := &Defaults{}
531
532 module.AddProperties(props...)
533 module.AddProperties(
534 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500535 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100536 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400537 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700538 &BaseCompilerProperties{},
539 &BinaryCompilerProperties{},
540 &LibraryCompilerProperties{},
541 &ProcMacroCompilerProperties{},
542 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400543 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700544 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400545 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400546 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200547 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500548 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700549 )
550
551 android.InitDefaultsModule(module)
552 return module
553}
554
555func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700556 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557}
558
Ivan Lozano183a3212019-10-18 14:18:45 -0700559func (mod *Module) CcLibrary() bool {
560 if mod.compiler != nil {
561 if _, ok := mod.compiler.(*libraryDecorator); ok {
562 return true
563 }
564 }
565 return false
566}
567
568func (mod *Module) CcLibraryInterface() bool {
569 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400570 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
571 // VariantIs{Static,Shared} is set.
572 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700573 return true
574 }
575 }
576 return false
577}
578
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800579func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700580 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700581 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800582 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700583 }
584 }
585 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
586}
587
588func (mod *Module) SetStatic() {
589 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700590 if library, ok := mod.compiler.(libraryInterface); ok {
591 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700592 return
593 }
594 }
595 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
596}
597
598func (mod *Module) SetShared() {
599 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700600 if library, ok := mod.compiler.(libraryInterface); ok {
601 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700602 return
603 }
604 }
605 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
606}
607
Ivan Lozano183a3212019-10-18 14:18:45 -0700608func (mod *Module) BuildStaticVariant() bool {
609 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700610 if library, ok := mod.compiler.(libraryInterface); ok {
611 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700612 }
613 }
614 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
615}
616
617func (mod *Module) BuildSharedVariant() bool {
618 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700619 if library, ok := mod.compiler.(libraryInterface); ok {
620 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700621 }
622 }
623 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
624}
625
Ivan Lozano183a3212019-10-18 14:18:45 -0700626func (mod *Module) Module() android.Module {
627 return mod
628}
629
Ivan Lozano183a3212019-10-18 14:18:45 -0700630func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900631 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
632 return mod.compiler.strippedOutputFilePath()
633 }
634 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700635}
636
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400637func (mod *Module) CoverageFiles() android.Paths {
638 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800639 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400640 }
641 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
642}
643
Jiyong Park459feca2020-12-15 11:02:21 +0900644func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900645 if !mod.EverInstallable() {
646 return false
647 }
648
Jiyong Park459feca2020-12-15 11:02:21 +0900649 // The apex variant is not installable because it is included in the APEX and won't appear
650 // in the system partition as a standalone file.
651 if !apexInfo.IsForPlatform() {
652 return false
653 }
654
Jiyong Parke54f07e2021-04-07 15:08:04 +0900655 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900656}
657
Ivan Lozano183a3212019-10-18 14:18:45 -0700658var _ cc.LinkableInterface = (*Module)(nil)
659
Ivan Lozanoffee3342019-08-27 12:03:00 -0700660func (mod *Module) Init() android.Module {
661 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500662 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663
664 if mod.compiler != nil {
665 mod.AddProperties(mod.compiler.compilerProps()...)
666 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400667 if mod.coverage != nil {
668 mod.AddProperties(mod.coverage.props()...)
669 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200670 if mod.clippy != nil {
671 mod.AddProperties(mod.clippy.props()...)
672 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400673 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700674 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400675 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500676 if mod.sanitize != nil {
677 mod.AddProperties(mod.sanitize.props()...)
678 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400679
Ivan Lozanoffee3342019-08-27 12:03:00 -0700680 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900681 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682
683 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700684 return mod
685}
686
687func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
688 return &Module{
689 hod: hod,
690 multilib: multilib,
691 }
692}
693func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
694 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400695 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200696 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500697 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700698 return module
699}
700
701type ModuleContext interface {
702 android.ModuleContext
703 ModuleContextIntf
704}
705
706type BaseModuleContext interface {
707 android.BaseModuleContext
708 ModuleContextIntf
709}
710
711type DepsContext interface {
712 android.BottomUpMutatorContext
713 ModuleContextIntf
714}
715
716type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200717 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719}
720
721type depsContext struct {
722 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723}
724
725type moduleContext struct {
726 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700727}
728
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200729type baseModuleContext struct {
730 android.BaseModuleContext
731}
732
733func (ctx *moduleContext) RustModule() *Module {
734 return ctx.Module().(*Module)
735}
736
737func (ctx *moduleContext) toolchain() config.Toolchain {
738 return ctx.RustModule().toolchain(ctx)
739}
740
741func (ctx *depsContext) RustModule() *Module {
742 return ctx.Module().(*Module)
743}
744
745func (ctx *depsContext) toolchain() config.Toolchain {
746 return ctx.RustModule().toolchain(ctx)
747}
748
749func (ctx *baseModuleContext) RustModule() *Module {
750 return ctx.Module().(*Module)
751}
752
753func (ctx *baseModuleContext) toolchain() config.Toolchain {
754 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400755}
756
757func (mod *Module) nativeCoverage() bool {
758 return mod.compiler != nil && mod.compiler.nativeCoverage()
759}
760
Ivan Lozanod7586b62021-04-01 09:49:36 -0400761func (mod *Module) EverInstallable() bool {
762 return mod.compiler != nil &&
763 // Check to see whether the module is actually ever installable.
764 mod.compiler.everInstallable()
765}
766
767func (mod *Module) Installable() *bool {
768 return mod.Properties.Installable
769}
770
Ivan Lozanoffee3342019-08-27 12:03:00 -0700771func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
772 if mod.cachedToolchain == nil {
773 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
774 }
775 return mod.cachedToolchain
776}
777
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200778func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
779 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
780}
781
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
783}
784
785func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
786 ctx := &moduleContext{
787 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789
Jiyong Park99644e92020-11-17 22:21:02 +0900790 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
791 if !apexInfo.IsForPlatform() {
792 mod.hideApexVariantFromMake = true
793 }
794
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500796 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
797
798 // Differentiate static libraries that are vendor available
799 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500800 mod.Properties.SubName += cc.VendorSuffix
801 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
802 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500803 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804
805 if !toolchain.Supported() {
806 // This toolchain's unsupported, there's nothing to do for this mod.
807 return
808 }
809
810 deps := mod.depsToPaths(ctx)
811 flags := Flags{
812 Toolchain: toolchain,
813 }
814
815 if mod.compiler != nil {
816 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400817 }
818 if mod.coverage != nil {
819 flags, deps = mod.coverage.flags(ctx, flags, deps)
820 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200821 if mod.clippy != nil {
822 flags, deps = mod.clippy.flags(ctx, flags, deps)
823 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500824 if mod.sanitize != nil {
825 flags, deps = mod.sanitize.flags(ctx, flags, deps)
826 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400827
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200828 // SourceProvider needs to call GenerateSource() before compiler calls
829 // compile() so it can provide the source. A SourceProvider has
830 // multiple variants (e.g. source, rlib, dylib). Only the "source"
831 // variant is responsible for effectively generating the source. The
832 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400833 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200834 if mod.compiler.(libraryInterface).source() {
835 mod.sourceProvider.GenerateSource(ctx, deps)
836 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
837 } else {
838 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
839 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700840 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200841 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400842 }
843
844 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100845 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900846 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900847 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200848 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900849
Dan Albert06feee92021-03-19 15:06:02 -0700850 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
851
Ivan Lozano1921e802021-05-20 13:39:16 -0400852 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
853 // RECOVERY_SNAPSHOT_VERSION is current.
854 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
855 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
856 lib.collectHeadersForSnapshot(ctx, deps)
857 }
858 }
859
Jiyong Park459feca2020-12-15 11:02:21 +0900860 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
861 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200862 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400863 }
Chris Wailes74be7642021-07-22 16:20:28 -0700864
865 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 }
867}
868
869func (mod *Module) deps(ctx DepsContext) Deps {
870 deps := Deps{}
871
872 if mod.compiler != nil {
873 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400874 }
875 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700876 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877 }
878
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400879 if mod.coverage != nil {
880 deps = mod.coverage.deps(ctx, deps)
881 }
882
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500883 if mod.sanitize != nil {
884 deps = mod.sanitize.deps(ctx, deps)
885 }
886
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
888 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700889 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
891 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
892 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400893 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 return deps
895
896}
897
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898type dependencyTag struct {
899 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800900 name string
901 library bool
902 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700903}
904
Jiyong Park65b62242020-11-25 12:44:59 +0900905// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
906// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
907func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800908 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900909}
910
911var _ android.InstallNeededDependencyTag = dependencyTag{}
912
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400914 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
915 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
916 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800917 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400918 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200919 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920)
921
Jiyong Park99644e92020-11-17 22:21:02 +0900922func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
923 tag, ok := depTag.(dependencyTag)
924 return ok && tag == dylibDepTag
925}
926
Jiyong Park94e22fd2021-04-08 18:19:15 +0900927func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
928 tag, ok := depTag.(dependencyTag)
929 return ok && tag == rlibDepTag
930}
931
Matthew Maurer0f003b12020-06-29 14:34:06 -0700932type autoDep struct {
933 variation string
934 depTag dependencyTag
935}
936
937var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200938 rlibVariation = "rlib"
939 dylibVariation = "dylib"
940 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
941 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700942)
943
944type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500945 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700946}
947
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400948func (mod *Module) begin(ctx BaseModuleContext) {
949 if mod.coverage != nil {
950 mod.coverage.begin(ctx)
951 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500952 if mod.sanitize != nil {
953 mod.sanitize.begin(ctx)
954 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400955}
956
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
958 var depPaths PathDeps
959
960 directRlibDeps := []*Module{}
961 directDylibDeps := []*Module{}
962 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900963 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700964 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400965 directSrcProvidersDeps := []*Module{}
966 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967
968 ctx.VisitDirectDeps(func(dep android.Module) {
969 depName := ctx.OtherModuleName(dep)
970 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400971
Ivan Lozano89435d12020-07-31 11:01:18 -0400972 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700973 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400974 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700975
Ivan Lozanoffee3342019-08-27 12:03:00 -0700976 switch depTag {
977 case dylibDepTag:
978 dylib, ok := rustDep.compiler.(libraryInterface)
979 if !ok || !dylib.dylib() {
980 ctx.ModuleErrorf("mod %q not an dylib library", depName)
981 return
982 }
983 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400984 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700985 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400986
Ivan Lozanoffee3342019-08-27 12:03:00 -0700987 rlib, ok := rustDep.compiler.(libraryInterface)
988 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400989 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990 return
991 }
992 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400993 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994 case procMacroDepTag:
995 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400996 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +0100997 }
998
999 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001000 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1001 // OS/Arch variant is used.
1002 var helper string
1003 if ctx.Host() {
1004 helper = "missing 'host_supported'?"
1005 } else {
1006 helper = "device module defined?"
1007 }
1008
1009 if dep.Target().Os != ctx.Os() {
1010 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1011 return
1012 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1013 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1014 return
1015 }
1016 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017 }
1018
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001019 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001020 if depTag != procMacroDepTag {
1021 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1022 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1023 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1024 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025 }
1026
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001028 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001029 if !linkFile.Valid() {
1030 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1031 depName, ctx.ModuleName())
1032 return
1033 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001035 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1036 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001037 }
1038 }
1039
Ivan Lozano89435d12020-07-31 11:01:18 -04001040 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001041 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001042 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001043 if _, ok := ccDep.(*Module); !ok {
1044 if ccDep.Module().Target().Os != ctx.Os() {
1045 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1046 return
1047 }
1048 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1049 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1050 return
1051 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001052 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001053 linkObject := ccDep.OutputFile()
1054 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001055
Ivan Lozano2093af22020-08-25 12:48:19 -04001056 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001057 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1058 }
1059
1060 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001061 switch {
1062 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001063 if cc.IsWholeStaticLib(depTag) {
1064 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1065 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001066 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1067 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001068 } else {
1069 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 -05001070 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001071 }
1072
1073 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1074 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001075 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001076 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1077
Colin Cross0de8a1e2020-09-18 14:15:30 -07001078 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1079 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1080 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1081 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1082 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001083 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001084 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001085 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001086 // For the shared lib dependencies, we may link to the stub variant
1087 // of the dependency depending on the context (e.g. if this
1088 // dependency crosses the APEX boundaries).
1089 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1090
1091 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1092 // object (either from stub or non-stub) to use.
1093 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1094 linkPath = linkPathFromFilePath(linkObject.Path())
1095
Ivan Lozanoffee3342019-08-27 12:03:00 -07001096 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001097 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001098 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1099 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1100 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1101 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001102 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001103
1104 // Record baseLibName for snapshots.
1105 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001106 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001107
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001108 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001109 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001110 case cc.IsHeaderDepTag(depTag):
1111 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1112 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1113 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1114 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001115 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001116 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001117 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001118 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001119 }
1120
1121 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001122 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1123 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001124 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001125 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001126 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001127
1128 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001129 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001130 // These are usually genrules which don't have per-target variants.
1131 directSrcDeps = append(directSrcDeps, srcDep)
1132 }
1133 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134 })
1135
1136 var rlibDepFiles RustLibraries
1137 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001138 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001139 }
1140 var dylibDepFiles RustLibraries
1141 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001142 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001143 }
1144 var procMacroDepFiles RustLibraries
1145 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001146 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001147 }
1148
1149 var staticLibDepFiles android.Paths
1150 for _, dep := range directStaticLibDeps {
1151 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1152 }
1153
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001154 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001155 var sharedLibDepFiles android.Paths
1156 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001157 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1158 if dep.TableOfContents.Valid() {
1159 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001160 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001161 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001162 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001163 }
1164
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001165 var srcProviderDepFiles android.Paths
1166 for _, dep := range directSrcProvidersDeps {
1167 srcs, _ := dep.OutputFiles("")
1168 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1169 }
1170 for _, dep := range directSrcDeps {
1171 srcs := dep.Srcs()
1172 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1173 }
1174
Ivan Lozanoffee3342019-08-27 12:03:00 -07001175 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1176 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1177 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001178 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001179 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1180 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001181 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182
1183 // Dedup exported flags from dependencies
1184 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001185 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001186 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001187 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1188 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1189 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001190
1191 return depPaths
1192}
1193
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001194func (mod *Module) InstallInData() bool {
1195 if mod.compiler == nil {
1196 return false
1197 }
1198 return mod.compiler.inData()
1199}
1200
Ivan Lozanoffee3342019-08-27 12:03:00 -07001201func linkPathFromFilePath(filepath android.Path) string {
1202 return strings.Split(filepath.String(), filepath.Base())[0]
1203}
Ivan Lozanod648c432020-02-06 12:05:10 -05001204
Ivan Lozanoffee3342019-08-27 12:03:00 -07001205func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1206 ctx := &depsContext{
1207 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001208 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001209
1210 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001211 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001212 var snapshotInfo *cc.SnapshotInfo
1213
1214 if ctx.Os() == android.Android {
1215 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1216 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001217
Ivan Lozano2b081132020-09-08 12:46:52 -04001218 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001219 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001220 stdLinkage = "rlib-std"
1221 }
1222
1223 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001224
Ivan Lozano2b081132020-09-08 12:46:52 -04001225 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1226 rlibDepVariations = append(rlibDepVariations,
1227 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1228 }
1229
Ivan Lozano1921e802021-05-20 13:39:16 -04001230 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001231 for _, lib := range deps.Rlibs {
1232 depTag := rlibDepTag
1233 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1234
1235 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1236 {Mutator: "rust_libraries", Variation: rlibVariation},
1237 }...), depTag, lib)
1238 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001239
1240 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001241 actx.AddVariationDependencies(
1242 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001243 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001244 dylibDepTag, deps.Dylibs...)
1245
Ivan Lozano1921e802021-05-20 13:39:16 -04001246 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001247 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1248 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001249 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001250 for _, lib := range deps.Rustlibs {
1251 depTag := autoDep.depTag
1252 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1253 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1254 {Mutator: "rust_libraries", Variation: autoDep.variation},
1255 }...), depTag, lib)
1256 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001257 } else {
1258 actx.AddVariationDependencies(
1259 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1260 autoDep.depTag, deps.Rustlibs...)
1261 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001262 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001263
1264 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001265 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001266 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001267 for _, lib := range deps.Stdlibs {
1268 depTag := rlibDepTag
1269 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1270
1271 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1272 depTag, lib)
1273 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001274 } else {
1275 actx.AddVariationDependencies(
1276 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1277 dylibDepTag, deps.Stdlibs...)
1278 }
1279 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001280
1281 for _, lib := range deps.SharedLibs {
1282 depTag := cc.SharedDepTag()
1283 name, version := cc.StubsLibNameAndVersion(lib)
1284
1285 variations := []blueprint.Variation{
1286 {Mutator: "link", Variation: "shared"},
1287 }
1288 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1289 }
1290
1291 for _, lib := range deps.WholeStaticLibs {
1292 depTag := cc.StaticDepTag(true)
1293 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1294
1295 actx.AddVariationDependencies([]blueprint.Variation{
1296 {Mutator: "link", Variation: "static"},
1297 }, depTag, lib)
1298 }
1299
1300 for _, lib := range deps.StaticLibs {
1301 depTag := cc.StaticDepTag(false)
1302 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1303
1304 actx.AddVariationDependencies([]blueprint.Variation{
1305 {Mutator: "link", Variation: "static"},
1306 }, depTag, lib)
1307 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001308
Zach Johnson3df4e632020-11-06 11:56:27 -08001309 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1310
Colin Cross565cafd2020-09-25 18:47:38 -07001311 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001312 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001313 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1314 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001315 }
1316 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001317 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1318 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001319 }
1320
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001321 if mod.sourceProvider != nil {
1322 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1323 bindgen.Properties.Custom_bindgen != "" {
1324 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1325 bindgen.Properties.Custom_bindgen)
1326 }
1327 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001328
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001329 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001330 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001331}
1332
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001333func BeginMutator(ctx android.BottomUpMutatorContext) {
1334 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1335 mod.beginMutator(ctx)
1336 }
1337}
1338
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001339func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1340 ctx := &baseModuleContext{
1341 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001342 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001343
1344 mod.begin(ctx)
1345}
1346
Ivan Lozanoffee3342019-08-27 12:03:00 -07001347func (mod *Module) Name() string {
1348 name := mod.ModuleBase.Name()
1349 if p, ok := mod.compiler.(interface {
1350 Name(string) string
1351 }); ok {
1352 name = p.Name(name)
1353 }
1354 return name
1355}
1356
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001357func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001358 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001359 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001360 }
1361}
1362
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001363var _ android.HostToolProvider = (*Module)(nil)
1364
1365func (mod *Module) HostToolPath() android.OptionalPath {
1366 if !mod.Host() {
1367 return android.OptionalPath{}
1368 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001369 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1370 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001371 }
1372 return android.OptionalPath{}
1373}
1374
Jiyong Park99644e92020-11-17 22:21:02 +09001375var _ android.ApexModule = (*Module)(nil)
1376
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001377func (mod *Module) minSdkVersion() string {
1378 return String(mod.Properties.Min_sdk_version)
1379}
1380
Jiyong Park45bf82e2020-12-15 22:29:02 +09001381var _ android.ApexModule = (*Module)(nil)
1382
1383// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001384func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001385 minSdkVersion := mod.minSdkVersion()
1386 if minSdkVersion == "apex_inherit" {
1387 return nil
1388 }
1389 if minSdkVersion == "" {
1390 return fmt.Errorf("min_sdk_version is not specificed")
1391 }
1392
1393 // Not using nativeApiLevelFromUser because the context here is not
1394 // necessarily a native context.
1395 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1396 if err != nil {
1397 return err
1398 }
1399
1400 if ver.GreaterThan(sdkVersion) {
1401 return fmt.Errorf("newer SDK(%v)", ver)
1402 }
Jiyong Park99644e92020-11-17 22:21:02 +09001403 return nil
1404}
1405
Jiyong Park45bf82e2020-12-15 22:29:02 +09001406// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001407func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1408 depTag := ctx.OtherModuleDependencyTag(dep)
1409
1410 if ccm, ok := dep.(*cc.Module); ok {
1411 if ccm.HasStubsVariants() {
1412 if cc.IsSharedDepTag(depTag) {
1413 // dynamic dep to a stubs lib crosses APEX boundary
1414 return false
1415 }
1416 if cc.IsRuntimeDepTag(depTag) {
1417 // runtime dep to a stubs lib also crosses APEX boundary
1418 return false
1419 }
1420
1421 if cc.IsHeaderDepTag(depTag) {
1422 return false
1423 }
1424 }
1425 if mod.Static() && cc.IsSharedDepTag(depTag) {
1426 // shared_lib dependency from a static lib is considered as crossing
1427 // the APEX boundary because the dependency doesn't actually is
1428 // linked; the dependency is used only during the compilation phase.
1429 return false
1430 }
1431 }
1432
1433 if depTag == procMacroDepTag {
1434 return false
1435 }
1436
1437 return true
1438}
1439
1440// Overrides ApexModule.IsInstallabeToApex()
1441func (mod *Module) IsInstallableToApex() bool {
1442 if mod.compiler != nil {
1443 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1444 return true
1445 }
1446 if _, ok := mod.compiler.(*binaryDecorator); ok {
1447 return true
1448 }
1449 }
1450 return false
1451}
1452
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001453// If a library file has a "lib" prefix, extract the library name without the prefix.
1454func libNameFromFilePath(filepath android.Path) (string, bool) {
1455 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1456 if strings.HasPrefix(libName, "lib") {
1457 libName = libName[3:]
1458 return libName, true
1459 }
1460 return "", false
1461}
1462
Ivan Lozanoffee3342019-08-27 12:03:00 -07001463var Bool = proptools.Bool
1464var BoolDefault = proptools.BoolDefault
1465var String = proptools.String
1466var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001467
1468var _ android.OutputFileProducer = (*Module)(nil)