blob: cdeecc191fca157c06bb4c724c9736f75c715023 [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
Colin Crossc511bc52020-04-07 16:50:32 +0000331func (mod *Module) AlwaysSdk() bool {
332 return false
333}
334
Jiyong Park2286afd2020-06-16 21:58:53 +0900335func (mod *Module) IsSdkVariant() bool {
336 return false
337}
338
Colin Cross1348ce32020-10-01 13:37:16 -0700339func (mod *Module) SplitPerApiLevel() bool {
340 return false
341}
342
Ivan Lozanoffee3342019-08-27 12:03:00 -0700343type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400344 Dylibs []string
345 Rlibs []string
346 Rustlibs []string
347 Stdlibs []string
348 ProcMacros []string
349 SharedLibs []string
350 StaticLibs []string
351 WholeStaticLibs []string
352 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700353
354 CrtBegin, CrtEnd string
355}
356
357type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500358 DyLibs RustLibraries
359 RLibs RustLibraries
360 SharedLibs android.Paths
361 SharedLibDeps android.Paths
362 StaticLibs android.Paths
363 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500364
365 // depFlags and depLinkFlags are rustc and linker (clang) flags.
366 depFlags []string
367 depLinkFlags []string
368
369 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
370 // Both of these are exported and propagate to dependencies.
371 linkDirs []string
372 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700373
Ivan Lozano45901ed2020-07-24 16:05:01 -0400374 // Used by bindgen modules which call clang
375 depClangFlags []string
376 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400377 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400378 depSystemIncludePaths android.Paths
379
Ivan Lozanof1c84332019-09-20 11:00:37 -0700380 CrtBegin android.OptionalPath
381 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700382
383 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500384 SrcDeps android.Paths
385 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700386}
387
388type RustLibraries []RustLibrary
389
390type RustLibrary struct {
391 Path android.Path
392 CrateName string
393}
394
395type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100396 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397 compilerFlags(ctx ModuleContext, flags Flags) Flags
398 compilerProps() []interface{}
399 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
400 compilerDeps(ctx DepsContext, deps Deps) Deps
401 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700402 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100404 // Output directory in which source-generated code from dependencies is
405 // copied. This is equivalent to Cargo's OUT_DIR variable.
406 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700407
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800408 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200409 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400411 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400412
413 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400414
415 Disabled() bool
416 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400417
Ivan Lozanodd055472020-09-28 13:22:45 -0400418 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500419 isDependencyRoot() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900420
421 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400422}
423
Matthew Maurerbb3add12020-06-25 09:34:12 -0700424type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700425 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400426 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700427}
428
429type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400430 linkDirs []string
431 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700432}
433
Matthew Maurerbb3add12020-06-25 09:34:12 -0700434func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
435 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
436}
437
Ivan Lozano2093af22020-08-25 12:48:19 -0400438func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
439 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
440}
441
Colin Cross0de8a1e2020-09-18 14:15:30 -0700442func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
443 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700444 LinkDirs: flagExporter.linkDirs,
445 LinkObjects: flagExporter.linkObjects,
446 })
447}
448
Matthew Maurerbb3add12020-06-25 09:34:12 -0700449var _ exportedFlagsProducer = (*flagExporter)(nil)
450
451func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700452 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700453}
454
Colin Cross0de8a1e2020-09-18 14:15:30 -0700455type FlagExporterInfo struct {
456 Flags []string
457 LinkDirs []string // TODO: this should be android.Paths
458 LinkObjects []string // TODO: this should be android.Paths
459}
460
461var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
462
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400463func (mod *Module) isCoverageVariant() bool {
464 return mod.coverage.Properties.IsCoverageVariant
465}
466
467var _ cc.Coverage = (*Module)(nil)
468
469func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
470 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
471}
472
Ivan Lozanod7586b62021-04-01 09:49:36 -0400473func (mod *Module) VndkVersion() string {
474 return mod.Properties.VndkVersion
475}
476
477func (mod *Module) PreventInstall() bool {
478 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400479}
480
481func (mod *Module) HideFromMake() {
482 mod.Properties.HideFromMake = true
483}
484
485func (mod *Module) MarkAsCoverageVariant(coverage bool) {
486 mod.coverage.Properties.IsCoverageVariant = coverage
487}
488
489func (mod *Module) EnableCoverageIfNeeded() {
490 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700491}
492
493func defaultsFactory() android.Module {
494 return DefaultsFactory()
495}
496
497type Defaults struct {
498 android.ModuleBase
499 android.DefaultsModuleBase
500}
501
502func DefaultsFactory(props ...interface{}) android.Module {
503 module := &Defaults{}
504
505 module.AddProperties(props...)
506 module.AddProperties(
507 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500508 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100509 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400510 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700511 &BaseCompilerProperties{},
512 &BinaryCompilerProperties{},
513 &LibraryCompilerProperties{},
514 &ProcMacroCompilerProperties{},
515 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400516 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700517 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400518 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400519 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200520 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500521 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700522 )
523
524 android.InitDefaultsModule(module)
525 return module
526}
527
528func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700529 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530}
531
Ivan Lozano183a3212019-10-18 14:18:45 -0700532func (mod *Module) CcLibrary() bool {
533 if mod.compiler != nil {
534 if _, ok := mod.compiler.(*libraryDecorator); ok {
535 return true
536 }
537 }
538 return false
539}
540
541func (mod *Module) CcLibraryInterface() bool {
542 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400543 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
544 // VariantIs{Static,Shared} is set.
545 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700546 return true
547 }
548 }
549 return false
550}
551
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800552func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700553 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700554 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800555 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700556 }
557 }
558 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
559}
560
561func (mod *Module) SetStatic() {
562 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700563 if library, ok := mod.compiler.(libraryInterface); ok {
564 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700565 return
566 }
567 }
568 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
569}
570
571func (mod *Module) SetShared() {
572 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700573 if library, ok := mod.compiler.(libraryInterface); ok {
574 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700575 return
576 }
577 }
578 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
579}
580
Ivan Lozano183a3212019-10-18 14:18:45 -0700581func (mod *Module) BuildStaticVariant() bool {
582 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700583 if library, ok := mod.compiler.(libraryInterface); ok {
584 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700585 }
586 }
587 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
588}
589
590func (mod *Module) BuildSharedVariant() bool {
591 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700592 if library, ok := mod.compiler.(libraryInterface); ok {
593 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700594 }
595 }
596 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
597}
598
Ivan Lozano183a3212019-10-18 14:18:45 -0700599func (mod *Module) Module() android.Module {
600 return mod
601}
602
Ivan Lozano183a3212019-10-18 14:18:45 -0700603func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900604 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
605 return mod.compiler.strippedOutputFilePath()
606 }
607 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700608}
609
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400610func (mod *Module) CoverageFiles() android.Paths {
611 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800612 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400613 }
614 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
615}
616
Jiyong Park459feca2020-12-15 11:02:21 +0900617func (mod *Module) installable(apexInfo android.ApexInfo) bool {
618 // The apex variant is not installable because it is included in the APEX and won't appear
619 // in the system partition as a standalone file.
620 if !apexInfo.IsForPlatform() {
621 return false
622 }
623
Jiyong Parke54f07e2021-04-07 15:08:04 +0900624 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900625}
626
Ivan Lozano183a3212019-10-18 14:18:45 -0700627var _ cc.LinkableInterface = (*Module)(nil)
628
Ivan Lozanoffee3342019-08-27 12:03:00 -0700629func (mod *Module) Init() android.Module {
630 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500631 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700632
633 if mod.compiler != nil {
634 mod.AddProperties(mod.compiler.compilerProps()...)
635 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400636 if mod.coverage != nil {
637 mod.AddProperties(mod.coverage.props()...)
638 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200639 if mod.clippy != nil {
640 mod.AddProperties(mod.clippy.props()...)
641 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400642 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700643 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400644 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500645 if mod.sanitize != nil {
646 mod.AddProperties(mod.sanitize.props()...)
647 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400648
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900650 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651
652 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700653 return mod
654}
655
656func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
657 return &Module{
658 hod: hod,
659 multilib: multilib,
660 }
661}
662func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
663 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400664 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200665 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500666 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667 return module
668}
669
670type ModuleContext interface {
671 android.ModuleContext
672 ModuleContextIntf
673}
674
675type BaseModuleContext interface {
676 android.BaseModuleContext
677 ModuleContextIntf
678}
679
680type DepsContext interface {
681 android.BottomUpMutatorContext
682 ModuleContextIntf
683}
684
685type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200686 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700687 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700688}
689
690type depsContext struct {
691 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692}
693
694type moduleContext struct {
695 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700696}
697
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200698type baseModuleContext struct {
699 android.BaseModuleContext
700}
701
702func (ctx *moduleContext) RustModule() *Module {
703 return ctx.Module().(*Module)
704}
705
706func (ctx *moduleContext) toolchain() config.Toolchain {
707 return ctx.RustModule().toolchain(ctx)
708}
709
710func (ctx *depsContext) RustModule() *Module {
711 return ctx.Module().(*Module)
712}
713
714func (ctx *depsContext) toolchain() config.Toolchain {
715 return ctx.RustModule().toolchain(ctx)
716}
717
718func (ctx *baseModuleContext) RustModule() *Module {
719 return ctx.Module().(*Module)
720}
721
722func (ctx *baseModuleContext) toolchain() config.Toolchain {
723 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400724}
725
726func (mod *Module) nativeCoverage() bool {
727 return mod.compiler != nil && mod.compiler.nativeCoverage()
728}
729
Ivan Lozanod7586b62021-04-01 09:49:36 -0400730func (mod *Module) EverInstallable() bool {
731 return mod.compiler != nil &&
732 // Check to see whether the module is actually ever installable.
733 mod.compiler.everInstallable()
734}
735
736func (mod *Module) Installable() *bool {
737 return mod.Properties.Installable
738}
739
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
741 if mod.cachedToolchain == nil {
742 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
743 }
744 return mod.cachedToolchain
745}
746
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200747func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
748 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
749}
750
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
752}
753
754func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
755 ctx := &moduleContext{
756 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700757 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758
Jiyong Park99644e92020-11-17 22:21:02 +0900759 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
760 if !apexInfo.IsForPlatform() {
761 mod.hideApexVariantFromMake = true
762 }
763
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500765 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
766
767 // Differentiate static libraries that are vendor available
768 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500769 mod.Properties.SubName += cc.VendorSuffix
770 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
771 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500772 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773
774 if !toolchain.Supported() {
775 // This toolchain's unsupported, there's nothing to do for this mod.
776 return
777 }
778
779 deps := mod.depsToPaths(ctx)
780 flags := Flags{
781 Toolchain: toolchain,
782 }
783
784 if mod.compiler != nil {
785 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400786 }
787 if mod.coverage != nil {
788 flags, deps = mod.coverage.flags(ctx, flags, deps)
789 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200790 if mod.clippy != nil {
791 flags, deps = mod.clippy.flags(ctx, flags, deps)
792 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500793 if mod.sanitize != nil {
794 flags, deps = mod.sanitize.flags(ctx, flags, deps)
795 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400796
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200797 // SourceProvider needs to call GenerateSource() before compiler calls
798 // compile() so it can provide the source. A SourceProvider has
799 // multiple variants (e.g. source, rlib, dylib). Only the "source"
800 // variant is responsible for effectively generating the source. The
801 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400802 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200803 if mod.compiler.(libraryInterface).source() {
804 mod.sourceProvider.GenerateSource(ctx, deps)
805 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
806 } else {
807 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
808 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700809 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200810 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400811 }
812
813 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100814 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900815 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900816 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200817 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900818
Dan Albert06feee92021-03-19 15:06:02 -0700819 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
820
Jiyong Park459feca2020-12-15 11:02:21 +0900821 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
822 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200823 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400824 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825 }
826}
827
828func (mod *Module) deps(ctx DepsContext) Deps {
829 deps := Deps{}
830
831 if mod.compiler != nil {
832 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400833 }
834 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700835 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836 }
837
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400838 if mod.coverage != nil {
839 deps = mod.coverage.deps(ctx, deps)
840 }
841
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500842 if mod.sanitize != nil {
843 deps = mod.sanitize.deps(ctx, deps)
844 }
845
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
847 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700848 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700849 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
850 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
851 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400852 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 return deps
854
855}
856
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857type dependencyTag struct {
858 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800859 name string
860 library bool
861 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862}
863
Jiyong Park65b62242020-11-25 12:44:59 +0900864// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
865// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
866func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800867 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900868}
869
870var _ android.InstallNeededDependencyTag = dependencyTag{}
871
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400873 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
874 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
875 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800876 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400877 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200878 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879)
880
Jiyong Park99644e92020-11-17 22:21:02 +0900881func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
882 tag, ok := depTag.(dependencyTag)
883 return ok && tag == dylibDepTag
884}
885
Jiyong Park94e22fd2021-04-08 18:19:15 +0900886func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
887 tag, ok := depTag.(dependencyTag)
888 return ok && tag == rlibDepTag
889}
890
Matthew Maurer0f003b12020-06-29 14:34:06 -0700891type autoDep struct {
892 variation string
893 depTag dependencyTag
894}
895
896var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200897 rlibVariation = "rlib"
898 dylibVariation = "dylib"
899 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
900 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700901)
902
903type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500904 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700905}
906
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400907func (mod *Module) begin(ctx BaseModuleContext) {
908 if mod.coverage != nil {
909 mod.coverage.begin(ctx)
910 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500911 if mod.sanitize != nil {
912 mod.sanitize.begin(ctx)
913 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400914}
915
Ivan Lozanoffee3342019-08-27 12:03:00 -0700916func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
917 var depPaths PathDeps
918
919 directRlibDeps := []*Module{}
920 directDylibDeps := []*Module{}
921 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700922 directSharedLibDeps := [](cc.LinkableInterface){}
923 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400924 directSrcProvidersDeps := []*Module{}
925 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926
927 ctx.VisitDirectDeps(func(dep android.Module) {
928 depName := ctx.OtherModuleName(dep)
929 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400930
Ivan Lozano89435d12020-07-31 11:01:18 -0400931 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700932 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400933 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700934
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 switch depTag {
936 case dylibDepTag:
937 dylib, ok := rustDep.compiler.(libraryInterface)
938 if !ok || !dylib.dylib() {
939 ctx.ModuleErrorf("mod %q not an dylib library", depName)
940 return
941 }
942 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400943 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700944 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400945
Ivan Lozanoffee3342019-08-27 12:03:00 -0700946 rlib, ok := rustDep.compiler.(libraryInterface)
947 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400948 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949 return
950 }
951 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400952 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953 case procMacroDepTag:
954 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400955 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400956 case android.SourceDepTag:
957 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
958 // OS/Arch variant is used.
959 var helper string
960 if ctx.Host() {
961 helper = "missing 'host_supported'?"
962 } else {
963 helper = "device module defined?"
964 }
965
966 if dep.Target().Os != ctx.Os() {
967 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
968 return
969 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
970 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
971 return
972 }
973 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974 }
975
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400976 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700977 if depTag != procMacroDepTag {
978 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
979 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
980 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
981 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982 }
983
Ivan Lozanoffee3342019-08-27 12:03:00 -0700984 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900985 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400986 if !linkFile.Valid() {
987 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
988 depName, ctx.ModuleName())
989 return
990 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700991 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700992 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
993 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994 }
995 }
996
Ivan Lozano89435d12020-07-31 11:01:18 -0400997 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700998 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400999 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001000 if _, ok := ccDep.(*Module); !ok {
1001 if ccDep.Module().Target().Os != ctx.Os() {
1002 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1003 return
1004 }
1005 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1006 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1007 return
1008 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001009 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001010 linkObject := ccDep.OutputFile()
1011 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001012
Ivan Lozano2093af22020-08-25 12:48:19 -04001013 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1015 }
1016
1017 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001018 switch {
1019 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001020 if cc.IsWholeStaticLib(depTag) {
1021 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1022 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001023 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1024 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001025 } else {
1026 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 -05001027 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001028 }
1029
1030 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1031 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001032 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001033 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1034
Colin Cross0de8a1e2020-09-18 14:15:30 -07001035 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1036 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1037 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1038 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1039 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001041 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001042 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001044 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001045 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1046 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1047 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1048 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1049 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050 directSharedLibDeps = append(directSharedLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001051 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001053 case cc.IsHeaderDepTag(depTag):
1054 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1055 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1056 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1057 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001058 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001059 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001060 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001061 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062 }
1063
1064 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001065 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1066 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001067 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001068 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001069 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001070
1071 if srcDep, ok := dep.(android.SourceFileProducer); ok {
1072 switch depTag {
1073 case android.SourceDepTag:
1074 // These are usually genrules which don't have per-target variants.
1075 directSrcDeps = append(directSrcDeps, srcDep)
1076 }
1077 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078 })
1079
1080 var rlibDepFiles RustLibraries
1081 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001082 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001083 }
1084 var dylibDepFiles RustLibraries
1085 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001086 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087 }
1088 var procMacroDepFiles RustLibraries
1089 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001090 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001091 }
1092
1093 var staticLibDepFiles android.Paths
1094 for _, dep := range directStaticLibDeps {
1095 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1096 }
1097
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001098 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001099 var sharedLibDepFiles android.Paths
1100 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001101 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1102 if dep.Toc().Valid() {
1103 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1104 } else {
1105 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1106 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001107 }
1108
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001109 var srcProviderDepFiles android.Paths
1110 for _, dep := range directSrcProvidersDeps {
1111 srcs, _ := dep.OutputFiles("")
1112 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1113 }
1114 for _, dep := range directSrcDeps {
1115 srcs := dep.Srcs()
1116 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1117 }
1118
Ivan Lozanoffee3342019-08-27 12:03:00 -07001119 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1120 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1121 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001122 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001123 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1124 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001125 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001126
1127 // Dedup exported flags from dependencies
1128 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001129 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001130 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001131 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1132 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1133 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134
1135 return depPaths
1136}
1137
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001138func (mod *Module) InstallInData() bool {
1139 if mod.compiler == nil {
1140 return false
1141 }
1142 return mod.compiler.inData()
1143}
1144
Ivan Lozanoffee3342019-08-27 12:03:00 -07001145func linkPathFromFilePath(filepath android.Path) string {
1146 return strings.Split(filepath.String(), filepath.Base())[0]
1147}
Ivan Lozanod648c432020-02-06 12:05:10 -05001148
Ivan Lozanoffee3342019-08-27 12:03:00 -07001149func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1150 ctx := &depsContext{
1151 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001152 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001153
1154 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001155 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001156
Ivan Lozano2b081132020-09-08 12:46:52 -04001157 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001158 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001159 stdLinkage = "rlib-std"
1160 }
1161
1162 rlibDepVariations := commonDepVariations
1163 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1164 rlibDepVariations = append(rlibDepVariations,
1165 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1166 }
1167
Ivan Lozano52767be2019-10-18 14:49:46 -07001168 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001169 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001170 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001171 rlibDepTag, deps.Rlibs...)
1172 actx.AddVariationDependencies(
1173 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001174 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001175 dylibDepTag, deps.Dylibs...)
1176
Ivan Lozano042504f2020-08-18 14:31:23 -04001177 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1178 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001179 if autoDep.depTag == rlibDepTag {
1180 actx.AddVariationDependencies(
1181 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1182 autoDep.depTag, deps.Rustlibs...)
1183 } else {
1184 actx.AddVariationDependencies(
1185 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1186 autoDep.depTag, deps.Rustlibs...)
1187 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001188 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001189 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001190 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001191 actx.AddVariationDependencies(
1192 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1193 rlibDepTag, deps.Stdlibs...)
1194 } else {
1195 actx.AddVariationDependencies(
1196 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1197 dylibDepTag, deps.Stdlibs...)
1198 }
1199 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001200 actx.AddVariationDependencies(append(commonDepVariations,
1201 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001202 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001203 actx.AddVariationDependencies(append(commonDepVariations,
1204 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001205 cc.StaticDepTag(false), deps.StaticLibs...)
1206 actx.AddVariationDependencies(append(commonDepVariations,
1207 blueprint.Variation{Mutator: "link", Variation: "static"}),
1208 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001209
Zach Johnson3df4e632020-11-06 11:56:27 -08001210 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1211
Colin Cross565cafd2020-09-25 18:47:38 -07001212 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001213 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001214 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001215 }
1216 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001217 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001218 }
1219
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001220 if mod.sourceProvider != nil {
1221 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1222 bindgen.Properties.Custom_bindgen != "" {
1223 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1224 bindgen.Properties.Custom_bindgen)
1225 }
1226 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001227 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001228 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001229}
1230
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001231func BeginMutator(ctx android.BottomUpMutatorContext) {
1232 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1233 mod.beginMutator(ctx)
1234 }
1235}
1236
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001237func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1238 ctx := &baseModuleContext{
1239 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001240 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001241
1242 mod.begin(ctx)
1243}
1244
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245func (mod *Module) Name() string {
1246 name := mod.ModuleBase.Name()
1247 if p, ok := mod.compiler.(interface {
1248 Name(string) string
1249 }); ok {
1250 name = p.Name(name)
1251 }
1252 return name
1253}
1254
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001255func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001256 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001257 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001258 }
1259}
1260
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001261var _ android.HostToolProvider = (*Module)(nil)
1262
1263func (mod *Module) HostToolPath() android.OptionalPath {
1264 if !mod.Host() {
1265 return android.OptionalPath{}
1266 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001267 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1268 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001269 }
1270 return android.OptionalPath{}
1271}
1272
Jiyong Park99644e92020-11-17 22:21:02 +09001273var _ android.ApexModule = (*Module)(nil)
1274
Ivan Lozanoab1f1ac2022-01-11 12:02:06 -05001275func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001276 return String(mod.Properties.Min_sdk_version)
1277}
1278
Jiyong Park45bf82e2020-12-15 22:29:02 +09001279// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001280func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoab1f1ac2022-01-11 12:02:06 -05001281 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001282 if minSdkVersion == "apex_inherit" {
1283 return nil
1284 }
1285 if minSdkVersion == "" {
1286 return fmt.Errorf("min_sdk_version is not specificed")
1287 }
1288
1289 // Not using nativeApiLevelFromUser because the context here is not
1290 // necessarily a native context.
1291 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1292 if err != nil {
1293 return err
1294 }
1295
1296 if ver.GreaterThan(sdkVersion) {
1297 return fmt.Errorf("newer SDK(%v)", ver)
1298 }
Jiyong Park99644e92020-11-17 22:21:02 +09001299 return nil
1300}
1301
Jiyong Park45bf82e2020-12-15 22:29:02 +09001302// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001303func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1304 depTag := ctx.OtherModuleDependencyTag(dep)
1305
1306 if ccm, ok := dep.(*cc.Module); ok {
1307 if ccm.HasStubsVariants() {
1308 if cc.IsSharedDepTag(depTag) {
1309 // dynamic dep to a stubs lib crosses APEX boundary
1310 return false
1311 }
1312 if cc.IsRuntimeDepTag(depTag) {
1313 // runtime dep to a stubs lib also crosses APEX boundary
1314 return false
1315 }
1316
1317 if cc.IsHeaderDepTag(depTag) {
1318 return false
1319 }
1320 }
1321 if mod.Static() && cc.IsSharedDepTag(depTag) {
1322 // shared_lib dependency from a static lib is considered as crossing
1323 // the APEX boundary because the dependency doesn't actually is
1324 // linked; the dependency is used only during the compilation phase.
1325 return false
1326 }
1327 }
1328
1329 if depTag == procMacroDepTag {
1330 return false
1331 }
1332
1333 return true
1334}
1335
1336// Overrides ApexModule.IsInstallabeToApex()
1337func (mod *Module) IsInstallableToApex() bool {
1338 if mod.compiler != nil {
1339 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1340 return true
1341 }
1342 if _, ok := mod.compiler.(*binaryDecorator); ok {
1343 return true
1344 }
1345 }
1346 return false
1347}
1348
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001349// If a library file has a "lib" prefix, extract the library name without the prefix.
1350func libNameFromFilePath(filepath android.Path) (string, bool) {
1351 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1352 if strings.HasPrefix(libName, "lib") {
1353 libName = libName[3:]
1354 return libName, true
1355 }
1356 return "", false
1357}
1358
Ivan Lozanoffee3342019-08-27 12:03:00 -07001359var Bool = proptools.Bool
1360var BoolDefault = proptools.BoolDefault
1361var String = proptools.String
1362var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001363
1364var _ android.OutputFileProducer = (*Module)(nil)