blob: 83add87b702cd484d82522d7522cfcc300be6d67 [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 Lozanoec6e9912021-01-21 15:23:29 -0500270 DyLibs RustLibraries
271 RLibs RustLibraries
272 SharedLibs android.Paths
273 SharedLibDeps android.Paths
274 StaticLibs android.Paths
275 ProcMacros RustLibraries
276 linkDirs []string
277 depFlags []string
278 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700280
Ivan Lozano45901ed2020-07-24 16:05:01 -0400281 // Used by bindgen modules which call clang
282 depClangFlags []string
283 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400284 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400285 depSystemIncludePaths android.Paths
286
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400287 coverageFiles android.Paths
288
Ivan Lozanof1c84332019-09-20 11:00:37 -0700289 CrtBegin android.OptionalPath
290 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700291
292 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500293 SrcDeps android.Paths
294 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700295}
296
297type RustLibraries []RustLibrary
298
299type RustLibrary struct {
300 Path android.Path
301 CrateName string
302}
303
304type compiler interface {
305 compilerFlags(ctx ModuleContext, flags Flags) Flags
306 compilerProps() []interface{}
307 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
308 compilerDeps(ctx DepsContext, deps Deps) Deps
309 crateName() string
310
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800311 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200312 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700313 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400314
315 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400316
317 Disabled() bool
318 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400319
Ivan Lozanodd055472020-09-28 13:22:45 -0400320 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500321 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400322}
323
Matthew Maurerbb3add12020-06-25 09:34:12 -0700324type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700325 exportLinkDirs(...string)
326 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400327 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700328}
329
330type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400331 depFlags []string
332 linkDirs []string
333 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700334}
335
Matthew Maurerbb3add12020-06-25 09:34:12 -0700336func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
337 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
338}
339
340func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
341 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
342}
343
Ivan Lozano2093af22020-08-25 12:48:19 -0400344func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
345 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
346}
347
Colin Cross0de8a1e2020-09-18 14:15:30 -0700348func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
349 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
350 Flags: flagExporter.depFlags,
351 LinkDirs: flagExporter.linkDirs,
352 LinkObjects: flagExporter.linkObjects,
353 })
354}
355
Matthew Maurerbb3add12020-06-25 09:34:12 -0700356var _ exportedFlagsProducer = (*flagExporter)(nil)
357
358func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700359 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700360}
361
Colin Cross0de8a1e2020-09-18 14:15:30 -0700362type FlagExporterInfo struct {
363 Flags []string
364 LinkDirs []string // TODO: this should be android.Paths
365 LinkObjects []string // TODO: this should be android.Paths
366}
367
368var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
369
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400370func (mod *Module) isCoverageVariant() bool {
371 return mod.coverage.Properties.IsCoverageVariant
372}
373
374var _ cc.Coverage = (*Module)(nil)
375
376func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
377 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
378}
379
380func (mod *Module) PreventInstall() {
381 mod.Properties.PreventInstall = true
382}
383
384func (mod *Module) HideFromMake() {
385 mod.Properties.HideFromMake = true
386}
387
388func (mod *Module) MarkAsCoverageVariant(coverage bool) {
389 mod.coverage.Properties.IsCoverageVariant = coverage
390}
391
392func (mod *Module) EnableCoverageIfNeeded() {
393 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394}
395
396func defaultsFactory() android.Module {
397 return DefaultsFactory()
398}
399
400type Defaults struct {
401 android.ModuleBase
402 android.DefaultsModuleBase
403}
404
405func DefaultsFactory(props ...interface{}) android.Module {
406 module := &Defaults{}
407
408 module.AddProperties(props...)
409 module.AddProperties(
410 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500411 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400412 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700413 &BaseCompilerProperties{},
414 &BinaryCompilerProperties{},
415 &LibraryCompilerProperties{},
416 &ProcMacroCompilerProperties{},
417 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400418 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700419 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400420 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400421 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200422 &ClippyProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700423 )
424
425 android.InitDefaultsModule(module)
426 return module
427}
428
429func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700430 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431}
432
Ivan Lozano183a3212019-10-18 14:18:45 -0700433func (mod *Module) CcLibrary() bool {
434 if mod.compiler != nil {
435 if _, ok := mod.compiler.(*libraryDecorator); ok {
436 return true
437 }
438 }
439 return false
440}
441
442func (mod *Module) CcLibraryInterface() bool {
443 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400444 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
445 // VariantIs{Static,Shared} is set.
446 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700447 return true
448 }
449 }
450 return false
451}
452
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800453func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700454 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700455 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800456 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700457 }
458 }
459 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
460}
461
462func (mod *Module) SetStatic() {
463 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700464 if library, ok := mod.compiler.(libraryInterface); ok {
465 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700466 return
467 }
468 }
469 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
470}
471
472func (mod *Module) SetShared() {
473 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700474 if library, ok := mod.compiler.(libraryInterface); ok {
475 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700476 return
477 }
478 }
479 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
480}
481
Ivan Lozano183a3212019-10-18 14:18:45 -0700482func (mod *Module) BuildStaticVariant() bool {
483 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700484 if library, ok := mod.compiler.(libraryInterface); ok {
485 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700486 }
487 }
488 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
489}
490
491func (mod *Module) BuildSharedVariant() bool {
492 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700493 if library, ok := mod.compiler.(libraryInterface); ok {
494 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700495 }
496 }
497 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
498}
499
Ivan Lozano183a3212019-10-18 14:18:45 -0700500func (mod *Module) Module() android.Module {
501 return mod
502}
503
Ivan Lozano183a3212019-10-18 14:18:45 -0700504func (mod *Module) OutputFile() android.OptionalPath {
505 return mod.outputFile
506}
507
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400508func (mod *Module) CoverageFiles() android.Paths {
509 if mod.compiler != nil {
Matthew Maurerc761eec2020-06-25 00:47:46 -0700510 if !mod.compiler.nativeCoverage() {
511 return android.Paths{}
512 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513 if library, ok := mod.compiler.(*libraryDecorator); ok {
514 if library.coverageFile != nil {
515 return android.Paths{library.coverageFile}
516 }
517 return android.Paths{}
518 }
519 }
520 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
521}
522
Jiyong Park459feca2020-12-15 11:02:21 +0900523func (mod *Module) installable(apexInfo android.ApexInfo) bool {
524 // The apex variant is not installable because it is included in the APEX and won't appear
525 // in the system partition as a standalone file.
526 if !apexInfo.IsForPlatform() {
527 return false
528 }
529
530 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
531}
532
Ivan Lozano183a3212019-10-18 14:18:45 -0700533var _ cc.LinkableInterface = (*Module)(nil)
534
Ivan Lozanoffee3342019-08-27 12:03:00 -0700535func (mod *Module) Init() android.Module {
536 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500537 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700538
539 if mod.compiler != nil {
540 mod.AddProperties(mod.compiler.compilerProps()...)
541 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400542 if mod.coverage != nil {
543 mod.AddProperties(mod.coverage.props()...)
544 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200545 if mod.clippy != nil {
546 mod.AddProperties(mod.clippy.props()...)
547 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400548 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700549 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400550 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400551
Ivan Lozanoffee3342019-08-27 12:03:00 -0700552 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900553 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700554
555 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556 return mod
557}
558
559func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
560 return &Module{
561 hod: hod,
562 multilib: multilib,
563 }
564}
565func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
566 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400567 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200568 module.clippy = &clippy{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 return module
570}
571
572type ModuleContext interface {
573 android.ModuleContext
574 ModuleContextIntf
575}
576
577type BaseModuleContext interface {
578 android.BaseModuleContext
579 ModuleContextIntf
580}
581
582type DepsContext interface {
583 android.BottomUpMutatorContext
584 ModuleContextIntf
585}
586
587type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200588 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700589 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700590}
591
592type depsContext struct {
593 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594}
595
596type moduleContext struct {
597 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598}
599
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200600type baseModuleContext struct {
601 android.BaseModuleContext
602}
603
604func (ctx *moduleContext) RustModule() *Module {
605 return ctx.Module().(*Module)
606}
607
608func (ctx *moduleContext) toolchain() config.Toolchain {
609 return ctx.RustModule().toolchain(ctx)
610}
611
612func (ctx *depsContext) RustModule() *Module {
613 return ctx.Module().(*Module)
614}
615
616func (ctx *depsContext) toolchain() config.Toolchain {
617 return ctx.RustModule().toolchain(ctx)
618}
619
620func (ctx *baseModuleContext) RustModule() *Module {
621 return ctx.Module().(*Module)
622}
623
624func (ctx *baseModuleContext) toolchain() config.Toolchain {
625 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400626}
627
628func (mod *Module) nativeCoverage() bool {
629 return mod.compiler != nil && mod.compiler.nativeCoverage()
630}
631
Ivan Lozanoffee3342019-08-27 12:03:00 -0700632func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
633 if mod.cachedToolchain == nil {
634 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
635 }
636 return mod.cachedToolchain
637}
638
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200639func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
640 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
641}
642
Ivan Lozanoffee3342019-08-27 12:03:00 -0700643func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
644}
645
646func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
647 ctx := &moduleContext{
648 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700649 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700650
Jiyong Park99644e92020-11-17 22:21:02 +0900651 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
652 if !apexInfo.IsForPlatform() {
653 mod.hideApexVariantFromMake = true
654 }
655
Ivan Lozanoffee3342019-08-27 12:03:00 -0700656 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500657 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
658
659 // Differentiate static libraries that are vendor available
660 if mod.UseVndk() {
661 mod.Properties.SubName += ".vendor"
662 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700663
664 if !toolchain.Supported() {
665 // This toolchain's unsupported, there's nothing to do for this mod.
666 return
667 }
668
669 deps := mod.depsToPaths(ctx)
670 flags := Flags{
671 Toolchain: toolchain,
672 }
673
674 if mod.compiler != nil {
675 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400676 }
677 if mod.coverage != nil {
678 flags, deps = mod.coverage.flags(ctx, flags, deps)
679 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200680 if mod.clippy != nil {
681 flags, deps = mod.clippy.flags(ctx, flags, deps)
682 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400683
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200684 // SourceProvider needs to call GenerateSource() before compiler calls
685 // compile() so it can provide the source. A SourceProvider has
686 // multiple variants (e.g. source, rlib, dylib). Only the "source"
687 // variant is responsible for effectively generating the source. The
688 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400689 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200690 if mod.compiler.(libraryInterface).source() {
691 mod.sourceProvider.GenerateSource(ctx, deps)
692 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
693 } else {
694 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
695 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700696 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200697 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400698 }
699
700 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700701 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400702
Ivan Lozanoffee3342019-08-27 12:03:00 -0700703 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900704
705 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
706 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200707 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400708 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709 }
710}
711
712func (mod *Module) deps(ctx DepsContext) Deps {
713 deps := Deps{}
714
715 if mod.compiler != nil {
716 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400717 }
718 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700719 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720 }
721
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400722 if mod.coverage != nil {
723 deps = mod.coverage.deps(ctx, deps)
724 }
725
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
727 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700728 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
730 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
731 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
732
733 return deps
734
735}
736
Ivan Lozanoffee3342019-08-27 12:03:00 -0700737type dependencyTag struct {
738 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800739 name string
740 library bool
741 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700742}
743
Jiyong Park65b62242020-11-25 12:44:59 +0900744// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
745// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
746func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800747 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900748}
749
750var _ android.InstallNeededDependencyTag = dependencyTag{}
751
Ivan Lozanoffee3342019-08-27 12:03:00 -0700752var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400753 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
754 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
755 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800756 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400757 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200758 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700759)
760
Jiyong Park99644e92020-11-17 22:21:02 +0900761func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
762 tag, ok := depTag.(dependencyTag)
763 return ok && tag == dylibDepTag
764}
765
Matthew Maurer0f003b12020-06-29 14:34:06 -0700766type autoDep struct {
767 variation string
768 depTag dependencyTag
769}
770
771var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200772 rlibVariation = "rlib"
773 dylibVariation = "dylib"
774 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
775 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700776)
777
778type autoDeppable interface {
Ivan Lozano042504f2020-08-18 14:31:23 -0400779 autoDep(ctx BaseModuleContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700780}
781
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400782func (mod *Module) begin(ctx BaseModuleContext) {
783 if mod.coverage != nil {
784 mod.coverage.begin(ctx)
785 }
786}
787
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
789 var depPaths PathDeps
790
791 directRlibDeps := []*Module{}
792 directDylibDeps := []*Module{}
793 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700794 directSharedLibDeps := [](cc.LinkableInterface){}
795 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400796 directSrcProvidersDeps := []*Module{}
797 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798
799 ctx.VisitDirectDeps(func(dep android.Module) {
800 depName := ctx.OtherModuleName(dep)
801 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400802 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700804
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805 switch depTag {
806 case dylibDepTag:
807 dylib, ok := rustDep.compiler.(libraryInterface)
808 if !ok || !dylib.dylib() {
809 ctx.ModuleErrorf("mod %q not an dylib library", depName)
810 return
811 }
812 directDylibDeps = append(directDylibDeps, rustDep)
813 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
814 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400815
Ivan Lozanoffee3342019-08-27 12:03:00 -0700816 rlib, ok := rustDep.compiler.(libraryInterface)
817 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400818 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700819 return
820 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400821 depPaths.coverageFiles = append(depPaths.coverageFiles, rustDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400823 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824 case procMacroDepTag:
825 directProcMacroDeps = append(directProcMacroDeps, rustDep)
826 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400827 case android.SourceDepTag:
828 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
829 // OS/Arch variant is used.
830 var helper string
831 if ctx.Host() {
832 helper = "missing 'host_supported'?"
833 } else {
834 helper = "device module defined?"
835 }
836
837 if dep.Target().Os != ctx.Os() {
838 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
839 return
840 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
841 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
842 return
843 }
844 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700845 }
846
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400847 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700848 if depTag != procMacroDepTag {
849 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
850 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
851 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
852 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700853 }
854
Ivan Lozanoffee3342019-08-27 12:03:00 -0700855 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400856 linkFile := rustDep.outputFile
857 if !linkFile.Valid() {
858 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
859 depName, ctx.ModuleName())
860 return
861 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700862 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700863 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
864 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700865 }
866 }
867
Ivan Lozano89435d12020-07-31 11:01:18 -0400868 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700869 //Handle C dependencies
870 if _, ok := ccDep.(*Module); !ok {
871 if ccDep.Module().Target().Os != ctx.Os() {
872 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
873 return
874 }
875 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
876 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
877 return
878 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700879 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400880 linkObject := ccDep.OutputFile()
881 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500882
Ivan Lozano2093af22020-08-25 12:48:19 -0400883 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
885 }
886
887 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700888 switch {
889 case cc.IsStaticDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400891 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700892 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
893 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
894 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
895 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
896 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400897 depPaths.coverageFiles = append(depPaths.coverageFiles, ccDep.CoverageFiles()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 directStaticLibDeps = append(directStaticLibDeps, ccDep)
899 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700900 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400902 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700903 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
904 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
905 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
906 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
907 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700908 directSharedLibDeps = append(directSharedLibDeps, ccDep)
909 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
910 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800911 case cc.IsHeaderDepTag(depTag):
912 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
913 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
914 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
915 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700916 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400917 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700918 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400919 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920 }
921
922 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700923 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
924 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400925 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400928
929 if srcDep, ok := dep.(android.SourceFileProducer); ok {
930 switch depTag {
931 case android.SourceDepTag:
932 // These are usually genrules which don't have per-target variants.
933 directSrcDeps = append(directSrcDeps, srcDep)
934 }
935 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936 })
937
938 var rlibDepFiles RustLibraries
939 for _, dep := range directRlibDeps {
940 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
941 }
942 var dylibDepFiles RustLibraries
943 for _, dep := range directDylibDeps {
944 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
945 }
946 var procMacroDepFiles RustLibraries
947 for _, dep := range directProcMacroDeps {
948 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
949 }
950
951 var staticLibDepFiles android.Paths
952 for _, dep := range directStaticLibDeps {
953 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
954 }
955
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500956 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 var sharedLibDepFiles android.Paths
958 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500959 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
960 if dep.Toc().Valid() {
961 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
962 } else {
963 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
964 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700965 }
966
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400967 var srcProviderDepFiles android.Paths
968 for _, dep := range directSrcProvidersDeps {
969 srcs, _ := dep.OutputFiles("")
970 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
971 }
972 for _, dep := range directSrcDeps {
973 srcs := dep.Srcs()
974 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
975 }
976
Ivan Lozanoffee3342019-08-27 12:03:00 -0700977 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
978 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
979 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500980 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700981 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
982 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400983 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700984
985 // Dedup exported flags from dependencies
986 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500987 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700988 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400989 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
990 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
991 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700992
993 return depPaths
994}
995
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800996func (mod *Module) InstallInData() bool {
997 if mod.compiler == nil {
998 return false
999 }
1000 return mod.compiler.inData()
1001}
1002
Ivan Lozanoffee3342019-08-27 12:03:00 -07001003func linkPathFromFilePath(filepath android.Path) string {
1004 return strings.Split(filepath.String(), filepath.Base())[0]
1005}
Ivan Lozanod648c432020-02-06 12:05:10 -05001006
Ivan Lozanoffee3342019-08-27 12:03:00 -07001007func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1008 ctx := &depsContext{
1009 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001010 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011
1012 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001013 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001014
Ivan Lozano2b081132020-09-08 12:46:52 -04001015 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001016 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001017 stdLinkage = "rlib-std"
1018 }
1019
1020 rlibDepVariations := commonDepVariations
1021 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1022 rlibDepVariations = append(rlibDepVariations,
1023 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1024 }
1025
Ivan Lozano52767be2019-10-18 14:49:46 -07001026 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001027 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001028 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001029 rlibDepTag, deps.Rlibs...)
1030 actx.AddVariationDependencies(
1031 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001032 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001033 dylibDepTag, deps.Dylibs...)
1034
Ivan Lozano042504f2020-08-18 14:31:23 -04001035 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1036 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001037 if autoDep.depTag == rlibDepTag {
1038 actx.AddVariationDependencies(
1039 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1040 autoDep.depTag, deps.Rustlibs...)
1041 } else {
1042 actx.AddVariationDependencies(
1043 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1044 autoDep.depTag, deps.Rustlibs...)
1045 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001046 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001047 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001048 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001049 actx.AddVariationDependencies(
1050 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1051 rlibDepTag, deps.Stdlibs...)
1052 } else {
1053 actx.AddVariationDependencies(
1054 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1055 dylibDepTag, deps.Stdlibs...)
1056 }
1057 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001058 actx.AddVariationDependencies(append(commonDepVariations,
1059 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001060 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001061 actx.AddVariationDependencies(append(commonDepVariations,
1062 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001063 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001064
Zach Johnson3df4e632020-11-06 11:56:27 -08001065 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1066
Colin Cross565cafd2020-09-25 18:47:38 -07001067 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001068 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001069 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001070 }
1071 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001072 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001073 }
1074
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001075 if mod.sourceProvider != nil {
1076 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1077 bindgen.Properties.Custom_bindgen != "" {
1078 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1079 bindgen.Properties.Custom_bindgen)
1080 }
1081 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001082 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001083 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001084}
1085
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001086func BeginMutator(ctx android.BottomUpMutatorContext) {
1087 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1088 mod.beginMutator(ctx)
1089 }
1090}
1091
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001092func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1093 ctx := &baseModuleContext{
1094 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001095 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001096
1097 mod.begin(ctx)
1098}
1099
Ivan Lozanoffee3342019-08-27 12:03:00 -07001100func (mod *Module) Name() string {
1101 name := mod.ModuleBase.Name()
1102 if p, ok := mod.compiler.(interface {
1103 Name(string) string
1104 }); ok {
1105 name = p.Name(name)
1106 }
1107 return name
1108}
1109
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001110func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001111 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001112 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001113 }
1114}
1115
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001116var _ android.HostToolProvider = (*Module)(nil)
1117
1118func (mod *Module) HostToolPath() android.OptionalPath {
1119 if !mod.Host() {
1120 return android.OptionalPath{}
1121 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001122 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1123 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001124 }
1125 return android.OptionalPath{}
1126}
1127
Jiyong Park99644e92020-11-17 22:21:02 +09001128var _ android.ApexModule = (*Module)(nil)
1129
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001130func (mod *Module) minSdkVersion() string {
1131 return String(mod.Properties.Min_sdk_version)
1132}
1133
Jiyong Park45bf82e2020-12-15 22:29:02 +09001134var _ android.ApexModule = (*Module)(nil)
1135
1136// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001137func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001138 minSdkVersion := mod.minSdkVersion()
1139 if minSdkVersion == "apex_inherit" {
1140 return nil
1141 }
1142 if minSdkVersion == "" {
1143 return fmt.Errorf("min_sdk_version is not specificed")
1144 }
1145
1146 // Not using nativeApiLevelFromUser because the context here is not
1147 // necessarily a native context.
1148 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1149 if err != nil {
1150 return err
1151 }
1152
1153 if ver.GreaterThan(sdkVersion) {
1154 return fmt.Errorf("newer SDK(%v)", ver)
1155 }
Jiyong Park99644e92020-11-17 22:21:02 +09001156 return nil
1157}
1158
Jiyong Park45bf82e2020-12-15 22:29:02 +09001159// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001160func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1161 depTag := ctx.OtherModuleDependencyTag(dep)
1162
1163 if ccm, ok := dep.(*cc.Module); ok {
1164 if ccm.HasStubsVariants() {
1165 if cc.IsSharedDepTag(depTag) {
1166 // dynamic dep to a stubs lib crosses APEX boundary
1167 return false
1168 }
1169 if cc.IsRuntimeDepTag(depTag) {
1170 // runtime dep to a stubs lib also crosses APEX boundary
1171 return false
1172 }
1173
1174 if cc.IsHeaderDepTag(depTag) {
1175 return false
1176 }
1177 }
1178 if mod.Static() && cc.IsSharedDepTag(depTag) {
1179 // shared_lib dependency from a static lib is considered as crossing
1180 // the APEX boundary because the dependency doesn't actually is
1181 // linked; the dependency is used only during the compilation phase.
1182 return false
1183 }
1184 }
1185
1186 if depTag == procMacroDepTag {
1187 return false
1188 }
1189
1190 return true
1191}
1192
1193// Overrides ApexModule.IsInstallabeToApex()
1194func (mod *Module) IsInstallableToApex() bool {
1195 if mod.compiler != nil {
1196 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1197 return true
1198 }
1199 if _, ok := mod.compiler.(*binaryDecorator); ok {
1200 return true
1201 }
1202 }
1203 return false
1204}
1205
Ivan Lozanoffee3342019-08-27 12:03:00 -07001206var Bool = proptools.Bool
1207var BoolDefault = proptools.BoolDefault
1208var String = proptools.String
1209var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001210
1211var _ android.OutputFileProducer = (*Module)(nil)