blob: a2d02923079c204a216c529ce40e949b05d08915 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// Copyright 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package rust
16
17import (
Ivan Lozano183a3212019-10-18 14:18:45 -070018 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
25 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020026 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027 "android/soong/rust/config"
28)
29
30var pctx = android.NewPackageContext("android/soong/rust")
31
32func init() {
33 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070034
35 android.AddNeverAllowRules(
36 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070037 NotIn(config.RustAllowedPaths...).
38 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070039
40 android.RegisterModuleType("rust_defaults", defaultsFactory)
41 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
42 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040043 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040044 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070048}
49
50type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040051 GlobalRustFlags []string // Flags that apply globally to rust
52 GlobalLinkFlags []string // Flags that apply globally to linker
53 RustFlags []string // Flags that apply to rust
54 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070056 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040057 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070059}
60
61type BaseProperties struct {
62 AndroidMkRlibs []string
63 AndroidMkDylibs []string
64 AndroidMkProcMacroLibs []string
65 AndroidMkSharedLibs []string
66 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040067
Ivan Lozano6a884432020-12-02 09:15:16 -050068 ImageVariationPrefix string `blueprint:"mutated"`
69 VndkVersion string `blueprint:"mutated"`
70 SubName string `blueprint:"mutated"`
71
72 // Set by imageMutator
73 CoreVariantNeeded bool `blueprint:"mutated"`
74 ExtraVariants []string `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040075
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050076 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
77 Min_sdk_version *string
78
Ivan Lozano43845682020-07-09 21:03:28 -040079 PreventInstall bool
80 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070081}
82
83type Module struct {
84 android.ModuleBase
85 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090086 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070087
Ivan Lozano6a884432020-12-02 09:15:16 -050088 VendorProperties cc.VendorProperties
89
Ivan Lozanoffee3342019-08-27 12:03:00 -070090 Properties BaseProperties
91
92 hod android.HostOrDeviceSupported
93 multilib android.Multilib
94
Ivan Lozano6a884432020-12-02 09:15:16 -050095 makeLinkType string
96
Ivan Lozanoffee3342019-08-27 12:03:00 -070097 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040098 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020099 clippy *clippy
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400101 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700102 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400103
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200104 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900105
106 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107}
108
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500109func (mod *Module) Header() bool {
110 //TODO: If Rust libraries provide header variants, this needs to be updated.
111 return false
112}
113
114func (mod *Module) SetPreventInstall() {
115 mod.Properties.PreventInstall = true
116}
117
118// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
119func (mod *Module) InVendor() bool {
120 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
121}
122
123func (mod *Module) SetHideFromMake() {
124 mod.Properties.HideFromMake = true
125}
126
127func (mod *Module) SanitizePropDefined() bool {
128 return false
129}
130
131func (mod *Module) IsDependencyRoot() bool {
132 if mod.compiler != nil {
133 return mod.compiler.isDependencyRoot()
134 }
135 panic("IsDependencyRoot called on a non-compiler Rust module")
136}
137
138func (mod *Module) IsPrebuilt() bool {
139 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
140 return true
141 }
142 return false
143}
144
Ivan Lozano43845682020-07-09 21:03:28 -0400145func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
146 switch tag {
147 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700148 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400149 return mod.sourceProvider.Srcs(), nil
150 } else {
151 if mod.outputFile.Valid() {
152 return android.Paths{mod.outputFile.Path()}, nil
153 }
154 return android.Paths{}, nil
155 }
156 default:
157 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
158 }
159}
160
Ivan Lozano52767be2019-10-18 14:49:46 -0700161func (mod *Module) SelectedStl() string {
162 return ""
163}
164
Ivan Lozano2b262972019-11-21 12:30:50 -0800165func (mod *Module) NonCcVariants() bool {
166 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400167 if _, ok := mod.compiler.(libraryInterface); ok {
168 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800169 }
170 }
171 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
172}
173
Ivan Lozano52767be2019-10-18 14:49:46 -0700174func (mod *Module) Static() bool {
175 if mod.compiler != nil {
176 if library, ok := mod.compiler.(libraryInterface); ok {
177 return library.static()
178 }
179 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400180 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700181}
182
183func (mod *Module) Shared() bool {
184 if mod.compiler != nil {
185 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400186 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700187 }
188 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400189 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700190}
191
192func (mod *Module) Toc() android.OptionalPath {
193 if mod.compiler != nil {
194 if _, ok := mod.compiler.(libraryInterface); ok {
195 return android.OptionalPath{}
196 }
197 }
198 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
199}
200
Colin Crossc511bc52020-04-07 16:50:32 +0000201func (mod *Module) UseSdk() bool {
202 return false
203}
204
Ivan Lozano6a884432020-12-02 09:15:16 -0500205// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
206// "product" and "vendor" variant modules return true for this function.
207// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
208// "soc_specific: true" and more vendor installed modules are included here.
209// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
210// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700211func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500212 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700213}
214
215func (mod *Module) MustUseVendorVariant() bool {
216 return false
217}
218
219func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500220 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700221 return false
222}
223
Ivan Lozanof9e21722020-12-02 09:00:51 -0500224func (mod *Module) IsVndkExt() bool {
225 return false
226}
227
Colin Cross127bb8b2020-12-16 16:46:01 -0800228func (c *Module) IsVndkPrivate() bool {
229 return false
230}
231
232func (c *Module) IsLlndk() bool {
233 return false
234}
235
236func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500237 return false
238}
239
Ivan Lozano52767be2019-10-18 14:49:46 -0700240func (mod *Module) SdkVersion() string {
241 return ""
242}
243
Colin Crossc511bc52020-04-07 16:50:32 +0000244func (mod *Module) AlwaysSdk() bool {
245 return false
246}
247
Jiyong Park2286afd2020-06-16 21:58:53 +0900248func (mod *Module) IsSdkVariant() bool {
249 return false
250}
251
Colin Cross1348ce32020-10-01 13:37:16 -0700252func (mod *Module) SplitPerApiLevel() bool {
253 return false
254}
255
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256type Deps struct {
257 Dylibs []string
258 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700259 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400260 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 ProcMacros []string
262 SharedLibs []string
263 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800264 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265
266 CrtBegin, CrtEnd string
267}
268
269type PathDeps struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400270 DyLibs RustLibraries
271 RLibs RustLibraries
272 SharedLibs android.Paths
273 StaticLibs android.Paths
274 ProcMacros RustLibraries
275 linkDirs []string
276 depFlags []string
277 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700278 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700279
Ivan Lozano45901ed2020-07-24 16:05:01 -0400280 // Used by bindgen modules which call clang
281 depClangFlags []string
282 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400283 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400284 depSystemIncludePaths android.Paths
285
Ivan Lozanof1c84332019-09-20 11:00:37 -0700286 CrtBegin android.OptionalPath
287 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700288
289 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500290 SrcDeps android.Paths
291 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700292}
293
294type RustLibraries []RustLibrary
295
296type RustLibrary struct {
297 Path android.Path
298 CrateName string
299}
300
301type compiler interface {
302 compilerFlags(ctx ModuleContext, flags Flags) Flags
303 compilerProps() []interface{}
304 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
305 compilerDeps(ctx DepsContext, deps Deps) Deps
306 crateName() string
307
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800308 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200309 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700310 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400311
312 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400313
314 Disabled() bool
315 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400316
Ivan Lozanodd055472020-09-28 13:22:45 -0400317 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500318 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400319}
320
Matthew Maurerbb3add12020-06-25 09:34:12 -0700321type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700322 exportLinkDirs(...string)
323 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400324 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700325}
326
327type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400328 depFlags []string
329 linkDirs []string
330 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700331}
332
Matthew Maurerbb3add12020-06-25 09:34:12 -0700333func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
334 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
335}
336
337func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
338 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
339}
340
Ivan Lozano2093af22020-08-25 12:48:19 -0400341func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
342 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
343}
344
Colin Cross0de8a1e2020-09-18 14:15:30 -0700345func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
346 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
347 Flags: flagExporter.depFlags,
348 LinkDirs: flagExporter.linkDirs,
349 LinkObjects: flagExporter.linkObjects,
350 })
351}
352
Matthew Maurerbb3add12020-06-25 09:34:12 -0700353var _ exportedFlagsProducer = (*flagExporter)(nil)
354
355func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700356 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700357}
358
Colin Cross0de8a1e2020-09-18 14:15:30 -0700359type FlagExporterInfo struct {
360 Flags []string
361 LinkDirs []string // TODO: this should be android.Paths
362 LinkObjects []string // TODO: this should be android.Paths
363}
364
365var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
366
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400367func (mod *Module) isCoverageVariant() bool {
368 return mod.coverage.Properties.IsCoverageVariant
369}
370
371var _ cc.Coverage = (*Module)(nil)
372
373func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
374 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
375}
376
377func (mod *Module) PreventInstall() {
378 mod.Properties.PreventInstall = true
379}
380
381func (mod *Module) HideFromMake() {
382 mod.Properties.HideFromMake = true
383}
384
385func (mod *Module) MarkAsCoverageVariant(coverage bool) {
386 mod.coverage.Properties.IsCoverageVariant = coverage
387}
388
389func (mod *Module) EnableCoverageIfNeeded() {
390 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700391}
392
393func defaultsFactory() android.Module {
394 return DefaultsFactory()
395}
396
397type Defaults struct {
398 android.ModuleBase
399 android.DefaultsModuleBase
400}
401
402func DefaultsFactory(props ...interface{}) android.Module {
403 module := &Defaults{}
404
405 module.AddProperties(props...)
406 module.AddProperties(
407 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500408 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400409 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700410 &BaseCompilerProperties{},
411 &BinaryCompilerProperties{},
412 &LibraryCompilerProperties{},
413 &ProcMacroCompilerProperties{},
414 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400415 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700416 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400417 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400418 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200419 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420 )
421
422 android.InitDefaultsModule(module)
423 return module
424}
425
426func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700427 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428}
429
Ivan Lozano183a3212019-10-18 14:18:45 -0700430func (mod *Module) CcLibrary() bool {
431 if mod.compiler != nil {
432 if _, ok := mod.compiler.(*libraryDecorator); ok {
433 return true
434 }
435 }
436 return false
437}
438
439func (mod *Module) CcLibraryInterface() bool {
440 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400441 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
442 // VariantIs{Static,Shared} is set.
443 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700444 return true
445 }
446 }
447 return false
448}
449
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800450func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700451 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700452 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800453 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700454 }
455 }
456 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
457}
458
459func (mod *Module) SetStatic() {
460 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700461 if library, ok := mod.compiler.(libraryInterface); ok {
462 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700463 return
464 }
465 }
466 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
467}
468
469func (mod *Module) SetShared() {
470 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700471 if library, ok := mod.compiler.(libraryInterface); ok {
472 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700473 return
474 }
475 }
476 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
477}
478
Ivan Lozano183a3212019-10-18 14:18:45 -0700479func (mod *Module) BuildStaticVariant() bool {
480 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700481 if library, ok := mod.compiler.(libraryInterface); ok {
482 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700483 }
484 }
485 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
486}
487
488func (mod *Module) BuildSharedVariant() bool {
489 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700490 if library, ok := mod.compiler.(libraryInterface); ok {
491 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700492 }
493 }
494 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
495}
496
Ivan Lozano183a3212019-10-18 14:18:45 -0700497func (mod *Module) Module() android.Module {
498 return mod
499}
500
Ivan Lozano183a3212019-10-18 14:18:45 -0700501func (mod *Module) OutputFile() android.OptionalPath {
502 return mod.outputFile
503}
504
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400505func (mod *Module) CoverageFiles() android.Paths {
506 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800507 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400508 }
509 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
510}
511
Jiyong Park459feca2020-12-15 11:02:21 +0900512func (mod *Module) installable(apexInfo android.ApexInfo) bool {
513 // The apex variant is not installable because it is included in the APEX and won't appear
514 // in the system partition as a standalone file.
515 if !apexInfo.IsForPlatform() {
516 return false
517 }
518
519 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
520}
521
Ivan Lozano183a3212019-10-18 14:18:45 -0700522var _ cc.LinkableInterface = (*Module)(nil)
523
Ivan Lozanoffee3342019-08-27 12:03:00 -0700524func (mod *Module) Init() android.Module {
525 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500526 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700527
528 if mod.compiler != nil {
529 mod.AddProperties(mod.compiler.compilerProps()...)
530 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400531 if mod.coverage != nil {
532 mod.AddProperties(mod.coverage.props()...)
533 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200534 if mod.clippy != nil {
535 mod.AddProperties(mod.clippy.props()...)
536 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400537 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700538 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400539 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900542 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543
544 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700545 return mod
546}
547
548func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
549 return &Module{
550 hod: hod,
551 multilib: multilib,
552 }
553}
554func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
555 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400556 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200557 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558 return module
559}
560
561type ModuleContext interface {
562 android.ModuleContext
563 ModuleContextIntf
564}
565
566type BaseModuleContext interface {
567 android.BaseModuleContext
568 ModuleContextIntf
569}
570
571type DepsContext interface {
572 android.BottomUpMutatorContext
573 ModuleContextIntf
574}
575
576type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200577 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579}
580
581type depsContext struct {
582 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700583}
584
585type moduleContext struct {
586 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587}
588
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200589type baseModuleContext struct {
590 android.BaseModuleContext
591}
592
593func (ctx *moduleContext) RustModule() *Module {
594 return ctx.Module().(*Module)
595}
596
597func (ctx *moduleContext) toolchain() config.Toolchain {
598 return ctx.RustModule().toolchain(ctx)
599}
600
601func (ctx *depsContext) RustModule() *Module {
602 return ctx.Module().(*Module)
603}
604
605func (ctx *depsContext) toolchain() config.Toolchain {
606 return ctx.RustModule().toolchain(ctx)
607}
608
609func (ctx *baseModuleContext) RustModule() *Module {
610 return ctx.Module().(*Module)
611}
612
613func (ctx *baseModuleContext) toolchain() config.Toolchain {
614 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400615}
616
617func (mod *Module) nativeCoverage() bool {
618 return mod.compiler != nil && mod.compiler.nativeCoverage()
619}
620
Ivan Lozanoffee3342019-08-27 12:03:00 -0700621func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
622 if mod.cachedToolchain == nil {
623 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
624 }
625 return mod.cachedToolchain
626}
627
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200628func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
629 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
630}
631
Ivan Lozanoffee3342019-08-27 12:03:00 -0700632func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
633}
634
635func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
636 ctx := &moduleContext{
637 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700638 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700639
Jiyong Park99644e92020-11-17 22:21:02 +0900640 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
641 if !apexInfo.IsForPlatform() {
642 mod.hideApexVariantFromMake = true
643 }
644
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500646 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
647
648 // Differentiate static libraries that are vendor available
649 if mod.UseVndk() {
650 mod.Properties.SubName += ".vendor"
651 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652
653 if !toolchain.Supported() {
654 // This toolchain's unsupported, there's nothing to do for this mod.
655 return
656 }
657
658 deps := mod.depsToPaths(ctx)
659 flags := Flags{
660 Toolchain: toolchain,
661 }
662
663 if mod.compiler != nil {
664 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400665 }
666 if mod.coverage != nil {
667 flags, deps = mod.coverage.flags(ctx, flags, deps)
668 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200669 if mod.clippy != nil {
670 flags, deps = mod.clippy.flags(ctx, flags, deps)
671 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400672
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200673 // SourceProvider needs to call GenerateSource() before compiler calls
674 // compile() so it can provide the source. A SourceProvider has
675 // multiple variants (e.g. source, rlib, dylib). Only the "source"
676 // variant is responsible for effectively generating the source. The
677 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400678 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200679 if mod.compiler.(libraryInterface).source() {
680 mod.sourceProvider.GenerateSource(ctx, deps)
681 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
682 } else {
683 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
684 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700685 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200686 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400687 }
688
689 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700690 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400691
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900693
694 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
695 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200696 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400697 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700698 }
699}
700
701func (mod *Module) deps(ctx DepsContext) Deps {
702 deps := Deps{}
703
704 if mod.compiler != nil {
705 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400706 }
707 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700708 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709 }
710
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400711 if mod.coverage != nil {
712 deps = mod.coverage.deps(ctx, deps)
713 }
714
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
716 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700717 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700718 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
719 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
720 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
721
722 return deps
723
724}
725
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726type dependencyTag struct {
727 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800728 name string
729 library bool
730 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700731}
732
Jiyong Park65b62242020-11-25 12:44:59 +0900733// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
734// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
735func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800736 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900737}
738
739var _ android.InstallNeededDependencyTag = dependencyTag{}
740
Ivan Lozanoffee3342019-08-27 12:03:00 -0700741var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400742 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
743 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
744 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800745 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400746 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200747 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700748)
749
Jiyong Park99644e92020-11-17 22:21:02 +0900750func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
751 tag, ok := depTag.(dependencyTag)
752 return ok && tag == dylibDepTag
753}
754
Matthew Maurer0f003b12020-06-29 14:34:06 -0700755type autoDep struct {
756 variation string
757 depTag dependencyTag
758}
759
760var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200761 rlibVariation = "rlib"
762 dylibVariation = "dylib"
763 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
764 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700765)
766
767type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400768 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700769}
770
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400771func (mod *Module) begin(ctx BaseModuleContext) {
772 if mod.coverage != nil {
773 mod.coverage.begin(ctx)
774 }
775}
776
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
778 var depPaths PathDeps
779
780 directRlibDeps := []*Module{}
781 directDylibDeps := []*Module{}
782 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700783 directSharedLibDeps := [](cc.LinkableInterface){}
784 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400785 directSrcProvidersDeps := []*Module{}
786 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787
788 ctx.VisitDirectDeps(func(dep android.Module) {
789 depName := ctx.OtherModuleName(dep)
790 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400791 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700792 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700793
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 switch depTag {
795 case dylibDepTag:
796 dylib, ok := rustDep.compiler.(libraryInterface)
797 if !ok || !dylib.dylib() {
798 ctx.ModuleErrorf("mod %q not an dylib library", depName)
799 return
800 }
801 directDylibDeps = append(directDylibDeps, rustDep)
802 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
803 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400804
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 rlib, ok := rustDep.compiler.(libraryInterface)
806 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400807 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700808 return
809 }
810 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400811 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700812 case procMacroDepTag:
813 directProcMacroDeps = append(directProcMacroDeps, rustDep)
814 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400815 case android.SourceDepTag:
816 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
817 // OS/Arch variant is used.
818 var helper string
819 if ctx.Host() {
820 helper = "missing 'host_supported'?"
821 } else {
822 helper = "device module defined?"
823 }
824
825 if dep.Target().Os != ctx.Os() {
826 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
827 return
828 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
829 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
830 return
831 }
832 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700833 }
834
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400835 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700836 if depTag != procMacroDepTag {
837 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
838 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
839 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
840 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 }
842
Ivan Lozanoffee3342019-08-27 12:03:00 -0700843 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400844 linkFile := rustDep.outputFile
845 if !linkFile.Valid() {
846 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
847 depName, ctx.ModuleName())
848 return
849 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700850 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700851 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
852 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 }
854 }
855
Ivan Lozano89435d12020-07-31 11:01:18 -0400856 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700857 //Handle C dependencies
858 if _, ok := ccDep.(*Module); !ok {
859 if ccDep.Module().Target().Os != ctx.Os() {
860 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
861 return
862 }
863 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
864 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
865 return
866 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700867 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400868 linkObject := ccDep.OutputFile()
869 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500870
Ivan Lozano2093af22020-08-25 12:48:19 -0400871 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
873 }
874
875 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700876 switch {
877 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400879 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700880 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
881 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
882 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
883 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
884 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700885 directStaticLibDeps = append(directStaticLibDeps, ccDep)
886 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700887 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400889 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700890 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
891 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
892 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
893 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
894 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700895 directSharedLibDeps = append(directSharedLibDeps, ccDep)
896 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
897 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800898 case cc.IsHeaderDepTag(depTag):
899 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
900 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
901 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
902 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700903 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400904 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700905 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400906 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907 }
908
909 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700910 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
911 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400912 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700913 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400915
916 if srcDep, ok := dep.(android.SourceFileProducer); ok {
917 switch depTag {
918 case android.SourceDepTag:
919 // These are usually genrules which don't have per-target variants.
920 directSrcDeps = append(directSrcDeps, srcDep)
921 }
922 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700923 })
924
925 var rlibDepFiles RustLibraries
926 for _, dep := range directRlibDeps {
927 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
928 }
929 var dylibDepFiles RustLibraries
930 for _, dep := range directDylibDeps {
931 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
932 }
933 var procMacroDepFiles RustLibraries
934 for _, dep := range directProcMacroDeps {
935 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
936 }
937
938 var staticLibDepFiles android.Paths
939 for _, dep := range directStaticLibDeps {
940 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
941 }
942
943 var sharedLibDepFiles android.Paths
944 for _, dep := range directSharedLibDeps {
945 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
946 }
947
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400948 var srcProviderDepFiles android.Paths
949 for _, dep := range directSrcProvidersDeps {
950 srcs, _ := dep.OutputFiles("")
951 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
952 }
953 for _, dep := range directSrcDeps {
954 srcs := dep.Srcs()
955 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
956 }
957
Ivan Lozanoffee3342019-08-27 12:03:00 -0700958 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
959 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
960 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
961 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
962 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400963 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700964
965 // Dedup exported flags from dependencies
966 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
967 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400968 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
969 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
970 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700971
972 return depPaths
973}
974
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800975func (mod *Module) InstallInData() bool {
976 if mod.compiler == nil {
977 return false
978 }
979 return mod.compiler.inData()
980}
981
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982func linkPathFromFilePath(filepath android.Path) string {
983 return strings.Split(filepath.String(), filepath.Base())[0]
984}
Ivan Lozanod648c432020-02-06 12:05:10 -0500985
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
987 ctx := &depsContext{
988 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700989 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990
991 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -0700992 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -0400993
Ivan Lozano2b081132020-09-08 12:46:52 -0400994 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -0400995 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400996 stdLinkage = "rlib-std"
997 }
998
999 rlibDepVariations := commonDepVariations
1000 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1001 rlibDepVariations = append(rlibDepVariations,
1002 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1003 }
1004
Ivan Lozano52767be2019-10-18 14:49:46 -07001005 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001006 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001007 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001008 rlibDepTag, deps.Rlibs...)
1009 actx.AddVariationDependencies(
1010 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001011 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001012 dylibDepTag, deps.Dylibs...)
1013
Ivan Lozano042504f2020-08-18 14:31:23 -04001014 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1015 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001016 if autoDep.depTag == rlibDepTag {
1017 actx.AddVariationDependencies(
1018 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1019 autoDep.depTag, deps.Rustlibs...)
1020 } else {
1021 actx.AddVariationDependencies(
1022 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1023 autoDep.depTag, deps.Rustlibs...)
1024 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001025 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001026 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001027 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001028 actx.AddVariationDependencies(
1029 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1030 rlibDepTag, deps.Stdlibs...)
1031 } else {
1032 actx.AddVariationDependencies(
1033 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1034 dylibDepTag, deps.Stdlibs...)
1035 }
1036 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001037 actx.AddVariationDependencies(append(commonDepVariations,
1038 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001039 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001040 actx.AddVariationDependencies(append(commonDepVariations,
1041 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001042 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001043
Zach Johnson3df4e632020-11-06 11:56:27 -08001044 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1045
Colin Cross565cafd2020-09-25 18:47:38 -07001046 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001047 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001048 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001049 }
1050 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001051 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001052 }
1053
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001054 if mod.sourceProvider != nil {
1055 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1056 bindgen.Properties.Custom_bindgen != "" {
1057 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1058 bindgen.Properties.Custom_bindgen)
1059 }
1060 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001061 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001062 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001063}
1064
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001065func BeginMutator(ctx android.BottomUpMutatorContext) {
1066 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1067 mod.beginMutator(ctx)
1068 }
1069}
1070
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001071func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1072 ctx := &baseModuleContext{
1073 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001074 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001075
1076 mod.begin(ctx)
1077}
1078
Ivan Lozanoffee3342019-08-27 12:03:00 -07001079func (mod *Module) Name() string {
1080 name := mod.ModuleBase.Name()
1081 if p, ok := mod.compiler.(interface {
1082 Name(string) string
1083 }); ok {
1084 name = p.Name(name)
1085 }
1086 return name
1087}
1088
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001089func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001090 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001091 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001092 }
1093}
1094
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001095var _ android.HostToolProvider = (*Module)(nil)
1096
1097func (mod *Module) HostToolPath() android.OptionalPath {
1098 if !mod.Host() {
1099 return android.OptionalPath{}
1100 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001101 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1102 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001103 }
1104 return android.OptionalPath{}
1105}
1106
Jiyong Park99644e92020-11-17 22:21:02 +09001107var _ android.ApexModule = (*Module)(nil)
1108
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001109func (mod *Module) minSdkVersion() string {
1110 return String(mod.Properties.Min_sdk_version)
1111}
1112
Jiyong Park45bf82e2020-12-15 22:29:02 +09001113var _ android.ApexModule = (*Module)(nil)
1114
1115// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001116func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001117 minSdkVersion := mod.minSdkVersion()
1118 if minSdkVersion == "apex_inherit" {
1119 return nil
1120 }
1121 if minSdkVersion == "" {
1122 return fmt.Errorf("min_sdk_version is not specificed")
1123 }
1124
1125 // Not using nativeApiLevelFromUser because the context here is not
1126 // necessarily a native context.
1127 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1128 if err != nil {
1129 return err
1130 }
1131
1132 if ver.GreaterThan(sdkVersion) {
1133 return fmt.Errorf("newer SDK(%v)", ver)
1134 }
Jiyong Park99644e92020-11-17 22:21:02 +09001135 return nil
1136}
1137
Jiyong Park45bf82e2020-12-15 22:29:02 +09001138// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001139func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1140 depTag := ctx.OtherModuleDependencyTag(dep)
1141
1142 if ccm, ok := dep.(*cc.Module); ok {
1143 if ccm.HasStubsVariants() {
1144 if cc.IsSharedDepTag(depTag) {
1145 // dynamic dep to a stubs lib crosses APEX boundary
1146 return false
1147 }
1148 if cc.IsRuntimeDepTag(depTag) {
1149 // runtime dep to a stubs lib also crosses APEX boundary
1150 return false
1151 }
1152
1153 if cc.IsHeaderDepTag(depTag) {
1154 return false
1155 }
1156 }
1157 if mod.Static() && cc.IsSharedDepTag(depTag) {
1158 // shared_lib dependency from a static lib is considered as crossing
1159 // the APEX boundary because the dependency doesn't actually is
1160 // linked; the dependency is used only during the compilation phase.
1161 return false
1162 }
1163 }
1164
1165 if depTag == procMacroDepTag {
1166 return false
1167 }
1168
1169 return true
1170}
1171
1172// Overrides ApexModule.IsInstallabeToApex()
1173func (mod *Module) IsInstallableToApex() bool {
1174 if mod.compiler != nil {
1175 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1176 return true
1177 }
1178 if _, ok := mod.compiler.(*binaryDecorator); ok {
1179 return true
1180 }
1181 }
1182 return false
1183}
1184
Ivan Lozanoffee3342019-08-27 12:03:00 -07001185var Bool = proptools.Bool
1186var BoolDefault = proptools.BoolDefault
1187var String = proptools.String
1188var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001189
1190var _ android.OutputFileProducer = (*Module)(nil)