blob: 46c8f250cc9aa84c0c25563e08d1ef6f914d9a34 [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 {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900622 if !mod.EverInstallable() {
623 return false
624 }
625
Jiyong Park459feca2020-12-15 11:02:21 +0900626 // The apex variant is not installable because it is included in the APEX and won't appear
627 // in the system partition as a standalone file.
628 if !apexInfo.IsForPlatform() {
629 return false
630 }
631
Jiyong Parke54f07e2021-04-07 15:08:04 +0900632 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900633}
634
Ivan Lozano183a3212019-10-18 14:18:45 -0700635var _ cc.LinkableInterface = (*Module)(nil)
636
Ivan Lozanoffee3342019-08-27 12:03:00 -0700637func (mod *Module) Init() android.Module {
638 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500639 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700640
641 if mod.compiler != nil {
642 mod.AddProperties(mod.compiler.compilerProps()...)
643 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400644 if mod.coverage != nil {
645 mod.AddProperties(mod.coverage.props()...)
646 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200647 if mod.clippy != nil {
648 mod.AddProperties(mod.clippy.props()...)
649 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400650 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700651 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400652 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500653 if mod.sanitize != nil {
654 mod.AddProperties(mod.sanitize.props()...)
655 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400656
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900658 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659
660 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661 return mod
662}
663
664func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
665 return &Module{
666 hod: hod,
667 multilib: multilib,
668 }
669}
670func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
671 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400672 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200673 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500674 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675 return module
676}
677
678type ModuleContext interface {
679 android.ModuleContext
680 ModuleContextIntf
681}
682
683type BaseModuleContext interface {
684 android.BaseModuleContext
685 ModuleContextIntf
686}
687
688type DepsContext interface {
689 android.BottomUpMutatorContext
690 ModuleContextIntf
691}
692
693type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200694 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700695 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700696}
697
698type depsContext struct {
699 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700}
701
702type moduleContext struct {
703 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704}
705
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200706type baseModuleContext struct {
707 android.BaseModuleContext
708}
709
710func (ctx *moduleContext) RustModule() *Module {
711 return ctx.Module().(*Module)
712}
713
714func (ctx *moduleContext) toolchain() config.Toolchain {
715 return ctx.RustModule().toolchain(ctx)
716}
717
718func (ctx *depsContext) RustModule() *Module {
719 return ctx.Module().(*Module)
720}
721
722func (ctx *depsContext) toolchain() config.Toolchain {
723 return ctx.RustModule().toolchain(ctx)
724}
725
726func (ctx *baseModuleContext) RustModule() *Module {
727 return ctx.Module().(*Module)
728}
729
730func (ctx *baseModuleContext) toolchain() config.Toolchain {
731 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400732}
733
734func (mod *Module) nativeCoverage() bool {
735 return mod.compiler != nil && mod.compiler.nativeCoverage()
736}
737
Ivan Lozanod7586b62021-04-01 09:49:36 -0400738func (mod *Module) EverInstallable() bool {
739 return mod.compiler != nil &&
740 // Check to see whether the module is actually ever installable.
741 mod.compiler.everInstallable()
742}
743
744func (mod *Module) Installable() *bool {
745 return mod.Properties.Installable
746}
747
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
749 if mod.cachedToolchain == nil {
750 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
751 }
752 return mod.cachedToolchain
753}
754
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200755func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
756 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
757}
758
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
760}
761
762func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
763 ctx := &moduleContext{
764 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700765 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700766
Jiyong Park99644e92020-11-17 22:21:02 +0900767 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
768 if !apexInfo.IsForPlatform() {
769 mod.hideApexVariantFromMake = true
770 }
771
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500773 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
774
775 // Differentiate static libraries that are vendor available
776 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500777 mod.Properties.SubName += cc.VendorSuffix
778 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
779 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500780 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781
782 if !toolchain.Supported() {
783 // This toolchain's unsupported, there's nothing to do for this mod.
784 return
785 }
786
787 deps := mod.depsToPaths(ctx)
788 flags := Flags{
789 Toolchain: toolchain,
790 }
791
792 if mod.compiler != nil {
793 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400794 }
795 if mod.coverage != nil {
796 flags, deps = mod.coverage.flags(ctx, flags, deps)
797 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200798 if mod.clippy != nil {
799 flags, deps = mod.clippy.flags(ctx, flags, deps)
800 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500801 if mod.sanitize != nil {
802 flags, deps = mod.sanitize.flags(ctx, flags, deps)
803 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400804
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200805 // SourceProvider needs to call GenerateSource() before compiler calls
806 // compile() so it can provide the source. A SourceProvider has
807 // multiple variants (e.g. source, rlib, dylib). Only the "source"
808 // variant is responsible for effectively generating the source. The
809 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400810 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200811 if mod.compiler.(libraryInterface).source() {
812 mod.sourceProvider.GenerateSource(ctx, deps)
813 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
814 } else {
815 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
816 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700817 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200818 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400819 }
820
821 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100822 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900823 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900824 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200825 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900826
Dan Albert06feee92021-03-19 15:06:02 -0700827 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
828
Jiyong Park459feca2020-12-15 11:02:21 +0900829 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
830 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200831 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400832 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 }
834}
835
836func (mod *Module) deps(ctx DepsContext) Deps {
837 deps := Deps{}
838
839 if mod.compiler != nil {
840 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400841 }
842 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700843 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 }
845
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400846 if mod.coverage != nil {
847 deps = mod.coverage.deps(ctx, deps)
848 }
849
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500850 if mod.sanitize != nil {
851 deps = mod.sanitize.deps(ctx, deps)
852 }
853
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
855 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700856 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
858 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
859 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400860 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 return deps
862
863}
864
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865type dependencyTag struct {
866 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800867 name string
868 library bool
869 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700870}
871
Jiyong Park65b62242020-11-25 12:44:59 +0900872// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
873// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
874func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800875 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900876}
877
878var _ android.InstallNeededDependencyTag = dependencyTag{}
879
Ivan Lozanoffee3342019-08-27 12:03:00 -0700880var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400881 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
882 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
883 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800884 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400885 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200886 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887)
888
Jiyong Park99644e92020-11-17 22:21:02 +0900889func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
890 tag, ok := depTag.(dependencyTag)
891 return ok && tag == dylibDepTag
892}
893
Jiyong Park94e22fd2021-04-08 18:19:15 +0900894func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
895 tag, ok := depTag.(dependencyTag)
896 return ok && tag == rlibDepTag
897}
898
Matthew Maurer0f003b12020-06-29 14:34:06 -0700899type autoDep struct {
900 variation string
901 depTag dependencyTag
902}
903
904var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200905 rlibVariation = "rlib"
906 dylibVariation = "dylib"
907 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
908 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700909)
910
911type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500912 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700913}
914
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400915func (mod *Module) begin(ctx BaseModuleContext) {
916 if mod.coverage != nil {
917 mod.coverage.begin(ctx)
918 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500919 if mod.sanitize != nil {
920 mod.sanitize.begin(ctx)
921 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400922}
923
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
925 var depPaths PathDeps
926
927 directRlibDeps := []*Module{}
928 directDylibDeps := []*Module{}
929 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700930 directSharedLibDeps := [](cc.LinkableInterface){}
931 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400932 directSrcProvidersDeps := []*Module{}
933 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934
935 ctx.VisitDirectDeps(func(dep android.Module) {
936 depName := ctx.OtherModuleName(dep)
937 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400938
Ivan Lozano89435d12020-07-31 11:01:18 -0400939 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700940 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400941 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700942
Ivan Lozanoffee3342019-08-27 12:03:00 -0700943 switch depTag {
944 case dylibDepTag:
945 dylib, ok := rustDep.compiler.(libraryInterface)
946 if !ok || !dylib.dylib() {
947 ctx.ModuleErrorf("mod %q not an dylib library", depName)
948 return
949 }
950 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400951 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700952 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400953
Ivan Lozanoffee3342019-08-27 12:03:00 -0700954 rlib, ok := rustDep.compiler.(libraryInterface)
955 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400956 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 return
958 }
959 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400960 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961 case procMacroDepTag:
962 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400963 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400964 case android.SourceDepTag:
965 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
966 // OS/Arch variant is used.
967 var helper string
968 if ctx.Host() {
969 helper = "missing 'host_supported'?"
970 } else {
971 helper = "device module defined?"
972 }
973
974 if dep.Target().Os != ctx.Os() {
975 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
976 return
977 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
978 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
979 return
980 }
981 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982 }
983
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400984 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700985 if depTag != procMacroDepTag {
986 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
987 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
988 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
989 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990 }
991
Ivan Lozanoffee3342019-08-27 12:03:00 -0700992 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900993 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400994 if !linkFile.Valid() {
995 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
996 depName, ctx.ModuleName())
997 return
998 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700999 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001000 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1001 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002 }
1003 }
1004
Ivan Lozano89435d12020-07-31 11:01:18 -04001005 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001006 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001007 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001008 if _, ok := ccDep.(*Module); !ok {
1009 if ccDep.Module().Target().Os != ctx.Os() {
1010 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1011 return
1012 }
1013 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1014 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1015 return
1016 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001017 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001018 linkObject := ccDep.OutputFile()
1019 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001020
Ivan Lozano2093af22020-08-25 12:48:19 -04001021 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001022 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1023 }
1024
1025 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001026 switch {
1027 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001028 if cc.IsWholeStaticLib(depTag) {
1029 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1030 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001031 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1032 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001033 } else {
1034 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 -05001035 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001036 }
1037
1038 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1039 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001040 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001041 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1042
Colin Cross0de8a1e2020-09-18 14:15:30 -07001043 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1044 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1045 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1046 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1047 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001048 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001049 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001050 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001052 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001053 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1054 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1055 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1056 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1057 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001058 directSharedLibDeps = append(directSharedLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001059 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001060 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001061 case cc.IsHeaderDepTag(depTag):
1062 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1063 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1064 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1065 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001066 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001067 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001068 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001069 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070 }
1071
1072 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001073 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1074 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001075 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001076 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001077 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001078
1079 if srcDep, ok := dep.(android.SourceFileProducer); ok {
1080 switch depTag {
1081 case android.SourceDepTag:
1082 // These are usually genrules which don't have per-target variants.
1083 directSrcDeps = append(directSrcDeps, srcDep)
1084 }
1085 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086 })
1087
1088 var rlibDepFiles RustLibraries
1089 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001090 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001091 }
1092 var dylibDepFiles RustLibraries
1093 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001094 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001095 }
1096 var procMacroDepFiles RustLibraries
1097 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001098 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001099 }
1100
1101 var staticLibDepFiles android.Paths
1102 for _, dep := range directStaticLibDeps {
1103 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1104 }
1105
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001106 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001107 var sharedLibDepFiles android.Paths
1108 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001109 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1110 if dep.Toc().Valid() {
1111 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1112 } else {
1113 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1114 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001115 }
1116
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001117 var srcProviderDepFiles android.Paths
1118 for _, dep := range directSrcProvidersDeps {
1119 srcs, _ := dep.OutputFiles("")
1120 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1121 }
1122 for _, dep := range directSrcDeps {
1123 srcs := dep.Srcs()
1124 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1125 }
1126
Ivan Lozanoffee3342019-08-27 12:03:00 -07001127 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1128 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1129 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001130 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001131 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1132 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001133 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134
1135 // Dedup exported flags from dependencies
1136 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001137 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001138 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001139 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1140 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1141 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001142
1143 return depPaths
1144}
1145
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001146func (mod *Module) InstallInData() bool {
1147 if mod.compiler == nil {
1148 return false
1149 }
1150 return mod.compiler.inData()
1151}
1152
Ivan Lozanoffee3342019-08-27 12:03:00 -07001153func linkPathFromFilePath(filepath android.Path) string {
1154 return strings.Split(filepath.String(), filepath.Base())[0]
1155}
Ivan Lozanod648c432020-02-06 12:05:10 -05001156
Ivan Lozanoffee3342019-08-27 12:03:00 -07001157func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1158 ctx := &depsContext{
1159 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001160 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001161
1162 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001163 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001164
Ivan Lozano2b081132020-09-08 12:46:52 -04001165 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001166 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001167 stdLinkage = "rlib-std"
1168 }
1169
1170 rlibDepVariations := commonDepVariations
1171 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1172 rlibDepVariations = append(rlibDepVariations,
1173 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1174 }
1175
Ivan Lozano52767be2019-10-18 14:49:46 -07001176 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001177 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001178 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001179 rlibDepTag, deps.Rlibs...)
1180 actx.AddVariationDependencies(
1181 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001182 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001183 dylibDepTag, deps.Dylibs...)
1184
Ivan Lozano042504f2020-08-18 14:31:23 -04001185 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1186 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001187 if autoDep.depTag == rlibDepTag {
1188 actx.AddVariationDependencies(
1189 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1190 autoDep.depTag, deps.Rustlibs...)
1191 } else {
1192 actx.AddVariationDependencies(
1193 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1194 autoDep.depTag, deps.Rustlibs...)
1195 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001196 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001197 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001198 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001199 actx.AddVariationDependencies(
1200 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1201 rlibDepTag, deps.Stdlibs...)
1202 } else {
1203 actx.AddVariationDependencies(
1204 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1205 dylibDepTag, deps.Stdlibs...)
1206 }
1207 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001208 actx.AddVariationDependencies(append(commonDepVariations,
1209 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001210 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001211 actx.AddVariationDependencies(append(commonDepVariations,
1212 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001213 cc.StaticDepTag(false), deps.StaticLibs...)
1214 actx.AddVariationDependencies(append(commonDepVariations,
1215 blueprint.Variation{Mutator: "link", Variation: "static"}),
1216 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001217
Zach Johnson3df4e632020-11-06 11:56:27 -08001218 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1219
Colin Cross565cafd2020-09-25 18:47:38 -07001220 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001221 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001222 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001223 }
1224 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001225 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001226 }
1227
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001228 if mod.sourceProvider != nil {
1229 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1230 bindgen.Properties.Custom_bindgen != "" {
1231 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1232 bindgen.Properties.Custom_bindgen)
1233 }
1234 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001235 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001236 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001237}
1238
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001239func BeginMutator(ctx android.BottomUpMutatorContext) {
1240 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1241 mod.beginMutator(ctx)
1242 }
1243}
1244
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001245func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1246 ctx := &baseModuleContext{
1247 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001248 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001249
1250 mod.begin(ctx)
1251}
1252
Ivan Lozanoffee3342019-08-27 12:03:00 -07001253func (mod *Module) Name() string {
1254 name := mod.ModuleBase.Name()
1255 if p, ok := mod.compiler.(interface {
1256 Name(string) string
1257 }); ok {
1258 name = p.Name(name)
1259 }
1260 return name
1261}
1262
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001263func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001264 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001265 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001266 }
1267}
1268
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001269var _ android.HostToolProvider = (*Module)(nil)
1270
1271func (mod *Module) HostToolPath() android.OptionalPath {
1272 if !mod.Host() {
1273 return android.OptionalPath{}
1274 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001275 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1276 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001277 }
1278 return android.OptionalPath{}
1279}
1280
Jiyong Park99644e92020-11-17 22:21:02 +09001281var _ android.ApexModule = (*Module)(nil)
1282
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001283func (mod *Module) minSdkVersion() string {
1284 return String(mod.Properties.Min_sdk_version)
1285}
1286
Jiyong Park45bf82e2020-12-15 22:29:02 +09001287var _ android.ApexModule = (*Module)(nil)
1288
1289// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001290func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001291 minSdkVersion := mod.minSdkVersion()
1292 if minSdkVersion == "apex_inherit" {
1293 return nil
1294 }
1295 if minSdkVersion == "" {
1296 return fmt.Errorf("min_sdk_version is not specificed")
1297 }
1298
1299 // Not using nativeApiLevelFromUser because the context here is not
1300 // necessarily a native context.
1301 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1302 if err != nil {
1303 return err
1304 }
1305
1306 if ver.GreaterThan(sdkVersion) {
1307 return fmt.Errorf("newer SDK(%v)", ver)
1308 }
Jiyong Park99644e92020-11-17 22:21:02 +09001309 return nil
1310}
1311
Jiyong Park45bf82e2020-12-15 22:29:02 +09001312// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001313func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1314 depTag := ctx.OtherModuleDependencyTag(dep)
1315
1316 if ccm, ok := dep.(*cc.Module); ok {
1317 if ccm.HasStubsVariants() {
1318 if cc.IsSharedDepTag(depTag) {
1319 // dynamic dep to a stubs lib crosses APEX boundary
1320 return false
1321 }
1322 if cc.IsRuntimeDepTag(depTag) {
1323 // runtime dep to a stubs lib also crosses APEX boundary
1324 return false
1325 }
1326
1327 if cc.IsHeaderDepTag(depTag) {
1328 return false
1329 }
1330 }
1331 if mod.Static() && cc.IsSharedDepTag(depTag) {
1332 // shared_lib dependency from a static lib is considered as crossing
1333 // the APEX boundary because the dependency doesn't actually is
1334 // linked; the dependency is used only during the compilation phase.
1335 return false
1336 }
1337 }
1338
1339 if depTag == procMacroDepTag {
1340 return false
1341 }
1342
1343 return true
1344}
1345
1346// Overrides ApexModule.IsInstallabeToApex()
1347func (mod *Module) IsInstallableToApex() bool {
1348 if mod.compiler != nil {
1349 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1350 return true
1351 }
1352 if _, ok := mod.compiler.(*binaryDecorator); ok {
1353 return true
1354 }
1355 }
1356 return false
1357}
1358
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001359// If a library file has a "lib" prefix, extract the library name without the prefix.
1360func libNameFromFilePath(filepath android.Path) (string, bool) {
1361 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1362 if strings.HasPrefix(libName, "lib") {
1363 libName = libName[3:]
1364 return libName, true
1365 }
1366 return "", false
1367}
1368
Ivan Lozanoffee3342019-08-27 12:03:00 -07001369var Bool = proptools.Bool
1370var BoolDefault = proptools.BoolDefault
1371var String = proptools.String
1372var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001373
1374var _ android.OutputFileProducer = (*Module)(nil)