blob: 105384676aeb2a27fb4beec2be2b47f8853682fb [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
Jiyong Park459feca2020-12-15 11:02:21 +0900485func (mod *Module) installable(apexInfo android.ApexInfo) bool {
486 // The apex variant is not installable because it is included in the APEX and won't appear
487 // in the system partition as a standalone file.
488 if !apexInfo.IsForPlatform() {
489 return false
490 }
491
492 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
493}
494
Ivan Lozano183a3212019-10-18 14:18:45 -0700495var _ cc.LinkableInterface = (*Module)(nil)
496
Ivan Lozanoffee3342019-08-27 12:03:00 -0700497func (mod *Module) Init() android.Module {
498 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500499 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700500
501 if mod.compiler != nil {
502 mod.AddProperties(mod.compiler.compilerProps()...)
503 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400504 if mod.coverage != nil {
505 mod.AddProperties(mod.coverage.props()...)
506 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200507 if mod.clippy != nil {
508 mod.AddProperties(mod.clippy.props()...)
509 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400510 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700511 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400512 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513
Ivan Lozanoffee3342019-08-27 12:03:00 -0700514 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900515 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700516
517 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518 return mod
519}
520
521func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
522 return &Module{
523 hod: hod,
524 multilib: multilib,
525 }
526}
527func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
528 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400529 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200530 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531 return module
532}
533
534type ModuleContext interface {
535 android.ModuleContext
536 ModuleContextIntf
537}
538
539type BaseModuleContext interface {
540 android.BaseModuleContext
541 ModuleContextIntf
542}
543
544type DepsContext interface {
545 android.BottomUpMutatorContext
546 ModuleContextIntf
547}
548
549type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200550 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700552}
553
554type depsContext struct {
555 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556}
557
558type moduleContext struct {
559 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560}
561
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200562type baseModuleContext struct {
563 android.BaseModuleContext
564}
565
566func (ctx *moduleContext) RustModule() *Module {
567 return ctx.Module().(*Module)
568}
569
570func (ctx *moduleContext) toolchain() config.Toolchain {
571 return ctx.RustModule().toolchain(ctx)
572}
573
574func (ctx *depsContext) RustModule() *Module {
575 return ctx.Module().(*Module)
576}
577
578func (ctx *depsContext) toolchain() config.Toolchain {
579 return ctx.RustModule().toolchain(ctx)
580}
581
582func (ctx *baseModuleContext) RustModule() *Module {
583 return ctx.Module().(*Module)
584}
585
586func (ctx *baseModuleContext) toolchain() config.Toolchain {
587 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400588}
589
590func (mod *Module) nativeCoverage() bool {
591 return mod.compiler != nil && mod.compiler.nativeCoverage()
592}
593
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
595 if mod.cachedToolchain == nil {
596 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
597 }
598 return mod.cachedToolchain
599}
600
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200601func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
602 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
603}
604
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
606}
607
608func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
609 ctx := &moduleContext{
610 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700611 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700612
Jiyong Park99644e92020-11-17 22:21:02 +0900613 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
614 if !apexInfo.IsForPlatform() {
615 mod.hideApexVariantFromMake = true
616 }
617
Ivan Lozanoffee3342019-08-27 12:03:00 -0700618 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500619 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
620
621 // Differentiate static libraries that are vendor available
622 if mod.UseVndk() {
623 mod.Properties.SubName += ".vendor"
624 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700625
626 if !toolchain.Supported() {
627 // This toolchain's unsupported, there's nothing to do for this mod.
628 return
629 }
630
631 deps := mod.depsToPaths(ctx)
632 flags := Flags{
633 Toolchain: toolchain,
634 }
635
636 if mod.compiler != nil {
637 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400638 }
639 if mod.coverage != nil {
640 flags, deps = mod.coverage.flags(ctx, flags, deps)
641 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200642 if mod.clippy != nil {
643 flags, deps = mod.clippy.flags(ctx, flags, deps)
644 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400645
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200646 // SourceProvider needs to call GenerateSource() before compiler calls
647 // compile() so it can provide the source. A SourceProvider has
648 // multiple variants (e.g. source, rlib, dylib). Only the "source"
649 // variant is responsible for effectively generating the source. The
650 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400651 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200652 if mod.compiler.(libraryInterface).source() {
653 mod.sourceProvider.GenerateSource(ctx, deps)
654 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
655 } else {
656 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
657 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700658 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200659 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400660 }
661
662 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400664
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900666
667 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
668 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200669 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400670 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700671 }
672}
673
674func (mod *Module) deps(ctx DepsContext) Deps {
675 deps := Deps{}
676
677 if mod.compiler != nil {
678 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400679 }
680 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700681 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700682 }
683
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400684 if mod.coverage != nil {
685 deps = mod.coverage.deps(ctx, deps)
686 }
687
Ivan Lozanoffee3342019-08-27 12:03:00 -0700688 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
689 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700690 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
692 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
693 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
694
695 return deps
696
697}
698
Ivan Lozanoffee3342019-08-27 12:03:00 -0700699type dependencyTag struct {
700 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800701 name string
702 library bool
703 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704}
705
Jiyong Park65b62242020-11-25 12:44:59 +0900706// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
707// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
708func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800709 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900710}
711
712var _ android.InstallNeededDependencyTag = dependencyTag{}
713
Ivan Lozanoffee3342019-08-27 12:03:00 -0700714var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400715 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
716 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
717 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800718 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400719 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200720 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721)
722
Jiyong Park99644e92020-11-17 22:21:02 +0900723func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
724 tag, ok := depTag.(dependencyTag)
725 return ok && tag == dylibDepTag
726}
727
Matthew Maurer0f003b12020-06-29 14:34:06 -0700728type autoDep struct {
729 variation string
730 depTag dependencyTag
731}
732
733var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200734 rlibVariation = "rlib"
735 dylibVariation = "dylib"
736 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
737 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700738)
739
740type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400741 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700742}
743
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400744func (mod *Module) begin(ctx BaseModuleContext) {
745 if mod.coverage != nil {
746 mod.coverage.begin(ctx)
747 }
748}
749
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
751 var depPaths PathDeps
752
753 directRlibDeps := []*Module{}
754 directDylibDeps := []*Module{}
755 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700756 directSharedLibDeps := [](cc.LinkableInterface){}
757 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400758 directSrcProvidersDeps := []*Module{}
759 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700760
761 ctx.VisitDirectDeps(func(dep android.Module) {
762 depName := ctx.OtherModuleName(dep)
763 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400764 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700765 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700766
Ivan Lozanoffee3342019-08-27 12:03:00 -0700767 switch depTag {
768 case dylibDepTag:
769 dylib, ok := rustDep.compiler.(libraryInterface)
770 if !ok || !dylib.dylib() {
771 ctx.ModuleErrorf("mod %q not an dylib library", depName)
772 return
773 }
774 directDylibDeps = append(directDylibDeps, rustDep)
775 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
776 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 rlib, ok := rustDep.compiler.(libraryInterface)
779 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400780 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700781 return
782 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400783 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700784 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400785 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786 case procMacroDepTag:
787 directProcMacroDeps = append(directProcMacroDeps, rustDep)
788 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400789 case android.SourceDepTag:
790 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
791 // OS/Arch variant is used.
792 var helper string
793 if ctx.Host() {
794 helper = "missing 'host_supported'?"
795 } else {
796 helper = "device module defined?"
797 }
798
799 if dep.Target().Os != ctx.Os() {
800 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
801 return
802 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
803 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
804 return
805 }
806 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700807 }
808
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400809 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700810 if depTag != procMacroDepTag {
811 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
812 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
813 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
814 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 }
816
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400818 linkFile := rustDep.outputFile
819 if !linkFile.Valid() {
820 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
821 depName, ctx.ModuleName())
822 return
823 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700825 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
826 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700827 }
828 }
829
Ivan Lozano89435d12020-07-31 11:01:18 -0400830 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700831 //Handle C dependencies
832 if _, ok := ccDep.(*Module); !ok {
833 if ccDep.Module().Target().Os != ctx.Os() {
834 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
835 return
836 }
837 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
838 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
839 return
840 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700841 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400842 linkObject := ccDep.OutputFile()
843 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500844
Ivan Lozano2093af22020-08-25 12:48:19 -0400845 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700846 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
847 }
848
849 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700850 switch {
851 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400853 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700854 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
855 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
856 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
857 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
858 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400859 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700860 directStaticLibDeps = append(directStaticLibDeps, ccDep)
861 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700862 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400864 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700865 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
866 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
867 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
868 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
869 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700870 directSharedLibDeps = append(directSharedLibDeps, ccDep)
871 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
872 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800873 case cc.IsHeaderDepTag(depTag):
874 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
875 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
876 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
877 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700878 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400879 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700880 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400881 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 }
883
884 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700885 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
886 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400887 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700889 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400890
891 if srcDep, ok := dep.(android.SourceFileProducer); ok {
892 switch depTag {
893 case android.SourceDepTag:
894 // These are usually genrules which don't have per-target variants.
895 directSrcDeps = append(directSrcDeps, srcDep)
896 }
897 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 })
899
900 var rlibDepFiles RustLibraries
901 for _, dep := range directRlibDeps {
902 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
903 }
904 var dylibDepFiles RustLibraries
905 for _, dep := range directDylibDeps {
906 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
907 }
908 var procMacroDepFiles RustLibraries
909 for _, dep := range directProcMacroDeps {
910 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
911 }
912
913 var staticLibDepFiles android.Paths
914 for _, dep := range directStaticLibDeps {
915 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
916 }
917
918 var sharedLibDepFiles android.Paths
919 for _, dep := range directSharedLibDeps {
920 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
921 }
922
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400923 var srcProviderDepFiles android.Paths
924 for _, dep := range directSrcProvidersDeps {
925 srcs, _ := dep.OutputFiles("")
926 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
927 }
928 for _, dep := range directSrcDeps {
929 srcs := dep.Srcs()
930 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
931 }
932
Ivan Lozanoffee3342019-08-27 12:03:00 -0700933 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
934 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
935 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
936 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
937 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400938 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700939
940 // Dedup exported flags from dependencies
941 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
942 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400943 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
944 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
945 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700946
947 return depPaths
948}
949
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800950func (mod *Module) InstallInData() bool {
951 if mod.compiler == nil {
952 return false
953 }
954 return mod.compiler.inData()
955}
956
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957func linkPathFromFilePath(filepath android.Path) string {
958 return strings.Split(filepath.String(), filepath.Base())[0]
959}
Ivan Lozanod648c432020-02-06 12:05:10 -0500960
Ivan Lozanoffee3342019-08-27 12:03:00 -0700961func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
962 ctx := &depsContext{
963 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700964 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700965
966 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700967 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -0400968
Ivan Lozano2b081132020-09-08 12:46:52 -0400969 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400970 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400971 stdLinkage = "rlib-std"
972 }
973
974 rlibDepVariations := commonDepVariations
975 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
976 rlibDepVariations = append(rlibDepVariations,
977 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
978 }
979
Ivan Lozano52767be2019-10-18 14:49:46 -0700980 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -0400981 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200982 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700983 rlibDepTag, deps.Rlibs...)
984 actx.AddVariationDependencies(
985 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200986 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -0700987 dylibDepTag, deps.Dylibs...)
988
Ivan Lozano042504f2020-08-18 14:31:23 -0400989 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
990 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -0400991 if autoDep.depTag == rlibDepTag {
992 actx.AddVariationDependencies(
993 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
994 autoDep.depTag, deps.Rustlibs...)
995 } else {
996 actx.AddVariationDependencies(
997 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
998 autoDep.depTag, deps.Rustlibs...)
999 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001000 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001001 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001002 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001003 actx.AddVariationDependencies(
1004 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1005 rlibDepTag, deps.Stdlibs...)
1006 } else {
1007 actx.AddVariationDependencies(
1008 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1009 dylibDepTag, deps.Stdlibs...)
1010 }
1011 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001012 actx.AddVariationDependencies(append(commonDepVariations,
1013 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001014 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001015 actx.AddVariationDependencies(append(commonDepVariations,
1016 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001017 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001018
Zach Johnson3df4e632020-11-06 11:56:27 -08001019 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1020
Colin Cross565cafd2020-09-25 18:47:38 -07001021 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001022 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001023 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001024 }
1025 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001026 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001027 }
1028
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001029 if mod.sourceProvider != nil {
1030 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1031 bindgen.Properties.Custom_bindgen != "" {
1032 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1033 bindgen.Properties.Custom_bindgen)
1034 }
1035 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001036 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001037 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038}
1039
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001040func BeginMutator(ctx android.BottomUpMutatorContext) {
1041 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1042 mod.beginMutator(ctx)
1043 }
1044}
1045
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001046func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1047 ctx := &baseModuleContext{
1048 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001049 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001050
1051 mod.begin(ctx)
1052}
1053
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054func (mod *Module) Name() string {
1055 name := mod.ModuleBase.Name()
1056 if p, ok := mod.compiler.(interface {
1057 Name(string) string
1058 }); ok {
1059 name = p.Name(name)
1060 }
1061 return name
1062}
1063
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001064func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001065 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001066 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001067 }
1068}
1069
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001070var _ android.HostToolProvider = (*Module)(nil)
1071
1072func (mod *Module) HostToolPath() android.OptionalPath {
1073 if !mod.Host() {
1074 return android.OptionalPath{}
1075 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001076 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1077 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001078 }
1079 return android.OptionalPath{}
1080}
1081
Jiyong Park99644e92020-11-17 22:21:02 +09001082var _ android.ApexModule = (*Module)(nil)
1083
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001084func (mod *Module) minSdkVersion() string {
1085 return String(mod.Properties.Min_sdk_version)
1086}
1087
Jiyong Park45bf82e2020-12-15 22:29:02 +09001088var _ android.ApexModule = (*Module)(nil)
1089
1090// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001091func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001092 minSdkVersion := mod.minSdkVersion()
1093 if minSdkVersion == "apex_inherit" {
1094 return nil
1095 }
1096 if minSdkVersion == "" {
1097 return fmt.Errorf("min_sdk_version is not specificed")
1098 }
1099
1100 // Not using nativeApiLevelFromUser because the context here is not
1101 // necessarily a native context.
1102 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1103 if err != nil {
1104 return err
1105 }
1106
1107 if ver.GreaterThan(sdkVersion) {
1108 return fmt.Errorf("newer SDK(%v)", ver)
1109 }
Jiyong Park99644e92020-11-17 22:21:02 +09001110 return nil
1111}
1112
Jiyong Park45bf82e2020-12-15 22:29:02 +09001113// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001114func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1115 depTag := ctx.OtherModuleDependencyTag(dep)
1116
1117 if ccm, ok := dep.(*cc.Module); ok {
1118 if ccm.HasStubsVariants() {
1119 if cc.IsSharedDepTag(depTag) {
1120 // dynamic dep to a stubs lib crosses APEX boundary
1121 return false
1122 }
1123 if cc.IsRuntimeDepTag(depTag) {
1124 // runtime dep to a stubs lib also crosses APEX boundary
1125 return false
1126 }
1127
1128 if cc.IsHeaderDepTag(depTag) {
1129 return false
1130 }
1131 }
1132 if mod.Static() && cc.IsSharedDepTag(depTag) {
1133 // shared_lib dependency from a static lib is considered as crossing
1134 // the APEX boundary because the dependency doesn't actually is
1135 // linked; the dependency is used only during the compilation phase.
1136 return false
1137 }
1138 }
1139
1140 if depTag == procMacroDepTag {
1141 return false
1142 }
1143
1144 return true
1145}
1146
1147// Overrides ApexModule.IsInstallabeToApex()
1148func (mod *Module) IsInstallableToApex() bool {
1149 if mod.compiler != nil {
1150 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1151 return true
1152 }
1153 if _, ok := mod.compiler.(*binaryDecorator); ok {
1154 return true
1155 }
1156 }
1157 return false
1158}
1159
Ivan Lozanoffee3342019-08-27 12:03:00 -07001160var Bool = proptools.Bool
1161var BoolDefault = proptools.BoolDefault
1162var String = proptools.String
1163var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001164
1165var _ android.OutputFileProducer = (*Module)(nil)