blob: 566ad3787797a567cd1112d49aa507a05bbbfab9 [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
Ivan Lozanoe6d30982021-02-05 10:57:43 -050077 CoreVariantNeeded bool `blueprint:"mutated"`
78 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
79 ExtraVariants []string `blueprint:"mutated"`
80
81 // Make this module available when building for vendor ramdisk.
82 // On device without a dedicated recovery partition, the module is only
83 // available after switching root into
84 // /first_stage_ramdisk. To expose the module before switching root, install
85 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
86 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040087
Ivan Lozano3e9f9e42020-12-04 15:05:43 -050088 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
89 Min_sdk_version *string
90
Ivan Lozano43845682020-07-09 21:03:28 -040091 PreventInstall bool
92 HideFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070093}
94
95type Module struct {
96 android.ModuleBase
97 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +090098 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -070099
Ivan Lozano6a884432020-12-02 09:15:16 -0500100 VendorProperties cc.VendorProperties
101
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102 Properties BaseProperties
103
104 hod android.HostOrDeviceSupported
105 multilib android.Multilib
106
Ivan Lozano6a884432020-12-02 09:15:16 -0500107 makeLinkType string
108
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400110 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200111 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500112 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400114 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700115 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400116
Jiyong Parke54f07e2021-04-07 15:08:04 +0900117 // Unstripped output. This is usually used when this module is linked to another module
118 // as a library. The stripped output which is used for installation can be found via
119 // compiler.strippedOutputFile if it exists.
120 unstrippedOutputFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900121
122 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123}
124
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500125func (mod *Module) Header() bool {
126 //TODO: If Rust libraries provide header variants, this needs to be updated.
127 return false
128}
129
130func (mod *Module) SetPreventInstall() {
131 mod.Properties.PreventInstall = true
132}
133
134// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
135func (mod *Module) InVendor() bool {
136 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
137}
138
139func (mod *Module) SetHideFromMake() {
140 mod.Properties.HideFromMake = true
141}
142
143func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500144 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
145 // nil since we need compiler to actually sanitize.
146 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500147}
148
149func (mod *Module) IsDependencyRoot() bool {
150 if mod.compiler != nil {
151 return mod.compiler.isDependencyRoot()
152 }
153 panic("IsDependencyRoot called on a non-compiler Rust module")
154}
155
156func (mod *Module) IsPrebuilt() bool {
157 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
158 return true
159 }
160 return false
161}
162
Ivan Lozano43845682020-07-09 21:03:28 -0400163func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
164 switch tag {
165 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700166 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400167 return mod.sourceProvider.Srcs(), nil
168 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900169 if mod.OutputFile().Valid() {
170 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400171 }
172 return android.Paths{}, nil
173 }
174 default:
175 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
176 }
177}
178
Ivan Lozano52767be2019-10-18 14:49:46 -0700179func (mod *Module) SelectedStl() string {
180 return ""
181}
182
Ivan Lozano2b262972019-11-21 12:30:50 -0800183func (mod *Module) NonCcVariants() bool {
184 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400185 if _, ok := mod.compiler.(libraryInterface); ok {
186 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800187 }
188 }
189 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
190}
191
Ivan Lozano52767be2019-10-18 14:49:46 -0700192func (mod *Module) Static() bool {
193 if mod.compiler != nil {
194 if library, ok := mod.compiler.(libraryInterface); ok {
195 return library.static()
196 }
197 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400198 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700199}
200
201func (mod *Module) Shared() bool {
202 if mod.compiler != nil {
203 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400204 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700205 }
206 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400207 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700208}
209
210func (mod *Module) Toc() android.OptionalPath {
211 if mod.compiler != nil {
212 if _, ok := mod.compiler.(libraryInterface); ok {
213 return android.OptionalPath{}
214 }
215 }
216 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
217}
218
Colin Crossc511bc52020-04-07 16:50:32 +0000219func (mod *Module) UseSdk() bool {
220 return false
221}
222
Ivan Lozano6a884432020-12-02 09:15:16 -0500223// Returns true if the module is using VNDK libraries instead of the libraries in /system/lib or /system/lib64.
224// "product" and "vendor" variant modules return true for this function.
225// When BOARD_VNDK_VERSION is set, vendor variants of "vendor_available: true", "vendor: true",
226// "soc_specific: true" and more vendor installed modules are included here.
227// When PRODUCT_PRODUCT_VNDK_VERSION is set, product variants of "vendor_available: true" or
228// "product_specific: true" modules are included here.
Ivan Lozano52767be2019-10-18 14:49:46 -0700229func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500230 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700231}
232
233func (mod *Module) MustUseVendorVariant() bool {
234 return false
235}
236
237func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500238 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700239 return false
240}
241
Ivan Lozanof9e21722020-12-02 09:00:51 -0500242func (mod *Module) IsVndkExt() bool {
243 return false
244}
245
Colin Cross127bb8b2020-12-16 16:46:01 -0800246func (c *Module) IsVndkPrivate() bool {
247 return false
248}
249
250func (c *Module) IsLlndk() bool {
251 return false
252}
253
254func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500255 return false
256}
257
Ivan Lozano52767be2019-10-18 14:49:46 -0700258func (mod *Module) SdkVersion() string {
259 return ""
260}
261
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900262func (mod *Module) MinSdkVersion() string {
263 return ""
264}
265
Colin Crossc511bc52020-04-07 16:50:32 +0000266func (mod *Module) AlwaysSdk() bool {
267 return false
268}
269
Jiyong Park2286afd2020-06-16 21:58:53 +0900270func (mod *Module) IsSdkVariant() bool {
271 return false
272}
273
Colin Cross1348ce32020-10-01 13:37:16 -0700274func (mod *Module) SplitPerApiLevel() bool {
275 return false
276}
277
Ivan Lozanoffee3342019-08-27 12:03:00 -0700278type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400279 Dylibs []string
280 Rlibs []string
281 Rustlibs []string
282 Stdlibs []string
283 ProcMacros []string
284 SharedLibs []string
285 StaticLibs []string
286 WholeStaticLibs []string
287 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700288
289 CrtBegin, CrtEnd string
290}
291
292type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500293 DyLibs RustLibraries
294 RLibs RustLibraries
295 SharedLibs android.Paths
296 SharedLibDeps android.Paths
297 StaticLibs android.Paths
298 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500299
300 // depFlags and depLinkFlags are rustc and linker (clang) flags.
301 depFlags []string
302 depLinkFlags []string
303
304 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
305 // Both of these are exported and propagate to dependencies.
306 linkDirs []string
307 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700308
Ivan Lozano45901ed2020-07-24 16:05:01 -0400309 // Used by bindgen modules which call clang
310 depClangFlags []string
311 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400312 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400313 depSystemIncludePaths android.Paths
314
Ivan Lozanof1c84332019-09-20 11:00:37 -0700315 CrtBegin android.OptionalPath
316 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700317
318 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500319 SrcDeps android.Paths
320 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321}
322
323type RustLibraries []RustLibrary
324
325type RustLibrary struct {
326 Path android.Path
327 CrateName string
328}
329
330type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100331 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700332 compilerFlags(ctx ModuleContext, flags Flags) Flags
333 compilerProps() []interface{}
334 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
335 compilerDeps(ctx DepsContext, deps Deps) Deps
336 crateName() string
337
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100338 // Output directory in which source-generated code from dependencies is
339 // copied. This is equivalent to Cargo's OUT_DIR variable.
340 CargoOutDir() android.OptionalPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800341 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200342 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700343 relativeInstallPath() string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400344
345 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400346
347 Disabled() bool
348 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400349
Ivan Lozanodd055472020-09-28 13:22:45 -0400350 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500351 isDependencyRoot() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900352
353 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400354}
355
Matthew Maurerbb3add12020-06-25 09:34:12 -0700356type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700357 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400358 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700359}
360
361type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400362 linkDirs []string
363 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700364}
365
Matthew Maurerbb3add12020-06-25 09:34:12 -0700366func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
367 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
368}
369
Ivan Lozano2093af22020-08-25 12:48:19 -0400370func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
371 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
372}
373
Colin Cross0de8a1e2020-09-18 14:15:30 -0700374func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
375 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700376 LinkDirs: flagExporter.linkDirs,
377 LinkObjects: flagExporter.linkObjects,
378 })
379}
380
Matthew Maurerbb3add12020-06-25 09:34:12 -0700381var _ exportedFlagsProducer = (*flagExporter)(nil)
382
383func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700384 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700385}
386
Colin Cross0de8a1e2020-09-18 14:15:30 -0700387type FlagExporterInfo struct {
388 Flags []string
389 LinkDirs []string // TODO: this should be android.Paths
390 LinkObjects []string // TODO: this should be android.Paths
391}
392
393var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
394
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400395func (mod *Module) isCoverageVariant() bool {
396 return mod.coverage.Properties.IsCoverageVariant
397}
398
399var _ cc.Coverage = (*Module)(nil)
400
401func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
402 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
403}
404
405func (mod *Module) PreventInstall() {
406 mod.Properties.PreventInstall = true
407}
408
409func (mod *Module) HideFromMake() {
410 mod.Properties.HideFromMake = true
411}
412
413func (mod *Module) MarkAsCoverageVariant(coverage bool) {
414 mod.coverage.Properties.IsCoverageVariant = coverage
415}
416
417func (mod *Module) EnableCoverageIfNeeded() {
418 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700419}
420
421func defaultsFactory() android.Module {
422 return DefaultsFactory()
423}
424
425type Defaults struct {
426 android.ModuleBase
427 android.DefaultsModuleBase
428}
429
430func DefaultsFactory(props ...interface{}) android.Module {
431 module := &Defaults{}
432
433 module.AddProperties(props...)
434 module.AddProperties(
435 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500436 &cc.VendorProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400437 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438 &BaseCompilerProperties{},
439 &BinaryCompilerProperties{},
440 &LibraryCompilerProperties{},
441 &ProcMacroCompilerProperties{},
442 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400443 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700444 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400445 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400446 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200447 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500448 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449 )
450
451 android.InitDefaultsModule(module)
452 return module
453}
454
455func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700456 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700457}
458
Ivan Lozano183a3212019-10-18 14:18:45 -0700459func (mod *Module) CcLibrary() bool {
460 if mod.compiler != nil {
461 if _, ok := mod.compiler.(*libraryDecorator); ok {
462 return true
463 }
464 }
465 return false
466}
467
468func (mod *Module) CcLibraryInterface() bool {
469 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400470 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
471 // VariantIs{Static,Shared} is set.
472 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700473 return true
474 }
475 }
476 return false
477}
478
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800479func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700480 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700481 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800482 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700483 }
484 }
485 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
486}
487
488func (mod *Module) SetStatic() {
489 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700490 if library, ok := mod.compiler.(libraryInterface); ok {
491 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700492 return
493 }
494 }
495 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
496}
497
498func (mod *Module) SetShared() {
499 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700500 if library, ok := mod.compiler.(libraryInterface); ok {
501 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700502 return
503 }
504 }
505 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
506}
507
Ivan Lozano183a3212019-10-18 14:18:45 -0700508func (mod *Module) BuildStaticVariant() bool {
509 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700510 if library, ok := mod.compiler.(libraryInterface); ok {
511 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700512 }
513 }
514 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
515}
516
517func (mod *Module) BuildSharedVariant() bool {
518 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700519 if library, ok := mod.compiler.(libraryInterface); ok {
520 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700521 }
522 }
523 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
524}
525
Ivan Lozano183a3212019-10-18 14:18:45 -0700526func (mod *Module) Module() android.Module {
527 return mod
528}
529
Ivan Lozano183a3212019-10-18 14:18:45 -0700530func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900531 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
532 return mod.compiler.strippedOutputFilePath()
533 }
534 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700535}
536
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400537func (mod *Module) CoverageFiles() android.Paths {
538 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800539 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400540 }
541 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
542}
543
Jiyong Park459feca2020-12-15 11:02:21 +0900544func (mod *Module) installable(apexInfo android.ApexInfo) bool {
545 // The apex variant is not installable because it is included in the APEX and won't appear
546 // in the system partition as a standalone file.
547 if !apexInfo.IsForPlatform() {
548 return false
549 }
550
Jiyong Parke54f07e2021-04-07 15:08:04 +0900551 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900552}
553
Ivan Lozano183a3212019-10-18 14:18:45 -0700554var _ cc.LinkableInterface = (*Module)(nil)
555
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556func (mod *Module) Init() android.Module {
557 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500558 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700559
560 if mod.compiler != nil {
561 mod.AddProperties(mod.compiler.compilerProps()...)
562 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400563 if mod.coverage != nil {
564 mod.AddProperties(mod.coverage.props()...)
565 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200566 if mod.clippy != nil {
567 mod.AddProperties(mod.clippy.props()...)
568 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400569 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700570 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400571 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500572 if mod.sanitize != nil {
573 mod.AddProperties(mod.sanitize.props()...)
574 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400575
Ivan Lozanoffee3342019-08-27 12:03:00 -0700576 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900577 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700578
579 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700580 return mod
581}
582
583func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
584 return &Module{
585 hod: hod,
586 multilib: multilib,
587 }
588}
589func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
590 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400591 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200592 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500593 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700594 return module
595}
596
597type ModuleContext interface {
598 android.ModuleContext
599 ModuleContextIntf
600}
601
602type BaseModuleContext interface {
603 android.BaseModuleContext
604 ModuleContextIntf
605}
606
607type DepsContext interface {
608 android.BottomUpMutatorContext
609 ModuleContextIntf
610}
611
612type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200613 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700614 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700615}
616
617type depsContext struct {
618 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700619}
620
621type moduleContext struct {
622 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700623}
624
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200625type baseModuleContext struct {
626 android.BaseModuleContext
627}
628
629func (ctx *moduleContext) RustModule() *Module {
630 return ctx.Module().(*Module)
631}
632
633func (ctx *moduleContext) toolchain() config.Toolchain {
634 return ctx.RustModule().toolchain(ctx)
635}
636
637func (ctx *depsContext) RustModule() *Module {
638 return ctx.Module().(*Module)
639}
640
641func (ctx *depsContext) toolchain() config.Toolchain {
642 return ctx.RustModule().toolchain(ctx)
643}
644
645func (ctx *baseModuleContext) RustModule() *Module {
646 return ctx.Module().(*Module)
647}
648
649func (ctx *baseModuleContext) toolchain() config.Toolchain {
650 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400651}
652
653func (mod *Module) nativeCoverage() bool {
654 return mod.compiler != nil && mod.compiler.nativeCoverage()
655}
656
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
658 if mod.cachedToolchain == nil {
659 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
660 }
661 return mod.cachedToolchain
662}
663
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200664func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
665 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
666}
667
Ivan Lozanoffee3342019-08-27 12:03:00 -0700668func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
669}
670
671func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
672 ctx := &moduleContext{
673 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700675
Jiyong Park99644e92020-11-17 22:21:02 +0900676 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
677 if !apexInfo.IsForPlatform() {
678 mod.hideApexVariantFromMake = true
679 }
680
Ivan Lozanoffee3342019-08-27 12:03:00 -0700681 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500682 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
683
684 // Differentiate static libraries that are vendor available
685 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500686 mod.Properties.SubName += cc.VendorSuffix
687 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
688 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500689 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700690
691 if !toolchain.Supported() {
692 // This toolchain's unsupported, there's nothing to do for this mod.
693 return
694 }
695
696 deps := mod.depsToPaths(ctx)
697 flags := Flags{
698 Toolchain: toolchain,
699 }
700
701 if mod.compiler != nil {
702 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400703 }
704 if mod.coverage != nil {
705 flags, deps = mod.coverage.flags(ctx, flags, deps)
706 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200707 if mod.clippy != nil {
708 flags, deps = mod.clippy.flags(ctx, flags, deps)
709 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500710 if mod.sanitize != nil {
711 flags, deps = mod.sanitize.flags(ctx, flags, deps)
712 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400713
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200714 // SourceProvider needs to call GenerateSource() before compiler calls
715 // compile() so it can provide the source. A SourceProvider has
716 // multiple variants (e.g. source, rlib, dylib). Only the "source"
717 // variant is responsible for effectively generating the source. The
718 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400719 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200720 if mod.compiler.(libraryInterface).source() {
721 mod.sourceProvider.GenerateSource(ctx, deps)
722 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
723 } else {
724 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
725 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700726 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200727 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400728 }
729
730 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100731 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900732 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400733
Jiyong Parke54f07e2021-04-07 15:08:04 +0900734 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900735
736 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
737 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200738 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400739 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700740 }
741}
742
743func (mod *Module) deps(ctx DepsContext) Deps {
744 deps := Deps{}
745
746 if mod.compiler != nil {
747 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400748 }
749 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700750 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700751 }
752
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400753 if mod.coverage != nil {
754 deps = mod.coverage.deps(ctx, deps)
755 }
756
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500757 if mod.sanitize != nil {
758 deps = mod.sanitize.deps(ctx, deps)
759 }
760
Ivan Lozanoffee3342019-08-27 12:03:00 -0700761 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
762 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700763 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700764 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
765 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
766 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400767 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700768 return deps
769
770}
771
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772type dependencyTag struct {
773 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800774 name string
775 library bool
776 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777}
778
Jiyong Park65b62242020-11-25 12:44:59 +0900779// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
780// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
781func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800782 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900783}
784
785var _ android.InstallNeededDependencyTag = dependencyTag{}
786
Ivan Lozanoffee3342019-08-27 12:03:00 -0700787var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400788 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
789 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
790 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800791 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400792 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200793 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794)
795
Jiyong Park99644e92020-11-17 22:21:02 +0900796func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
797 tag, ok := depTag.(dependencyTag)
798 return ok && tag == dylibDepTag
799}
800
Matthew Maurer0f003b12020-06-29 14:34:06 -0700801type autoDep struct {
802 variation string
803 depTag dependencyTag
804}
805
806var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200807 rlibVariation = "rlib"
808 dylibVariation = "dylib"
809 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
810 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700811)
812
813type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500814 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700815}
816
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400817func (mod *Module) begin(ctx BaseModuleContext) {
818 if mod.coverage != nil {
819 mod.coverage.begin(ctx)
820 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500821 if mod.sanitize != nil {
822 mod.sanitize.begin(ctx)
823 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400824}
825
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
827 var depPaths PathDeps
828
829 directRlibDeps := []*Module{}
830 directDylibDeps := []*Module{}
831 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700832 directSharedLibDeps := [](cc.LinkableInterface){}
833 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400834 directSrcProvidersDeps := []*Module{}
835 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700836
837 ctx.VisitDirectDeps(func(dep android.Module) {
838 depName := ctx.OtherModuleName(dep)
839 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozano89435d12020-07-31 11:01:18 -0400840 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700841 //Handle Rust Modules
Ivan Lozano70e0a072019-09-13 14:23:15 -0700842
Ivan Lozanoffee3342019-08-27 12:03:00 -0700843 switch depTag {
844 case dylibDepTag:
845 dylib, ok := rustDep.compiler.(libraryInterface)
846 if !ok || !dylib.dylib() {
847 ctx.ModuleErrorf("mod %q not an dylib library", depName)
848 return
849 }
850 directDylibDeps = append(directDylibDeps, rustDep)
851 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, depName)
852 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400853
Ivan Lozanoffee3342019-08-27 12:03:00 -0700854 rlib, ok := rustDep.compiler.(libraryInterface)
855 if !ok || !rlib.rlib() {
Ivan Lozano2b081132020-09-08 12:46:52 -0400856 ctx.ModuleErrorf("mod %q not an rlib library", depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700857 return
858 }
859 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozano2b081132020-09-08 12:46:52 -0400860 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, depName+rustDep.Properties.SubName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700861 case procMacroDepTag:
862 directProcMacroDeps = append(directProcMacroDeps, rustDep)
863 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, depName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400864 case android.SourceDepTag:
865 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
866 // OS/Arch variant is used.
867 var helper string
868 if ctx.Host() {
869 helper = "missing 'host_supported'?"
870 } else {
871 helper = "device module defined?"
872 }
873
874 if dep.Target().Os != ctx.Os() {
875 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
876 return
877 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
878 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
879 return
880 }
881 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 }
883
Ivan Lozano2bbcacf2020-08-07 09:00:50 -0400884 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -0700885 if depTag != procMacroDepTag {
886 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
887 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
888 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
889 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 }
891
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900893 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400894 if !linkFile.Valid() {
895 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
896 depName, ctx.ModuleName())
897 return
898 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -0700900 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
901 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700902 }
903 }
904
Ivan Lozano89435d12020-07-31 11:01:18 -0400905 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -0700906 //Handle C dependencies
907 if _, ok := ccDep.(*Module); !ok {
908 if ccDep.Module().Target().Os != ctx.Os() {
909 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
910 return
911 }
912 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
913 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
914 return
915 }
Ivan Lozano70e0a072019-09-13 14:23:15 -0700916 }
Ivan Lozano2093af22020-08-25 12:48:19 -0400917 linkObject := ccDep.OutputFile()
918 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -0500919
Ivan Lozano2093af22020-08-25 12:48:19 -0400920 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
922 }
923
924 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -0700925 switch {
926 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -0400927 if cc.IsWholeStaticLib(depTag) {
928 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
929 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500930 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
931 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400932 } else {
933 ctx.ModuleErrorf("'%q' cannot be listed as a whole_static_library in Rust modules unless the output is prefixed by 'lib'", depName, ctx.ModuleName())
Ivan Lozanofb6f36f2021-02-05 12:27:08 -0500934 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500935 }
936
937 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
938 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -0400939 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500940 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
941
Colin Cross0de8a1e2020-09-18 14:15:30 -0700942 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
943 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
944 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
945 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
946 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700947 directStaticLibDeps = append(directStaticLibDeps, ccDep)
948 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, depName)
Colin Cross6e511a92020-07-27 21:26:48 -0700949 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -0700950 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400951 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -0700952 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
953 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
954 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
955 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
956 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700957 directSharedLibDeps = append(directSharedLibDeps, ccDep)
958 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, depName)
959 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -0800960 case cc.IsHeaderDepTag(depTag):
961 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
962 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
963 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
964 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -0700965 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400966 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -0700967 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -0400968 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -0700969 }
970
971 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -0700972 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
973 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -0400974 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700975 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700976 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400977
978 if srcDep, ok := dep.(android.SourceFileProducer); ok {
979 switch depTag {
980 case android.SourceDepTag:
981 // These are usually genrules which don't have per-target variants.
982 directSrcDeps = append(directSrcDeps, srcDep)
983 }
984 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700985 })
986
987 var rlibDepFiles RustLibraries
988 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900989 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990 }
991 var dylibDepFiles RustLibraries
992 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900993 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994 }
995 var procMacroDepFiles RustLibraries
996 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900997 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998 }
999
1000 var staticLibDepFiles android.Paths
1001 for _, dep := range directStaticLibDeps {
1002 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1003 }
1004
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001005 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001006 var sharedLibDepFiles android.Paths
1007 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001008 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1009 if dep.Toc().Valid() {
1010 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1011 } else {
1012 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1013 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014 }
1015
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001016 var srcProviderDepFiles android.Paths
1017 for _, dep := range directSrcProvidersDeps {
1018 srcs, _ := dep.OutputFiles("")
1019 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1020 }
1021 for _, dep := range directSrcDeps {
1022 srcs := dep.Srcs()
1023 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1024 }
1025
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1027 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1028 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001029 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1031 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001032 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033
1034 // Dedup exported flags from dependencies
1035 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001036 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001037 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001038 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1039 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1040 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001041
1042 return depPaths
1043}
1044
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001045func (mod *Module) InstallInData() bool {
1046 if mod.compiler == nil {
1047 return false
1048 }
1049 return mod.compiler.inData()
1050}
1051
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052func linkPathFromFilePath(filepath android.Path) string {
1053 return strings.Split(filepath.String(), filepath.Base())[0]
1054}
Ivan Lozanod648c432020-02-06 12:05:10 -05001055
Ivan Lozanoffee3342019-08-27 12:03:00 -07001056func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1057 ctx := &depsContext{
1058 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001059 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001060
1061 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001062 var commonDepVariations []blueprint.Variation
Ivan Lozanodd055472020-09-28 13:22:45 -04001063
Ivan Lozano2b081132020-09-08 12:46:52 -04001064 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001065 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001066 stdLinkage = "rlib-std"
1067 }
1068
1069 rlibDepVariations := commonDepVariations
1070 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1071 rlibDepVariations = append(rlibDepVariations,
1072 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1073 }
1074
Ivan Lozano52767be2019-10-18 14:49:46 -07001075 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001076 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001077 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001078 rlibDepTag, deps.Rlibs...)
1079 actx.AddVariationDependencies(
1080 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001081 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001082 dylibDepTag, deps.Dylibs...)
1083
Ivan Lozano042504f2020-08-18 14:31:23 -04001084 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1085 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001086 if autoDep.depTag == rlibDepTag {
1087 actx.AddVariationDependencies(
1088 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1089 autoDep.depTag, deps.Rustlibs...)
1090 } else {
1091 actx.AddVariationDependencies(
1092 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1093 autoDep.depTag, deps.Rustlibs...)
1094 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001095 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001096 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001097 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001098 actx.AddVariationDependencies(
1099 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "rlib"}),
1100 rlibDepTag, deps.Stdlibs...)
1101 } else {
1102 actx.AddVariationDependencies(
1103 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1104 dylibDepTag, deps.Stdlibs...)
1105 }
1106 }
Ivan Lozano52767be2019-10-18 14:49:46 -07001107 actx.AddVariationDependencies(append(commonDepVariations,
1108 blueprint.Variation{Mutator: "link", Variation: "shared"}),
Colin Cross6e511a92020-07-27 21:26:48 -07001109 cc.SharedDepTag(), deps.SharedLibs...)
Ivan Lozano52767be2019-10-18 14:49:46 -07001110 actx.AddVariationDependencies(append(commonDepVariations,
1111 blueprint.Variation{Mutator: "link", Variation: "static"}),
Ivan Lozano63bb7682021-03-23 15:53:44 -04001112 cc.StaticDepTag(false), deps.StaticLibs...)
1113 actx.AddVariationDependencies(append(commonDepVariations,
1114 blueprint.Variation{Mutator: "link", Variation: "static"}),
1115 cc.StaticDepTag(true), deps.WholeStaticLibs...)
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001116
Zach Johnson3df4e632020-11-06 11:56:27 -08001117 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1118
Colin Cross565cafd2020-09-25 18:47:38 -07001119 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001120 if deps.CrtBegin != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001121 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag, deps.CrtBegin)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001122 }
1123 if deps.CrtEnd != "" {
Dan Albert92fe7402020-07-15 13:33:30 -07001124 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag, deps.CrtEnd)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001125 }
1126
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001127 if mod.sourceProvider != nil {
1128 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1129 bindgen.Properties.Custom_bindgen != "" {
1130 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1131 bindgen.Properties.Custom_bindgen)
1132 }
1133 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001134 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001135 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136}
1137
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001138func BeginMutator(ctx android.BottomUpMutatorContext) {
1139 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1140 mod.beginMutator(ctx)
1141 }
1142}
1143
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001144func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1145 ctx := &baseModuleContext{
1146 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001147 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001148
1149 mod.begin(ctx)
1150}
1151
Ivan Lozanoffee3342019-08-27 12:03:00 -07001152func (mod *Module) Name() string {
1153 name := mod.ModuleBase.Name()
1154 if p, ok := mod.compiler.(interface {
1155 Name(string) string
1156 }); ok {
1157 name = p.Name(name)
1158 }
1159 return name
1160}
1161
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001162func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001163 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001164 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001165 }
1166}
1167
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001168var _ android.HostToolProvider = (*Module)(nil)
1169
1170func (mod *Module) HostToolPath() android.OptionalPath {
1171 if !mod.Host() {
1172 return android.OptionalPath{}
1173 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001174 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1175 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001176 }
1177 return android.OptionalPath{}
1178}
1179
Jiyong Park99644e92020-11-17 22:21:02 +09001180var _ android.ApexModule = (*Module)(nil)
1181
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001182func (mod *Module) minSdkVersion() string {
1183 return String(mod.Properties.Min_sdk_version)
1184}
1185
Jiyong Park45bf82e2020-12-15 22:29:02 +09001186var _ android.ApexModule = (*Module)(nil)
1187
1188// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001189func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001190 minSdkVersion := mod.minSdkVersion()
1191 if minSdkVersion == "apex_inherit" {
1192 return nil
1193 }
1194 if minSdkVersion == "" {
1195 return fmt.Errorf("min_sdk_version is not specificed")
1196 }
1197
1198 // Not using nativeApiLevelFromUser because the context here is not
1199 // necessarily a native context.
1200 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1201 if err != nil {
1202 return err
1203 }
1204
1205 if ver.GreaterThan(sdkVersion) {
1206 return fmt.Errorf("newer SDK(%v)", ver)
1207 }
Jiyong Park99644e92020-11-17 22:21:02 +09001208 return nil
1209}
1210
Jiyong Park45bf82e2020-12-15 22:29:02 +09001211// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001212func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1213 depTag := ctx.OtherModuleDependencyTag(dep)
1214
1215 if ccm, ok := dep.(*cc.Module); ok {
1216 if ccm.HasStubsVariants() {
1217 if cc.IsSharedDepTag(depTag) {
1218 // dynamic dep to a stubs lib crosses APEX boundary
1219 return false
1220 }
1221 if cc.IsRuntimeDepTag(depTag) {
1222 // runtime dep to a stubs lib also crosses APEX boundary
1223 return false
1224 }
1225
1226 if cc.IsHeaderDepTag(depTag) {
1227 return false
1228 }
1229 }
1230 if mod.Static() && cc.IsSharedDepTag(depTag) {
1231 // shared_lib dependency from a static lib is considered as crossing
1232 // the APEX boundary because the dependency doesn't actually is
1233 // linked; the dependency is used only during the compilation phase.
1234 return false
1235 }
1236 }
1237
1238 if depTag == procMacroDepTag {
1239 return false
1240 }
1241
1242 return true
1243}
1244
1245// Overrides ApexModule.IsInstallabeToApex()
1246func (mod *Module) IsInstallableToApex() bool {
1247 if mod.compiler != nil {
1248 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1249 return true
1250 }
1251 if _, ok := mod.compiler.(*binaryDecorator); ok {
1252 return true
1253 }
1254 }
1255 return false
1256}
1257
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001258// If a library file has a "lib" prefix, extract the library name without the prefix.
1259func libNameFromFilePath(filepath android.Path) (string, bool) {
1260 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1261 if strings.HasPrefix(libName, "lib") {
1262 libName = libName[3:]
1263 return libName, true
1264 }
1265 return "", false
1266}
1267
Ivan Lozanoffee3342019-08-27 12:03:00 -07001268var Bool = proptools.Bool
1269var BoolDefault = proptools.BoolDefault
1270var String = proptools.String
1271var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001272
1273var _ android.OutputFileProducer = (*Module)(nil)