blob: 9cd54a2517bdc5a311a1354fe7f1177d407f7aa3 [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 Lozano1921e802021-05-20 13:39:16 -040088 // Used by vendor snapshot to record dependencies from snapshot modules.
89 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090090 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040091
Ivan Lozanoe6d30982021-02-05 10:57:43 -050092 // Make this module available when building for vendor ramdisk.
93 // On device without a dedicated recovery partition, the module is only
94 // available after switching root into
95 // /first_stage_ramdisk. To expose the module before switching root, install
96 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
97 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040098
Ivan Lozano1921e802021-05-20 13:39:16 -040099 // Normally Soong uses the directory structure to decide which modules
100 // should be included (framework) or excluded (non-framework) from the
101 // different snapshots (vendor, recovery, etc.), but this property
102 // allows a partner to exclude a module normally thought of as a
103 // framework module from the vendor snapshot.
104 Exclude_from_vendor_snapshot *bool
105
106 // Normally Soong uses the directory structure to decide which modules
107 // should be included (framework) or excluded (non-framework) from the
108 // different snapshots (vendor, recovery, etc.), but this property
109 // allows a partner to exclude a module normally thought of as a
110 // framework module from the recovery snapshot.
111 Exclude_from_recovery_snapshot *bool
112
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500113 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
114 Min_sdk_version *string
115
Ivan Lozano43845682020-07-09 21:03:28 -0400116 PreventInstall bool
117 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400118 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119}
120
121type Module struct {
hamzeh41ad8812021-07-07 14:00:07 -0700122 cc.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123
Ivan Lozano6a884432020-12-02 09:15:16 -0500124 VendorProperties cc.VendorProperties
125
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126 Properties BaseProperties
127
128 hod android.HostOrDeviceSupported
129 multilib android.Multilib
130
Ivan Lozano6a884432020-12-02 09:15:16 -0500131 makeLinkType string
132
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400134 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200135 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500136 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400138 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700139 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400140
Jiyong Parke54f07e2021-04-07 15:08:04 +0900141 // Unstripped output. This is usually used when this module is linked to another module
142 // as a library. The stripped output which is used for installation can be found via
143 // compiler.strippedOutputFile if it exists.
144 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700145 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900146
147 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148}
149
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500150func (mod *Module) Header() bool {
151 //TODO: If Rust libraries provide header variants, this needs to be updated.
152 return false
153}
154
155func (mod *Module) SetPreventInstall() {
156 mod.Properties.PreventInstall = true
157}
158
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500159func (mod *Module) SetHideFromMake() {
160 mod.Properties.HideFromMake = true
161}
162
Ivan Lozanod7586b62021-04-01 09:49:36 -0400163func (c *Module) HiddenFromMake() bool {
164 return c.Properties.HideFromMake
165}
166
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500167func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500168 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
169 // nil since we need compiler to actually sanitize.
170 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171}
172
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500173func (mod *Module) IsPrebuilt() bool {
174 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
175 return true
176 }
177 return false
178}
179
Ivan Lozano43845682020-07-09 21:03:28 -0400180func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
181 switch tag {
182 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700183 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400184 return mod.sourceProvider.Srcs(), nil
185 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900186 if mod.OutputFile().Valid() {
187 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400188 }
189 return android.Paths{}, nil
190 }
191 default:
192 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
193 }
194}
195
Ivan Lozano52767be2019-10-18 14:49:46 -0700196func (mod *Module) SelectedStl() string {
197 return ""
198}
199
Ivan Lozano2b262972019-11-21 12:30:50 -0800200func (mod *Module) NonCcVariants() bool {
201 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400202 if _, ok := mod.compiler.(libraryInterface); ok {
203 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800204 }
205 }
206 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
207}
208
Ivan Lozano52767be2019-10-18 14:49:46 -0700209func (mod *Module) Static() bool {
210 if mod.compiler != nil {
211 if library, ok := mod.compiler.(libraryInterface); ok {
212 return library.static()
213 }
214 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400215 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700216}
217
218func (mod *Module) Shared() bool {
219 if mod.compiler != nil {
220 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400221 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700222 }
223 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400224 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700225}
226
Ivan Lozanod7586b62021-04-01 09:49:36 -0400227func (mod *Module) Dylib() bool {
228 if mod.compiler != nil {
229 if library, ok := mod.compiler.(libraryInterface); ok {
230 return library.dylib()
231 }
232 }
233 return false
234}
235
236func (mod *Module) Rlib() bool {
237 if mod.compiler != nil {
238 if library, ok := mod.compiler.(libraryInterface); ok {
239 return library.rlib()
240 }
241 }
242 return false
243}
244
245func (mod *Module) Binary() bool {
246 if mod.compiler != nil {
247 if _, ok := mod.compiler.(*binaryDecorator); ok {
248 return true
249 }
250 }
251 return false
252}
253
Justin Yun5e035862021-06-29 20:50:37 +0900254func (mod *Module) StaticExecutable() bool {
255 if !mod.Binary() {
256 return false
257 }
258 return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
259}
260
Ivan Lozanod7586b62021-04-01 09:49:36 -0400261func (mod *Module) Object() bool {
262 // Rust has no modules which produce only object files.
263 return false
264}
265
Ivan Lozano52767be2019-10-18 14:49:46 -0700266func (mod *Module) Toc() android.OptionalPath {
267 if mod.compiler != nil {
268 if _, ok := mod.compiler.(libraryInterface); ok {
269 return android.OptionalPath{}
270 }
271 }
272 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
273}
274
Colin Crossc511bc52020-04-07 16:50:32 +0000275func (mod *Module) UseSdk() bool {
276 return false
277}
278
Ivan Lozanod7586b62021-04-01 09:49:36 -0400279func (mod *Module) RelativeInstallPath() string {
280 if mod.compiler != nil {
281 return mod.compiler.relativeInstallPath()
282 }
283 return ""
284}
285
Ivan Lozano52767be2019-10-18 14:49:46 -0700286func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500287 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700288}
289
Jiyong Park7d55b612021-06-11 17:22:09 +0900290func (mod *Module) Bootstrap() bool {
291 return false
292}
293
Ivan Lozano52767be2019-10-18 14:49:46 -0700294func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400295 return true
296}
297
298func (mod *Module) SubName() string {
299 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700300}
301
302func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500303 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700304 return false
305}
306
Ivan Lozanof9e21722020-12-02 09:00:51 -0500307func (mod *Module) IsVndkExt() bool {
308 return false
309}
310
Ivan Lozanod7586b62021-04-01 09:49:36 -0400311func (mod *Module) IsVndkSp() bool {
312 return false
313}
314
Colin Cross127bb8b2020-12-16 16:46:01 -0800315func (c *Module) IsVndkPrivate() bool {
316 return false
317}
318
319func (c *Module) IsLlndk() bool {
320 return false
321}
322
323func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500324 return false
325}
326
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400327func (mod *Module) KernelHeadersDecorator() bool {
328 return false
329}
330
Colin Cross1f3f1302021-04-26 18:37:44 -0700331func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400332 return false
333}
334
Colin Cross5271fea2021-04-27 13:06:04 -0700335func (m *Module) NeedsVendorPublicLibraryVariants() bool {
336 return false
337}
338
Ivan Lozanod7586b62021-04-01 09:49:36 -0400339func (mod *Module) HasLlndkStubs() bool {
340 return false
341}
342
343func (mod *Module) StubsVersion() string {
344 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
345}
346
Ivan Lozano52767be2019-10-18 14:49:46 -0700347func (mod *Module) SdkVersion() string {
348 return ""
349}
350
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900351func (mod *Module) MinSdkVersion() string {
352 return ""
353}
354
Colin Crossc511bc52020-04-07 16:50:32 +0000355func (mod *Module) AlwaysSdk() bool {
356 return false
357}
358
Jiyong Park2286afd2020-06-16 21:58:53 +0900359func (mod *Module) IsSdkVariant() bool {
360 return false
361}
362
Colin Cross1348ce32020-10-01 13:37:16 -0700363func (mod *Module) SplitPerApiLevel() bool {
364 return false
365}
366
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400368 Dylibs []string
369 Rlibs []string
370 Rustlibs []string
371 Stdlibs []string
372 ProcMacros []string
373 SharedLibs []string
374 StaticLibs []string
375 WholeStaticLibs []string
376 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700377
378 CrtBegin, CrtEnd string
379}
380
381type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500382 DyLibs RustLibraries
383 RLibs RustLibraries
384 SharedLibs android.Paths
385 SharedLibDeps android.Paths
386 StaticLibs android.Paths
387 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500388
389 // depFlags and depLinkFlags are rustc and linker (clang) flags.
390 depFlags []string
391 depLinkFlags []string
392
393 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
394 // Both of these are exported and propagate to dependencies.
395 linkDirs []string
396 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700397
Ivan Lozano45901ed2020-07-24 16:05:01 -0400398 // Used by bindgen modules which call clang
399 depClangFlags []string
400 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400401 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400402 depSystemIncludePaths android.Paths
403
Ivan Lozanof1c84332019-09-20 11:00:37 -0700404 CrtBegin android.OptionalPath
405 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700406
407 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500408 SrcDeps android.Paths
409 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410}
411
412type RustLibraries []RustLibrary
413
414type RustLibrary struct {
415 Path android.Path
416 CrateName string
417}
418
419type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100420 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700421 compilerFlags(ctx ModuleContext, flags Flags) Flags
422 compilerProps() []interface{}
423 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
424 compilerDeps(ctx DepsContext, deps Deps) Deps
425 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700426 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100428 // Output directory in which source-generated code from dependencies is
429 // copied. This is equivalent to Cargo's OUT_DIR variable.
430 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700431
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800432 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200433 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700434 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400435 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400436
437 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400438
439 Disabled() bool
440 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400441
Ivan Lozanodd055472020-09-28 13:22:45 -0400442 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900443
444 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400445}
446
Matthew Maurerbb3add12020-06-25 09:34:12 -0700447type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700448 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400449 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700450}
451
452type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400453 linkDirs []string
454 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700455}
456
Matthew Maurerbb3add12020-06-25 09:34:12 -0700457func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
458 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
459}
460
Ivan Lozano2093af22020-08-25 12:48:19 -0400461func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
462 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
463}
464
Colin Cross0de8a1e2020-09-18 14:15:30 -0700465func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
466 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700467 LinkDirs: flagExporter.linkDirs,
468 LinkObjects: flagExporter.linkObjects,
469 })
470}
471
Matthew Maurerbb3add12020-06-25 09:34:12 -0700472var _ exportedFlagsProducer = (*flagExporter)(nil)
473
474func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700475 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700476}
477
Colin Cross0de8a1e2020-09-18 14:15:30 -0700478type FlagExporterInfo struct {
479 Flags []string
480 LinkDirs []string // TODO: this should be android.Paths
481 LinkObjects []string // TODO: this should be android.Paths
482}
483
484var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
485
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486func (mod *Module) isCoverageVariant() bool {
487 return mod.coverage.Properties.IsCoverageVariant
488}
489
490var _ cc.Coverage = (*Module)(nil)
491
492func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
493 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
494}
495
Ivan Lozanod7586b62021-04-01 09:49:36 -0400496func (mod *Module) VndkVersion() string {
497 return mod.Properties.VndkVersion
498}
499
500func (mod *Module) PreventInstall() bool {
501 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400502}
503
504func (mod *Module) HideFromMake() {
505 mod.Properties.HideFromMake = true
506}
507
508func (mod *Module) MarkAsCoverageVariant(coverage bool) {
509 mod.coverage.Properties.IsCoverageVariant = coverage
510}
511
512func (mod *Module) EnableCoverageIfNeeded() {
513 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700514}
515
516func defaultsFactory() android.Module {
517 return DefaultsFactory()
518}
519
520type Defaults struct {
521 android.ModuleBase
522 android.DefaultsModuleBase
523}
524
525func DefaultsFactory(props ...interface{}) android.Module {
526 module := &Defaults{}
527
528 module.AddProperties(props...)
529 module.AddProperties(
530 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500531 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100532 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400533 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534 &BaseCompilerProperties{},
535 &BinaryCompilerProperties{},
536 &LibraryCompilerProperties{},
537 &ProcMacroCompilerProperties{},
538 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400539 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700540 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400541 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400542 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200543 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500544 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700545 )
546
547 android.InitDefaultsModule(module)
548 return module
549}
550
551func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700552 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553}
554
Ivan Lozano183a3212019-10-18 14:18:45 -0700555func (mod *Module) CcLibrary() bool {
556 if mod.compiler != nil {
557 if _, ok := mod.compiler.(*libraryDecorator); ok {
558 return true
559 }
560 }
561 return false
562}
563
564func (mod *Module) CcLibraryInterface() bool {
565 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400566 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
567 // VariantIs{Static,Shared} is set.
568 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700569 return true
570 }
571 }
572 return false
573}
574
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800575func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700576 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700577 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800578 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700579 }
580 }
581 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
582}
583
584func (mod *Module) SetStatic() {
585 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700586 if library, ok := mod.compiler.(libraryInterface); ok {
587 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700588 return
589 }
590 }
591 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
592}
593
594func (mod *Module) SetShared() {
595 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700596 if library, ok := mod.compiler.(libraryInterface); ok {
597 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700598 return
599 }
600 }
601 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
602}
603
Ivan Lozano183a3212019-10-18 14:18:45 -0700604func (mod *Module) BuildStaticVariant() bool {
605 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700606 if library, ok := mod.compiler.(libraryInterface); ok {
607 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700608 }
609 }
610 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
611}
612
613func (mod *Module) BuildSharedVariant() bool {
614 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700615 if library, ok := mod.compiler.(libraryInterface); ok {
616 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700617 }
618 }
619 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
620}
621
Ivan Lozano183a3212019-10-18 14:18:45 -0700622func (mod *Module) Module() android.Module {
623 return mod
624}
625
Ivan Lozano183a3212019-10-18 14:18:45 -0700626func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900627 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
628 return mod.compiler.strippedOutputFilePath()
629 }
630 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700631}
632
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400633func (mod *Module) CoverageFiles() android.Paths {
634 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800635 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400636 }
637 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
638}
639
Jiyong Park459feca2020-12-15 11:02:21 +0900640func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900641 if !mod.EverInstallable() {
642 return false
643 }
644
Jiyong Park459feca2020-12-15 11:02:21 +0900645 // The apex variant is not installable because it is included in the APEX and won't appear
646 // in the system partition as a standalone file.
647 if !apexInfo.IsForPlatform() {
648 return false
649 }
650
Jiyong Parke54f07e2021-04-07 15:08:04 +0900651 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900652}
653
Ivan Lozano183a3212019-10-18 14:18:45 -0700654var _ cc.LinkableInterface = (*Module)(nil)
655
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656func (mod *Module) Init() android.Module {
657 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500658 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659
660 if mod.compiler != nil {
661 mod.AddProperties(mod.compiler.compilerProps()...)
662 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400663 if mod.coverage != nil {
664 mod.AddProperties(mod.coverage.props()...)
665 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200666 if mod.clippy != nil {
667 mod.AddProperties(mod.clippy.props()...)
668 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400669 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700670 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400671 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500672 if mod.sanitize != nil {
673 mod.AddProperties(mod.sanitize.props()...)
674 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400675
Ivan Lozanoffee3342019-08-27 12:03:00 -0700676 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900677 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678
679 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700680 return mod
681}
682
683func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
684 return &Module{
685 hod: hod,
686 multilib: multilib,
687 }
688}
689func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
690 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400691 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200692 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500693 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694 return module
695}
696
697type ModuleContext interface {
698 android.ModuleContext
699 ModuleContextIntf
700}
701
702type BaseModuleContext interface {
703 android.BaseModuleContext
704 ModuleContextIntf
705}
706
707type DepsContext interface {
708 android.BottomUpMutatorContext
709 ModuleContextIntf
710}
711
712type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200713 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700714 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715}
716
717type depsContext struct {
718 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719}
720
721type moduleContext struct {
722 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700723}
724
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200725type baseModuleContext struct {
726 android.BaseModuleContext
727}
728
729func (ctx *moduleContext) RustModule() *Module {
730 return ctx.Module().(*Module)
731}
732
733func (ctx *moduleContext) toolchain() config.Toolchain {
734 return ctx.RustModule().toolchain(ctx)
735}
736
737func (ctx *depsContext) RustModule() *Module {
738 return ctx.Module().(*Module)
739}
740
741func (ctx *depsContext) toolchain() config.Toolchain {
742 return ctx.RustModule().toolchain(ctx)
743}
744
745func (ctx *baseModuleContext) RustModule() *Module {
746 return ctx.Module().(*Module)
747}
748
749func (ctx *baseModuleContext) toolchain() config.Toolchain {
750 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400751}
752
753func (mod *Module) nativeCoverage() bool {
754 return mod.compiler != nil && mod.compiler.nativeCoverage()
755}
756
Ivan Lozanod7586b62021-04-01 09:49:36 -0400757func (mod *Module) EverInstallable() bool {
758 return mod.compiler != nil &&
759 // Check to see whether the module is actually ever installable.
760 mod.compiler.everInstallable()
761}
762
763func (mod *Module) Installable() *bool {
764 return mod.Properties.Installable
765}
766
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
768 if mod.cachedToolchain == nil {
769 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
770 }
771 return mod.cachedToolchain
772}
773
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200774func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
775 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
776}
777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
779}
780
781func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
782 ctx := &moduleContext{
783 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700785
Jiyong Park99644e92020-11-17 22:21:02 +0900786 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
787 if !apexInfo.IsForPlatform() {
788 mod.hideApexVariantFromMake = true
789 }
790
Ivan Lozanoffee3342019-08-27 12:03:00 -0700791 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500792 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
793
794 // Differentiate static libraries that are vendor available
795 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500796 mod.Properties.SubName += cc.VendorSuffix
797 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
798 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500799 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800
801 if !toolchain.Supported() {
802 // This toolchain's unsupported, there's nothing to do for this mod.
803 return
804 }
805
806 deps := mod.depsToPaths(ctx)
807 flags := Flags{
808 Toolchain: toolchain,
809 }
810
811 if mod.compiler != nil {
812 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400813 }
814 if mod.coverage != nil {
815 flags, deps = mod.coverage.flags(ctx, flags, deps)
816 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200817 if mod.clippy != nil {
818 flags, deps = mod.clippy.flags(ctx, flags, deps)
819 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500820 if mod.sanitize != nil {
821 flags, deps = mod.sanitize.flags(ctx, flags, deps)
822 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400823
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200824 // SourceProvider needs to call GenerateSource() before compiler calls
825 // compile() so it can provide the source. A SourceProvider has
826 // multiple variants (e.g. source, rlib, dylib). Only the "source"
827 // variant is responsible for effectively generating the source. The
828 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400829 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200830 if mod.compiler.(libraryInterface).source() {
831 mod.sourceProvider.GenerateSource(ctx, deps)
832 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
833 } else {
834 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
835 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700836 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200837 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400838 }
839
840 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100841 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900842 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900843 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200844 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900845
Dan Albert06feee92021-03-19 15:06:02 -0700846 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
847
Ivan Lozano1921e802021-05-20 13:39:16 -0400848 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
849 // RECOVERY_SNAPSHOT_VERSION is current.
850 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
851 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
852 lib.collectHeadersForSnapshot(ctx, deps)
853 }
854 }
855
Jiyong Park459feca2020-12-15 11:02:21 +0900856 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
857 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200858 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400859 }
Chris Wailes74be7642021-07-22 16:20:28 -0700860
861 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 }
863}
864
865func (mod *Module) deps(ctx DepsContext) Deps {
866 deps := Deps{}
867
868 if mod.compiler != nil {
869 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400870 }
871 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700872 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 }
874
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400875 if mod.coverage != nil {
876 deps = mod.coverage.deps(ctx, deps)
877 }
878
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500879 if mod.sanitize != nil {
880 deps = mod.sanitize.deps(ctx, deps)
881 }
882
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
884 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700885 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
887 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
888 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400889 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 return deps
891
892}
893
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894type dependencyTag struct {
895 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800896 name string
897 library bool
898 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899}
900
Jiyong Park65b62242020-11-25 12:44:59 +0900901// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
902// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
903func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800904 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900905}
906
907var _ android.InstallNeededDependencyTag = dependencyTag{}
908
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400910 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
911 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
912 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800913 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400914 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200915 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700916)
917
Jiyong Park99644e92020-11-17 22:21:02 +0900918func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
919 tag, ok := depTag.(dependencyTag)
920 return ok && tag == dylibDepTag
921}
922
Jiyong Park94e22fd2021-04-08 18:19:15 +0900923func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
924 tag, ok := depTag.(dependencyTag)
925 return ok && tag == rlibDepTag
926}
927
Matthew Maurer0f003b12020-06-29 14:34:06 -0700928type autoDep struct {
929 variation string
930 depTag dependencyTag
931}
932
933var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200934 rlibVariation = "rlib"
935 dylibVariation = "dylib"
936 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
937 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700938)
939
940type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500941 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700942}
943
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400944func (mod *Module) begin(ctx BaseModuleContext) {
945 if mod.coverage != nil {
946 mod.coverage.begin(ctx)
947 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500948 if mod.sanitize != nil {
949 mod.sanitize.begin(ctx)
950 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400951}
952
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
954 var depPaths PathDeps
955
956 directRlibDeps := []*Module{}
957 directDylibDeps := []*Module{}
958 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900959 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700960 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400961 directSrcProvidersDeps := []*Module{}
962 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963
964 ctx.VisitDirectDeps(func(dep android.Module) {
965 depName := ctx.OtherModuleName(dep)
966 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400967
Ivan Lozano89435d12020-07-31 11:01:18 -0400968 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700969 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400970 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700971
Ivan Lozanoffee3342019-08-27 12:03:00 -0700972 switch depTag {
973 case dylibDepTag:
974 dylib, ok := rustDep.compiler.(libraryInterface)
975 if !ok || !dylib.dylib() {
976 ctx.ModuleErrorf("mod %q not an dylib library", depName)
977 return
978 }
979 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400980 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700981 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400982
Ivan Lozanoffee3342019-08-27 12:03:00 -0700983 rlib, ok := rustDep.compiler.(libraryInterface)
984 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400985 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986 return
987 }
988 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400989 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990 case procMacroDepTag:
991 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400992 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +0100993 }
994
995 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400996 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
997 // OS/Arch variant is used.
998 var helper string
999 if ctx.Host() {
1000 helper = "missing 'host_supported'?"
1001 } else {
1002 helper = "device module defined?"
1003 }
1004
1005 if dep.Target().Os != ctx.Os() {
1006 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1007 return
1008 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1009 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1010 return
1011 }
1012 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001013 }
1014
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001015 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001016 if depTag != procMacroDepTag {
1017 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1018 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1019 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1020 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001021 }
1022
Ivan Lozanoffee3342019-08-27 12:03:00 -07001023 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001024 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001025 if !linkFile.Valid() {
1026 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1027 depName, ctx.ModuleName())
1028 return
1029 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001031 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1032 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033 }
1034 }
1035
Ivan Lozano89435d12020-07-31 11:01:18 -04001036 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001037 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001038 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001039 if _, ok := ccDep.(*Module); !ok {
1040 if ccDep.Module().Target().Os != ctx.Os() {
1041 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1042 return
1043 }
1044 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1045 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1046 return
1047 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001048 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001049 linkObject := ccDep.OutputFile()
1050 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001051
Ivan Lozano2093af22020-08-25 12:48:19 -04001052 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001053 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1054 }
1055
1056 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001057 switch {
1058 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001059 if cc.IsWholeStaticLib(depTag) {
1060 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1061 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001062 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1063 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001064 } else {
1065 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 -05001066 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001067 }
1068
1069 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1070 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001071 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001072 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1073
Colin Cross0de8a1e2020-09-18 14:15:30 -07001074 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1075 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1076 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1077 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1078 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001079 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001080 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001081 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001082 // For the shared lib dependencies, we may link to the stub variant
1083 // of the dependency depending on the context (e.g. if this
1084 // dependency crosses the APEX boundaries).
1085 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1086
1087 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1088 // object (either from stub or non-stub) to use.
1089 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1090 linkPath = linkPathFromFilePath(linkObject.Path())
1091
Ivan Lozanoffee3342019-08-27 12:03:00 -07001092 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001093 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001094 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1095 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1096 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1097 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001098 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001099
1100 // Record baseLibName for snapshots.
1101 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001102 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001103
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001104 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001105 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001106 case cc.IsHeaderDepTag(depTag):
1107 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1108 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1109 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1110 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001111 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001112 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001113 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001114 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001115 }
1116
1117 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001118 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1119 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001120 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001121 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001122 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001123
1124 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001125 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001126 // These are usually genrules which don't have per-target variants.
1127 directSrcDeps = append(directSrcDeps, srcDep)
1128 }
1129 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001130 })
1131
1132 var rlibDepFiles RustLibraries
1133 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001134 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001135 }
1136 var dylibDepFiles RustLibraries
1137 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001138 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001139 }
1140 var procMacroDepFiles RustLibraries
1141 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001142 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001143 }
1144
1145 var staticLibDepFiles android.Paths
1146 for _, dep := range directStaticLibDeps {
1147 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1148 }
1149
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001150 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001151 var sharedLibDepFiles android.Paths
1152 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001153 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1154 if dep.TableOfContents.Valid() {
1155 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001156 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001157 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001158 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001159 }
1160
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001161 var srcProviderDepFiles android.Paths
1162 for _, dep := range directSrcProvidersDeps {
1163 srcs, _ := dep.OutputFiles("")
1164 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1165 }
1166 for _, dep := range directSrcDeps {
1167 srcs := dep.Srcs()
1168 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1169 }
1170
Ivan Lozanoffee3342019-08-27 12:03:00 -07001171 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1172 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1173 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001174 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001175 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1176 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001177 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001178
1179 // Dedup exported flags from dependencies
1180 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001181 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001183 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1184 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1185 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001186
1187 return depPaths
1188}
1189
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001190func (mod *Module) InstallInData() bool {
1191 if mod.compiler == nil {
1192 return false
1193 }
1194 return mod.compiler.inData()
1195}
1196
Ivan Lozanoffee3342019-08-27 12:03:00 -07001197func linkPathFromFilePath(filepath android.Path) string {
1198 return strings.Split(filepath.String(), filepath.Base())[0]
1199}
Ivan Lozanod648c432020-02-06 12:05:10 -05001200
Ivan Lozanoffee3342019-08-27 12:03:00 -07001201func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1202 ctx := &depsContext{
1203 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001204 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001205
1206 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001207 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001208 var snapshotInfo *cc.SnapshotInfo
1209
1210 if ctx.Os() == android.Android {
1211 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1212 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001213
Ivan Lozano2b081132020-09-08 12:46:52 -04001214 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001215 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001216 stdLinkage = "rlib-std"
1217 }
1218
1219 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001220
Ivan Lozano2b081132020-09-08 12:46:52 -04001221 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1222 rlibDepVariations = append(rlibDepVariations,
1223 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1224 }
1225
Ivan Lozano1921e802021-05-20 13:39:16 -04001226 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001227 for _, lib := range deps.Rlibs {
1228 depTag := rlibDepTag
1229 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1230
1231 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1232 {Mutator: "rust_libraries", Variation: rlibVariation},
1233 }...), depTag, lib)
1234 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001235
1236 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001237 actx.AddVariationDependencies(
1238 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001239 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001240 dylibDepTag, deps.Dylibs...)
1241
Ivan Lozano1921e802021-05-20 13:39:16 -04001242 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001243 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1244 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001245 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001246 for _, lib := range deps.Rustlibs {
1247 depTag := autoDep.depTag
1248 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1249 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1250 {Mutator: "rust_libraries", Variation: autoDep.variation},
1251 }...), depTag, lib)
1252 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001253 } else {
1254 actx.AddVariationDependencies(
1255 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1256 autoDep.depTag, deps.Rustlibs...)
1257 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001258 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001259
1260 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001261 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001262 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001263 for _, lib := range deps.Stdlibs {
1264 depTag := rlibDepTag
1265 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1266
1267 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1268 depTag, lib)
1269 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001270 } else {
1271 actx.AddVariationDependencies(
1272 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1273 dylibDepTag, deps.Stdlibs...)
1274 }
1275 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001276
1277 for _, lib := range deps.SharedLibs {
1278 depTag := cc.SharedDepTag()
1279 name, version := cc.StubsLibNameAndVersion(lib)
1280
1281 variations := []blueprint.Variation{
1282 {Mutator: "link", Variation: "shared"},
1283 }
1284 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1285 }
1286
1287 for _, lib := range deps.WholeStaticLibs {
1288 depTag := cc.StaticDepTag(true)
1289 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1290
1291 actx.AddVariationDependencies([]blueprint.Variation{
1292 {Mutator: "link", Variation: "static"},
1293 }, depTag, lib)
1294 }
1295
1296 for _, lib := range deps.StaticLibs {
1297 depTag := cc.StaticDepTag(false)
1298 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1299
1300 actx.AddVariationDependencies([]blueprint.Variation{
1301 {Mutator: "link", Variation: "static"},
1302 }, depTag, lib)
1303 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001304
Zach Johnson3df4e632020-11-06 11:56:27 -08001305 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1306
Colin Cross565cafd2020-09-25 18:47:38 -07001307 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001308 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001309 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1310 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001311 }
1312 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001313 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1314 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001315 }
1316
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001317 if mod.sourceProvider != nil {
1318 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1319 bindgen.Properties.Custom_bindgen != "" {
1320 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1321 bindgen.Properties.Custom_bindgen)
1322 }
1323 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001324
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001325 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001326 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001327}
1328
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001329func BeginMutator(ctx android.BottomUpMutatorContext) {
1330 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1331 mod.beginMutator(ctx)
1332 }
1333}
1334
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001335func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1336 ctx := &baseModuleContext{
1337 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001338 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001339
1340 mod.begin(ctx)
1341}
1342
Ivan Lozanoffee3342019-08-27 12:03:00 -07001343func (mod *Module) Name() string {
1344 name := mod.ModuleBase.Name()
1345 if p, ok := mod.compiler.(interface {
1346 Name(string) string
1347 }); ok {
1348 name = p.Name(name)
1349 }
1350 return name
1351}
1352
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001353func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001354 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001355 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001356 }
1357}
1358
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001359var _ android.HostToolProvider = (*Module)(nil)
1360
1361func (mod *Module) HostToolPath() android.OptionalPath {
1362 if !mod.Host() {
1363 return android.OptionalPath{}
1364 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001365 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1366 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001367 }
1368 return android.OptionalPath{}
1369}
1370
Jiyong Park99644e92020-11-17 22:21:02 +09001371var _ android.ApexModule = (*Module)(nil)
1372
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001373func (mod *Module) minSdkVersion() string {
1374 return String(mod.Properties.Min_sdk_version)
1375}
1376
Jiyong Park45bf82e2020-12-15 22:29:02 +09001377var _ android.ApexModule = (*Module)(nil)
1378
1379// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001380func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001381 minSdkVersion := mod.minSdkVersion()
1382 if minSdkVersion == "apex_inherit" {
1383 return nil
1384 }
1385 if minSdkVersion == "" {
1386 return fmt.Errorf("min_sdk_version is not specificed")
1387 }
1388
1389 // Not using nativeApiLevelFromUser because the context here is not
1390 // necessarily a native context.
1391 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1392 if err != nil {
1393 return err
1394 }
1395
1396 if ver.GreaterThan(sdkVersion) {
1397 return fmt.Errorf("newer SDK(%v)", ver)
1398 }
Jiyong Park99644e92020-11-17 22:21:02 +09001399 return nil
1400}
1401
Jiyong Park45bf82e2020-12-15 22:29:02 +09001402// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001403func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1404 depTag := ctx.OtherModuleDependencyTag(dep)
1405
1406 if ccm, ok := dep.(*cc.Module); ok {
1407 if ccm.HasStubsVariants() {
1408 if cc.IsSharedDepTag(depTag) {
1409 // dynamic dep to a stubs lib crosses APEX boundary
1410 return false
1411 }
1412 if cc.IsRuntimeDepTag(depTag) {
1413 // runtime dep to a stubs lib also crosses APEX boundary
1414 return false
1415 }
1416
1417 if cc.IsHeaderDepTag(depTag) {
1418 return false
1419 }
1420 }
1421 if mod.Static() && cc.IsSharedDepTag(depTag) {
1422 // shared_lib dependency from a static lib is considered as crossing
1423 // the APEX boundary because the dependency doesn't actually is
1424 // linked; the dependency is used only during the compilation phase.
1425 return false
1426 }
1427 }
1428
1429 if depTag == procMacroDepTag {
1430 return false
1431 }
1432
1433 return true
1434}
1435
1436// Overrides ApexModule.IsInstallabeToApex()
1437func (mod *Module) IsInstallableToApex() bool {
1438 if mod.compiler != nil {
1439 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1440 return true
1441 }
1442 if _, ok := mod.compiler.(*binaryDecorator); ok {
1443 return true
1444 }
1445 }
1446 return false
1447}
1448
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001449// If a library file has a "lib" prefix, extract the library name without the prefix.
1450func libNameFromFilePath(filepath android.Path) (string, bool) {
1451 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1452 if strings.HasPrefix(libName, "lib") {
1453 libName = libName[3:]
1454 return libName, true
1455 }
1456 return "", false
1457}
1458
Ivan Lozanoffee3342019-08-27 12:03:00 -07001459var Bool = proptools.Bool
1460var BoolDefault = proptools.BoolDefault
1461var String = proptools.String
1462var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001463
1464var _ android.OutputFileProducer = (*Module)(nil)