blob: cda01d82c40f4c04854c087a4c36f4fdc211d2aa [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"
25 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020026 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "android/soong/rust/config"
28)
29
30var pctx = android.NewPackageContext("android/soong/rust")
31
32func init() {
33 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
35 android.AddNeverAllowRules(
36 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070037 NotIn(config.RustAllowedPaths...).
38 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070039
40 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050045
46 })
47 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
48 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 })
50 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020051 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040055 GlobalRustFlags []string // Flags that apply globally to rust
56 GlobalLinkFlags []string // Flags that apply globally to linker
57 RustFlags []string // Flags that apply to rust
58 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070060 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type BaseProperties struct {
66 AndroidMkRlibs []string
67 AndroidMkDylibs []string
68 AndroidMkProcMacroLibs []string
69 AndroidMkSharedLibs []string
70 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040071
Ivan Lozano6a884432020-12-02 09:15:16 -050072 ImageVariationPrefix string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
75
76 // Set by imageMutator
77 CoreVariantNeeded bool `blueprint:"mutated"`
78 ExtraVariants []string `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040079
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050080 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
81 Min_sdk_version *string
82
Ivan Lozano43845682020-07-09 21:03:28 -040083 PreventInstall bool
84 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070085}
86
87type Module struct {
88 android.ModuleBase
89 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090090 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
Ivan Lozano6a884432020-12-02 09:15:16 -050092 VendorProperties cc.VendorProperties
93
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 Properties BaseProperties
95
96 hod android.HostOrDeviceSupported
97 multilib android.Multilib
98
Ivan Lozano6a884432020-12-02 09:15:16 -050099 makeLinkType string
100
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400102 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200103 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500104 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400106 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700107 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400108
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200109 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900110
111 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112}
113
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500114func (mod *Module) Header() bool {
115 //TODO: If Rust libraries provide header variants, this needs to be updated.
116 return false
117}
118
119func (mod *Module) SetPreventInstall() {
120 mod.Properties.PreventInstall = true
121}
122
123// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
124func (mod *Module) InVendor() bool {
125 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
126}
127
128func (mod *Module) SetHideFromMake() {
129 mod.Properties.HideFromMake = true
130}
131
132func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500133 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
134 // nil since we need compiler to actually sanitize.
135 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136}
137
138func (mod *Module) IsDependencyRoot() bool {
139 if mod.compiler != nil {
140 return mod.compiler.isDependencyRoot()
141 }
142 panic("IsDependencyRoot called on a non-compiler Rust module")
143}
144
145func (mod *Module) IsPrebuilt() bool {
146 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
147 return true
148 }
149 return false
150}
151
Ivan Lozano43845682020-07-09 21:03:28 -0400152func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
153 switch tag {
154 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700155 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400156 return mod.sourceProvider.Srcs(), nil
157 } else {
158 if mod.outputFile.Valid() {
159 return android.Paths{mod.outputFile.Path()}, nil
160 }
161 return android.Paths{}, nil
162 }
163 default:
164 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
165 }
166}
167
Ivan Lozano52767be2019-10-18 14:49:46 -0700168func (mod *Module) SelectedStl() string {
169 return ""
170}
171
Ivan Lozano2b262972019-11-21 12:30:50 -0800172func (mod *Module) NonCcVariants() bool {
173 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400174 if _, ok := mod.compiler.(libraryInterface); ok {
175 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800176 }
177 }
178 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
179}
180
Ivan Lozano52767be2019-10-18 14:49:46 -0700181func (mod *Module) Static() bool {
182 if mod.compiler != nil {
183 if library, ok := mod.compiler.(libraryInterface); ok {
184 return library.static()
185 }
186 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400187 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700188}
189
190func (mod *Module) Shared() bool {
191 if mod.compiler != nil {
192 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400193 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700194 }
195 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400196 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700197}
198
199func (mod *Module) Toc() android.OptionalPath {
200 if mod.compiler != nil {
201 if _, ok := mod.compiler.(libraryInterface); ok {
202 return android.OptionalPath{}
203 }
204 }
205 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
206}
207
Colin Crossc511bc52020-04-07 16:50:32 +0000208func (mod *Module) UseSdk() bool {
209 return false
210}
211
Ivan Lozano6a884432020-12-02 09:15:16 -0500212// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
213// "product" and "vendor" variant modules return true for this function.
214// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
215// "soc_specific: true" and more vendor installed modules are included here.
216// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
217// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700218func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500219 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700220}
221
222func (mod *Module) MustUseVendorVariant() bool {
223 return false
224}
225
226func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500227 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700228 return false
229}
230
Ivan Lozanof9e21722020-12-02 09:00:51 -0500231func (mod *Module) IsVndkExt() bool {
232 return false
233}
234
Colin Cross127bb8b2020-12-16 16:46:01 -0800235func (c *Module) IsVndkPrivate() bool {
236 return false
237}
238
239func (c *Module) IsLlndk() bool {
240 return false
241}
242
243func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500244 return false
245}
246
Ivan Lozano52767be2019-10-18 14:49:46 -0700247func (mod *Module) SdkVersion() string {
248 return ""
249}
250
Colin Crossc511bc52020-04-07 16:50:32 +0000251func (mod *Module) AlwaysSdk() bool {
252 return false
253}
254
Jiyong Park2286afd2020-06-16 21:58:53 +0900255func (mod *Module) IsSdkVariant() bool {
256 return false
257}
258
Colin Cross1348ce32020-10-01 13:37:16 -0700259func (mod *Module) SplitPerApiLevel() bool {
260 return false
261}
262
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263type Deps struct {
264 Dylibs []string
265 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700266 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400267 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268 ProcMacros []string
269 SharedLibs []string
270 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800271 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700272
273 CrtBegin, CrtEnd string
274}
275
276type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500277 DyLibs RustLibraries
278 RLibs RustLibraries
279 SharedLibs android.Paths
280 SharedLibDeps android.Paths
281 StaticLibs android.Paths
282 ProcMacros RustLibraries
283 linkDirs []string
284 depFlags []string
285 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700287
Ivan Lozano45901ed2020-07-24 16:05:01 -0400288 // Used by bindgen modules which call clang
289 depClangFlags []string
290 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400291 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400292 depSystemIncludePaths android.Paths
293
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400294 coverageFiles android.Paths
295
Ivan Lozanof1c84332019-09-20 11:00:37 -0700296 CrtBegin android.OptionalPath
297 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700298
299 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500300 SrcDeps android.Paths
301 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700302}
303
304type RustLibraries []RustLibrary
305
306type RustLibrary struct {
307 Path android.Path
308 CrateName string
309}
310
311type compiler interface {
312 compilerFlags(ctx ModuleContext, flags Flags) Flags
313 compilerProps() []interface{}
314 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
315 compilerDeps(ctx DepsContext, deps Deps) Deps
316 crateName() string
317
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800318 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200319 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700320 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400321
322 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400323
324 Disabled() bool
325 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400326
Ivan Lozanodd055472020-09-28 13:22:45 -0400327 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500328 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400329}
330
Matthew Maurerbb3add12020-06-25 09:34:12 -0700331type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700332 exportLinkDirs(...string)
333 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400334 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700335}
336
337type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400338 depFlags []string
339 linkDirs []string
340 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700341}
342
Matthew Maurerbb3add12020-06-25 09:34:12 -0700343func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
344 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
345}
346
347func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
348 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
349}
350
Ivan Lozano2093af22020-08-25 12:48:19 -0400351func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
352 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
353}
354
Colin Cross0de8a1e2020-09-18 14:15:30 -0700355func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
356 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
357 Flags: flagExporter.depFlags,
358 LinkDirs: flagExporter.linkDirs,
359 LinkObjects: flagExporter.linkObjects,
360 })
361}
362
Matthew Maurerbb3add12020-06-25 09:34:12 -0700363var _ exportedFlagsProducer = (*flagExporter)(nil)
364
365func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700366 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700367}
368
Colin Cross0de8a1e2020-09-18 14:15:30 -0700369type FlagExporterInfo struct {
370 Flags []string
371 LinkDirs []string // TODO: this should be android.Paths
372 LinkObjects []string // TODO: this should be android.Paths
373}
374
375var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
376
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400377func (mod *Module) isCoverageVariant() bool {
378 return mod.coverage.Properties.IsCoverageVariant
379}
380
381var _ cc.Coverage = (*Module)(nil)
382
383func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
384 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
385}
386
387func (mod *Module) PreventInstall() {
388 mod.Properties.PreventInstall = true
389}
390
391func (mod *Module) HideFromMake() {
392 mod.Properties.HideFromMake = true
393}
394
395func (mod *Module) MarkAsCoverageVariant(coverage bool) {
396 mod.coverage.Properties.IsCoverageVariant = coverage
397}
398
399func (mod *Module) EnableCoverageIfNeeded() {
400 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700401}
402
403func defaultsFactory() android.Module {
404 return DefaultsFactory()
405}
406
407type Defaults struct {
408 android.ModuleBase
409 android.DefaultsModuleBase
410}
411
412func DefaultsFactory(props ...interface{}) android.Module {
413 module := &Defaults{}
414
415 module.AddProperties(props...)
416 module.AddProperties(
417 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500418 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400419 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420 &BaseCompilerProperties{},
421 &BinaryCompilerProperties{},
422 &LibraryCompilerProperties{},
423 &ProcMacroCompilerProperties{},
424 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400425 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700426 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400427 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400428 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200429 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500430 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431 )
432
433 android.InitDefaultsModule(module)
434 return module
435}
436
437func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700438 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700439}
440
Ivan Lozano183a3212019-10-18 14:18:45 -0700441func (mod *Module) CcLibrary() bool {
442 if mod.compiler != nil {
443 if _, ok := mod.compiler.(*libraryDecorator); ok {
444 return true
445 }
446 }
447 return false
448}
449
450func (mod *Module) CcLibraryInterface() bool {
451 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400452 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
453 // VariantIs{Static,Shared} is set.
454 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700455 return true
456 }
457 }
458 return false
459}
460
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800461func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700462 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700463 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800464 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700465 }
466 }
467 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
468}
469
470func (mod *Module) SetStatic() {
471 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700472 if library, ok := mod.compiler.(libraryInterface); ok {
473 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700474 return
475 }
476 }
477 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
478}
479
480func (mod *Module) SetShared() {
481 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700482 if library, ok := mod.compiler.(libraryInterface); ok {
483 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700484 return
485 }
486 }
487 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
488}
489
Ivan Lozano183a3212019-10-18 14:18:45 -0700490func (mod *Module) BuildStaticVariant() bool {
491 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700492 if library, ok := mod.compiler.(libraryInterface); ok {
493 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700494 }
495 }
496 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
497}
498
499func (mod *Module) BuildSharedVariant() bool {
500 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700501 if library, ok := mod.compiler.(libraryInterface); ok {
502 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700503 }
504 }
505 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
506}
507
Ivan Lozano183a3212019-10-18 14:18:45 -0700508func (mod *Module) Module() android.Module {
509 return mod
510}
511
Ivan Lozano183a3212019-10-18 14:18:45 -0700512func (mod *Module) OutputFile() android.OptionalPath {
513 return mod.outputFile
514}
515
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400516func (mod *Module) CoverageFiles() android.Paths {
517 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700518 if !mod.compiler.nativeCoverage() {
519 return android.Paths{}
520 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400521 if library, ok := mod.compiler.(*libraryDecorator); ok {
522 if library.coverageFile != nil {
523 return android.Paths{library.coverageFile}
524 }
525 return android.Paths{}
526 }
527 }
528 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
529}
530
Jiyong Park459feca2020-12-15 11:02:21 +0900531func (mod *Module) installable(apexInfo android.ApexInfo) bool {
532 // The apex variant is not installable because it is included in the APEX and won't appear
533 // in the system partition as a standalone file.
534 if !apexInfo.IsForPlatform() {
535 return false
536 }
537
538 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
539}
540
Ivan Lozano183a3212019-10-18 14:18:45 -0700541var _ cc.LinkableInterface = (*Module)(nil)
542
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543func (mod *Module) Init() android.Module {
544 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500545 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546
547 if mod.compiler != nil {
548 mod.AddProperties(mod.compiler.compilerProps()...)
549 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400550 if mod.coverage != nil {
551 mod.AddProperties(mod.coverage.props()...)
552 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200553 if mod.clippy != nil {
554 mod.AddProperties(mod.clippy.props()...)
555 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400556 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700557 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400558 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500559 if mod.sanitize != nil {
560 mod.AddProperties(mod.sanitize.props()...)
561 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400562
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900564 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700565
566 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567 return mod
568}
569
570func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
571 return &Module{
572 hod: hod,
573 multilib: multilib,
574 }
575}
576func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
577 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400578 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200579 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500580 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700581 return module
582}
583
584type ModuleContext interface {
585 android.ModuleContext
586 ModuleContextIntf
587}
588
589type BaseModuleContext interface {
590 android.BaseModuleContext
591 ModuleContextIntf
592}
593
594type DepsContext interface {
595 android.BottomUpMutatorContext
596 ModuleContextIntf
597}
598
599type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200600 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700601 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602}
603
604type depsContext struct {
605 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
608type moduleContext struct {
609 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700610}
611
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200612type baseModuleContext struct {
613 android.BaseModuleContext
614}
615
616func (ctx *moduleContext) RustModule() *Module {
617 return ctx.Module().(*Module)
618}
619
620func (ctx *moduleContext) toolchain() config.Toolchain {
621 return ctx.RustModule().toolchain(ctx)
622}
623
624func (ctx *depsContext) RustModule() *Module {
625 return ctx.Module().(*Module)
626}
627
628func (ctx *depsContext) toolchain() config.Toolchain {
629 return ctx.RustModule().toolchain(ctx)
630}
631
632func (ctx *baseModuleContext) RustModule() *Module {
633 return ctx.Module().(*Module)
634}
635
636func (ctx *baseModuleContext) toolchain() config.Toolchain {
637 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400638}
639
640func (mod *Module) nativeCoverage() bool {
641 return mod.compiler != nil && mod.compiler.nativeCoverage()
642}
643
Ivan Lozanoffee3342019-08-27 12:03:00 -0700644func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
645 if mod.cachedToolchain == nil {
646 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
647 }
648 return mod.cachedToolchain
649}
650
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200651func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
652 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
653}
654
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
656}
657
658func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
659 ctx := &moduleContext{
660 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662
Jiyong Park99644e92020-11-17 22:21:02 +0900663 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
664 if !apexInfo.IsForPlatform() {
665 mod.hideApexVariantFromMake = true
666 }
667
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500669 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
670
671 // Differentiate static libraries that are vendor available
672 if mod.UseVndk() {
673 mod.Properties.SubName += ".vendor"
674 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675
676 if !toolchain.Supported() {
677 // This toolchain's unsupported, there's nothing to do for this mod.
678 return
679 }
680
681 deps := mod.depsToPaths(ctx)
682 flags := Flags{
683 Toolchain: toolchain,
684 }
685
686 if mod.compiler != nil {
687 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400688 }
689 if mod.coverage != nil {
690 flags, deps = mod.coverage.flags(ctx, flags, deps)
691 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200692 if mod.clippy != nil {
693 flags, deps = mod.clippy.flags(ctx, flags, deps)
694 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500695 if mod.sanitize != nil {
696 flags, deps = mod.sanitize.flags(ctx, flags, deps)
697 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400698
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200699 // SourceProvider needs to call GenerateSource() before compiler calls
700 // compile() so it can provide the source. A SourceProvider has
701 // multiple variants (e.g. source, rlib, dylib). Only the "source"
702 // variant is responsible for effectively generating the source. The
703 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400704 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200705 if mod.compiler.(libraryInterface).source() {
706 mod.sourceProvider.GenerateSource(ctx, deps)
707 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
708 } else {
709 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
710 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700711 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200712 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400713 }
714
715 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700716 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400717
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900719
720 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
721 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200722 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400723 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724 }
725}
726
727func (mod *Module) deps(ctx DepsContext) Deps {
728 deps := Deps{}
729
730 if mod.compiler != nil {
731 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400732 }
733 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700734 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700735 }
736
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400737 if mod.coverage != nil {
738 deps = mod.coverage.deps(ctx, deps)
739 }
740
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500741 if mod.sanitize != nil {
742 deps = mod.sanitize.deps(ctx, deps)
743 }
744
Ivan Lozanoffee3342019-08-27 12:03:00 -0700745 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
746 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700747 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
749 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
750 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
751
752 return deps
753
754}
755
Ivan Lozanoffee3342019-08-27 12:03:00 -0700756type dependencyTag struct {
757 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800758 name string
759 library bool
760 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761}
762
Jiyong Park65b62242020-11-25 12:44:59 +0900763// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
764// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
765func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800766 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900767}
768
769var _ android.InstallNeededDependencyTag = dependencyTag{}
770
Ivan Lozanoffee3342019-08-27 12:03:00 -0700771var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400772 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
773 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
774 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800775 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400776 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200777 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778)
779
Jiyong Park99644e92020-11-17 22:21:02 +0900780func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
781 tag, ok := depTag.(dependencyTag)
782 return ok && tag == dylibDepTag
783}
784
Matthew Maurer0f003b12020-06-29 14:34:06 -0700785type autoDep struct {
786 variation string
787 depTag dependencyTag
788}
789
790var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200791 rlibVariation = "rlib"
792 dylibVariation = "dylib"
793 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
794 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700795)
796
797type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400798 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700799}
800
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400801func (mod *Module) begin(ctx BaseModuleContext) {
802 if mod.coverage != nil {
803 mod.coverage.begin(ctx)
804 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500805 if mod.sanitize != nil {
806 mod.sanitize.begin(ctx)
807 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400808}
809
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
811 var depPaths PathDeps
812
813 directRlibDeps := []*Module{}
814 directDylibDeps := []*Module{}
815 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700816 directSharedLibDeps := [](cc.LinkableInterface){}
817 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400818 directSrcProvidersDeps := []*Module{}
819 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700820
821 ctx.VisitDirectDeps(func(dep android.Module) {
822 depName := ctx.OtherModuleName(dep)
823 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400824 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700826
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 switch depTag {
828 case dylibDepTag:
829 dylib, ok := rustDep.compiler.(libraryInterface)
830 if !ok || !dylib.dylib() {
831 ctx.ModuleErrorf("mod %q not an dylib library", depName)
832 return
833 }
834 directDylibDeps = append(directDylibDeps, rustDep)
835 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
836 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400837
Ivan Lozanoffee3342019-08-27 12:03:00 -0700838 rlib, ok := rustDep.compiler.(libraryInterface)
839 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400840 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 return
842 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400843 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400845 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 case procMacroDepTag:
847 directProcMacroDeps = append(directProcMacroDeps, rustDep)
848 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400849 case android.SourceDepTag:
850 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
851 // OS/Arch variant is used.
852 var helper string
853 if ctx.Host() {
854 helper = "missing 'host_supported'?"
855 } else {
856 helper = "device module defined?"
857 }
858
859 if dep.Target().Os != ctx.Os() {
860 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
861 return
862 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
863 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
864 return
865 }
866 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700867 }
868
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400869 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700870 if depTag != procMacroDepTag {
871 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
872 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
873 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
874 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700875 }
876
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400878 linkFile := rustDep.outputFile
879 if !linkFile.Valid() {
880 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
881 depName, ctx.ModuleName())
882 return
883 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700885 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
886 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700887 }
888 }
889
Ivan Lozano89435d12020-07-31 11:01:18 -0400890 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700891 //Handle C dependencies
892 if _, ok := ccDep.(*Module); !ok {
893 if ccDep.Module().Target().Os != ctx.Os() {
894 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
895 return
896 }
897 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
898 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
899 return
900 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700901 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400902 linkObject := ccDep.OutputFile()
903 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500904
Ivan Lozano2093af22020-08-25 12:48:19 -0400905 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700906 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
907 }
908
909 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700910 switch {
911 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400913 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700914 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
915 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
916 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
917 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
918 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400919 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920 directStaticLibDeps = append(directStaticLibDeps, ccDep)
921 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700922 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700923 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400924 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700925 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
926 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
927 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
928 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
929 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930 directSharedLibDeps = append(directSharedLibDeps, ccDep)
931 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
932 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800933 case cc.IsHeaderDepTag(depTag):
934 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
935 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
936 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
937 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700938 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400939 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700940 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400941 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700942 }
943
944 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700945 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
946 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400947 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700948 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400950
951 if srcDep, ok := dep.(android.SourceFileProducer); ok {
952 switch depTag {
953 case android.SourceDepTag:
954 // These are usually genrules which don't have per-target variants.
955 directSrcDeps = append(directSrcDeps, srcDep)
956 }
957 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700958 })
959
960 var rlibDepFiles RustLibraries
961 for _, dep := range directRlibDeps {
962 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
963 }
964 var dylibDepFiles RustLibraries
965 for _, dep := range directDylibDeps {
966 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
967 }
968 var procMacroDepFiles RustLibraries
969 for _, dep := range directProcMacroDeps {
970 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
971 }
972
973 var staticLibDepFiles android.Paths
974 for _, dep := range directStaticLibDeps {
975 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
976 }
977
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500978 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979 var sharedLibDepFiles android.Paths
980 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500981 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
982 if dep.Toc().Valid() {
983 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
984 } else {
985 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
986 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700987 }
988
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400989 var srcProviderDepFiles android.Paths
990 for _, dep := range directSrcProvidersDeps {
991 srcs, _ := dep.OutputFiles("")
992 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
993 }
994 for _, dep := range directSrcDeps {
995 srcs := dep.Srcs()
996 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
997 }
998
Ivan Lozanoffee3342019-08-27 12:03:00 -0700999 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1000 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1001 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001002 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001003 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1004 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001005 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001006
1007 // Dedup exported flags from dependencies
1008 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001009 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001010 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001011 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1012 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1013 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014
1015 return depPaths
1016}
1017
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001018func (mod *Module) InstallInData() bool {
1019 if mod.compiler == nil {
1020 return false
1021 }
1022 return mod.compiler.inData()
1023}
1024
Ivan Lozanoffee3342019-08-27 12:03:00 -07001025func linkPathFromFilePath(filepath android.Path) string {
1026 return strings.Split(filepath.String(), filepath.Base())[0]
1027}
Ivan Lozanod648c432020-02-06 12:05:10 -05001028
Ivan Lozanoffee3342019-08-27 12:03:00 -07001029func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1030 ctx := &depsContext{
1031 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033
1034 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001035 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001036
Ivan Lozano2b081132020-09-08 12:46:52 -04001037 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001038 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001039 stdLinkage = "rlib-std"
1040 }
1041
1042 rlibDepVariations := commonDepVariations
1043 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1044 rlibDepVariations = append(rlibDepVariations,
1045 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1046 }
1047
Ivan Lozano52767be2019-10-18 14:49:46 -07001048 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001049 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001050 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001051 rlibDepTag, deps.Rlibs...)
1052 actx.AddVariationDependencies(
1053 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001054 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001055 dylibDepTag, deps.Dylibs...)
1056
Ivan Lozano042504f2020-08-18 14:31:23 -04001057 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1058 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001059 if autoDep.depTag == rlibDepTag {
1060 actx.AddVariationDependencies(
1061 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1062 autoDep.depTag, deps.Rustlibs...)
1063 } else {
1064 actx.AddVariationDependencies(
1065 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1066 autoDep.depTag, deps.Rustlibs...)
1067 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001068 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001069 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001070 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001071 actx.AddVariationDependencies(
1072 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1073 rlibDepTag, deps.Stdlibs...)
1074 } else {
1075 actx.AddVariationDependencies(
1076 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1077 dylibDepTag, deps.Stdlibs...)
1078 }
1079 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001080 actx.AddVariationDependencies(append(commonDepVariations,
1081 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001082 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001083 actx.AddVariationDependencies(append(commonDepVariations,
1084 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001085 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001086
Zach Johnson3df4e632020-11-06 11:56:27 -08001087 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1088
Colin Cross565cafd2020-09-25 18:47:38 -07001089 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001090 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001091 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001092 }
1093 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001094 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001095 }
1096
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001097 if mod.sourceProvider != nil {
1098 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1099 bindgen.Properties.Custom_bindgen != "" {
1100 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1101 bindgen.Properties.Custom_bindgen)
1102 }
1103 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001104 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001105 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001106}
1107
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001108func BeginMutator(ctx android.BottomUpMutatorContext) {
1109 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1110 mod.beginMutator(ctx)
1111 }
1112}
1113
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001114func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1115 ctx := &baseModuleContext{
1116 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001117 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001118
1119 mod.begin(ctx)
1120}
1121
Ivan Lozanoffee3342019-08-27 12:03:00 -07001122func (mod *Module) Name() string {
1123 name := mod.ModuleBase.Name()
1124 if p, ok := mod.compiler.(interface {
1125 Name(string) string
1126 }); ok {
1127 name = p.Name(name)
1128 }
1129 return name
1130}
1131
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001132func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001133 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001134 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001135 }
1136}
1137
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001138var _ android.HostToolProvider = (*Module)(nil)
1139
1140func (mod *Module) HostToolPath() android.OptionalPath {
1141 if !mod.Host() {
1142 return android.OptionalPath{}
1143 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001144 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1145 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001146 }
1147 return android.OptionalPath{}
1148}
1149
Jiyong Park99644e92020-11-17 22:21:02 +09001150var _ android.ApexModule = (*Module)(nil)
1151
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001152func (mod *Module) minSdkVersion() string {
1153 return String(mod.Properties.Min_sdk_version)
1154}
1155
Jiyong Park45bf82e2020-12-15 22:29:02 +09001156var _ android.ApexModule = (*Module)(nil)
1157
1158// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001159func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001160 minSdkVersion := mod.minSdkVersion()
1161 if minSdkVersion == "apex_inherit" {
1162 return nil
1163 }
1164 if minSdkVersion == "" {
1165 return fmt.Errorf("min_sdk_version is not specificed")
1166 }
1167
1168 // Not using nativeApiLevelFromUser because the context here is not
1169 // necessarily a native context.
1170 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1171 if err != nil {
1172 return err
1173 }
1174
1175 if ver.GreaterThan(sdkVersion) {
1176 return fmt.Errorf("newer SDK(%v)", ver)
1177 }
Jiyong Park99644e92020-11-17 22:21:02 +09001178 return nil
1179}
1180
Jiyong Park45bf82e2020-12-15 22:29:02 +09001181// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001182func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1183 depTag := ctx.OtherModuleDependencyTag(dep)
1184
1185 if ccm, ok := dep.(*cc.Module); ok {
1186 if ccm.HasStubsVariants() {
1187 if cc.IsSharedDepTag(depTag) {
1188 // dynamic dep to a stubs lib crosses APEX boundary
1189 return false
1190 }
1191 if cc.IsRuntimeDepTag(depTag) {
1192 // runtime dep to a stubs lib also crosses APEX boundary
1193 return false
1194 }
1195
1196 if cc.IsHeaderDepTag(depTag) {
1197 return false
1198 }
1199 }
1200 if mod.Static() && cc.IsSharedDepTag(depTag) {
1201 // shared_lib dependency from a static lib is considered as crossing
1202 // the APEX boundary because the dependency doesn't actually is
1203 // linked; the dependency is used only during the compilation phase.
1204 return false
1205 }
1206 }
1207
1208 if depTag == procMacroDepTag {
1209 return false
1210 }
1211
1212 return true
1213}
1214
1215// Overrides ApexModule.IsInstallabeToApex()
1216func (mod *Module) IsInstallableToApex() bool {
1217 if mod.compiler != nil {
1218 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1219 return true
1220 }
1221 if _, ok := mod.compiler.(*binaryDecorator); ok {
1222 return true
1223 }
1224 }
1225 return false
1226}
1227
Ivan Lozanoffee3342019-08-27 12:03:00 -07001228var Bool = proptools.Bool
1229var BoolDefault = proptools.BoolDefault
1230var String = proptools.String
1231var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001232
1233var _ android.OutputFileProducer = (*Module)(nil)