blob: 9738b467f15636d2b120fcbed475908c3fa59fab [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
Ivan Lozanof1c84332019-09-20 11:00:37 -070061 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040062 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020063 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070064}
65
66type BaseProperties struct {
67 AndroidMkRlibs []string
68 AndroidMkDylibs []string
69 AndroidMkProcMacroLibs []string
70 AndroidMkSharedLibs []string
71 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040072
Ivan Lozano6a884432020-12-02 09:15:16 -050073 ImageVariationPrefix string `blueprint:"mutated"`
74 VndkVersion string `blueprint:"mutated"`
75 SubName string `blueprint:"mutated"`
76
Ivan Lozanoc08897c2021-04-02 12:41:32 -040077 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
78 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
79 // appended before SubName.
80 RustSubName string `blueprint:"mutated"`
81
Ivan Lozano6a884432020-12-02 09:15:16 -050082 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050083 CoreVariantNeeded bool `blueprint:"mutated"`
84 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
85 ExtraVariants []string `blueprint:"mutated"`
86
87 // Make this module available when building for vendor ramdisk.
88 // On device without a dedicated recovery partition, the module is only
89 // available after switching root into
90 // /first_stage_ramdisk. To expose the module before switching root, install
91 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
92 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040093
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050094 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
95 Min_sdk_version *string
96
Ivan Lozano43845682020-07-09 21:03:28 -040097 PreventInstall bool
98 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070099}
100
101type Module struct {
102 android.ModuleBase
103 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +0900104 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105
Ivan Lozano6a884432020-12-02 09:15:16 -0500106 VendorProperties cc.VendorProperties
107
Ivan Lozanoffee3342019-08-27 12:03:00 -0700108 Properties BaseProperties
109
110 hod android.HostOrDeviceSupported
111 multilib android.Multilib
112
Ivan Lozano6a884432020-12-02 09:15:16 -0500113 makeLinkType string
114
Ivan Lozanoffee3342019-08-27 12:03:00 -0700115 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400116 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200117 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500118 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400120 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700121 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400122
Jiyong Parke54f07e2021-04-07 15:08:04 +0900123 // Unstripped output. This is usually used when this module is linked to another module
124 // as a library. The stripped output which is used for installation can be found via
125 // compiler.strippedOutputFile if it exists.
126 unstrippedOutputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900127
128 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700129}
130
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500131func (mod *Module) Header() bool {
132 //TODO: If Rust libraries provide header variants, this needs to be updated.
133 return false
134}
135
136func (mod *Module) SetPreventInstall() {
137 mod.Properties.PreventInstall = true
138}
139
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500140func (mod *Module) SetHideFromMake() {
141 mod.Properties.HideFromMake = true
142}
143
144func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500145 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
146 // nil since we need compiler to actually sanitize.
147 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500148}
149
150func (mod *Module) IsDependencyRoot() bool {
151 if mod.compiler != nil {
152 return mod.compiler.isDependencyRoot()
153 }
154 panic("IsDependencyRoot called on a non-compiler Rust module")
155}
156
157func (mod *Module) IsPrebuilt() bool {
158 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
159 return true
160 }
161 return false
162}
163
Ivan Lozano43845682020-07-09 21:03:28 -0400164func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
165 switch tag {
166 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700167 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400168 return mod.sourceProvider.Srcs(), nil
169 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900170 if mod.OutputFile().Valid() {
171 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400172 }
173 return android.Paths{}, nil
174 }
175 default:
176 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
177 }
178}
179
Ivan Lozano52767be2019-10-18 14:49:46 -0700180func (mod *Module) SelectedStl() string {
181 return ""
182}
183
Ivan Lozano2b262972019-11-21 12:30:50 -0800184func (mod *Module) NonCcVariants() bool {
185 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400186 if _, ok := mod.compiler.(libraryInterface); ok {
187 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800188 }
189 }
190 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
191}
192
Ivan Lozano52767be2019-10-18 14:49:46 -0700193func (mod *Module) Static() bool {
194 if mod.compiler != nil {
195 if library, ok := mod.compiler.(libraryInterface); ok {
196 return library.static()
197 }
198 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400199 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700200}
201
202func (mod *Module) Shared() bool {
203 if mod.compiler != nil {
204 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400205 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700206 }
207 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400208 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700209}
210
211func (mod *Module) Toc() android.OptionalPath {
212 if mod.compiler != nil {
213 if _, ok := mod.compiler.(libraryInterface); ok {
214 return android.OptionalPath{}
215 }
216 }
217 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
218}
219
Colin Crossc511bc52020-04-07 16:50:32 +0000220func (mod *Module) UseSdk() bool {
221 return false
222}
223
Ivan Lozano6a884432020-12-02 09:15:16 -0500224// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
225// "product" and "vendor" variant modules return true for this function.
226// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
227// "soc_specific: true" and more vendor installed modules are included here.
228// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
229// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700230func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500231 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700232}
233
234func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400235 return true
236}
237
238func (mod *Module) SubName() string {
239 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700240}
241
242func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500243 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700244 return false
245}
246
Ivan Lozanof9e21722020-12-02 09:00:51 -0500247func (mod *Module) IsVndkExt() bool {
248 return false
249}
250
Colin Cross127bb8b2020-12-16 16:46:01 -0800251func (c *Module) IsVndkPrivate() bool {
252 return false
253}
254
255func (c *Module) IsLlndk() bool {
256 return false
257}
258
259func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500260 return false
261}
262
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400263func (m *Module) IsLlndkHeaders() bool {
264 return false
265}
266
267func (m *Module) IsLlndkLibrary() bool {
268 return false
269}
270
271func (mod *Module) KernelHeadersDecorator() bool {
272 return false
273}
274
275func (m *Module) HasLlndkStubs() bool {
276 return false
277}
278
Ivan Lozano52767be2019-10-18 14:49:46 -0700279func (mod *Module) SdkVersion() string {
280 return ""
281}
282
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900283func (mod *Module) MinSdkVersion() string {
284 return ""
285}
286
Colin Crossc511bc52020-04-07 16:50:32 +0000287func (mod *Module) AlwaysSdk() bool {
288 return false
289}
290
Jiyong Park2286afd2020-06-16 21:58:53 +0900291func (mod *Module) IsSdkVariant() bool {
292 return false
293}
294
Colin Cross1348ce32020-10-01 13:37:16 -0700295func (mod *Module) SplitPerApiLevel() bool {
296 return false
297}
298
Ivan Lozanoffee3342019-08-27 12:03:00 -0700299type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400300 Dylibs []string
301 Rlibs []string
302 Rustlibs []string
303 Stdlibs []string
304 ProcMacros []string
305 SharedLibs []string
306 StaticLibs []string
307 WholeStaticLibs []string
308 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700309
310 CrtBegin, CrtEnd string
311}
312
313type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500314 DyLibs RustLibraries
315 RLibs RustLibraries
316 SharedLibs android.Paths
317 SharedLibDeps android.Paths
318 StaticLibs android.Paths
319 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500320
321 // depFlags and depLinkFlags are rustc and linker (clang) flags.
322 depFlags []string
323 depLinkFlags []string
324
325 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
326 // Both of these are exported and propagate to dependencies.
327 linkDirs []string
328 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700329
Ivan Lozano45901ed2020-07-24 16:05:01 -0400330 // Used by bindgen modules which call clang
331 depClangFlags []string
332 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400333 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400334 depSystemIncludePaths android.Paths
335
Ivan Lozanof1c84332019-09-20 11:00:37 -0700336 CrtBegin android.OptionalPath
337 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700338
339 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500340 SrcDeps android.Paths
341 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700342}
343
344type RustLibraries []RustLibrary
345
346type RustLibrary struct {
347 Path android.Path
348 CrateName string
349}
350
351type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100352 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700353 compilerFlags(ctx ModuleContext, flags Flags) Flags
354 compilerProps() []interface{}
355 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
356 compilerDeps(ctx DepsContext, deps Deps) Deps
357 crateName() string
358
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100359 // Output directory in which source-generated code from dependencies is
360 // copied. This is equivalent to Cargo's OUT_DIR variable.
361 CargoOutDir() android.OptionalPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800362 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200363 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700364 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400365
366 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400367
368 Disabled() bool
369 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400370
Ivan Lozanodd055472020-09-28 13:22:45 -0400371 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500372 isDependencyRoot() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900373
374 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400375}
376
Matthew Maurerbb3add12020-06-25 09:34:12 -0700377type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700378 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400379 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700380}
381
382type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400383 linkDirs []string
384 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700385}
386
Matthew Maurerbb3add12020-06-25 09:34:12 -0700387func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
388 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
389}
390
Ivan Lozano2093af22020-08-25 12:48:19 -0400391func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
392 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
393}
394
Colin Cross0de8a1e2020-09-18 14:15:30 -0700395func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
396 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700397 LinkDirs: flagExporter.linkDirs,
398 LinkObjects: flagExporter.linkObjects,
399 })
400}
401
Matthew Maurerbb3add12020-06-25 09:34:12 -0700402var _ exportedFlagsProducer = (*flagExporter)(nil)
403
404func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700405 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700406}
407
Colin Cross0de8a1e2020-09-18 14:15:30 -0700408type FlagExporterInfo struct {
409 Flags []string
410 LinkDirs []string // TODO: this should be android.Paths
411 LinkObjects []string // TODO: this should be android.Paths
412}
413
414var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
415
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400416func (mod *Module) isCoverageVariant() bool {
417 return mod.coverage.Properties.IsCoverageVariant
418}
419
420var _ cc.Coverage = (*Module)(nil)
421
422func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
423 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
424}
425
426func (mod *Module) PreventInstall() {
427 mod.Properties.PreventInstall = true
428}
429
430func (mod *Module) HideFromMake() {
431 mod.Properties.HideFromMake = true
432}
433
434func (mod *Module) MarkAsCoverageVariant(coverage bool) {
435 mod.coverage.Properties.IsCoverageVariant = coverage
436}
437
438func (mod *Module) EnableCoverageIfNeeded() {
439 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440}
441
442func defaultsFactory() android.Module {
443 return DefaultsFactory()
444}
445
446type Defaults struct {
447 android.ModuleBase
448 android.DefaultsModuleBase
449}
450
451func DefaultsFactory(props ...interface{}) android.Module {
452 module := &Defaults{}
453
454 module.AddProperties(props...)
455 module.AddProperties(
456 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500457 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100458 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400459 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700460 &BaseCompilerProperties{},
461 &BinaryCompilerProperties{},
462 &LibraryCompilerProperties{},
463 &ProcMacroCompilerProperties{},
464 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400465 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700466 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400467 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400468 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200469 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500470 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471 )
472
473 android.InitDefaultsModule(module)
474 return module
475}
476
477func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700478 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700479}
480
Ivan Lozano183a3212019-10-18 14:18:45 -0700481func (mod *Module) CcLibrary() bool {
482 if mod.compiler != nil {
483 if _, ok := mod.compiler.(*libraryDecorator); ok {
484 return true
485 }
486 }
487 return false
488}
489
490func (mod *Module) CcLibraryInterface() bool {
491 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400492 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
493 // VariantIs{Static,Shared} is set.
494 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700495 return true
496 }
497 }
498 return false
499}
500
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800501func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700502 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700503 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800504 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700505 }
506 }
507 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
508}
509
510func (mod *Module) SetStatic() {
511 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700512 if library, ok := mod.compiler.(libraryInterface); ok {
513 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700514 return
515 }
516 }
517 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
518}
519
520func (mod *Module) SetShared() {
521 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700522 if library, ok := mod.compiler.(libraryInterface); ok {
523 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700524 return
525 }
526 }
527 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
528}
529
Ivan Lozano183a3212019-10-18 14:18:45 -0700530func (mod *Module) BuildStaticVariant() bool {
531 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700532 if library, ok := mod.compiler.(libraryInterface); ok {
533 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700534 }
535 }
536 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
537}
538
539func (mod *Module) BuildSharedVariant() bool {
540 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700541 if library, ok := mod.compiler.(libraryInterface); ok {
542 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700543 }
544 }
545 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
546}
547
Ivan Lozano183a3212019-10-18 14:18:45 -0700548func (mod *Module) Module() android.Module {
549 return mod
550}
551
Ivan Lozano183a3212019-10-18 14:18:45 -0700552func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900553 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
554 return mod.compiler.strippedOutputFilePath()
555 }
556 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700557}
558
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400559func (mod *Module) CoverageFiles() android.Paths {
560 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800561 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400562 }
563 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
564}
565
Jiyong Park459feca2020-12-15 11:02:21 +0900566func (mod *Module) installable(apexInfo android.ApexInfo) bool {
567 // The apex variant is not installable because it is included in the APEX and won't appear
568 // in the system partition as a standalone file.
569 if !apexInfo.IsForPlatform() {
570 return false
571 }
572
Jiyong Parke54f07e2021-04-07 15:08:04 +0900573 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900574}
575
Ivan Lozano183a3212019-10-18 14:18:45 -0700576var _ cc.LinkableInterface = (*Module)(nil)
577
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578func (mod *Module) Init() android.Module {
579 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500580 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581
582 if mod.compiler != nil {
583 mod.AddProperties(mod.compiler.compilerProps()...)
584 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400585 if mod.coverage != nil {
586 mod.AddProperties(mod.coverage.props()...)
587 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200588 if mod.clippy != nil {
589 mod.AddProperties(mod.clippy.props()...)
590 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400591 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700592 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400593 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500594 if mod.sanitize != nil {
595 mod.AddProperties(mod.sanitize.props()...)
596 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400597
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900599 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600
601 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602 return mod
603}
604
605func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
606 return &Module{
607 hod: hod,
608 multilib: multilib,
609 }
610}
611func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
612 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400613 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200614 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500615 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700616 return module
617}
618
619type ModuleContext interface {
620 android.ModuleContext
621 ModuleContextIntf
622}
623
624type BaseModuleContext interface {
625 android.BaseModuleContext
626 ModuleContextIntf
627}
628
629type DepsContext interface {
630 android.BottomUpMutatorContext
631 ModuleContextIntf
632}
633
634type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200635 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700636 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700637}
638
639type depsContext struct {
640 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700641}
642
643type moduleContext struct {
644 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645}
646
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200647type baseModuleContext struct {
648 android.BaseModuleContext
649}
650
651func (ctx *moduleContext) RustModule() *Module {
652 return ctx.Module().(*Module)
653}
654
655func (ctx *moduleContext) toolchain() config.Toolchain {
656 return ctx.RustModule().toolchain(ctx)
657}
658
659func (ctx *depsContext) RustModule() *Module {
660 return ctx.Module().(*Module)
661}
662
663func (ctx *depsContext) toolchain() config.Toolchain {
664 return ctx.RustModule().toolchain(ctx)
665}
666
667func (ctx *baseModuleContext) RustModule() *Module {
668 return ctx.Module().(*Module)
669}
670
671func (ctx *baseModuleContext) toolchain() config.Toolchain {
672 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400673}
674
675func (mod *Module) nativeCoverage() bool {
676 return mod.compiler != nil && mod.compiler.nativeCoverage()
677}
678
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
680 if mod.cachedToolchain == nil {
681 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
682 }
683 return mod.cachedToolchain
684}
685
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200686func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
687 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
688}
689
Ivan Lozanoffee3342019-08-27 12:03:00 -0700690func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
691}
692
693func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
694 ctx := &moduleContext{
695 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700696 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700697
Jiyong Park99644e92020-11-17 22:21:02 +0900698 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
699 if !apexInfo.IsForPlatform() {
700 mod.hideApexVariantFromMake = true
701 }
702
Ivan Lozanoffee3342019-08-27 12:03:00 -0700703 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500704 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
705
706 // Differentiate static libraries that are vendor available
707 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500708 mod.Properties.SubName += cc.VendorSuffix
709 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
710 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500711 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700712
713 if !toolchain.Supported() {
714 // This toolchain's unsupported, there's nothing to do for this mod.
715 return
716 }
717
718 deps := mod.depsToPaths(ctx)
719 flags := Flags{
720 Toolchain: toolchain,
721 }
722
723 if mod.compiler != nil {
724 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400725 }
726 if mod.coverage != nil {
727 flags, deps = mod.coverage.flags(ctx, flags, deps)
728 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200729 if mod.clippy != nil {
730 flags, deps = mod.clippy.flags(ctx, flags, deps)
731 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500732 if mod.sanitize != nil {
733 flags, deps = mod.sanitize.flags(ctx, flags, deps)
734 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400735
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200736 // SourceProvider needs to call GenerateSource() before compiler calls
737 // compile() so it can provide the source. A SourceProvider has
738 // multiple variants (e.g. source, rlib, dylib). Only the "source"
739 // variant is responsible for effectively generating the source. The
740 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400741 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200742 if mod.compiler.(libraryInterface).source() {
743 mod.sourceProvider.GenerateSource(ctx, deps)
744 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
745 } else {
746 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
747 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700748 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200749 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400750 }
751
752 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100753 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900754 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900755 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200756 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900757
758 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
759 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200760 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400761 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 }
763}
764
765func (mod *Module) deps(ctx DepsContext) Deps {
766 deps := Deps{}
767
768 if mod.compiler != nil {
769 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400770 }
771 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700772 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700773 }
774
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400775 if mod.coverage != nil {
776 deps = mod.coverage.deps(ctx, deps)
777 }
778
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500779 if mod.sanitize != nil {
780 deps = mod.sanitize.deps(ctx, deps)
781 }
782
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
784 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700785 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
787 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
788 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400789 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790 return deps
791
792}
793
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794type dependencyTag struct {
795 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800796 name string
797 library bool
798 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799}
800
Jiyong Park65b62242020-11-25 12:44:59 +0900801// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
802// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
803func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800804 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900805}
806
807var _ android.InstallNeededDependencyTag = dependencyTag{}
808
Ivan Lozanoffee3342019-08-27 12:03:00 -0700809var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400810 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
811 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
812 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800813 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400814 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200815 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816)
817
Jiyong Park99644e92020-11-17 22:21:02 +0900818func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
819 tag, ok := depTag.(dependencyTag)
820 return ok && tag == dylibDepTag
821}
822
Jiyong Park94e22fd2021-04-08 18:19:15 +0900823func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
824 tag, ok := depTag.(dependencyTag)
825 return ok && tag == rlibDepTag
826}
827
Matthew Maurer0f003b12020-06-29 14:34:06 -0700828type autoDep struct {
829 variation string
830 depTag dependencyTag
831}
832
833var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200834 rlibVariation = "rlib"
835 dylibVariation = "dylib"
836 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
837 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700838)
839
840type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500841 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700842}
843
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400844func (mod *Module) begin(ctx BaseModuleContext) {
845 if mod.coverage != nil {
846 mod.coverage.begin(ctx)
847 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500848 if mod.sanitize != nil {
849 mod.sanitize.begin(ctx)
850 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400851}
852
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
854 var depPaths PathDeps
855
856 directRlibDeps := []*Module{}
857 directDylibDeps := []*Module{}
858 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700859 directSharedLibDeps := [](cc.LinkableInterface){}
860 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400861 directSrcProvidersDeps := []*Module{}
862 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863
864 ctx.VisitDirectDeps(func(dep android.Module) {
865 depName := ctx.OtherModuleName(dep)
866 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400867
Ivan Lozano89435d12020-07-31 11:01:18 -0400868 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400870 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700871
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 switch depTag {
873 case dylibDepTag:
874 dylib, ok := rustDep.compiler.(libraryInterface)
875 if !ok || !dylib.dylib() {
876 ctx.ModuleErrorf("mod %q not an dylib library", depName)
877 return
878 }
879 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400880 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400882
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 rlib, ok := rustDep.compiler.(libraryInterface)
884 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400885 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886 return
887 }
888 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400889 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 case procMacroDepTag:
891 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400892 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400893 case android.SourceDepTag:
894 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
895 // OS/Arch variant is used.
896 var helper string
897 if ctx.Host() {
898 helper = "missing 'host_supported'?"
899 } else {
900 helper = "device module defined?"
901 }
902
903 if dep.Target().Os != ctx.Os() {
904 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
905 return
906 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
907 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
908 return
909 }
910 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 }
912
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400913 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700914 if depTag != procMacroDepTag {
915 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
916 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
917 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
918 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919 }
920
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900922 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400923 if !linkFile.Valid() {
924 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
925 depName, ctx.ModuleName())
926 return
927 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700928 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700929 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
930 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700931 }
932 }
933
Ivan Lozano89435d12020-07-31 11:01:18 -0400934 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700935 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400936 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -0700937 if _, ok := ccDep.(*Module); !ok {
938 if ccDep.Module().Target().Os != ctx.Os() {
939 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
940 return
941 }
942 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
943 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
944 return
945 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700946 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400947 linkObject := ccDep.OutputFile()
948 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500949
Ivan Lozano2093af22020-08-25 12:48:19 -0400950 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700951 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
952 }
953
954 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700955 switch {
956 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -0400957 if cc.IsWholeStaticLib(depTag) {
958 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
959 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500960 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
961 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400962 } else {
963 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 -0500964 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500965 }
966
967 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
968 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400969 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500970 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
971
Colin Cross0de8a1e2020-09-18 14:15:30 -0700972 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
973 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
974 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
975 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
976 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700977 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400978 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -0700979 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700980 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400981 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700982 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
983 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
984 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
985 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
986 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700987 directSharedLibDeps = append(directSharedLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400988 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700989 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800990 case cc.IsHeaderDepTag(depTag):
991 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
992 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
993 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
994 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700995 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400996 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700997 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400998 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700999 }
1000
1001 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001002 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1003 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001004 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001005 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001006 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001007
1008 if srcDep, ok := dep.(android.SourceFileProducer); ok {
1009 switch depTag {
1010 case android.SourceDepTag:
1011 // These are usually genrules which don't have per-target variants.
1012 directSrcDeps = append(directSrcDeps, srcDep)
1013 }
1014 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015 })
1016
1017 var rlibDepFiles RustLibraries
1018 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001019 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020 }
1021 var dylibDepFiles RustLibraries
1022 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001023 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024 }
1025 var procMacroDepFiles RustLibraries
1026 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001027 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001028 }
1029
1030 var staticLibDepFiles android.Paths
1031 for _, dep := range directStaticLibDeps {
1032 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1033 }
1034
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001035 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001036 var sharedLibDepFiles android.Paths
1037 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001038 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1039 if dep.Toc().Valid() {
1040 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1041 } else {
1042 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1043 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044 }
1045
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001046 var srcProviderDepFiles android.Paths
1047 for _, dep := range directSrcProvidersDeps {
1048 srcs, _ := dep.OutputFiles("")
1049 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1050 }
1051 for _, dep := range directSrcDeps {
1052 srcs := dep.Srcs()
1053 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1054 }
1055
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1057 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1058 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001059 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001060 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1061 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001062 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001063
1064 // Dedup exported flags from dependencies
1065 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001066 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001067 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001068 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1069 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1070 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001071
1072 return depPaths
1073}
1074
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001075func (mod *Module) InstallInData() bool {
1076 if mod.compiler == nil {
1077 return false
1078 }
1079 return mod.compiler.inData()
1080}
1081
Ivan Lozanoffee3342019-08-27 12:03:00 -07001082func linkPathFromFilePath(filepath android.Path) string {
1083 return strings.Split(filepath.String(), filepath.Base())[0]
1084}
Ivan Lozanod648c432020-02-06 12:05:10 -05001085
Ivan Lozanoffee3342019-08-27 12:03:00 -07001086func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1087 ctx := &depsContext{
1088 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001090
1091 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001092 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001093
Ivan Lozano2b081132020-09-08 12:46:52 -04001094 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001095 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001096 stdLinkage = "rlib-std"
1097 }
1098
1099 rlibDepVariations := commonDepVariations
1100 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1101 rlibDepVariations = append(rlibDepVariations,
1102 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1103 }
1104
Ivan Lozano52767be2019-10-18 14:49:46 -07001105 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001106 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001107 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001108 rlibDepTag, deps.Rlibs...)
1109 actx.AddVariationDependencies(
1110 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001111 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001112 dylibDepTag, deps.Dylibs...)
1113
Ivan Lozano042504f2020-08-18 14:31:23 -04001114 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1115 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001116 if autoDep.depTag == rlibDepTag {
1117 actx.AddVariationDependencies(
1118 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1119 autoDep.depTag, deps.Rustlibs...)
1120 } else {
1121 actx.AddVariationDependencies(
1122 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1123 autoDep.depTag, deps.Rustlibs...)
1124 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001125 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001126 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001127 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001128 actx.AddVariationDependencies(
1129 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1130 rlibDepTag, deps.Stdlibs...)
1131 } else {
1132 actx.AddVariationDependencies(
1133 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1134 dylibDepTag, deps.Stdlibs...)
1135 }
1136 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001137 actx.AddVariationDependencies(append(commonDepVariations,
1138 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001139 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001140 actx.AddVariationDependencies(append(commonDepVariations,
1141 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001142 cc.StaticDepTag(false), deps.StaticLibs...)
1143 actx.AddVariationDependencies(append(commonDepVariations,
1144 blueprint.Variation{Mutator: "link", Variation: "static"}),
1145 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001146
Zach Johnson3df4e632020-11-06 11:56:27 -08001147 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1148
Colin Cross565cafd2020-09-25 18:47:38 -07001149 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001150 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001151 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001152 }
1153 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001154 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001155 }
1156
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001157 if mod.sourceProvider != nil {
1158 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1159 bindgen.Properties.Custom_bindgen != "" {
1160 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1161 bindgen.Properties.Custom_bindgen)
1162 }
1163 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001164 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001165 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001166}
1167
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001168func BeginMutator(ctx android.BottomUpMutatorContext) {
1169 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1170 mod.beginMutator(ctx)
1171 }
1172}
1173
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001174func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1175 ctx := &baseModuleContext{
1176 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001177 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001178
1179 mod.begin(ctx)
1180}
1181
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182func (mod *Module) Name() string {
1183 name := mod.ModuleBase.Name()
1184 if p, ok := mod.compiler.(interface {
1185 Name(string) string
1186 }); ok {
1187 name = p.Name(name)
1188 }
1189 return name
1190}
1191
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001192func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001193 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001194 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001195 }
1196}
1197
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001198var _ android.HostToolProvider = (*Module)(nil)
1199
1200func (mod *Module) HostToolPath() android.OptionalPath {
1201 if !mod.Host() {
1202 return android.OptionalPath{}
1203 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001204 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1205 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001206 }
1207 return android.OptionalPath{}
1208}
1209
Jiyong Park99644e92020-11-17 22:21:02 +09001210var _ android.ApexModule = (*Module)(nil)
1211
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001212func (mod *Module) minSdkVersion() string {
1213 return String(mod.Properties.Min_sdk_version)
1214}
1215
Jiyong Park45bf82e2020-12-15 22:29:02 +09001216var _ android.ApexModule = (*Module)(nil)
1217
1218// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001219func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001220 minSdkVersion := mod.minSdkVersion()
1221 if minSdkVersion == "apex_inherit" {
1222 return nil
1223 }
1224 if minSdkVersion == "" {
1225 return fmt.Errorf("min_sdk_version is not specificed")
1226 }
1227
1228 // Not using nativeApiLevelFromUser because the context here is not
1229 // necessarily a native context.
1230 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1231 if err != nil {
1232 return err
1233 }
1234
1235 if ver.GreaterThan(sdkVersion) {
1236 return fmt.Errorf("newer SDK(%v)", ver)
1237 }
Jiyong Park99644e92020-11-17 22:21:02 +09001238 return nil
1239}
1240
Jiyong Park45bf82e2020-12-15 22:29:02 +09001241// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001242func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1243 depTag := ctx.OtherModuleDependencyTag(dep)
1244
1245 if ccm, ok := dep.(*cc.Module); ok {
1246 if ccm.HasStubsVariants() {
1247 if cc.IsSharedDepTag(depTag) {
1248 // dynamic dep to a stubs lib crosses APEX boundary
1249 return false
1250 }
1251 if cc.IsRuntimeDepTag(depTag) {
1252 // runtime dep to a stubs lib also crosses APEX boundary
1253 return false
1254 }
1255
1256 if cc.IsHeaderDepTag(depTag) {
1257 return false
1258 }
1259 }
1260 if mod.Static() && cc.IsSharedDepTag(depTag) {
1261 // shared_lib dependency from a static lib is considered as crossing
1262 // the APEX boundary because the dependency doesn't actually is
1263 // linked; the dependency is used only during the compilation phase.
1264 return false
1265 }
1266 }
1267
1268 if depTag == procMacroDepTag {
1269 return false
1270 }
1271
1272 return true
1273}
1274
1275// Overrides ApexModule.IsInstallabeToApex()
1276func (mod *Module) IsInstallableToApex() bool {
1277 if mod.compiler != nil {
1278 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1279 return true
1280 }
1281 if _, ok := mod.compiler.(*binaryDecorator); ok {
1282 return true
1283 }
1284 }
1285 return false
1286}
1287
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001288// If a library file has a "lib" prefix, extract the library name without the prefix.
1289func libNameFromFilePath(filepath android.Path) (string, bool) {
1290 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1291 if strings.HasPrefix(libName, "lib") {
1292 libName = libName[3:]
1293 return libName, true
1294 }
1295 return "", false
1296}
1297
Ivan Lozanoffee3342019-08-27 12:03:00 -07001298var Bool = proptools.Bool
1299var BoolDefault = proptools.BoolDefault
1300var String = proptools.String
1301var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001302
1303var _ android.OutputFileProducer = (*Module)(nil)