blob: e2ead00950749cb5cafb3c9b594abe9227f9f79b [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 Lozano43845682020-07-09 21:03:28 -0400109func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
110 switch tag {
111 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700112 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400113 return mod.sourceProvider.Srcs(), nil
114 } else {
115 if mod.outputFile.Valid() {
116 return android.Paths{mod.outputFile.Path()}, nil
117 }
118 return android.Paths{}, nil
119 }
120 default:
121 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
122 }
123}
124
Ivan Lozano52767be2019-10-18 14:49:46 -0700125func (mod *Module) SelectedStl() string {
126 return ""
127}
128
Ivan Lozano2b262972019-11-21 12:30:50 -0800129func (mod *Module) NonCcVariants() bool {
130 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400131 if _, ok := mod.compiler.(libraryInterface); ok {
132 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800133 }
134 }
135 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
136}
137
Ivan Lozano52767be2019-10-18 14:49:46 -0700138func (mod *Module) Static() bool {
139 if mod.compiler != nil {
140 if library, ok := mod.compiler.(libraryInterface); ok {
141 return library.static()
142 }
143 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400144 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700145}
146
147func (mod *Module) Shared() bool {
148 if mod.compiler != nil {
149 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400150 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700151 }
152 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400153 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700154}
155
156func (mod *Module) Toc() android.OptionalPath {
157 if mod.compiler != nil {
158 if _, ok := mod.compiler.(libraryInterface); ok {
159 return android.OptionalPath{}
160 }
161 }
162 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
163}
164
Colin Crossc511bc52020-04-07 16:50:32 +0000165func (mod *Module) UseSdk() bool {
166 return false
167}
168
Ivan Lozano6a884432020-12-02 09:15:16 -0500169// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
170// "product" and "vendor" variant modules return true for this function.
171// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
172// "soc_specific: true" and more vendor installed modules are included here.
173// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
174// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700175func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500176 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700177}
178
179func (mod *Module) MustUseVendorVariant() bool {
180 return false
181}
182
183func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500184 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700185 return false
186}
187
Ivan Lozanof9e21722020-12-02 09:00:51 -0500188func (mod *Module) IsVndkExt() bool {
189 return false
190}
191
Colin Cross127bb8b2020-12-16 16:46:01 -0800192func (c *Module) IsVndkPrivate() bool {
193 return false
194}
195
196func (c *Module) IsLlndk() bool {
197 return false
198}
199
200func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500201 return false
202}
203
Ivan Lozano52767be2019-10-18 14:49:46 -0700204func (mod *Module) SdkVersion() string {
205 return ""
206}
207
Colin Crossc511bc52020-04-07 16:50:32 +0000208func (mod *Module) AlwaysSdk() bool {
209 return false
210}
211
Jiyong Park2286afd2020-06-16 21:58:53 +0900212func (mod *Module) IsSdkVariant() bool {
213 return false
214}
215
Colin Cross1348ce32020-10-01 13:37:16 -0700216func (mod *Module) SplitPerApiLevel() bool {
217 return false
218}
219
Ivan Lozanoffee3342019-08-27 12:03:00 -0700220type Deps struct {
221 Dylibs []string
222 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700223 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400224 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700225 ProcMacros []string
226 SharedLibs []string
227 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800228 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700229
230 CrtBegin, CrtEnd string
231}
232
233type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400234 DyLibs RustLibraries
235 RLibs RustLibraries
236 SharedLibs android.Paths
237 StaticLibs android.Paths
238 ProcMacros RustLibraries
239 linkDirs []string
240 depFlags []string
241 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700242 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700243
Ivan Lozano45901ed2020-07-24 16:05:01 -0400244 // Used by bindgen modules which call clang
245 depClangFlags []string
246 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400247 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400248 depSystemIncludePaths android.Paths
249
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400250 coverageFiles android.Paths
251
Ivan Lozanof1c84332019-09-20 11:00:37 -0700252 CrtBegin android.OptionalPath
253 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700254
255 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500256 SrcDeps android.Paths
257 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258}
259
260type RustLibraries []RustLibrary
261
262type RustLibrary struct {
263 Path android.Path
264 CrateName string
265}
266
267type compiler interface {
268 compilerFlags(ctx ModuleContext, flags Flags) Flags
269 compilerProps() []interface{}
270 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
271 compilerDeps(ctx DepsContext, deps Deps) Deps
272 crateName() string
273
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800274 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200275 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400277
278 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400279
280 Disabled() bool
281 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400282
Ivan Lozanodd055472020-09-28 13:22:45 -0400283 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400284}
285
Matthew Maurerbb3add12020-06-25 09:34:12 -0700286type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700287 exportLinkDirs(...string)
288 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400289 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700290}
291
292type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400293 depFlags []string
294 linkDirs []string
295 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700296}
297
Matthew Maurerbb3add12020-06-25 09:34:12 -0700298func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
299 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
300}
301
302func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
303 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
304}
305
Ivan Lozano2093af22020-08-25 12:48:19 -0400306func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
307 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
308}
309
Colin Cross0de8a1e2020-09-18 14:15:30 -0700310func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
311 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
312 Flags: flagExporter.depFlags,
313 LinkDirs: flagExporter.linkDirs,
314 LinkObjects: flagExporter.linkObjects,
315 })
316}
317
Matthew Maurerbb3add12020-06-25 09:34:12 -0700318var _ exportedFlagsProducer = (*flagExporter)(nil)
319
320func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700321 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700322}
323
Colin Cross0de8a1e2020-09-18 14:15:30 -0700324type FlagExporterInfo struct {
325 Flags []string
326 LinkDirs []string // TODO: this should be android.Paths
327 LinkObjects []string // TODO: this should be android.Paths
328}
329
330var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
331
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400332func (mod *Module) isCoverageVariant() bool {
333 return mod.coverage.Properties.IsCoverageVariant
334}
335
336var _ cc.Coverage = (*Module)(nil)
337
338func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
339 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
340}
341
342func (mod *Module) PreventInstall() {
343 mod.Properties.PreventInstall = true
344}
345
346func (mod *Module) HideFromMake() {
347 mod.Properties.HideFromMake = true
348}
349
350func (mod *Module) MarkAsCoverageVariant(coverage bool) {
351 mod.coverage.Properties.IsCoverageVariant = coverage
352}
353
354func (mod *Module) EnableCoverageIfNeeded() {
355 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700356}
357
358func defaultsFactory() android.Module {
359 return DefaultsFactory()
360}
361
362type Defaults struct {
363 android.ModuleBase
364 android.DefaultsModuleBase
365}
366
367func DefaultsFactory(props ...interface{}) android.Module {
368 module := &Defaults{}
369
370 module.AddProperties(props...)
371 module.AddProperties(
372 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500373 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400374 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375 &BaseCompilerProperties{},
376 &BinaryCompilerProperties{},
377 &LibraryCompilerProperties{},
378 &ProcMacroCompilerProperties{},
379 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400380 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700381 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400382 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400383 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200384 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385 )
386
387 android.InitDefaultsModule(module)
388 return module
389}
390
391func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700392 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700393}
394
Ivan Lozano183a3212019-10-18 14:18:45 -0700395func (mod *Module) CcLibrary() bool {
396 if mod.compiler != nil {
397 if _, ok := mod.compiler.(*libraryDecorator); ok {
398 return true
399 }
400 }
401 return false
402}
403
404func (mod *Module) CcLibraryInterface() bool {
405 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400406 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
407 // VariantIs{Static,Shared} is set.
408 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700409 return true
410 }
411 }
412 return false
413}
414
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800415func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700416 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700417 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800418 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700419 }
420 }
421 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
422}
423
424func (mod *Module) SetStatic() {
425 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700426 if library, ok := mod.compiler.(libraryInterface); ok {
427 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700428 return
429 }
430 }
431 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
432}
433
434func (mod *Module) SetShared() {
435 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700436 if library, ok := mod.compiler.(libraryInterface); ok {
437 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700438 return
439 }
440 }
441 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
442}
443
Ivan Lozano183a3212019-10-18 14:18:45 -0700444func (mod *Module) BuildStaticVariant() bool {
445 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700446 if library, ok := mod.compiler.(libraryInterface); ok {
447 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700448 }
449 }
450 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
451}
452
453func (mod *Module) BuildSharedVariant() bool {
454 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700455 if library, ok := mod.compiler.(libraryInterface); ok {
456 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700457 }
458 }
459 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
460}
461
Ivan Lozano183a3212019-10-18 14:18:45 -0700462func (mod *Module) Module() android.Module {
463 return mod
464}
465
Ivan Lozano183a3212019-10-18 14:18:45 -0700466func (mod *Module) OutputFile() android.OptionalPath {
467 return mod.outputFile
468}
469
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400470func (mod *Module) CoverageFiles() android.Paths {
471 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700472 if !mod.compiler.nativeCoverage() {
473 return android.Paths{}
474 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400475 if library, ok := mod.compiler.(*libraryDecorator); ok {
476 if library.coverageFile != nil {
477 return android.Paths{library.coverageFile}
478 }
479 return android.Paths{}
480 }
481 }
482 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
483}
484
Ivan Lozano183a3212019-10-18 14:18:45 -0700485var _ cc.LinkableInterface = (*Module)(nil)
486
Ivan Lozanoffee3342019-08-27 12:03:00 -0700487func (mod *Module) Init() android.Module {
488 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500489 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700490
491 if mod.compiler != nil {
492 mod.AddProperties(mod.compiler.compilerProps()...)
493 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400494 if mod.coverage != nil {
495 mod.AddProperties(mod.coverage.props()...)
496 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200497 if mod.clippy != nil {
498 mod.AddProperties(mod.clippy.props()...)
499 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400500 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700501 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400502 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400503
Ivan Lozanoffee3342019-08-27 12:03:00 -0700504 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900505 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700506
507 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700508 return mod
509}
510
511func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
512 return &Module{
513 hod: hod,
514 multilib: multilib,
515 }
516}
517func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
518 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400519 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200520 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700521 return module
522}
523
524type ModuleContext interface {
525 android.ModuleContext
526 ModuleContextIntf
527}
528
529type BaseModuleContext interface {
530 android.BaseModuleContext
531 ModuleContextIntf
532}
533
534type DepsContext interface {
535 android.BottomUpMutatorContext
536 ModuleContextIntf
537}
538
539type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200540 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542}
543
544type depsContext struct {
545 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700546}
547
548type moduleContext struct {
549 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550}
551
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200552type baseModuleContext struct {
553 android.BaseModuleContext
554}
555
556func (ctx *moduleContext) RustModule() *Module {
557 return ctx.Module().(*Module)
558}
559
560func (ctx *moduleContext) toolchain() config.Toolchain {
561 return ctx.RustModule().toolchain(ctx)
562}
563
564func (ctx *depsContext) RustModule() *Module {
565 return ctx.Module().(*Module)
566}
567
568func (ctx *depsContext) toolchain() config.Toolchain {
569 return ctx.RustModule().toolchain(ctx)
570}
571
572func (ctx *baseModuleContext) RustModule() *Module {
573 return ctx.Module().(*Module)
574}
575
576func (ctx *baseModuleContext) toolchain() config.Toolchain {
577 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400578}
579
580func (mod *Module) nativeCoverage() bool {
581 return mod.compiler != nil && mod.compiler.nativeCoverage()
582}
583
Ivan Lozanoffee3342019-08-27 12:03:00 -0700584func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
585 if mod.cachedToolchain == nil {
586 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
587 }
588 return mod.cachedToolchain
589}
590
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200591func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
592 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
593}
594
Ivan Lozanoffee3342019-08-27 12:03:00 -0700595func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
596}
597
598func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
599 ctx := &moduleContext{
600 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700601 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700602
Jiyong Park99644e92020-11-17 22:21:02 +0900603 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
604 if !apexInfo.IsForPlatform() {
605 mod.hideApexVariantFromMake = true
606 }
607
Ivan Lozanoffee3342019-08-27 12:03:00 -0700608 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500609 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
610
611 // Differentiate static libraries that are vendor available
612 if mod.UseVndk() {
613 mod.Properties.SubName += ".vendor"
614 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615
616 if !toolchain.Supported() {
617 // This toolchain's unsupported, there's nothing to do for this mod.
618 return
619 }
620
621 deps := mod.depsToPaths(ctx)
622 flags := Flags{
623 Toolchain: toolchain,
624 }
625
626 if mod.compiler != nil {
627 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400628 }
629 if mod.coverage != nil {
630 flags, deps = mod.coverage.flags(ctx, flags, deps)
631 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200632 if mod.clippy != nil {
633 flags, deps = mod.clippy.flags(ctx, flags, deps)
634 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400635
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200636 // SourceProvider needs to call GenerateSource() before compiler calls
637 // compile() so it can provide the source. A SourceProvider has
638 // multiple variants (e.g. source, rlib, dylib). Only the "source"
639 // variant is responsible for effectively generating the source. The
640 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400641 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200642 if mod.compiler.(libraryInterface).source() {
643 mod.sourceProvider.GenerateSource(ctx, deps)
644 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
645 } else {
646 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
647 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700648 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200649 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400650 }
651
652 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700653 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400654
Ivan Lozanoffee3342019-08-27 12:03:00 -0700655 mod.outputFile = android.OptionalPathForPath(outputFile)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400656 if mod.outputFile.Valid() && !mod.Properties.PreventInstall {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200657 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400658 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700659 }
660}
661
662func (mod *Module) deps(ctx DepsContext) Deps {
663 deps := Deps{}
664
665 if mod.compiler != nil {
666 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400667 }
668 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700669 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670 }
671
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400672 if mod.coverage != nil {
673 deps = mod.coverage.deps(ctx, deps)
674 }
675
Ivan Lozanoffee3342019-08-27 12:03:00 -0700676 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
677 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700678 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700679 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
680 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
681 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
682
683 return deps
684
685}
686
Ivan Lozanoffee3342019-08-27 12:03:00 -0700687type dependencyTag struct {
688 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800689 name string
690 library bool
691 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692}
693
Jiyong Park65b62242020-11-25 12:44:59 +0900694// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
695// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
696func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800697 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900698}
699
700var _ android.InstallNeededDependencyTag = dependencyTag{}
701
Ivan Lozanoffee3342019-08-27 12:03:00 -0700702var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400703 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
704 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
705 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800706 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400707 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200708 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709)
710
Jiyong Park99644e92020-11-17 22:21:02 +0900711func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
712 tag, ok := depTag.(dependencyTag)
713 return ok && tag == dylibDepTag
714}
715
Matthew Maurer0f003b12020-06-29 14:34:06 -0700716type autoDep struct {
717 variation string
718 depTag dependencyTag
719}
720
721var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200722 rlibVariation = "rlib"
723 dylibVariation = "dylib"
724 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
725 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700726)
727
728type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400729 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700730}
731
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400732func (mod *Module) begin(ctx BaseModuleContext) {
733 if mod.coverage != nil {
734 mod.coverage.begin(ctx)
735 }
736}
737
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
739 var depPaths PathDeps
740
741 directRlibDeps := []*Module{}
742 directDylibDeps := []*Module{}
743 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700744 directSharedLibDeps := [](cc.LinkableInterface){}
745 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400746 directSrcProvidersDeps := []*Module{}
747 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748
749 ctx.VisitDirectDeps(func(dep android.Module) {
750 depName := ctx.OtherModuleName(dep)
751 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400752 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700753 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700754
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755 switch depTag {
756 case dylibDepTag:
757 dylib, ok := rustDep.compiler.(libraryInterface)
758 if !ok || !dylib.dylib() {
759 ctx.ModuleErrorf("mod %q not an dylib library", depName)
760 return
761 }
762 directDylibDeps = append(directDylibDeps, rustDep)
763 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
764 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400765
Ivan Lozanoffee3342019-08-27 12:03:00 -0700766 rlib, ok := rustDep.compiler.(libraryInterface)
767 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400768 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700769 return
770 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400771 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400773 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700774 case procMacroDepTag:
775 directProcMacroDeps = append(directProcMacroDeps, rustDep)
776 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400777 case android.SourceDepTag:
778 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
779 // OS/Arch variant is used.
780 var helper string
781 if ctx.Host() {
782 helper = "missing 'host_supported'?"
783 } else {
784 helper = "device module defined?"
785 }
786
787 if dep.Target().Os != ctx.Os() {
788 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
789 return
790 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
791 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
792 return
793 }
794 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 }
796
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400797 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700798 if depTag != procMacroDepTag {
799 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
800 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
801 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
802 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803 }
804
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400806 linkFile := rustDep.outputFile
807 if !linkFile.Valid() {
808 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
809 depName, ctx.ModuleName())
810 return
811 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700813 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
814 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 }
816 }
817
Ivan Lozano89435d12020-07-31 11:01:18 -0400818 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700819 //Handle C dependencies
820 if _, ok := ccDep.(*Module); !ok {
821 if ccDep.Module().Target().Os != ctx.Os() {
822 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
823 return
824 }
825 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
826 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
827 return
828 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700829 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400830 linkObject := ccDep.OutputFile()
831 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500832
Ivan Lozano2093af22020-08-25 12:48:19 -0400833 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700834 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
835 }
836
837 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700838 switch {
839 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700840 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400841 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700842 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
843 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
844 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
845 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
846 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400847 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700848 directStaticLibDeps = append(directStaticLibDeps, ccDep)
849 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700850 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700851 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400852 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700853 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
854 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
855 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
856 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
857 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858 directSharedLibDeps = append(directSharedLibDeps, ccDep)
859 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
860 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800861 case cc.IsHeaderDepTag(depTag):
862 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
863 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
864 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
865 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700866 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400867 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700868 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400869 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700870 }
871
872 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700873 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
874 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400875 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700877 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400878
879 if srcDep, ok := dep.(android.SourceFileProducer); ok {
880 switch depTag {
881 case android.SourceDepTag:
882 // These are usually genrules which don't have per-target variants.
883 directSrcDeps = append(directSrcDeps, srcDep)
884 }
885 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886 })
887
888 var rlibDepFiles RustLibraries
889 for _, dep := range directRlibDeps {
890 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
891 }
892 var dylibDepFiles RustLibraries
893 for _, dep := range directDylibDeps {
894 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
895 }
896 var procMacroDepFiles RustLibraries
897 for _, dep := range directProcMacroDeps {
898 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
899 }
900
901 var staticLibDepFiles android.Paths
902 for _, dep := range directStaticLibDeps {
903 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
904 }
905
906 var sharedLibDepFiles android.Paths
907 for _, dep := range directSharedLibDeps {
908 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
909 }
910
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400911 var srcProviderDepFiles android.Paths
912 for _, dep := range directSrcProvidersDeps {
913 srcs, _ := dep.OutputFiles("")
914 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
915 }
916 for _, dep := range directSrcDeps {
917 srcs := dep.Srcs()
918 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
919 }
920
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
922 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
923 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
924 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
925 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400926 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927
928 // Dedup exported flags from dependencies
929 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
930 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400931 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
932 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
933 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700934
935 return depPaths
936}
937
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800938func (mod *Module) InstallInData() bool {
939 if mod.compiler == nil {
940 return false
941 }
942 return mod.compiler.inData()
943}
944
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945func linkPathFromFilePath(filepath android.Path) string {
946 return strings.Split(filepath.String(), filepath.Base())[0]
947}
Ivan Lozanod648c432020-02-06 12:05:10 -0500948
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
950 ctx := &depsContext{
951 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700952 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700953
954 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700955 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -0400956
Ivan Lozano2b081132020-09-08 12:46:52 -0400957 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400958 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400959 stdLinkage = "rlib-std"
960 }
961
962 rlibDepVariations := commonDepVariations
963 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
964 rlibDepVariations = append(rlibDepVariations,
965 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
966 }
967
Ivan Lozano52767be2019-10-18 14:49:46 -0700968 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400969 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200970 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700971 rlibDepTag, deps.Rlibs...)
972 actx.AddVariationDependencies(
973 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200974 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700975 dylibDepTag, deps.Dylibs...)
976
Ivan Lozano042504f2020-08-18 14:31:23 -0400977 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
978 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400979 if autoDep.depTag == rlibDepTag {
980 actx.AddVariationDependencies(
981 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
982 autoDep.depTag, deps.Rustlibs...)
983 } else {
984 actx.AddVariationDependencies(
985 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
986 autoDep.depTag, deps.Rustlibs...)
987 }
Matthew Maurer0f003b12020-06-29 14:34:06 -0700988 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400989 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -0400990 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400991 actx.AddVariationDependencies(
992 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
993 rlibDepTag, deps.Stdlibs...)
994 } else {
995 actx.AddVariationDependencies(
996 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
997 dylibDepTag, deps.Stdlibs...)
998 }
999 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001000 actx.AddVariationDependencies(append(commonDepVariations,
1001 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001002 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001003 actx.AddVariationDependencies(append(commonDepVariations,
1004 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001005 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001006
Zach Johnson3df4e632020-11-06 11:56:27 -08001007 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1008
Colin Cross565cafd2020-09-25 18:47:38 -07001009 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001010 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001011 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001012 }
1013 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001014 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001015 }
1016
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001017 if mod.sourceProvider != nil {
1018 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1019 bindgen.Properties.Custom_bindgen != "" {
1020 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1021 bindgen.Properties.Custom_bindgen)
1022 }
1023 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001024 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001025 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026}
1027
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001028func BeginMutator(ctx android.BottomUpMutatorContext) {
1029 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1030 mod.beginMutator(ctx)
1031 }
1032}
1033
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001034func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1035 ctx := &baseModuleContext{
1036 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001037 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001038
1039 mod.begin(ctx)
1040}
1041
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042func (mod *Module) Name() string {
1043 name := mod.ModuleBase.Name()
1044 if p, ok := mod.compiler.(interface {
1045 Name(string) string
1046 }); ok {
1047 name = p.Name(name)
1048 }
1049 return name
1050}
1051
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001052func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001053 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001054 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001055 }
1056}
1057
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001058var _ android.HostToolProvider = (*Module)(nil)
1059
1060func (mod *Module) HostToolPath() android.OptionalPath {
1061 if !mod.Host() {
1062 return android.OptionalPath{}
1063 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001064 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1065 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001066 }
1067 return android.OptionalPath{}
1068}
1069
Jiyong Park99644e92020-11-17 22:21:02 +09001070var _ android.ApexModule = (*Module)(nil)
1071
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001072func (mod *Module) minSdkVersion() string {
1073 return String(mod.Properties.Min_sdk_version)
1074}
1075
Jiyong Park45bf82e2020-12-15 22:29:02 +09001076var _ android.ApexModule = (*Module)(nil)
1077
1078// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001079func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001080 minSdkVersion := mod.minSdkVersion()
1081 if minSdkVersion == "apex_inherit" {
1082 return nil
1083 }
1084 if minSdkVersion == "" {
1085 return fmt.Errorf("min_sdk_version is not specificed")
1086 }
1087
1088 // Not using nativeApiLevelFromUser because the context here is not
1089 // necessarily a native context.
1090 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1091 if err != nil {
1092 return err
1093 }
1094
1095 if ver.GreaterThan(sdkVersion) {
1096 return fmt.Errorf("newer SDK(%v)", ver)
1097 }
Jiyong Park99644e92020-11-17 22:21:02 +09001098 return nil
1099}
1100
Jiyong Park45bf82e2020-12-15 22:29:02 +09001101// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001102func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1103 depTag := ctx.OtherModuleDependencyTag(dep)
1104
1105 if ccm, ok := dep.(*cc.Module); ok {
1106 if ccm.HasStubsVariants() {
1107 if cc.IsSharedDepTag(depTag) {
1108 // dynamic dep to a stubs lib crosses APEX boundary
1109 return false
1110 }
1111 if cc.IsRuntimeDepTag(depTag) {
1112 // runtime dep to a stubs lib also crosses APEX boundary
1113 return false
1114 }
1115
1116 if cc.IsHeaderDepTag(depTag) {
1117 return false
1118 }
1119 }
1120 if mod.Static() && cc.IsSharedDepTag(depTag) {
1121 // shared_lib dependency from a static lib is considered as crossing
1122 // the APEX boundary because the dependency doesn't actually is
1123 // linked; the dependency is used only during the compilation phase.
1124 return false
1125 }
1126 }
1127
1128 if depTag == procMacroDepTag {
1129 return false
1130 }
1131
1132 return true
1133}
1134
1135// Overrides ApexModule.IsInstallabeToApex()
1136func (mod *Module) IsInstallableToApex() bool {
1137 if mod.compiler != nil {
1138 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1139 return true
1140 }
1141 if _, ok := mod.compiler.(*binaryDecorator); ok {
1142 return true
1143 }
1144 }
1145 return false
1146}
1147
Ivan Lozanoffee3342019-08-27 12:03:00 -07001148var Bool = proptools.Bool
1149var BoolDefault = proptools.BoolDefault
1150var String = proptools.String
1151var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001152
1153var _ android.OutputFileProducer = (*Module)(nil)