blob: f068b3d7b82b2faf944ff8502e23a070847ed505 [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 Lozanoe169ad72019-09-18 08:42:54 -070038 NotIn(config.RustAllowedPaths...).
39 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
88 // Make this module available when building for vendor ramdisk.
89 // On device without a dedicated recovery partition, the module is only
90 // available after switching root into
91 // /first_stage_ramdisk. To expose the module before switching root, install
92 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
93 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040094
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050095 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
96 Min_sdk_version *string
97
Ivan Lozano43845682020-07-09 21:03:28 -040098 PreventInstall bool
99 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400100 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101}
102
103type Module struct {
104 android.ModuleBase
105 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +0900106 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107
Ivan Lozano6a884432020-12-02 09:15:16 -0500108 VendorProperties cc.VendorProperties
109
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110 Properties BaseProperties
111
112 hod android.HostOrDeviceSupported
113 multilib android.Multilib
114
Ivan Lozano6a884432020-12-02 09:15:16 -0500115 makeLinkType string
116
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400118 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200119 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500120 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400122 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700123 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400124
Jiyong Parke54f07e2021-04-07 15:08:04 +0900125 // Unstripped output. This is usually used when this module is linked to another module
126 // as a library. The stripped output which is used for installation can be found via
127 // compiler.strippedOutputFile if it exists.
128 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700129 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900130
131 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700132}
133
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500134func (mod *Module) Header() bool {
135 //TODO: If Rust libraries provide header variants, this needs to be updated.
136 return false
137}
138
139func (mod *Module) SetPreventInstall() {
140 mod.Properties.PreventInstall = true
141}
142
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500143func (mod *Module) SetHideFromMake() {
144 mod.Properties.HideFromMake = true
145}
146
Ivan Lozanod7586b62021-04-01 09:49:36 -0400147func (c *Module) HiddenFromMake() bool {
148 return c.Properties.HideFromMake
149}
150
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500151func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500152 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
153 // nil since we need compiler to actually sanitize.
154 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500155}
156
157func (mod *Module) IsDependencyRoot() bool {
158 if mod.compiler != nil {
159 return mod.compiler.isDependencyRoot()
160 }
161 panic("IsDependencyRoot called on a non-compiler Rust module")
162}
163
164func (mod *Module) IsPrebuilt() bool {
165 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
166 return true
167 }
168 return false
169}
170
Ivan Lozano43845682020-07-09 21:03:28 -0400171func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
172 switch tag {
173 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700174 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400175 return mod.sourceProvider.Srcs(), nil
176 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900177 if mod.OutputFile().Valid() {
178 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400179 }
180 return android.Paths{}, nil
181 }
182 default:
183 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
184 }
185}
186
Ivan Lozano52767be2019-10-18 14:49:46 -0700187func (mod *Module) SelectedStl() string {
188 return ""
189}
190
Ivan Lozano2b262972019-11-21 12:30:50 -0800191func (mod *Module) NonCcVariants() bool {
192 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400193 if _, ok := mod.compiler.(libraryInterface); ok {
194 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800195 }
196 }
197 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
198}
199
Ivan Lozano52767be2019-10-18 14:49:46 -0700200func (mod *Module) Static() bool {
201 if mod.compiler != nil {
202 if library, ok := mod.compiler.(libraryInterface); ok {
203 return library.static()
204 }
205 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400206 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700207}
208
209func (mod *Module) Shared() bool {
210 if mod.compiler != nil {
211 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400212 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700213 }
214 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400215 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700216}
217
Ivan Lozanod7586b62021-04-01 09:49:36 -0400218func (mod *Module) Dylib() bool {
219 if mod.compiler != nil {
220 if library, ok := mod.compiler.(libraryInterface); ok {
221 return library.dylib()
222 }
223 }
224 return false
225}
226
227func (mod *Module) Rlib() bool {
228 if mod.compiler != nil {
229 if library, ok := mod.compiler.(libraryInterface); ok {
230 return library.rlib()
231 }
232 }
233 return false
234}
235
236func (mod *Module) Binary() bool {
237 if mod.compiler != nil {
238 if _, ok := mod.compiler.(*binaryDecorator); ok {
239 return true
240 }
241 }
242 return false
243}
244
245func (mod *Module) Object() bool {
246 // Rust has no modules which produce only object files.
247 return false
248}
249
Ivan Lozano52767be2019-10-18 14:49:46 -0700250func (mod *Module) Toc() android.OptionalPath {
251 if mod.compiler != nil {
252 if _, ok := mod.compiler.(libraryInterface); ok {
253 return android.OptionalPath{}
254 }
255 }
256 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
257}
258
Colin Crossc511bc52020-04-07 16:50:32 +0000259func (mod *Module) UseSdk() bool {
260 return false
261}
262
Ivan Lozanod7586b62021-04-01 09:49:36 -0400263func (mod *Module) RelativeInstallPath() string {
264 if mod.compiler != nil {
265 return mod.compiler.relativeInstallPath()
266 }
267 return ""
268}
269
Ivan Lozano52767be2019-10-18 14:49:46 -0700270func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500271 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700272}
273
274func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400275 return true
276}
277
278func (mod *Module) SubName() string {
279 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700280}
281
282func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500283 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700284 return false
285}
286
Ivan Lozanof9e21722020-12-02 09:00:51 -0500287func (mod *Module) IsVndkExt() bool {
288 return false
289}
290
Ivan Lozanod7586b62021-04-01 09:49:36 -0400291func (mod *Module) IsVndkSp() bool {
292 return false
293}
294
Colin Cross127bb8b2020-12-16 16:46:01 -0800295func (c *Module) IsVndkPrivate() bool {
296 return false
297}
298
299func (c *Module) IsLlndk() bool {
300 return false
301}
302
303func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500304 return false
305}
306
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400307func (mod *Module) KernelHeadersDecorator() bool {
308 return false
309}
310
Colin Cross1f3f1302021-04-26 18:37:44 -0700311func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400312 return false
313}
314
Colin Cross5271fea2021-04-27 13:06:04 -0700315func (m *Module) NeedsVendorPublicLibraryVariants() bool {
316 return false
317}
318
Ivan Lozanod7586b62021-04-01 09:49:36 -0400319func (mod *Module) HasLlndkStubs() bool {
320 return false
321}
322
323func (mod *Module) StubsVersion() string {
324 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
325}
326
Ivan Lozano52767be2019-10-18 14:49:46 -0700327func (mod *Module) SdkVersion() string {
328 return ""
329}
330
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900331func (mod *Module) MinSdkVersion() string {
332 return ""
333}
334
Colin Crossc511bc52020-04-07 16:50:32 +0000335func (mod *Module) AlwaysSdk() bool {
336 return false
337}
338
Jiyong Park2286afd2020-06-16 21:58:53 +0900339func (mod *Module) IsSdkVariant() bool {
340 return false
341}
342
Colin Cross1348ce32020-10-01 13:37:16 -0700343func (mod *Module) SplitPerApiLevel() bool {
344 return false
345}
346
Ivan Lozanoffee3342019-08-27 12:03:00 -0700347type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400348 Dylibs []string
349 Rlibs []string
350 Rustlibs []string
351 Stdlibs []string
352 ProcMacros []string
353 SharedLibs []string
354 StaticLibs []string
355 WholeStaticLibs []string
356 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700357
358 CrtBegin, CrtEnd string
359}
360
361type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500362 DyLibs RustLibraries
363 RLibs RustLibraries
364 SharedLibs android.Paths
365 SharedLibDeps android.Paths
366 StaticLibs android.Paths
367 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500368
369 // depFlags and depLinkFlags are rustc and linker (clang) flags.
370 depFlags []string
371 depLinkFlags []string
372
373 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
374 // Both of these are exported and propagate to dependencies.
375 linkDirs []string
376 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700377
Ivan Lozano45901ed2020-07-24 16:05:01 -0400378 // Used by bindgen modules which call clang
379 depClangFlags []string
380 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400381 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400382 depSystemIncludePaths android.Paths
383
Ivan Lozanof1c84332019-09-20 11:00:37 -0700384 CrtBegin android.OptionalPath
385 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700386
387 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500388 SrcDeps android.Paths
389 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700390}
391
392type RustLibraries []RustLibrary
393
394type RustLibrary struct {
395 Path android.Path
396 CrateName string
397}
398
399type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100400 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700401 compilerFlags(ctx ModuleContext, flags Flags) Flags
402 compilerProps() []interface{}
403 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
404 compilerDeps(ctx DepsContext, deps Deps) Deps
405 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700406 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100408 // Output directory in which source-generated code from dependencies is
409 // copied. This is equivalent to Cargo's OUT_DIR variable.
410 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700411
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800412 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200413 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400415 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400416
417 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400418
419 Disabled() bool
420 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400421
Ivan Lozanodd055472020-09-28 13:22:45 -0400422 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500423 isDependencyRoot() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900424
425 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400426}
427
Matthew Maurerbb3add12020-06-25 09:34:12 -0700428type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700429 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400430 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700431}
432
433type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400434 linkDirs []string
435 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700436}
437
Matthew Maurerbb3add12020-06-25 09:34:12 -0700438func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
439 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
440}
441
Ivan Lozano2093af22020-08-25 12:48:19 -0400442func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
443 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
444}
445
Colin Cross0de8a1e2020-09-18 14:15:30 -0700446func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
447 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700448 LinkDirs: flagExporter.linkDirs,
449 LinkObjects: flagExporter.linkObjects,
450 })
451}
452
Matthew Maurerbb3add12020-06-25 09:34:12 -0700453var _ exportedFlagsProducer = (*flagExporter)(nil)
454
455func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700456 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700457}
458
Colin Cross0de8a1e2020-09-18 14:15:30 -0700459type FlagExporterInfo struct {
460 Flags []string
461 LinkDirs []string // TODO: this should be android.Paths
462 LinkObjects []string // TODO: this should be android.Paths
463}
464
465var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
466
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400467func (mod *Module) isCoverageVariant() bool {
468 return mod.coverage.Properties.IsCoverageVariant
469}
470
471var _ cc.Coverage = (*Module)(nil)
472
473func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
474 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
475}
476
Ivan Lozanod7586b62021-04-01 09:49:36 -0400477func (mod *Module) VndkVersion() string {
478 return mod.Properties.VndkVersion
479}
480
481func (mod *Module) PreventInstall() bool {
482 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400483}
484
485func (mod *Module) HideFromMake() {
486 mod.Properties.HideFromMake = true
487}
488
489func (mod *Module) MarkAsCoverageVariant(coverage bool) {
490 mod.coverage.Properties.IsCoverageVariant = coverage
491}
492
493func (mod *Module) EnableCoverageIfNeeded() {
494 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495}
496
497func defaultsFactory() android.Module {
498 return DefaultsFactory()
499}
500
501type Defaults struct {
502 android.ModuleBase
503 android.DefaultsModuleBase
504}
505
506func DefaultsFactory(props ...interface{}) android.Module {
507 module := &Defaults{}
508
509 module.AddProperties(props...)
510 module.AddProperties(
511 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500512 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100513 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400514 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700515 &BaseCompilerProperties{},
516 &BinaryCompilerProperties{},
517 &LibraryCompilerProperties{},
518 &ProcMacroCompilerProperties{},
519 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400520 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700521 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400522 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400523 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200524 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500525 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700526 )
527
528 android.InitDefaultsModule(module)
529 return module
530}
531
532func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700533 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534}
535
Ivan Lozano183a3212019-10-18 14:18:45 -0700536func (mod *Module) CcLibrary() bool {
537 if mod.compiler != nil {
538 if _, ok := mod.compiler.(*libraryDecorator); ok {
539 return true
540 }
541 }
542 return false
543}
544
545func (mod *Module) CcLibraryInterface() bool {
546 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400547 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
548 // VariantIs{Static,Shared} is set.
549 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700550 return true
551 }
552 }
553 return false
554}
555
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800556func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700557 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700558 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800559 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700560 }
561 }
562 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
563}
564
565func (mod *Module) SetStatic() {
566 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700567 if library, ok := mod.compiler.(libraryInterface); ok {
568 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700569 return
570 }
571 }
572 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
573}
574
575func (mod *Module) SetShared() {
576 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700577 if library, ok := mod.compiler.(libraryInterface); ok {
578 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700579 return
580 }
581 }
582 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
583}
584
Ivan Lozano183a3212019-10-18 14:18:45 -0700585func (mod *Module) BuildStaticVariant() bool {
586 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700587 if library, ok := mod.compiler.(libraryInterface); ok {
588 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700589 }
590 }
591 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
592}
593
594func (mod *Module) BuildSharedVariant() bool {
595 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700596 if library, ok := mod.compiler.(libraryInterface); ok {
597 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700598 }
599 }
600 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
601}
602
Ivan Lozano183a3212019-10-18 14:18:45 -0700603func (mod *Module) Module() android.Module {
604 return mod
605}
606
Ivan Lozano183a3212019-10-18 14:18:45 -0700607func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900608 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
609 return mod.compiler.strippedOutputFilePath()
610 }
611 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700612}
613
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400614func (mod *Module) CoverageFiles() android.Paths {
615 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800616 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400617 }
618 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
619}
620
Jiyong Park459feca2020-12-15 11:02:21 +0900621func (mod *Module) installable(apexInfo android.ApexInfo) bool {
622 // The apex variant is not installable because it is included in the APEX and won't appear
623 // in the system partition as a standalone file.
624 if !apexInfo.IsForPlatform() {
625 return false
626 }
627
Jiyong Parke54f07e2021-04-07 15:08:04 +0900628 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900629}
630
Ivan Lozano183a3212019-10-18 14:18:45 -0700631var _ cc.LinkableInterface = (*Module)(nil)
632
Ivan Lozanoffee3342019-08-27 12:03:00 -0700633func (mod *Module) Init() android.Module {
634 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500635 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700636
637 if mod.compiler != nil {
638 mod.AddProperties(mod.compiler.compilerProps()...)
639 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400640 if mod.coverage != nil {
641 mod.AddProperties(mod.coverage.props()...)
642 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200643 if mod.clippy != nil {
644 mod.AddProperties(mod.clippy.props()...)
645 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400646 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700647 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400648 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500649 if mod.sanitize != nil {
650 mod.AddProperties(mod.sanitize.props()...)
651 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400652
Ivan Lozanoffee3342019-08-27 12:03:00 -0700653 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900654 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655
656 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657 return mod
658}
659
660func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
661 return &Module{
662 hod: hod,
663 multilib: multilib,
664 }
665}
666func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
667 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400668 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200669 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500670 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700671 return module
672}
673
674type ModuleContext interface {
675 android.ModuleContext
676 ModuleContextIntf
677}
678
679type BaseModuleContext interface {
680 android.BaseModuleContext
681 ModuleContextIntf
682}
683
684type DepsContext interface {
685 android.BottomUpMutatorContext
686 ModuleContextIntf
687}
688
689type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200690 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692}
693
694type depsContext struct {
695 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700696}
697
698type moduleContext struct {
699 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700}
701
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200702type baseModuleContext struct {
703 android.BaseModuleContext
704}
705
706func (ctx *moduleContext) RustModule() *Module {
707 return ctx.Module().(*Module)
708}
709
710func (ctx *moduleContext) toolchain() config.Toolchain {
711 return ctx.RustModule().toolchain(ctx)
712}
713
714func (ctx *depsContext) RustModule() *Module {
715 return ctx.Module().(*Module)
716}
717
718func (ctx *depsContext) toolchain() config.Toolchain {
719 return ctx.RustModule().toolchain(ctx)
720}
721
722func (ctx *baseModuleContext) RustModule() *Module {
723 return ctx.Module().(*Module)
724}
725
726func (ctx *baseModuleContext) toolchain() config.Toolchain {
727 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400728}
729
730func (mod *Module) nativeCoverage() bool {
731 return mod.compiler != nil && mod.compiler.nativeCoverage()
732}
733
Ivan Lozanod7586b62021-04-01 09:49:36 -0400734func (mod *Module) EverInstallable() bool {
735 return mod.compiler != nil &&
736 // Check to see whether the module is actually ever installable.
737 mod.compiler.everInstallable()
738}
739
740func (mod *Module) Installable() *bool {
741 return mod.Properties.Installable
742}
743
Ivan Lozanoffee3342019-08-27 12:03:00 -0700744func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
745 if mod.cachedToolchain == nil {
746 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
747 }
748 return mod.cachedToolchain
749}
750
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200751func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
752 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
753}
754
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
756}
757
758func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
759 ctx := &moduleContext{
760 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762
Jiyong Park99644e92020-11-17 22:21:02 +0900763 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
764 if !apexInfo.IsForPlatform() {
765 mod.hideApexVariantFromMake = true
766 }
767
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500769 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
770
771 // Differentiate static libraries that are vendor available
772 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500773 mod.Properties.SubName += cc.VendorSuffix
774 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
775 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500776 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777
778 if !toolchain.Supported() {
779 // This toolchain's unsupported, there's nothing to do for this mod.
780 return
781 }
782
783 deps := mod.depsToPaths(ctx)
784 flags := Flags{
785 Toolchain: toolchain,
786 }
787
788 if mod.compiler != nil {
789 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400790 }
791 if mod.coverage != nil {
792 flags, deps = mod.coverage.flags(ctx, flags, deps)
793 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200794 if mod.clippy != nil {
795 flags, deps = mod.clippy.flags(ctx, flags, deps)
796 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500797 if mod.sanitize != nil {
798 flags, deps = mod.sanitize.flags(ctx, flags, deps)
799 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400800
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200801 // SourceProvider needs to call GenerateSource() before compiler calls
802 // compile() so it can provide the source. A SourceProvider has
803 // multiple variants (e.g. source, rlib, dylib). Only the "source"
804 // variant is responsible for effectively generating the source. The
805 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400806 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200807 if mod.compiler.(libraryInterface).source() {
808 mod.sourceProvider.GenerateSource(ctx, deps)
809 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
810 } else {
811 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
812 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700813 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200814 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400815 }
816
817 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100818 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900819 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900820 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200821 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900822
Dan Albert06feee92021-03-19 15:06:02 -0700823 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
824
Jiyong Park459feca2020-12-15 11:02:21 +0900825 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
826 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200827 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400828 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700829 }
830}
831
832func (mod *Module) deps(ctx DepsContext) Deps {
833 deps := Deps{}
834
835 if mod.compiler != nil {
836 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400837 }
838 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700839 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 }
841
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400842 if mod.coverage != nil {
843 deps = mod.coverage.deps(ctx, deps)
844 }
845
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500846 if mod.sanitize != nil {
847 deps = mod.sanitize.deps(ctx, deps)
848 }
849
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
851 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700852 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
854 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
855 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400856 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857 return deps
858
859}
860
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861type dependencyTag struct {
862 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800863 name string
864 library bool
865 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866}
867
Jiyong Park65b62242020-11-25 12:44:59 +0900868// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
869// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
870func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800871 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900872}
873
874var _ android.InstallNeededDependencyTag = dependencyTag{}
875
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400877 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
878 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
879 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800880 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400881 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200882 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883)
884
Jiyong Park99644e92020-11-17 22:21:02 +0900885func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
886 tag, ok := depTag.(dependencyTag)
887 return ok && tag == dylibDepTag
888}
889
Jiyong Park94e22fd2021-04-08 18:19:15 +0900890func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
891 tag, ok := depTag.(dependencyTag)
892 return ok && tag == rlibDepTag
893}
894
Matthew Maurer0f003b12020-06-29 14:34:06 -0700895type autoDep struct {
896 variation string
897 depTag dependencyTag
898}
899
900var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200901 rlibVariation = "rlib"
902 dylibVariation = "dylib"
903 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
904 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700905)
906
907type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500908 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700909}
910
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400911func (mod *Module) begin(ctx BaseModuleContext) {
912 if mod.coverage != nil {
913 mod.coverage.begin(ctx)
914 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500915 if mod.sanitize != nil {
916 mod.sanitize.begin(ctx)
917 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400918}
919
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
921 var depPaths PathDeps
922
923 directRlibDeps := []*Module{}
924 directDylibDeps := []*Module{}
925 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700926 directSharedLibDeps := [](cc.LinkableInterface){}
927 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400928 directSrcProvidersDeps := []*Module{}
929 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930
931 ctx.VisitDirectDeps(func(dep android.Module) {
932 depName := ctx.OtherModuleName(dep)
933 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400934
Ivan Lozano89435d12020-07-31 11:01:18 -0400935 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400937 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700938
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939 switch depTag {
940 case dylibDepTag:
941 dylib, ok := rustDep.compiler.(libraryInterface)
942 if !ok || !dylib.dylib() {
943 ctx.ModuleErrorf("mod %q not an dylib library", depName)
944 return
945 }
946 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400947 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400949
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950 rlib, ok := rustDep.compiler.(libraryInterface)
951 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400952 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953 return
954 }
955 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400956 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 case procMacroDepTag:
958 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400959 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400960 case android.SourceDepTag:
961 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
962 // OS/Arch variant is used.
963 var helper string
964 if ctx.Host() {
965 helper = "missing 'host_supported'?"
966 } else {
967 helper = "device module defined?"
968 }
969
970 if dep.Target().Os != ctx.Os() {
971 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
972 return
973 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
974 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
975 return
976 }
977 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700978 }
979
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400980 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700981 if depTag != procMacroDepTag {
982 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
983 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
984 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
985 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986 }
987
Ivan Lozanoffee3342019-08-27 12:03:00 -0700988 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900989 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400990 if !linkFile.Valid() {
991 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
992 depName, ctx.ModuleName())
993 return
994 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700995 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700996 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
997 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998 }
999 }
1000
Ivan Lozano89435d12020-07-31 11:01:18 -04001001 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001002 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001003 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001004 if _, ok := ccDep.(*Module); !ok {
1005 if ccDep.Module().Target().Os != ctx.Os() {
1006 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1007 return
1008 }
1009 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1010 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1011 return
1012 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001013 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001014 linkObject := ccDep.OutputFile()
1015 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001016
Ivan Lozano2093af22020-08-25 12:48:19 -04001017 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1019 }
1020
1021 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001022 switch {
1023 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001024 if cc.IsWholeStaticLib(depTag) {
1025 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1026 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001027 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1028 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001029 } else {
1030 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 -05001031 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001032 }
1033
1034 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1035 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001036 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001037 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1038
Colin Cross0de8a1e2020-09-18 14:15:30 -07001039 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1040 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1041 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1042 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1043 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001045 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001046 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001048 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001049 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1050 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1051 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1052 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1053 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054 directSharedLibDeps = append(directSharedLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001055 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001057 case cc.IsHeaderDepTag(depTag):
1058 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1059 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1060 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1061 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001062 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001063 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001064 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001065 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001066 }
1067
1068 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001069 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1070 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001071 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001072 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001073 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001074
1075 if srcDep, ok := dep.(android.SourceFileProducer); ok {
1076 switch depTag {
1077 case android.SourceDepTag:
1078 // These are usually genrules which don't have per-target variants.
1079 directSrcDeps = append(directSrcDeps, srcDep)
1080 }
1081 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001082 })
1083
1084 var rlibDepFiles RustLibraries
1085 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001086 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087 }
1088 var dylibDepFiles RustLibraries
1089 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001090 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001091 }
1092 var procMacroDepFiles RustLibraries
1093 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001094 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001095 }
1096
1097 var staticLibDepFiles android.Paths
1098 for _, dep := range directStaticLibDeps {
1099 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1100 }
1101
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001102 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001103 var sharedLibDepFiles android.Paths
1104 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001105 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1106 if dep.Toc().Valid() {
1107 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1108 } else {
1109 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1110 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001111 }
1112
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001113 var srcProviderDepFiles android.Paths
1114 for _, dep := range directSrcProvidersDeps {
1115 srcs, _ := dep.OutputFiles("")
1116 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1117 }
1118 for _, dep := range directSrcDeps {
1119 srcs := dep.Srcs()
1120 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1121 }
1122
Ivan Lozanoffee3342019-08-27 12:03:00 -07001123 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1124 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1125 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001126 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001127 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1128 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001129 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001130
1131 // Dedup exported flags from dependencies
1132 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001133 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001135 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1136 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1137 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001138
1139 return depPaths
1140}
1141
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001142func (mod *Module) InstallInData() bool {
1143 if mod.compiler == nil {
1144 return false
1145 }
1146 return mod.compiler.inData()
1147}
1148
Ivan Lozanoffee3342019-08-27 12:03:00 -07001149func linkPathFromFilePath(filepath android.Path) string {
1150 return strings.Split(filepath.String(), filepath.Base())[0]
1151}
Ivan Lozanod648c432020-02-06 12:05:10 -05001152
Ivan Lozanoffee3342019-08-27 12:03:00 -07001153func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1154 ctx := &depsContext{
1155 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001156 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001157
1158 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001159 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001160
Ivan Lozano2b081132020-09-08 12:46:52 -04001161 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001162 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001163 stdLinkage = "rlib-std"
1164 }
1165
1166 rlibDepVariations := commonDepVariations
1167 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1168 rlibDepVariations = append(rlibDepVariations,
1169 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1170 }
1171
Ivan Lozano52767be2019-10-18 14:49:46 -07001172 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001173 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001174 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001175 rlibDepTag, deps.Rlibs...)
1176 actx.AddVariationDependencies(
1177 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001178 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001179 dylibDepTag, deps.Dylibs...)
1180
Ivan Lozano042504f2020-08-18 14:31:23 -04001181 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1182 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001183 if autoDep.depTag == rlibDepTag {
1184 actx.AddVariationDependencies(
1185 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1186 autoDep.depTag, deps.Rustlibs...)
1187 } else {
1188 actx.AddVariationDependencies(
1189 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1190 autoDep.depTag, deps.Rustlibs...)
1191 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001192 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001193 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001194 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001195 actx.AddVariationDependencies(
1196 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1197 rlibDepTag, deps.Stdlibs...)
1198 } else {
1199 actx.AddVariationDependencies(
1200 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1201 dylibDepTag, deps.Stdlibs...)
1202 }
1203 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001204 actx.AddVariationDependencies(append(commonDepVariations,
1205 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001206 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001207 actx.AddVariationDependencies(append(commonDepVariations,
1208 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001209 cc.StaticDepTag(false), deps.StaticLibs...)
1210 actx.AddVariationDependencies(append(commonDepVariations,
1211 blueprint.Variation{Mutator: "link", Variation: "static"}),
1212 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001213
Zach Johnson3df4e632020-11-06 11:56:27 -08001214 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1215
Colin Cross565cafd2020-09-25 18:47:38 -07001216 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001217 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001218 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001219 }
1220 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001221 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001222 }
1223
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001224 if mod.sourceProvider != nil {
1225 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1226 bindgen.Properties.Custom_bindgen != "" {
1227 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1228 bindgen.Properties.Custom_bindgen)
1229 }
1230 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001231 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001232 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001233}
1234
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001235func BeginMutator(ctx android.BottomUpMutatorContext) {
1236 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1237 mod.beginMutator(ctx)
1238 }
1239}
1240
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001241func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1242 ctx := &baseModuleContext{
1243 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001244 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001245
1246 mod.begin(ctx)
1247}
1248
Ivan Lozanoffee3342019-08-27 12:03:00 -07001249func (mod *Module) Name() string {
1250 name := mod.ModuleBase.Name()
1251 if p, ok := mod.compiler.(interface {
1252 Name(string) string
1253 }); ok {
1254 name = p.Name(name)
1255 }
1256 return name
1257}
1258
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001259func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001260 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001261 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001262 }
1263}
1264
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001265var _ android.HostToolProvider = (*Module)(nil)
1266
1267func (mod *Module) HostToolPath() android.OptionalPath {
1268 if !mod.Host() {
1269 return android.OptionalPath{}
1270 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001271 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1272 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001273 }
1274 return android.OptionalPath{}
1275}
1276
Jiyong Park99644e92020-11-17 22:21:02 +09001277var _ android.ApexModule = (*Module)(nil)
1278
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001279func (mod *Module) minSdkVersion() string {
1280 return String(mod.Properties.Min_sdk_version)
1281}
1282
Jiyong Park45bf82e2020-12-15 22:29:02 +09001283var _ android.ApexModule = (*Module)(nil)
1284
1285// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001286func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001287 minSdkVersion := mod.minSdkVersion()
1288 if minSdkVersion == "apex_inherit" {
1289 return nil
1290 }
1291 if minSdkVersion == "" {
1292 return fmt.Errorf("min_sdk_version is not specificed")
1293 }
1294
1295 // Not using nativeApiLevelFromUser because the context here is not
1296 // necessarily a native context.
1297 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1298 if err != nil {
1299 return err
1300 }
1301
1302 if ver.GreaterThan(sdkVersion) {
1303 return fmt.Errorf("newer SDK(%v)", ver)
1304 }
Jiyong Park99644e92020-11-17 22:21:02 +09001305 return nil
1306}
1307
Jiyong Park45bf82e2020-12-15 22:29:02 +09001308// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001309func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1310 depTag := ctx.OtherModuleDependencyTag(dep)
1311
1312 if ccm, ok := dep.(*cc.Module); ok {
1313 if ccm.HasStubsVariants() {
1314 if cc.IsSharedDepTag(depTag) {
1315 // dynamic dep to a stubs lib crosses APEX boundary
1316 return false
1317 }
1318 if cc.IsRuntimeDepTag(depTag) {
1319 // runtime dep to a stubs lib also crosses APEX boundary
1320 return false
1321 }
1322
1323 if cc.IsHeaderDepTag(depTag) {
1324 return false
1325 }
1326 }
1327 if mod.Static() && cc.IsSharedDepTag(depTag) {
1328 // shared_lib dependency from a static lib is considered as crossing
1329 // the APEX boundary because the dependency doesn't actually is
1330 // linked; the dependency is used only during the compilation phase.
1331 return false
1332 }
1333 }
1334
1335 if depTag == procMacroDepTag {
1336 return false
1337 }
1338
1339 return true
1340}
1341
1342// Overrides ApexModule.IsInstallabeToApex()
1343func (mod *Module) IsInstallableToApex() bool {
1344 if mod.compiler != nil {
1345 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1346 return true
1347 }
1348 if _, ok := mod.compiler.(*binaryDecorator); ok {
1349 return true
1350 }
1351 }
1352 return false
1353}
1354
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001355// If a library file has a "lib" prefix, extract the library name without the prefix.
1356func libNameFromFilePath(filepath android.Path) (string, bool) {
1357 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1358 if strings.HasPrefix(libName, "lib") {
1359 libName = libName[3:]
1360 return libName, true
1361 }
1362 return "", false
1363}
1364
Ivan Lozanoffee3342019-08-27 12:03:00 -07001365var Bool = proptools.Bool
1366var BoolDefault = proptools.BoolDefault
1367var String = proptools.String
1368var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001369
1370var _ android.OutputFileProducer = (*Module)(nil)