blob: 1fa97af962b3f5701a97378839d164807dbf3f8b [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 Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040051 GlobalRustFlags []string // Flags that apply globally to rust
52 GlobalLinkFlags []string // Flags that apply globally to linker
53 RustFlags []string // Flags that apply to rust
54 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070056 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040057 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61type BaseProperties struct {
62 AndroidMkRlibs []string
63 AndroidMkDylibs []string
64 AndroidMkProcMacroLibs []string
65 AndroidMkSharedLibs []string
66 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040067
Ivan Lozano6a884432020-12-02 09:15:16 -050068 ImageVariationPrefix string `blueprint:"mutated"`
69 VndkVersion string `blueprint:"mutated"`
70 SubName string `blueprint:"mutated"`
71
72 // Set by imageMutator
73 CoreVariantNeeded bool `blueprint:"mutated"`
74 ExtraVariants []string `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040075
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050076 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
77 Min_sdk_version *string
78
Ivan Lozano43845682020-07-09 21:03:28 -040079 PreventInstall bool
80 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070081}
82
83type Module struct {
84 android.ModuleBase
85 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090086 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070087
Ivan Lozano6a884432020-12-02 09:15:16 -050088 VendorProperties cc.VendorProperties
89
Ivan Lozanoffee3342019-08-27 12:03:00 -070090 Properties BaseProperties
91
92 hod android.HostOrDeviceSupported
93 multilib android.Multilib
94
Ivan Lozano6a884432020-12-02 09:15:16 -050095 makeLinkType string
96
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040098 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020099 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400101 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700102 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400103
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200104 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900105
106 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107}
108
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500109func (mod *Module) Header() bool {
110 //TODO: If Rust libraries provide header variants, this needs to be updated.
111 return false
112}
113
114func (mod *Module) SetPreventInstall() {
115 mod.Properties.PreventInstall = true
116}
117
118// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
119func (mod *Module) InVendor() bool {
120 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
121}
122
123func (mod *Module) SetHideFromMake() {
124 mod.Properties.HideFromMake = true
125}
126
127func (mod *Module) SanitizePropDefined() bool {
128 return false
129}
130
131func (mod *Module) IsDependencyRoot() bool {
132 if mod.compiler != nil {
133 return mod.compiler.isDependencyRoot()
134 }
135 panic("IsDependencyRoot called on a non-compiler Rust module")
136}
137
138func (mod *Module) IsPrebuilt() bool {
139 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
140 return true
141 }
142 return false
143}
144
Ivan Lozano43845682020-07-09 21:03:28 -0400145func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
146 switch tag {
147 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700148 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400149 return mod.sourceProvider.Srcs(), nil
150 } else {
151 if mod.outputFile.Valid() {
152 return android.Paths{mod.outputFile.Path()}, nil
153 }
154 return android.Paths{}, nil
155 }
156 default:
157 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
158 }
159}
160
Ivan Lozano52767be2019-10-18 14:49:46 -0700161func (mod *Module) SelectedStl() string {
162 return ""
163}
164
Ivan Lozano2b262972019-11-21 12:30:50 -0800165func (mod *Module) NonCcVariants() bool {
166 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400167 if _, ok := mod.compiler.(libraryInterface); ok {
168 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800169 }
170 }
171 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
172}
173
Ivan Lozano52767be2019-10-18 14:49:46 -0700174func (mod *Module) Static() bool {
175 if mod.compiler != nil {
176 if library, ok := mod.compiler.(libraryInterface); ok {
177 return library.static()
178 }
179 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400180 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700181}
182
183func (mod *Module) Shared() bool {
184 if mod.compiler != nil {
185 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400186 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700187 }
188 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400189 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700190}
191
192func (mod *Module) Toc() android.OptionalPath {
193 if mod.compiler != nil {
194 if _, ok := mod.compiler.(libraryInterface); ok {
195 return android.OptionalPath{}
196 }
197 }
198 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
199}
200
Colin Crossc511bc52020-04-07 16:50:32 +0000201func (mod *Module) UseSdk() bool {
202 return false
203}
204
Ivan Lozano6a884432020-12-02 09:15:16 -0500205// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
206// "product" and "vendor" variant modules return true for this function.
207// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
208// "soc_specific: true" and more vendor installed modules are included here.
209// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
210// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700211func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500212 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700213}
214
215func (mod *Module) MustUseVendorVariant() bool {
216 return false
217}
218
219func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500220 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700221 return false
222}
223
Ivan Lozanof9e21722020-12-02 09:00:51 -0500224func (mod *Module) IsVndkExt() bool {
225 return false
226}
227
Colin Cross127bb8b2020-12-16 16:46:01 -0800228func (c *Module) IsVndkPrivate() bool {
229 return false
230}
231
232func (c *Module) IsLlndk() bool {
233 return false
234}
235
236func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500237 return false
238}
239
Ivan Lozano52767be2019-10-18 14:49:46 -0700240func (mod *Module) SdkVersion() string {
241 return ""
242}
243
Colin Crossc511bc52020-04-07 16:50:32 +0000244func (mod *Module) AlwaysSdk() bool {
245 return false
246}
247
Jiyong Park2286afd2020-06-16 21:58:53 +0900248func (mod *Module) IsSdkVariant() bool {
249 return false
250}
251
Colin Cross1348ce32020-10-01 13:37:16 -0700252func (mod *Module) SplitPerApiLevel() bool {
253 return false
254}
255
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256type Deps struct {
257 Dylibs []string
258 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700259 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400260 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 ProcMacros []string
262 SharedLibs []string
263 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800264 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265
266 CrtBegin, CrtEnd string
267}
268
269type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400270 DyLibs RustLibraries
271 RLibs RustLibraries
272 SharedLibs android.Paths
273 StaticLibs android.Paths
274 ProcMacros RustLibraries
275 linkDirs []string
276 depFlags []string
277 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700278 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700279
Ivan Lozano45901ed2020-07-24 16:05:01 -0400280 // Used by bindgen modules which call clang
281 depClangFlags []string
282 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400283 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400284 depSystemIncludePaths android.Paths
285
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400286 coverageFiles android.Paths
287
Ivan Lozanof1c84332019-09-20 11:00:37 -0700288 CrtBegin android.OptionalPath
289 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700290
291 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500292 SrcDeps android.Paths
293 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294}
295
296type RustLibraries []RustLibrary
297
298type RustLibrary struct {
299 Path android.Path
300 CrateName string
301}
302
303type compiler interface {
304 compilerFlags(ctx ModuleContext, flags Flags) Flags
305 compilerProps() []interface{}
306 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
307 compilerDeps(ctx DepsContext, deps Deps) Deps
308 crateName() string
309
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800310 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200311 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700312 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400313
314 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400315
316 Disabled() bool
317 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400318
Ivan Lozanodd055472020-09-28 13:22:45 -0400319 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500320 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400321}
322
Matthew Maurerbb3add12020-06-25 09:34:12 -0700323type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700324 exportLinkDirs(...string)
325 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400326 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700327}
328
329type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400330 depFlags []string
331 linkDirs []string
332 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700333}
334
Matthew Maurerbb3add12020-06-25 09:34:12 -0700335func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
336 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
337}
338
339func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
340 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
341}
342
Ivan Lozano2093af22020-08-25 12:48:19 -0400343func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
344 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
345}
346
Colin Cross0de8a1e2020-09-18 14:15:30 -0700347func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
348 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
349 Flags: flagExporter.depFlags,
350 LinkDirs: flagExporter.linkDirs,
351 LinkObjects: flagExporter.linkObjects,
352 })
353}
354
Matthew Maurerbb3add12020-06-25 09:34:12 -0700355var _ exportedFlagsProducer = (*flagExporter)(nil)
356
357func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700358 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700359}
360
Colin Cross0de8a1e2020-09-18 14:15:30 -0700361type FlagExporterInfo struct {
362 Flags []string
363 LinkDirs []string // TODO: this should be android.Paths
364 LinkObjects []string // TODO: this should be android.Paths
365}
366
367var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
368
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400369func (mod *Module) isCoverageVariant() bool {
370 return mod.coverage.Properties.IsCoverageVariant
371}
372
373var _ cc.Coverage = (*Module)(nil)
374
375func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
376 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
377}
378
379func (mod *Module) PreventInstall() {
380 mod.Properties.PreventInstall = true
381}
382
383func (mod *Module) HideFromMake() {
384 mod.Properties.HideFromMake = true
385}
386
387func (mod *Module) MarkAsCoverageVariant(coverage bool) {
388 mod.coverage.Properties.IsCoverageVariant = coverage
389}
390
391func (mod *Module) EnableCoverageIfNeeded() {
392 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393}
394
395func defaultsFactory() android.Module {
396 return DefaultsFactory()
397}
398
399type Defaults struct {
400 android.ModuleBase
401 android.DefaultsModuleBase
402}
403
404func DefaultsFactory(props ...interface{}) android.Module {
405 module := &Defaults{}
406
407 module.AddProperties(props...)
408 module.AddProperties(
409 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500410 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400411 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700412 &BaseCompilerProperties{},
413 &BinaryCompilerProperties{},
414 &LibraryCompilerProperties{},
415 &ProcMacroCompilerProperties{},
416 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400417 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700418 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400419 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400420 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200421 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700422 )
423
424 android.InitDefaultsModule(module)
425 return module
426}
427
428func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700429 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430}
431
Ivan Lozano183a3212019-10-18 14:18:45 -0700432func (mod *Module) CcLibrary() bool {
433 if mod.compiler != nil {
434 if _, ok := mod.compiler.(*libraryDecorator); ok {
435 return true
436 }
437 }
438 return false
439}
440
441func (mod *Module) CcLibraryInterface() bool {
442 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400443 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
444 // VariantIs{Static,Shared} is set.
445 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700446 return true
447 }
448 }
449 return false
450}
451
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800452func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700453 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700454 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800455 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700456 }
457 }
458 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
459}
460
461func (mod *Module) SetStatic() {
462 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700463 if library, ok := mod.compiler.(libraryInterface); ok {
464 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700465 return
466 }
467 }
468 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
469}
470
471func (mod *Module) SetShared() {
472 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700473 if library, ok := mod.compiler.(libraryInterface); ok {
474 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700475 return
476 }
477 }
478 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
479}
480
Ivan Lozano183a3212019-10-18 14:18:45 -0700481func (mod *Module) BuildStaticVariant() bool {
482 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700483 if library, ok := mod.compiler.(libraryInterface); ok {
484 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700485 }
486 }
487 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
488}
489
490func (mod *Module) BuildSharedVariant() bool {
491 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700492 if library, ok := mod.compiler.(libraryInterface); ok {
493 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700494 }
495 }
496 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
497}
498
Ivan Lozano183a3212019-10-18 14:18:45 -0700499func (mod *Module) Module() android.Module {
500 return mod
501}
502
Ivan Lozano183a3212019-10-18 14:18:45 -0700503func (mod *Module) OutputFile() android.OptionalPath {
504 return mod.outputFile
505}
506
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400507func (mod *Module) CoverageFiles() android.Paths {
508 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700509 if !mod.compiler.nativeCoverage() {
510 return android.Paths{}
511 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512 if library, ok := mod.compiler.(*libraryDecorator); ok {
513 if library.coverageFile != nil {
514 return android.Paths{library.coverageFile}
515 }
516 return android.Paths{}
517 }
518 }
519 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
520}
521
Jiyong Park459feca2020-12-15 11:02:21 +0900522func (mod *Module) installable(apexInfo android.ApexInfo) bool {
523 // The apex variant is not installable because it is included in the APEX and won't appear
524 // in the system partition as a standalone file.
525 if !apexInfo.IsForPlatform() {
526 return false
527 }
528
529 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
530}
531
Ivan Lozano183a3212019-10-18 14:18:45 -0700532var _ cc.LinkableInterface = (*Module)(nil)
533
Ivan Lozanoffee3342019-08-27 12:03:00 -0700534func (mod *Module) Init() android.Module {
535 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500536 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700537
538 if mod.compiler != nil {
539 mod.AddProperties(mod.compiler.compilerProps()...)
540 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400541 if mod.coverage != nil {
542 mod.AddProperties(mod.coverage.props()...)
543 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200544 if mod.clippy != nil {
545 mod.AddProperties(mod.clippy.props()...)
546 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400547 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700548 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400549 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400550
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900552 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553
554 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555 return mod
556}
557
558func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
559 return &Module{
560 hod: hod,
561 multilib: multilib,
562 }
563}
564func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
565 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400566 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200567 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568 return module
569}
570
571type ModuleContext interface {
572 android.ModuleContext
573 ModuleContextIntf
574}
575
576type BaseModuleContext interface {
577 android.BaseModuleContext
578 ModuleContextIntf
579}
580
581type DepsContext interface {
582 android.BottomUpMutatorContext
583 ModuleContextIntf
584}
585
586type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200587 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589}
590
591type depsContext struct {
592 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593}
594
595type moduleContext struct {
596 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700597}
598
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200599type baseModuleContext struct {
600 android.BaseModuleContext
601}
602
603func (ctx *moduleContext) RustModule() *Module {
604 return ctx.Module().(*Module)
605}
606
607func (ctx *moduleContext) toolchain() config.Toolchain {
608 return ctx.RustModule().toolchain(ctx)
609}
610
611func (ctx *depsContext) RustModule() *Module {
612 return ctx.Module().(*Module)
613}
614
615func (ctx *depsContext) toolchain() config.Toolchain {
616 return ctx.RustModule().toolchain(ctx)
617}
618
619func (ctx *baseModuleContext) RustModule() *Module {
620 return ctx.Module().(*Module)
621}
622
623func (ctx *baseModuleContext) toolchain() config.Toolchain {
624 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400625}
626
627func (mod *Module) nativeCoverage() bool {
628 return mod.compiler != nil && mod.compiler.nativeCoverage()
629}
630
Ivan Lozanoffee3342019-08-27 12:03:00 -0700631func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
632 if mod.cachedToolchain == nil {
633 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
634 }
635 return mod.cachedToolchain
636}
637
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200638func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
639 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
640}
641
Ivan Lozanoffee3342019-08-27 12:03:00 -0700642func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
643}
644
645func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
646 ctx := &moduleContext{
647 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649
Jiyong Park99644e92020-11-17 22:21:02 +0900650 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
651 if !apexInfo.IsForPlatform() {
652 mod.hideApexVariantFromMake = true
653 }
654
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500656 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
657
658 // Differentiate static libraries that are vendor available
659 if mod.UseVndk() {
660 mod.Properties.SubName += ".vendor"
661 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700662
663 if !toolchain.Supported() {
664 // This toolchain's unsupported, there's nothing to do for this mod.
665 return
666 }
667
668 deps := mod.depsToPaths(ctx)
669 flags := Flags{
670 Toolchain: toolchain,
671 }
672
673 if mod.compiler != nil {
674 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400675 }
676 if mod.coverage != nil {
677 flags, deps = mod.coverage.flags(ctx, flags, deps)
678 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200679 if mod.clippy != nil {
680 flags, deps = mod.clippy.flags(ctx, flags, deps)
681 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400682
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200683 // SourceProvider needs to call GenerateSource() before compiler calls
684 // compile() so it can provide the source. A SourceProvider has
685 // multiple variants (e.g. source, rlib, dylib). Only the "source"
686 // variant is responsible for effectively generating the source. The
687 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400688 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200689 if mod.compiler.(libraryInterface).source() {
690 mod.sourceProvider.GenerateSource(ctx, deps)
691 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
692 } else {
693 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
694 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700695 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200696 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400697 }
698
699 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700700 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400701
Ivan Lozanoffee3342019-08-27 12:03:00 -0700702 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900703
704 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
705 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200706 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400707 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708 }
709}
710
711func (mod *Module) deps(ctx DepsContext) Deps {
712 deps := Deps{}
713
714 if mod.compiler != nil {
715 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400716 }
717 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700718 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719 }
720
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400721 if mod.coverage != nil {
722 deps = mod.coverage.deps(ctx, deps)
723 }
724
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
726 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700727 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700728 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
729 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
730 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
731
732 return deps
733
734}
735
Ivan Lozanoffee3342019-08-27 12:03:00 -0700736type dependencyTag struct {
737 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800738 name string
739 library bool
740 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741}
742
Jiyong Park65b62242020-11-25 12:44:59 +0900743// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
744// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
745func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800746 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900747}
748
749var _ android.InstallNeededDependencyTag = dependencyTag{}
750
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400752 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
753 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
754 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800755 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400756 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200757 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758)
759
Jiyong Park99644e92020-11-17 22:21:02 +0900760func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
761 tag, ok := depTag.(dependencyTag)
762 return ok && tag == dylibDepTag
763}
764
Matthew Maurer0f003b12020-06-29 14:34:06 -0700765type autoDep struct {
766 variation string
767 depTag dependencyTag
768}
769
770var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200771 rlibVariation = "rlib"
772 dylibVariation = "dylib"
773 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
774 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700775)
776
777type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400778 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700779}
780
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400781func (mod *Module) begin(ctx BaseModuleContext) {
782 if mod.coverage != nil {
783 mod.coverage.begin(ctx)
784 }
785}
786
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
788 var depPaths PathDeps
789
790 directRlibDeps := []*Module{}
791 directDylibDeps := []*Module{}
792 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700793 directSharedLibDeps := [](cc.LinkableInterface){}
794 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400795 directSrcProvidersDeps := []*Module{}
796 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797
798 ctx.VisitDirectDeps(func(dep android.Module) {
799 depName := ctx.OtherModuleName(dep)
800 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400801 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700803
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804 switch depTag {
805 case dylibDepTag:
806 dylib, ok := rustDep.compiler.(libraryInterface)
807 if !ok || !dylib.dylib() {
808 ctx.ModuleErrorf("mod %q not an dylib library", depName)
809 return
810 }
811 directDylibDeps = append(directDylibDeps, rustDep)
812 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
813 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400814
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 rlib, ok := rustDep.compiler.(libraryInterface)
816 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400817 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818 return
819 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400820 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400822 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 case procMacroDepTag:
824 directProcMacroDeps = append(directProcMacroDeps, rustDep)
825 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400826 case android.SourceDepTag:
827 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
828 // OS/Arch variant is used.
829 var helper string
830 if ctx.Host() {
831 helper = "missing 'host_supported'?"
832 } else {
833 helper = "device module defined?"
834 }
835
836 if dep.Target().Os != ctx.Os() {
837 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
838 return
839 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
840 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
841 return
842 }
843 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700844 }
845
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400846 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700847 if depTag != procMacroDepTag {
848 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
849 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
850 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
851 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 }
853
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400855 linkFile := rustDep.outputFile
856 if !linkFile.Valid() {
857 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
858 depName, ctx.ModuleName())
859 return
860 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700862 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
863 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700864 }
865 }
866
Ivan Lozano89435d12020-07-31 11:01:18 -0400867 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700868 //Handle C dependencies
869 if _, ok := ccDep.(*Module); !ok {
870 if ccDep.Module().Target().Os != ctx.Os() {
871 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
872 return
873 }
874 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
875 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
876 return
877 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700878 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400879 linkObject := ccDep.OutputFile()
880 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500881
Ivan Lozano2093af22020-08-25 12:48:19 -0400882 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
884 }
885
886 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700887 switch {
888 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400890 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700891 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
892 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
893 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
894 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
895 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400896 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897 directStaticLibDeps = append(directStaticLibDeps, ccDep)
898 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700899 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400901 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700902 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
903 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
904 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
905 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
906 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907 directSharedLibDeps = append(directSharedLibDeps, ccDep)
908 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
909 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800910 case cc.IsHeaderDepTag(depTag):
911 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
912 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
913 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
914 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700915 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400916 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700917 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400918 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919 }
920
921 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700922 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
923 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400924 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700925 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400927
928 if srcDep, ok := dep.(android.SourceFileProducer); ok {
929 switch depTag {
930 case android.SourceDepTag:
931 // These are usually genrules which don't have per-target variants.
932 directSrcDeps = append(directSrcDeps, srcDep)
933 }
934 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 })
936
937 var rlibDepFiles RustLibraries
938 for _, dep := range directRlibDeps {
939 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
940 }
941 var dylibDepFiles RustLibraries
942 for _, dep := range directDylibDeps {
943 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
944 }
945 var procMacroDepFiles RustLibraries
946 for _, dep := range directProcMacroDeps {
947 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
948 }
949
950 var staticLibDepFiles android.Paths
951 for _, dep := range directStaticLibDeps {
952 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
953 }
954
955 var sharedLibDepFiles android.Paths
956 for _, dep := range directSharedLibDeps {
957 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
958 }
959
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400960 var srcProviderDepFiles android.Paths
961 for _, dep := range directSrcProvidersDeps {
962 srcs, _ := dep.OutputFiles("")
963 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
964 }
965 for _, dep := range directSrcDeps {
966 srcs := dep.Srcs()
967 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
968 }
969
Ivan Lozanoffee3342019-08-27 12:03:00 -0700970 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
971 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
972 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
973 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
974 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400975 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700976
977 // Dedup exported flags from dependencies
978 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
979 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400980 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
981 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
982 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700983
984 return depPaths
985}
986
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800987func (mod *Module) InstallInData() bool {
988 if mod.compiler == nil {
989 return false
990 }
991 return mod.compiler.inData()
992}
993
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994func linkPathFromFilePath(filepath android.Path) string {
995 return strings.Split(filepath.String(), filepath.Base())[0]
996}
Ivan Lozanod648c432020-02-06 12:05:10 -0500997
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
999 ctx := &depsContext{
1000 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001001 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002
1003 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001004 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001005
Ivan Lozano2b081132020-09-08 12:46:52 -04001006 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001007 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001008 stdLinkage = "rlib-std"
1009 }
1010
1011 rlibDepVariations := commonDepVariations
1012 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1013 rlibDepVariations = append(rlibDepVariations,
1014 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1015 }
1016
Ivan Lozano52767be2019-10-18 14:49:46 -07001017 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001018 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001019 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001020 rlibDepTag, deps.Rlibs...)
1021 actx.AddVariationDependencies(
1022 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001023 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001024 dylibDepTag, deps.Dylibs...)
1025
Ivan Lozano042504f2020-08-18 14:31:23 -04001026 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1027 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001028 if autoDep.depTag == rlibDepTag {
1029 actx.AddVariationDependencies(
1030 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1031 autoDep.depTag, deps.Rustlibs...)
1032 } else {
1033 actx.AddVariationDependencies(
1034 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1035 autoDep.depTag, deps.Rustlibs...)
1036 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001037 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001038 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001039 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001040 actx.AddVariationDependencies(
1041 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1042 rlibDepTag, deps.Stdlibs...)
1043 } else {
1044 actx.AddVariationDependencies(
1045 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1046 dylibDepTag, deps.Stdlibs...)
1047 }
1048 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001049 actx.AddVariationDependencies(append(commonDepVariations,
1050 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001051 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001052 actx.AddVariationDependencies(append(commonDepVariations,
1053 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001054 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001055
Zach Johnson3df4e632020-11-06 11:56:27 -08001056 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1057
Colin Cross565cafd2020-09-25 18:47:38 -07001058 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001059 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001060 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001061 }
1062 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001063 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001064 }
1065
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001066 if mod.sourceProvider != nil {
1067 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1068 bindgen.Properties.Custom_bindgen != "" {
1069 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1070 bindgen.Properties.Custom_bindgen)
1071 }
1072 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001073 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001074 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075}
1076
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001077func BeginMutator(ctx android.BottomUpMutatorContext) {
1078 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1079 mod.beginMutator(ctx)
1080 }
1081}
1082
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001083func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1084 ctx := &baseModuleContext{
1085 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001086 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001087
1088 mod.begin(ctx)
1089}
1090
Ivan Lozanoffee3342019-08-27 12:03:00 -07001091func (mod *Module) Name() string {
1092 name := mod.ModuleBase.Name()
1093 if p, ok := mod.compiler.(interface {
1094 Name(string) string
1095 }); ok {
1096 name = p.Name(name)
1097 }
1098 return name
1099}
1100
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001101func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001102 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001103 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001104 }
1105}
1106
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001107var _ android.HostToolProvider = (*Module)(nil)
1108
1109func (mod *Module) HostToolPath() android.OptionalPath {
1110 if !mod.Host() {
1111 return android.OptionalPath{}
1112 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001113 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1114 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001115 }
1116 return android.OptionalPath{}
1117}
1118
Jiyong Park99644e92020-11-17 22:21:02 +09001119var _ android.ApexModule = (*Module)(nil)
1120
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001121func (mod *Module) minSdkVersion() string {
1122 return String(mod.Properties.Min_sdk_version)
1123}
1124
Jiyong Park45bf82e2020-12-15 22:29:02 +09001125var _ android.ApexModule = (*Module)(nil)
1126
1127// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001128func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001129 minSdkVersion := mod.minSdkVersion()
1130 if minSdkVersion == "apex_inherit" {
1131 return nil
1132 }
1133 if minSdkVersion == "" {
1134 return fmt.Errorf("min_sdk_version is not specificed")
1135 }
1136
1137 // Not using nativeApiLevelFromUser because the context here is not
1138 // necessarily a native context.
1139 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1140 if err != nil {
1141 return err
1142 }
1143
1144 if ver.GreaterThan(sdkVersion) {
1145 return fmt.Errorf("newer SDK(%v)", ver)
1146 }
Jiyong Park99644e92020-11-17 22:21:02 +09001147 return nil
1148}
1149
Jiyong Park45bf82e2020-12-15 22:29:02 +09001150// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001151func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1152 depTag := ctx.OtherModuleDependencyTag(dep)
1153
1154 if ccm, ok := dep.(*cc.Module); ok {
1155 if ccm.HasStubsVariants() {
1156 if cc.IsSharedDepTag(depTag) {
1157 // dynamic dep to a stubs lib crosses APEX boundary
1158 return false
1159 }
1160 if cc.IsRuntimeDepTag(depTag) {
1161 // runtime dep to a stubs lib also crosses APEX boundary
1162 return false
1163 }
1164
1165 if cc.IsHeaderDepTag(depTag) {
1166 return false
1167 }
1168 }
1169 if mod.Static() && cc.IsSharedDepTag(depTag) {
1170 // shared_lib dependency from a static lib is considered as crossing
1171 // the APEX boundary because the dependency doesn't actually is
1172 // linked; the dependency is used only during the compilation phase.
1173 return false
1174 }
1175 }
1176
1177 if depTag == procMacroDepTag {
1178 return false
1179 }
1180
1181 return true
1182}
1183
1184// Overrides ApexModule.IsInstallabeToApex()
1185func (mod *Module) IsInstallableToApex() bool {
1186 if mod.compiler != nil {
1187 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1188 return true
1189 }
1190 if _, ok := mod.compiler.(*binaryDecorator); ok {
1191 return true
1192 }
1193 }
1194 return false
1195}
1196
Ivan Lozanoffee3342019-08-27 12:03:00 -07001197var Bool = proptools.Bool
1198var BoolDefault = proptools.BoolDefault
1199var String = proptools.String
1200var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001201
1202var _ android.OutputFileProducer = (*Module)(nil)