blob: 504b7a9bda721c0c0b0af50002ec24d1c30e6248 [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 Lozano6cd99e62020-02-11 08:24:25 -050045
46 })
47 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
48 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 })
50 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020051 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070052}
53
54type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040055 GlobalRustFlags []string // Flags that apply globally to rust
56 GlobalLinkFlags []string // Flags that apply globally to linker
57 RustFlags []string // Flags that apply to rust
58 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Ivan Lozanof1c84332019-09-20 11:00:37 -070060 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070063}
64
65type BaseProperties struct {
66 AndroidMkRlibs []string
67 AndroidMkDylibs []string
68 AndroidMkProcMacroLibs []string
69 AndroidMkSharedLibs []string
70 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040071
Ivan Lozano6a884432020-12-02 09:15:16 -050072 ImageVariationPrefix string `blueprint:"mutated"`
73 VndkVersion string `blueprint:"mutated"`
74 SubName string `blueprint:"mutated"`
75
76 // Set by imageMutator
77 CoreVariantNeeded bool `blueprint:"mutated"`
78 ExtraVariants []string `blueprint:"mutated"`
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040079
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050080 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
81 Min_sdk_version *string
82
Ivan Lozano43845682020-07-09 21:03:28 -040083 PreventInstall bool
84 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070085}
86
87type Module struct {
88 android.ModuleBase
89 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090090 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070091
Ivan Lozano6a884432020-12-02 09:15:16 -050092 VendorProperties cc.VendorProperties
93
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 Properties BaseProperties
95
96 hod android.HostOrDeviceSupported
97 multilib android.Multilib
98
Ivan Lozano6a884432020-12-02 09:15:16 -050099 makeLinkType string
100
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400102 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200103 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500104 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700105 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400106 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700107 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400108
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200109 outputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900110
111 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112}
113
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500114func (mod *Module) Header() bool {
115 //TODO: If Rust libraries provide header variants, this needs to be updated.
116 return false
117}
118
119func (mod *Module) SetPreventInstall() {
120 mod.Properties.PreventInstall = true
121}
122
123// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
124func (mod *Module) InVendor() bool {
125 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
126}
127
128func (mod *Module) SetHideFromMake() {
129 mod.Properties.HideFromMake = true
130}
131
132func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500133 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
134 // nil since we need compiler to actually sanitize.
135 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500136}
137
138func (mod *Module) IsDependencyRoot() bool {
139 if mod.compiler != nil {
140 return mod.compiler.isDependencyRoot()
141 }
142 panic("IsDependencyRoot called on a non-compiler Rust module")
143}
144
145func (mod *Module) IsPrebuilt() bool {
146 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
147 return true
148 }
149 return false
150}
151
Ivan Lozano43845682020-07-09 21:03:28 -0400152func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
153 switch tag {
154 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700155 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400156 return mod.sourceProvider.Srcs(), nil
157 } else {
158 if mod.outputFile.Valid() {
159 return android.Paths{mod.outputFile.Path()}, nil
160 }
161 return android.Paths{}, nil
162 }
163 default:
164 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
165 }
166}
167
Ivan Lozano52767be2019-10-18 14:49:46 -0700168func (mod *Module) SelectedStl() string {
169 return ""
170}
171
Ivan Lozano2b262972019-11-21 12:30:50 -0800172func (mod *Module) NonCcVariants() bool {
173 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400174 if _, ok := mod.compiler.(libraryInterface); ok {
175 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800176 }
177 }
178 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
179}
180
Ivan Lozano52767be2019-10-18 14:49:46 -0700181func (mod *Module) Static() bool {
182 if mod.compiler != nil {
183 if library, ok := mod.compiler.(libraryInterface); ok {
184 return library.static()
185 }
186 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400187 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700188}
189
190func (mod *Module) Shared() bool {
191 if mod.compiler != nil {
192 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400193 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700194 }
195 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400196 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700197}
198
199func (mod *Module) Toc() android.OptionalPath {
200 if mod.compiler != nil {
201 if _, ok := mod.compiler.(libraryInterface); ok {
202 return android.OptionalPath{}
203 }
204 }
205 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
206}
207
Colin Crossc511bc52020-04-07 16:50:32 +0000208func (mod *Module) UseSdk() bool {
209 return false
210}
211
Ivan Lozano6a884432020-12-02 09:15:16 -0500212// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
213// "product" and "vendor" variant modules return true for this function.
214// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
215// "soc_specific: true" and more vendor installed modules are included here.
216// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
217// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700218func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500219 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700220}
221
222func (mod *Module) MustUseVendorVariant() bool {
223 return false
224}
225
226func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500227 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700228 return false
229}
230
Ivan Lozanof9e21722020-12-02 09:00:51 -0500231func (mod *Module) IsVndkExt() bool {
232 return false
233}
234
Colin Cross127bb8b2020-12-16 16:46:01 -0800235func (c *Module) IsVndkPrivate() bool {
236 return false
237}
238
239func (c *Module) IsLlndk() bool {
240 return false
241}
242
243func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500244 return false
245}
246
Ivan Lozano52767be2019-10-18 14:49:46 -0700247func (mod *Module) SdkVersion() string {
248 return ""
249}
250
Colin Crossc511bc52020-04-07 16:50:32 +0000251func (mod *Module) AlwaysSdk() bool {
252 return false
253}
254
Jiyong Park2286afd2020-06-16 21:58:53 +0900255func (mod *Module) IsSdkVariant() bool {
256 return false
257}
258
Colin Cross1348ce32020-10-01 13:37:16 -0700259func (mod *Module) SplitPerApiLevel() bool {
260 return false
261}
262
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263type Deps struct {
264 Dylibs []string
265 Rlibs []string
Matthew Maurer0f003b12020-06-29 14:34:06 -0700266 Rustlibs []string
Ivan Lozano2b081132020-09-08 12:46:52 -0400267 Stdlibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268 ProcMacros []string
269 SharedLibs []string
270 StaticLibs []string
Zach Johnson3df4e632020-11-06 11:56:27 -0800271 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700272
273 CrtBegin, CrtEnd string
274}
275
276type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500277 DyLibs RustLibraries
278 RLibs RustLibraries
279 SharedLibs android.Paths
280 SharedLibDeps android.Paths
281 StaticLibs android.Paths
282 ProcMacros RustLibraries
283 linkDirs []string
284 depFlags []string
285 linkObjects []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286 //ReexportedDeps android.Paths
Ivan Lozanof1c84332019-09-20 11:00:37 -0700287
Ivan Lozano45901ed2020-07-24 16:05:01 -0400288 // Used by bindgen modules which call clang
289 depClangFlags []string
290 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400291 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400292 depSystemIncludePaths android.Paths
293
Ivan Lozanof1c84332019-09-20 11:00:37 -0700294 CrtBegin android.OptionalPath
295 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700296
297 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500298 SrcDeps android.Paths
299 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700300}
301
302type RustLibraries []RustLibrary
303
304type RustLibrary struct {
305 Path android.Path
306 CrateName string
307}
308
309type compiler interface {
310 compilerFlags(ctx ModuleContext, flags Flags) Flags
311 compilerProps() []interface{}
312 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
313 compilerDeps(ctx DepsContext, deps Deps) Deps
314 crateName() string
315
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800316 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200317 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400319
320 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400321
322 Disabled() bool
323 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400324
Ivan Lozanodd055472020-09-28 13:22:45 -0400325 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500326 isDependencyRoot() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400327}
328
Matthew Maurerbb3add12020-06-25 09:34:12 -0700329type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700330 exportLinkDirs(...string)
331 exportDepFlags(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400332 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700333}
334
335type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400336 depFlags []string
337 linkDirs []string
338 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700339}
340
Matthew Maurerbb3add12020-06-25 09:34:12 -0700341func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
342 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
343}
344
345func (flagExporter *flagExporter) exportDepFlags(flags ...string) {
346 flagExporter.depFlags = android.FirstUniqueStrings(append(flagExporter.depFlags, flags...))
347}
348
Ivan Lozano2093af22020-08-25 12:48:19 -0400349func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
350 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
351}
352
Colin Cross0de8a1e2020-09-18 14:15:30 -0700353func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
354 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
355 Flags: flagExporter.depFlags,
356 LinkDirs: flagExporter.linkDirs,
357 LinkObjects: flagExporter.linkObjects,
358 })
359}
360
Matthew Maurerbb3add12020-06-25 09:34:12 -0700361var _ exportedFlagsProducer = (*flagExporter)(nil)
362
363func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700364 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700365}
366
Colin Cross0de8a1e2020-09-18 14:15:30 -0700367type FlagExporterInfo struct {
368 Flags []string
369 LinkDirs []string // TODO: this should be android.Paths
370 LinkObjects []string // TODO: this should be android.Paths
371}
372
373var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
374
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400375func (mod *Module) isCoverageVariant() bool {
376 return mod.coverage.Properties.IsCoverageVariant
377}
378
379var _ cc.Coverage = (*Module)(nil)
380
381func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
382 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
383}
384
385func (mod *Module) PreventInstall() {
386 mod.Properties.PreventInstall = true
387}
388
389func (mod *Module) HideFromMake() {
390 mod.Properties.HideFromMake = true
391}
392
393func (mod *Module) MarkAsCoverageVariant(coverage bool) {
394 mod.coverage.Properties.IsCoverageVariant = coverage
395}
396
397func (mod *Module) EnableCoverageIfNeeded() {
398 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399}
400
401func defaultsFactory() android.Module {
402 return DefaultsFactory()
403}
404
405type Defaults struct {
406 android.ModuleBase
407 android.DefaultsModuleBase
408}
409
410func DefaultsFactory(props ...interface{}) android.Module {
411 module := &Defaults{}
412
413 module.AddProperties(props...)
414 module.AddProperties(
415 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500416 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400417 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418 &BaseCompilerProperties{},
419 &BinaryCompilerProperties{},
420 &LibraryCompilerProperties{},
421 &ProcMacroCompilerProperties{},
422 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400423 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700424 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400425 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400426 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200427 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500428 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700429 )
430
431 android.InitDefaultsModule(module)
432 return module
433}
434
435func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700436 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437}
438
Ivan Lozano183a3212019-10-18 14:18:45 -0700439func (mod *Module) CcLibrary() bool {
440 if mod.compiler != nil {
441 if _, ok := mod.compiler.(*libraryDecorator); ok {
442 return true
443 }
444 }
445 return false
446}
447
448func (mod *Module) CcLibraryInterface() bool {
449 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400450 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
451 // VariantIs{Static,Shared} is set.
452 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700453 return true
454 }
455 }
456 return false
457}
458
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800459func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700460 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700461 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800462 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700463 }
464 }
465 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
466}
467
468func (mod *Module) SetStatic() {
469 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700470 if library, ok := mod.compiler.(libraryInterface); ok {
471 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700472 return
473 }
474 }
475 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
476}
477
478func (mod *Module) SetShared() {
479 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700480 if library, ok := mod.compiler.(libraryInterface); ok {
481 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700482 return
483 }
484 }
485 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
486}
487
Ivan Lozano183a3212019-10-18 14:18:45 -0700488func (mod *Module) BuildStaticVariant() bool {
489 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700490 if library, ok := mod.compiler.(libraryInterface); ok {
491 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700492 }
493 }
494 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
495}
496
497func (mod *Module) BuildSharedVariant() bool {
498 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700499 if library, ok := mod.compiler.(libraryInterface); ok {
500 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700501 }
502 }
503 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
504}
505
Ivan Lozano183a3212019-10-18 14:18:45 -0700506func (mod *Module) Module() android.Module {
507 return mod
508}
509
Ivan Lozano183a3212019-10-18 14:18:45 -0700510func (mod *Module) OutputFile() android.OptionalPath {
511 return mod.outputFile
512}
513
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400514func (mod *Module) CoverageFiles() android.Paths {
515 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800516 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400517 }
518 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
519}
520
Jiyong Park459feca2020-12-15 11:02:21 +0900521func (mod *Module) installable(apexInfo android.ApexInfo) bool {
522 // The apex variant is not installable because it is included in the APEX and won't appear
523 // in the system partition as a standalone file.
524 if !apexInfo.IsForPlatform() {
525 return false
526 }
527
528 return mod.outputFile.Valid() && !mod.Properties.PreventInstall
529}
530
Ivan Lozano183a3212019-10-18 14:18:45 -0700531var _ cc.LinkableInterface = (*Module)(nil)
532
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533func (mod *Module) Init() android.Module {
534 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500535 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700536
537 if mod.compiler != nil {
538 mod.AddProperties(mod.compiler.compilerProps()...)
539 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540 if mod.coverage != nil {
541 mod.AddProperties(mod.coverage.props()...)
542 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200543 if mod.clippy != nil {
544 mod.AddProperties(mod.clippy.props()...)
545 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400546 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700547 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400548 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500549 if mod.sanitize != nil {
550 mod.AddProperties(mod.sanitize.props()...)
551 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400552
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900554 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555
556 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557 return mod
558}
559
560func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
561 return &Module{
562 hod: hod,
563 multilib: multilib,
564 }
565}
566func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
567 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400568 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200569 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500570 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700571 return module
572}
573
574type ModuleContext interface {
575 android.ModuleContext
576 ModuleContextIntf
577}
578
579type BaseModuleContext interface {
580 android.BaseModuleContext
581 ModuleContextIntf
582}
583
584type DepsContext interface {
585 android.BottomUpMutatorContext
586 ModuleContextIntf
587}
588
589type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200590 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700591 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700592}
593
594type depsContext struct {
595 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700596}
597
598type moduleContext struct {
599 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600}
601
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200602type baseModuleContext struct {
603 android.BaseModuleContext
604}
605
606func (ctx *moduleContext) RustModule() *Module {
607 return ctx.Module().(*Module)
608}
609
610func (ctx *moduleContext) toolchain() config.Toolchain {
611 return ctx.RustModule().toolchain(ctx)
612}
613
614func (ctx *depsContext) RustModule() *Module {
615 return ctx.Module().(*Module)
616}
617
618func (ctx *depsContext) toolchain() config.Toolchain {
619 return ctx.RustModule().toolchain(ctx)
620}
621
622func (ctx *baseModuleContext) RustModule() *Module {
623 return ctx.Module().(*Module)
624}
625
626func (ctx *baseModuleContext) toolchain() config.Toolchain {
627 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400628}
629
630func (mod *Module) nativeCoverage() bool {
631 return mod.compiler != nil && mod.compiler.nativeCoverage()
632}
633
Ivan Lozanoffee3342019-08-27 12:03:00 -0700634func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
635 if mod.cachedToolchain == nil {
636 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
637 }
638 return mod.cachedToolchain
639}
640
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200641func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
642 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
643}
644
Ivan Lozanoffee3342019-08-27 12:03:00 -0700645func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
646}
647
648func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
649 ctx := &moduleContext{
650 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700651 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700652
Jiyong Park99644e92020-11-17 22:21:02 +0900653 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
654 if !apexInfo.IsForPlatform() {
655 mod.hideApexVariantFromMake = true
656 }
657
Ivan Lozanoffee3342019-08-27 12:03:00 -0700658 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500659 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
660
661 // Differentiate static libraries that are vendor available
662 if mod.UseVndk() {
663 mod.Properties.SubName += ".vendor"
664 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700665
666 if !toolchain.Supported() {
667 // This toolchain's unsupported, there's nothing to do for this mod.
668 return
669 }
670
671 deps := mod.depsToPaths(ctx)
672 flags := Flags{
673 Toolchain: toolchain,
674 }
675
676 if mod.compiler != nil {
677 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400678 }
679 if mod.coverage != nil {
680 flags, deps = mod.coverage.flags(ctx, flags, deps)
681 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200682 if mod.clippy != nil {
683 flags, deps = mod.clippy.flags(ctx, flags, deps)
684 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500685 if mod.sanitize != nil {
686 flags, deps = mod.sanitize.flags(ctx, flags, deps)
687 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400688
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200689 // SourceProvider needs to call GenerateSource() before compiler calls
690 // compile() so it can provide the source. A SourceProvider has
691 // multiple variants (e.g. source, rlib, dylib). Only the "source"
692 // variant is responsible for effectively generating the source. The
693 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400694 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200695 if mod.compiler.(libraryInterface).source() {
696 mod.sourceProvider.GenerateSource(ctx, deps)
697 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
698 } else {
699 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
700 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700701 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200702 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400703 }
704
705 if mod.compiler != nil && !mod.compiler.Disabled() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700706 outputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400707
Ivan Lozanoffee3342019-08-27 12:03:00 -0700708 mod.outputFile = android.OptionalPathForPath(outputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900709
710 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
711 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200712 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400713 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700714 }
715}
716
717func (mod *Module) deps(ctx DepsContext) Deps {
718 deps := Deps{}
719
720 if mod.compiler != nil {
721 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400722 }
723 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700724 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725 }
726
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400727 if mod.coverage != nil {
728 deps = mod.coverage.deps(ctx, deps)
729 }
730
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500731 if mod.sanitize != nil {
732 deps = mod.sanitize.deps(ctx, deps)
733 }
734
Ivan Lozanoffee3342019-08-27 12:03:00 -0700735 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
736 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700737 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
739 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
740 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
741
742 return deps
743
744}
745
Ivan Lozanoffee3342019-08-27 12:03:00 -0700746type dependencyTag struct {
747 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800748 name string
749 library bool
750 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751}
752
Jiyong Park65b62242020-11-25 12:44:59 +0900753// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
754// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
755func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800756 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900757}
758
759var _ android.InstallNeededDependencyTag = dependencyTag{}
760
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400762 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
763 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
764 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800765 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400766 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200767 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768)
769
Jiyong Park99644e92020-11-17 22:21:02 +0900770func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
771 tag, ok := depTag.(dependencyTag)
772 return ok && tag == dylibDepTag
773}
774
Matthew Maurer0f003b12020-06-29 14:34:06 -0700775type autoDep struct {
776 variation string
777 depTag dependencyTag
778}
779
780var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200781 rlibVariation = "rlib"
782 dylibVariation = "dylib"
783 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
784 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700785)
786
787type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500788 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700789}
790
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400791func (mod *Module) begin(ctx BaseModuleContext) {
792 if mod.coverage != nil {
793 mod.coverage.begin(ctx)
794 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500795 if mod.sanitize != nil {
796 mod.sanitize.begin(ctx)
797 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400798}
799
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
801 var depPaths PathDeps
802
803 directRlibDeps := []*Module{}
804 directDylibDeps := []*Module{}
805 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700806 directSharedLibDeps := [](cc.LinkableInterface){}
807 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400808 directSrcProvidersDeps := []*Module{}
809 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810
811 ctx.VisitDirectDeps(func(dep android.Module) {
812 depName := ctx.OtherModuleName(dep)
813 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400814 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700815 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700816
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 switch depTag {
818 case dylibDepTag:
819 dylib, ok := rustDep.compiler.(libraryInterface)
820 if !ok || !dylib.dylib() {
821 ctx.ModuleErrorf("mod %q not an dylib library", depName)
822 return
823 }
824 directDylibDeps = append(directDylibDeps, rustDep)
825 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
826 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400827
Ivan Lozanoffee3342019-08-27 12:03:00 -0700828 rlib, ok := rustDep.compiler.(libraryInterface)
829 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400830 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700831 return
832 }
833 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400834 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700835 case procMacroDepTag:
836 directProcMacroDeps = append(directProcMacroDeps, rustDep)
837 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400838 case android.SourceDepTag:
839 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
840 // OS/Arch variant is used.
841 var helper string
842 if ctx.Host() {
843 helper = "missing 'host_supported'?"
844 } else {
845 helper = "device module defined?"
846 }
847
848 if dep.Target().Os != ctx.Os() {
849 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
850 return
851 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
852 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
853 return
854 }
855 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700856 }
857
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400858 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700859 if depTag != procMacroDepTag {
860 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
861 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
862 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
863 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700864 }
865
Ivan Lozanoffee3342019-08-27 12:03:00 -0700866 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400867 linkFile := rustDep.outputFile
868 if !linkFile.Valid() {
869 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
870 depName, ctx.ModuleName())
871 return
872 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700874 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
875 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700876 }
877 }
878
Ivan Lozano89435d12020-07-31 11:01:18 -0400879 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700880 //Handle C dependencies
881 if _, ok := ccDep.(*Module); !ok {
882 if ccDep.Module().Target().Os != ctx.Os() {
883 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
884 return
885 }
886 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
887 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
888 return
889 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700890 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400891 linkObject := ccDep.OutputFile()
892 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500893
Ivan Lozano2093af22020-08-25 12:48:19 -0400894 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700895 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
896 }
897
898 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700899 switch {
900 case cc.IsStaticDepTag(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 directStaticLibDeps = append(directStaticLibDeps, ccDep)
909 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700910 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400912 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700913 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
914 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
915 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
916 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
917 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700918 directSharedLibDeps = append(directSharedLibDeps, ccDep)
919 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
920 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800921 case cc.IsHeaderDepTag(depTag):
922 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
923 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
924 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
925 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700926 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400927 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700928 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400929 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930 }
931
932 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700933 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
934 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400935 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700936 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400938
939 if srcDep, ok := dep.(android.SourceFileProducer); ok {
940 switch depTag {
941 case android.SourceDepTag:
942 // These are usually genrules which don't have per-target variants.
943 directSrcDeps = append(directSrcDeps, srcDep)
944 }
945 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700946 })
947
948 var rlibDepFiles RustLibraries
949 for _, dep := range directRlibDeps {
950 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
951 }
952 var dylibDepFiles RustLibraries
953 for _, dep := range directDylibDeps {
954 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
955 }
956 var procMacroDepFiles RustLibraries
957 for _, dep := range directProcMacroDeps {
958 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.outputFile.Path(), CrateName: dep.CrateName()})
959 }
960
961 var staticLibDepFiles android.Paths
962 for _, dep := range directStaticLibDeps {
963 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
964 }
965
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500966 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967 var sharedLibDepFiles android.Paths
968 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500969 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
970 if dep.Toc().Valid() {
971 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
972 } else {
973 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
974 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975 }
976
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400977 var srcProviderDepFiles android.Paths
978 for _, dep := range directSrcProvidersDeps {
979 srcs, _ := dep.OutputFiles("")
980 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
981 }
982 for _, dep := range directSrcDeps {
983 srcs := dep.Srcs()
984 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
985 }
986
Ivan Lozanoffee3342019-08-27 12:03:00 -0700987 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
988 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
989 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500990 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700991 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
992 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400993 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994
995 // Dedup exported flags from dependencies
996 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500997 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -0400999 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1000 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1001 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002
1003 return depPaths
1004}
1005
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001006func (mod *Module) InstallInData() bool {
1007 if mod.compiler == nil {
1008 return false
1009 }
1010 return mod.compiler.inData()
1011}
1012
Ivan Lozanoffee3342019-08-27 12:03:00 -07001013func linkPathFromFilePath(filepath android.Path) string {
1014 return strings.Split(filepath.String(), filepath.Base())[0]
1015}
Ivan Lozanod648c432020-02-06 12:05:10 -05001016
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1018 ctx := &depsContext{
1019 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001020 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001021
1022 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001023 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001024
Ivan Lozano2b081132020-09-08 12:46:52 -04001025 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001026 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001027 stdLinkage = "rlib-std"
1028 }
1029
1030 rlibDepVariations := commonDepVariations
1031 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1032 rlibDepVariations = append(rlibDepVariations,
1033 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1034 }
1035
Ivan Lozano52767be2019-10-18 14:49:46 -07001036 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001037 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001038 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001039 rlibDepTag, deps.Rlibs...)
1040 actx.AddVariationDependencies(
1041 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001042 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001043 dylibDepTag, deps.Dylibs...)
1044
Ivan Lozano042504f2020-08-18 14:31:23 -04001045 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1046 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001047 if autoDep.depTag == rlibDepTag {
1048 actx.AddVariationDependencies(
1049 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1050 autoDep.depTag, deps.Rustlibs...)
1051 } else {
1052 actx.AddVariationDependencies(
1053 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1054 autoDep.depTag, deps.Rustlibs...)
1055 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001056 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001057 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001058 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001059 actx.AddVariationDependencies(
1060 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1061 rlibDepTag, deps.Stdlibs...)
1062 } else {
1063 actx.AddVariationDependencies(
1064 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1065 dylibDepTag, deps.Stdlibs...)
1066 }
1067 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001068 actx.AddVariationDependencies(append(commonDepVariations,
1069 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001070 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001071 actx.AddVariationDependencies(append(commonDepVariations,
1072 blueprint.Variation{Mutator: "link", Variation: "static"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001073 cc.StaticDepTag(), deps.StaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001074
Zach Johnson3df4e632020-11-06 11:56:27 -08001075 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1076
Colin Cross565cafd2020-09-25 18:47:38 -07001077 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001078 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001079 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001080 }
1081 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001082 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001083 }
1084
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001085 if mod.sourceProvider != nil {
1086 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1087 bindgen.Properties.Custom_bindgen != "" {
1088 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1089 bindgen.Properties.Custom_bindgen)
1090 }
1091 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001092 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001093 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001094}
1095
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001096func BeginMutator(ctx android.BottomUpMutatorContext) {
1097 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1098 mod.beginMutator(ctx)
1099 }
1100}
1101
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001102func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1103 ctx := &baseModuleContext{
1104 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001105 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001106
1107 mod.begin(ctx)
1108}
1109
Ivan Lozanoffee3342019-08-27 12:03:00 -07001110func (mod *Module) Name() string {
1111 name := mod.ModuleBase.Name()
1112 if p, ok := mod.compiler.(interface {
1113 Name(string) string
1114 }); ok {
1115 name = p.Name(name)
1116 }
1117 return name
1118}
1119
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001120func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001121 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001122 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001123 }
1124}
1125
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001126var _ android.HostToolProvider = (*Module)(nil)
1127
1128func (mod *Module) HostToolPath() android.OptionalPath {
1129 if !mod.Host() {
1130 return android.OptionalPath{}
1131 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001132 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1133 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001134 }
1135 return android.OptionalPath{}
1136}
1137
Jiyong Park99644e92020-11-17 22:21:02 +09001138var _ android.ApexModule = (*Module)(nil)
1139
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001140func (mod *Module) minSdkVersion() string {
1141 return String(mod.Properties.Min_sdk_version)
1142}
1143
Jiyong Park45bf82e2020-12-15 22:29:02 +09001144var _ android.ApexModule = (*Module)(nil)
1145
1146// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001147func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001148 minSdkVersion := mod.minSdkVersion()
1149 if minSdkVersion == "apex_inherit" {
1150 return nil
1151 }
1152 if minSdkVersion == "" {
1153 return fmt.Errorf("min_sdk_version is not specificed")
1154 }
1155
1156 // Not using nativeApiLevelFromUser because the context here is not
1157 // necessarily a native context.
1158 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1159 if err != nil {
1160 return err
1161 }
1162
1163 if ver.GreaterThan(sdkVersion) {
1164 return fmt.Errorf("newer SDK(%v)", ver)
1165 }
Jiyong Park99644e92020-11-17 22:21:02 +09001166 return nil
1167}
1168
Jiyong Park45bf82e2020-12-15 22:29:02 +09001169// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001170func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1171 depTag := ctx.OtherModuleDependencyTag(dep)
1172
1173 if ccm, ok := dep.(*cc.Module); ok {
1174 if ccm.HasStubsVariants() {
1175 if cc.IsSharedDepTag(depTag) {
1176 // dynamic dep to a stubs lib crosses APEX boundary
1177 return false
1178 }
1179 if cc.IsRuntimeDepTag(depTag) {
1180 // runtime dep to a stubs lib also crosses APEX boundary
1181 return false
1182 }
1183
1184 if cc.IsHeaderDepTag(depTag) {
1185 return false
1186 }
1187 }
1188 if mod.Static() && cc.IsSharedDepTag(depTag) {
1189 // shared_lib dependency from a static lib is considered as crossing
1190 // the APEX boundary because the dependency doesn't actually is
1191 // linked; the dependency is used only during the compilation phase.
1192 return false
1193 }
1194 }
1195
1196 if depTag == procMacroDepTag {
1197 return false
1198 }
1199
1200 return true
1201}
1202
1203// Overrides ApexModule.IsInstallabeToApex()
1204func (mod *Module) IsInstallableToApex() bool {
1205 if mod.compiler != nil {
1206 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1207 return true
1208 }
1209 if _, ok := mod.compiler.(*binaryDecorator); ok {
1210 return true
1211 }
1212 }
1213 return false
1214}
1215
Ivan Lozanoffee3342019-08-27 12:03:00 -07001216var Bool = proptools.Bool
1217var BoolDefault = proptools.BoolDefault
1218var String = proptools.String
1219var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001220
1221var _ android.OutputFileProducer = (*Module)(nil)