blob: 7a8687c8b7403fe11e213d3637334788e198e2d6 [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"
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +020025 "android/soong/bloaty"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "android/soong/cc"
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +020027 cc_config "android/soong/cc/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070028 "android/soong/rust/config"
29)
30
31var pctx = android.NewPackageContext("android/soong/rust")
32
33func init() {
34 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070035
36 android.AddNeverAllowRules(
37 android.NeverAllow().
Ivan Lozanoe169ad72019-09-18 08:42:54 -070038 NotIn(config.RustAllowedPaths...).
39 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070040
41 android.RegisterModuleType("rust_defaults", defaultsFactory)
42 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
43 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040044 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040045 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050046
47 })
48 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
49 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070050 })
51 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020052 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070053}
54
55type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040056 GlobalRustFlags []string // Flags that apply globally to rust
57 GlobalLinkFlags []string // Flags that apply globally to linker
58 RustFlags []string // Flags that apply to rust
59 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070061 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070062 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020064 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070065}
66
67type BaseProperties struct {
68 AndroidMkRlibs []string
69 AndroidMkDylibs []string
70 AndroidMkProcMacroLibs []string
71 AndroidMkSharedLibs []string
72 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040073
Ivan Lozano6a884432020-12-02 09:15:16 -050074 ImageVariationPrefix string `blueprint:"mutated"`
75 VndkVersion string `blueprint:"mutated"`
76 SubName string `blueprint:"mutated"`
77
Ivan Lozanoc08897c2021-04-02 12:41:32 -040078 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
79 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
80 // appended before SubName.
81 RustSubName string `blueprint:"mutated"`
82
Ivan Lozano6a884432020-12-02 09:15:16 -050083 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050084 CoreVariantNeeded bool `blueprint:"mutated"`
85 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
86 ExtraVariants []string `blueprint:"mutated"`
87
Ivan Lozano1921e802021-05-20 13:39:16 -040088 // Used by vendor snapshot to record dependencies from snapshot modules.
89 SnapshotSharedLibs []string `blueprint:"mutated"`
90
Ivan Lozanoe6d30982021-02-05 10:57:43 -050091 // Make this module available when building for vendor ramdisk.
92 // On device without a dedicated recovery partition, the module is only
93 // available after switching root into
94 // /first_stage_ramdisk. To expose the module before switching root, install
95 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
96 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040097
Ivan Lozano1921e802021-05-20 13:39:16 -040098 // Normally Soong uses the directory structure to decide which modules
99 // should be included (framework) or excluded (non-framework) from the
100 // different snapshots (vendor, recovery, etc.), but this property
101 // allows a partner to exclude a module normally thought of as a
102 // framework module from the vendor snapshot.
103 Exclude_from_vendor_snapshot *bool
104
105 // Normally Soong uses the directory structure to decide which modules
106 // should be included (framework) or excluded (non-framework) from the
107 // different snapshots (vendor, recovery, etc.), but this property
108 // allows a partner to exclude a module normally thought of as a
109 // framework module from the recovery snapshot.
110 Exclude_from_recovery_snapshot *bool
111
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500112 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
113 Min_sdk_version *string
114
Ivan Lozano43845682020-07-09 21:03:28 -0400115 PreventInstall bool
116 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400117 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118}
119
120type Module struct {
121 android.ModuleBase
122 android.DefaultableModuleBase
Jiyong Park99644e92020-11-17 22:21:02 +0900123 android.ApexModuleBase
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124
Ivan Lozano6a884432020-12-02 09:15:16 -0500125 VendorProperties cc.VendorProperties
126
Ivan Lozanoffee3342019-08-27 12:03:00 -0700127 Properties BaseProperties
128
129 hod android.HostOrDeviceSupported
130 multilib android.Multilib
131
Ivan Lozano6a884432020-12-02 09:15:16 -0500132 makeLinkType string
133
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400135 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200136 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500137 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400139 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700140 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400141
Jiyong Parke54f07e2021-04-07 15:08:04 +0900142 // Unstripped output. This is usually used when this module is linked to another module
143 // as a library. The stripped output which is used for installation can be found via
144 // compiler.strippedOutputFile if it exists.
145 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700146 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900147
148 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149}
150
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500151func (mod *Module) Header() bool {
152 //TODO: If Rust libraries provide header variants, this needs to be updated.
153 return false
154}
155
156func (mod *Module) SetPreventInstall() {
157 mod.Properties.PreventInstall = true
158}
159
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500160func (mod *Module) SetHideFromMake() {
161 mod.Properties.HideFromMake = true
162}
163
Ivan Lozanod7586b62021-04-01 09:49:36 -0400164func (c *Module) HiddenFromMake() bool {
165 return c.Properties.HideFromMake
166}
167
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500168func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500169 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
170 // nil since we need compiler to actually sanitize.
171 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500172}
173
174func (mod *Module) IsDependencyRoot() bool {
175 if mod.compiler != nil {
176 return mod.compiler.isDependencyRoot()
177 }
178 panic("IsDependencyRoot called on a non-compiler Rust module")
179}
180
181func (mod *Module) IsPrebuilt() bool {
182 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
183 return true
184 }
185 return false
186}
187
Ivan Lozano43845682020-07-09 21:03:28 -0400188func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
189 switch tag {
190 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700191 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400192 return mod.sourceProvider.Srcs(), nil
193 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900194 if mod.OutputFile().Valid() {
195 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400196 }
197 return android.Paths{}, nil
198 }
199 default:
200 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
201 }
202}
203
Ivan Lozano52767be2019-10-18 14:49:46 -0700204func (mod *Module) SelectedStl() string {
205 return ""
206}
207
Ivan Lozano2b262972019-11-21 12:30:50 -0800208func (mod *Module) NonCcVariants() bool {
209 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400210 if _, ok := mod.compiler.(libraryInterface); ok {
211 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800212 }
213 }
214 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
215}
216
Ivan Lozano52767be2019-10-18 14:49:46 -0700217func (mod *Module) Static() bool {
218 if mod.compiler != nil {
219 if library, ok := mod.compiler.(libraryInterface); ok {
220 return library.static()
221 }
222 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400223 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700224}
225
226func (mod *Module) Shared() bool {
227 if mod.compiler != nil {
228 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400229 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700230 }
231 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400232 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700233}
234
Ivan Lozanod7586b62021-04-01 09:49:36 -0400235func (mod *Module) Dylib() bool {
236 if mod.compiler != nil {
237 if library, ok := mod.compiler.(libraryInterface); ok {
238 return library.dylib()
239 }
240 }
241 return false
242}
243
244func (mod *Module) Rlib() bool {
245 if mod.compiler != nil {
246 if library, ok := mod.compiler.(libraryInterface); ok {
247 return library.rlib()
248 }
249 }
250 return false
251}
252
253func (mod *Module) Binary() bool {
254 if mod.compiler != nil {
255 if _, ok := mod.compiler.(*binaryDecorator); ok {
256 return true
257 }
258 }
259 return false
260}
261
262func (mod *Module) Object() bool {
263 // Rust has no modules which produce only object files.
264 return false
265}
266
Ivan Lozano52767be2019-10-18 14:49:46 -0700267func (mod *Module) Toc() android.OptionalPath {
268 if mod.compiler != nil {
269 if _, ok := mod.compiler.(libraryInterface); ok {
270 return android.OptionalPath{}
271 }
272 }
273 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
274}
275
Colin Crossc511bc52020-04-07 16:50:32 +0000276func (mod *Module) UseSdk() bool {
277 return false
278}
279
Ivan Lozanod7586b62021-04-01 09:49:36 -0400280func (mod *Module) RelativeInstallPath() string {
281 if mod.compiler != nil {
282 return mod.compiler.relativeInstallPath()
283 }
284 return ""
285}
286
Ivan Lozano52767be2019-10-18 14:49:46 -0700287func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500288 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700289}
290
291func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400292 return true
293}
294
295func (mod *Module) SubName() string {
296 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700297}
298
299func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500300 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700301 return false
302}
303
Ivan Lozanof9e21722020-12-02 09:00:51 -0500304func (mod *Module) IsVndkExt() bool {
305 return false
306}
307
Ivan Lozanod7586b62021-04-01 09:49:36 -0400308func (mod *Module) IsVndkSp() bool {
309 return false
310}
311
Colin Cross127bb8b2020-12-16 16:46:01 -0800312func (c *Module) IsVndkPrivate() bool {
313 return false
314}
315
316func (c *Module) IsLlndk() bool {
317 return false
318}
319
320func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500321 return false
322}
323
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400324func (mod *Module) KernelHeadersDecorator() bool {
325 return false
326}
327
Colin Cross1f3f1302021-04-26 18:37:44 -0700328func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400329 return false
330}
331
Colin Cross5271fea2021-04-27 13:06:04 -0700332func (m *Module) NeedsVendorPublicLibraryVariants() bool {
333 return false
334}
335
Ivan Lozanod7586b62021-04-01 09:49:36 -0400336func (mod *Module) HasLlndkStubs() bool {
337 return false
338}
339
340func (mod *Module) StubsVersion() string {
341 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
342}
343
Ivan Lozano52767be2019-10-18 14:49:46 -0700344func (mod *Module) SdkVersion() string {
345 return ""
346}
347
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900348func (mod *Module) MinSdkVersion() string {
349 return ""
350}
351
Colin Crossc511bc52020-04-07 16:50:32 +0000352func (mod *Module) AlwaysSdk() bool {
353 return false
354}
355
Jiyong Park2286afd2020-06-16 21:58:53 +0900356func (mod *Module) IsSdkVariant() bool {
357 return false
358}
359
Colin Cross1348ce32020-10-01 13:37:16 -0700360func (mod *Module) SplitPerApiLevel() bool {
361 return false
362}
363
Ivan Lozanoffee3342019-08-27 12:03:00 -0700364type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400365 Dylibs []string
366 Rlibs []string
367 Rustlibs []string
368 Stdlibs []string
369 ProcMacros []string
370 SharedLibs []string
371 StaticLibs []string
372 WholeStaticLibs []string
373 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700374
375 CrtBegin, CrtEnd string
376}
377
378type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500379 DyLibs RustLibraries
380 RLibs RustLibraries
381 SharedLibs android.Paths
382 SharedLibDeps android.Paths
383 StaticLibs android.Paths
384 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500385
386 // depFlags and depLinkFlags are rustc and linker (clang) flags.
387 depFlags []string
388 depLinkFlags []string
389
390 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
391 // Both of these are exported and propagate to dependencies.
392 linkDirs []string
393 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700394
Ivan Lozano45901ed2020-07-24 16:05:01 -0400395 // Used by bindgen modules which call clang
396 depClangFlags []string
397 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400398 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400399 depSystemIncludePaths android.Paths
400
Ivan Lozanof1c84332019-09-20 11:00:37 -0700401 CrtBegin android.OptionalPath
402 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700403
404 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500405 SrcDeps android.Paths
406 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407}
408
409type RustLibraries []RustLibrary
410
411type RustLibrary struct {
412 Path android.Path
413 CrateName string
414}
415
416type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100417 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418 compilerFlags(ctx ModuleContext, flags Flags) Flags
419 compilerProps() []interface{}
420 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
421 compilerDeps(ctx DepsContext, deps Deps) Deps
422 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700423 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700424
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100425 // Output directory in which source-generated code from dependencies is
426 // copied. This is equivalent to Cargo's OUT_DIR variable.
427 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700428
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800429 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200430 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400432 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400433
434 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400435
436 Disabled() bool
437 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400438
Ivan Lozanodd055472020-09-28 13:22:45 -0400439 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500440 isDependencyRoot() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900441
442 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400443}
444
Matthew Maurerbb3add12020-06-25 09:34:12 -0700445type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700446 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400447 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700448}
449
450type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400451 linkDirs []string
452 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700453}
454
Matthew Maurerbb3add12020-06-25 09:34:12 -0700455func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
456 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
457}
458
Ivan Lozano2093af22020-08-25 12:48:19 -0400459func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
460 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
461}
462
Colin Cross0de8a1e2020-09-18 14:15:30 -0700463func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
464 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700465 LinkDirs: flagExporter.linkDirs,
466 LinkObjects: flagExporter.linkObjects,
467 })
468}
469
Matthew Maurerbb3add12020-06-25 09:34:12 -0700470var _ exportedFlagsProducer = (*flagExporter)(nil)
471
472func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700473 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700474}
475
Colin Cross0de8a1e2020-09-18 14:15:30 -0700476type FlagExporterInfo struct {
477 Flags []string
478 LinkDirs []string // TODO: this should be android.Paths
479 LinkObjects []string // TODO: this should be android.Paths
480}
481
482var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
483
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400484func (mod *Module) isCoverageVariant() bool {
485 return mod.coverage.Properties.IsCoverageVariant
486}
487
488var _ cc.Coverage = (*Module)(nil)
489
490func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
491 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
492}
493
Ivan Lozanod7586b62021-04-01 09:49:36 -0400494func (mod *Module) VndkVersion() string {
495 return mod.Properties.VndkVersion
496}
497
498func (mod *Module) PreventInstall() bool {
499 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400500}
501
502func (mod *Module) HideFromMake() {
503 mod.Properties.HideFromMake = true
504}
505
506func (mod *Module) MarkAsCoverageVariant(coverage bool) {
507 mod.coverage.Properties.IsCoverageVariant = coverage
508}
509
510func (mod *Module) EnableCoverageIfNeeded() {
511 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700512}
513
514func defaultsFactory() android.Module {
515 return DefaultsFactory()
516}
517
518type Defaults struct {
519 android.ModuleBase
520 android.DefaultsModuleBase
521}
522
523func DefaultsFactory(props ...interface{}) android.Module {
524 module := &Defaults{}
525
526 module.AddProperties(props...)
527 module.AddProperties(
528 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500529 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100530 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400531 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700532 &BaseCompilerProperties{},
533 &BinaryCompilerProperties{},
534 &LibraryCompilerProperties{},
535 &ProcMacroCompilerProperties{},
536 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400537 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700538 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400539 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400540 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200541 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500542 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700543 )
544
545 android.InitDefaultsModule(module)
546 return module
547}
548
549func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700550 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551}
552
Ivan Lozano183a3212019-10-18 14:18:45 -0700553func (mod *Module) CcLibrary() bool {
554 if mod.compiler != nil {
555 if _, ok := mod.compiler.(*libraryDecorator); ok {
556 return true
557 }
558 }
559 return false
560}
561
562func (mod *Module) CcLibraryInterface() bool {
563 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400564 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
565 // VariantIs{Static,Shared} is set.
566 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700567 return true
568 }
569 }
570 return false
571}
572
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800573func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700574 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700575 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800576 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700577 }
578 }
579 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
580}
581
582func (mod *Module) SetStatic() {
583 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700584 if library, ok := mod.compiler.(libraryInterface); ok {
585 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700586 return
587 }
588 }
589 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
590}
591
592func (mod *Module) SetShared() {
593 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700594 if library, ok := mod.compiler.(libraryInterface); ok {
595 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700596 return
597 }
598 }
599 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
600}
601
Ivan Lozano183a3212019-10-18 14:18:45 -0700602func (mod *Module) BuildStaticVariant() bool {
603 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700604 if library, ok := mod.compiler.(libraryInterface); ok {
605 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700606 }
607 }
608 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
609}
610
611func (mod *Module) BuildSharedVariant() bool {
612 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700613 if library, ok := mod.compiler.(libraryInterface); ok {
614 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700615 }
616 }
617 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
618}
619
Ivan Lozano183a3212019-10-18 14:18:45 -0700620func (mod *Module) Module() android.Module {
621 return mod
622}
623
Ivan Lozano183a3212019-10-18 14:18:45 -0700624func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900625 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
626 return mod.compiler.strippedOutputFilePath()
627 }
628 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700629}
630
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400631func (mod *Module) CoverageFiles() android.Paths {
632 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800633 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400634 }
635 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
636}
637
Jiyong Park459feca2020-12-15 11:02:21 +0900638func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900639 if !mod.EverInstallable() {
640 return false
641 }
642
Jiyong Park459feca2020-12-15 11:02:21 +0900643 // The apex variant is not installable because it is included in the APEX and won't appear
644 // in the system partition as a standalone file.
645 if !apexInfo.IsForPlatform() {
646 return false
647 }
648
Jiyong Parke54f07e2021-04-07 15:08:04 +0900649 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900650}
651
Ivan Lozano183a3212019-10-18 14:18:45 -0700652var _ cc.LinkableInterface = (*Module)(nil)
653
Ivan Lozanoffee3342019-08-27 12:03:00 -0700654func (mod *Module) Init() android.Module {
655 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500656 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700657
658 if mod.compiler != nil {
659 mod.AddProperties(mod.compiler.compilerProps()...)
660 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400661 if mod.coverage != nil {
662 mod.AddProperties(mod.coverage.props()...)
663 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200664 if mod.clippy != nil {
665 mod.AddProperties(mod.clippy.props()...)
666 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400667 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700668 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400669 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500670 if mod.sanitize != nil {
671 mod.AddProperties(mod.sanitize.props()...)
672 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400673
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900675 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700676
677 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700678 return mod
679}
680
681func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
682 return &Module{
683 hod: hod,
684 multilib: multilib,
685 }
686}
687func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
688 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400689 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200690 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500691 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700692 return module
693}
694
695type ModuleContext interface {
696 android.ModuleContext
697 ModuleContextIntf
698}
699
700type BaseModuleContext interface {
701 android.BaseModuleContext
702 ModuleContextIntf
703}
704
705type DepsContext interface {
706 android.BottomUpMutatorContext
707 ModuleContextIntf
708}
709
710type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200711 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700712 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713}
714
715type depsContext struct {
716 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700717}
718
719type moduleContext struct {
720 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700721}
722
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200723type baseModuleContext struct {
724 android.BaseModuleContext
725}
726
727func (ctx *moduleContext) RustModule() *Module {
728 return ctx.Module().(*Module)
729}
730
731func (ctx *moduleContext) toolchain() config.Toolchain {
732 return ctx.RustModule().toolchain(ctx)
733}
734
735func (ctx *depsContext) RustModule() *Module {
736 return ctx.Module().(*Module)
737}
738
739func (ctx *depsContext) toolchain() config.Toolchain {
740 return ctx.RustModule().toolchain(ctx)
741}
742
743func (ctx *baseModuleContext) RustModule() *Module {
744 return ctx.Module().(*Module)
745}
746
747func (ctx *baseModuleContext) toolchain() config.Toolchain {
748 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400749}
750
751func (mod *Module) nativeCoverage() bool {
752 return mod.compiler != nil && mod.compiler.nativeCoverage()
753}
754
Ivan Lozanod7586b62021-04-01 09:49:36 -0400755func (mod *Module) EverInstallable() bool {
756 return mod.compiler != nil &&
757 // Check to see whether the module is actually ever installable.
758 mod.compiler.everInstallable()
759}
760
761func (mod *Module) Installable() *bool {
762 return mod.Properties.Installable
763}
764
Ivan Lozanoffee3342019-08-27 12:03:00 -0700765func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
766 if mod.cachedToolchain == nil {
767 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
768 }
769 return mod.cachedToolchain
770}
771
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200772func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
773 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
774}
775
Ivan Lozanoffee3342019-08-27 12:03:00 -0700776func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
777}
778
779func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
780 ctx := &moduleContext{
781 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783
Jiyong Park99644e92020-11-17 22:21:02 +0900784 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
785 if !apexInfo.IsForPlatform() {
786 mod.hideApexVariantFromMake = true
787 }
788
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500790 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
791
792 // Differentiate static libraries that are vendor available
793 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500794 mod.Properties.SubName += cc.VendorSuffix
795 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
796 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500797 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700798
799 if !toolchain.Supported() {
800 // This toolchain's unsupported, there's nothing to do for this mod.
801 return
802 }
803
804 deps := mod.depsToPaths(ctx)
805 flags := Flags{
806 Toolchain: toolchain,
807 }
808
809 if mod.compiler != nil {
810 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400811 }
812 if mod.coverage != nil {
813 flags, deps = mod.coverage.flags(ctx, flags, deps)
814 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200815 if mod.clippy != nil {
816 flags, deps = mod.clippy.flags(ctx, flags, deps)
817 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500818 if mod.sanitize != nil {
819 flags, deps = mod.sanitize.flags(ctx, flags, deps)
820 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400821
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200822 // SourceProvider needs to call GenerateSource() before compiler calls
823 // compile() so it can provide the source. A SourceProvider has
824 // multiple variants (e.g. source, rlib, dylib). Only the "source"
825 // variant is responsible for effectively generating the source. The
826 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400827 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200828 if mod.compiler.(libraryInterface).source() {
829 mod.sourceProvider.GenerateSource(ctx, deps)
830 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
831 } else {
832 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
833 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700834 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200835 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400836 }
837
838 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100839 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900840 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900841 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200842 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900843
Dan Albert06feee92021-03-19 15:06:02 -0700844 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
845
Ivan Lozano1921e802021-05-20 13:39:16 -0400846 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
847 // RECOVERY_SNAPSHOT_VERSION is current.
848 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
849 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
850 lib.collectHeadersForSnapshot(ctx, deps)
851 }
852 }
853
Jiyong Park459feca2020-12-15 11:02:21 +0900854 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
855 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200856 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400857 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700858 }
859}
860
861func (mod *Module) deps(ctx DepsContext) Deps {
862 deps := Deps{}
863
864 if mod.compiler != nil {
865 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400866 }
867 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700868 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700869 }
870
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400871 if mod.coverage != nil {
872 deps = mod.coverage.deps(ctx, deps)
873 }
874
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500875 if mod.sanitize != nil {
876 deps = mod.sanitize.deps(ctx, deps)
877 }
878
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
880 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700881 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700882 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
883 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
884 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400885 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700886 return deps
887
888}
889
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890type dependencyTag struct {
891 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800892 name string
893 library bool
894 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700895}
896
Jiyong Park65b62242020-11-25 12:44:59 +0900897// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
898// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
899func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800900 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900901}
902
903var _ android.InstallNeededDependencyTag = dependencyTag{}
904
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400906 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
907 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
908 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800909 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400910 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200911 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912)
913
Jiyong Park99644e92020-11-17 22:21:02 +0900914func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
915 tag, ok := depTag.(dependencyTag)
916 return ok && tag == dylibDepTag
917}
918
Jiyong Park94e22fd2021-04-08 18:19:15 +0900919func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
920 tag, ok := depTag.(dependencyTag)
921 return ok && tag == rlibDepTag
922}
923
Matthew Maurer0f003b12020-06-29 14:34:06 -0700924type autoDep struct {
925 variation string
926 depTag dependencyTag
927}
928
929var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200930 rlibVariation = "rlib"
931 dylibVariation = "dylib"
932 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
933 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700934)
935
936type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500937 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700938}
939
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400940func (mod *Module) begin(ctx BaseModuleContext) {
941 if mod.coverage != nil {
942 mod.coverage.begin(ctx)
943 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500944 if mod.sanitize != nil {
945 mod.sanitize.begin(ctx)
946 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400947}
948
Ivan Lozanoffee3342019-08-27 12:03:00 -0700949func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
950 var depPaths PathDeps
951
952 directRlibDeps := []*Module{}
953 directDylibDeps := []*Module{}
954 directProcMacroDeps := []*Module{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700955 directSharedLibDeps := [](cc.LinkableInterface){}
956 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400957 directSrcProvidersDeps := []*Module{}
958 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700959
960 ctx.VisitDirectDeps(func(dep android.Module) {
961 depName := ctx.OtherModuleName(dep)
962 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400963
Ivan Lozano89435d12020-07-31 11:01:18 -0400964 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700965 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400966 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700967
Ivan Lozanoffee3342019-08-27 12:03:00 -0700968 switch depTag {
969 case dylibDepTag:
970 dylib, ok := rustDep.compiler.(libraryInterface)
971 if !ok || !dylib.dylib() {
972 ctx.ModuleErrorf("mod %q not an dylib library", depName)
973 return
974 }
975 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400976 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700977 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400978
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979 rlib, ok := rustDep.compiler.(libraryInterface)
980 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400981 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982 return
983 }
984 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400985 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986 case procMacroDepTag:
987 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400988 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400989 case android.SourceDepTag:
990 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
991 // OS/Arch variant is used.
992 var helper string
993 if ctx.Host() {
994 helper = "missing 'host_supported'?"
995 } else {
996 helper = "device module defined?"
997 }
998
999 if dep.Target().Os != ctx.Os() {
1000 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1001 return
1002 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1003 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1004 return
1005 }
1006 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001007 }
1008
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001009 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001010 if depTag != procMacroDepTag {
1011 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1012 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1013 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1014 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015 }
1016
Ivan Lozanoffee3342019-08-27 12:03:00 -07001017 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001018 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001019 if !linkFile.Valid() {
1020 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1021 depName, ctx.ModuleName())
1022 return
1023 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001025 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1026 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027 }
1028 }
1029
Ivan Lozano89435d12020-07-31 11:01:18 -04001030 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001031 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001032 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001033 if _, ok := ccDep.(*Module); !ok {
1034 if ccDep.Module().Target().Os != ctx.Os() {
1035 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1036 return
1037 }
1038 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1039 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1040 return
1041 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001042 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001043 linkObject := ccDep.OutputFile()
1044 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001045
Ivan Lozano2093af22020-08-25 12:48:19 -04001046 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1048 }
1049
1050 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001051 switch {
1052 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001053 if cc.IsWholeStaticLib(depTag) {
1054 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1055 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001056 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1057 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001058 } else {
1059 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 -05001060 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001061 }
1062
1063 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1064 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001065 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001066 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1067
Colin Cross0de8a1e2020-09-18 14:15:30 -07001068 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1069 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1070 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1071 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1072 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001073 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001074 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001075 case cc.IsSharedDepTag(depTag):
Ivan Lozanoffee3342019-08-27 12:03:00 -07001076 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001077 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001078 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1079 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1080 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1081 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1082 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001083 directSharedLibDeps = append(directSharedLibDeps, ccDep)
Ivan Lozano1921e802021-05-20 13:39:16 -04001084
1085 // Record baseLibName for snapshots.
1086 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1087
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001088 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001090 case cc.IsHeaderDepTag(depTag):
1091 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1092 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1093 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1094 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001095 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001096 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001097 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001098 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001099 }
1100
1101 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001102 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1103 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001104 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001105 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001106 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001107
1108 if srcDep, ok := dep.(android.SourceFileProducer); ok {
1109 switch depTag {
1110 case android.SourceDepTag:
1111 // These are usually genrules which don't have per-target variants.
1112 directSrcDeps = append(directSrcDeps, srcDep)
1113 }
1114 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001115 })
1116
1117 var rlibDepFiles RustLibraries
1118 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001119 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001120 }
1121 var dylibDepFiles RustLibraries
1122 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001123 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001124 }
1125 var procMacroDepFiles RustLibraries
1126 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001127 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001128 }
1129
1130 var staticLibDepFiles android.Paths
1131 for _, dep := range directStaticLibDeps {
1132 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1133 }
1134
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001135 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136 var sharedLibDepFiles android.Paths
1137 for _, dep := range directSharedLibDeps {
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001138 sharedLibFiles = append(sharedLibFiles, dep.OutputFile().Path())
1139 if dep.Toc().Valid() {
1140 sharedLibDepFiles = append(sharedLibDepFiles, dep.Toc().Path())
1141 } else {
1142 sharedLibDepFiles = append(sharedLibDepFiles, dep.OutputFile().Path())
1143 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001144 }
1145
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001146 var srcProviderDepFiles android.Paths
1147 for _, dep := range directSrcProvidersDeps {
1148 srcs, _ := dep.OutputFiles("")
1149 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1150 }
1151 for _, dep := range directSrcDeps {
1152 srcs := dep.Srcs()
1153 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1154 }
1155
Ivan Lozanoffee3342019-08-27 12:03:00 -07001156 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1157 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1158 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001159 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001160 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1161 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001162 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001163
1164 // Dedup exported flags from dependencies
1165 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001166 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001167 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001168 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1169 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1170 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001171
1172 return depPaths
1173}
1174
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001175func (mod *Module) InstallInData() bool {
1176 if mod.compiler == nil {
1177 return false
1178 }
1179 return mod.compiler.inData()
1180}
1181
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182func linkPathFromFilePath(filepath android.Path) string {
1183 return strings.Split(filepath.String(), filepath.Base())[0]
1184}
Ivan Lozanod648c432020-02-06 12:05:10 -05001185
Ivan Lozanoffee3342019-08-27 12:03:00 -07001186func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1187 ctx := &depsContext{
1188 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001189 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001190
1191 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001192 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001193 var snapshotInfo *cc.SnapshotInfo
1194
1195 if ctx.Os() == android.Android {
1196 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1197 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001198
Ivan Lozano2b081132020-09-08 12:46:52 -04001199 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001200 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001201 stdLinkage = "rlib-std"
1202 }
1203
1204 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001205
Ivan Lozano2b081132020-09-08 12:46:52 -04001206 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1207 rlibDepVariations = append(rlibDepVariations,
1208 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1209 }
1210
Ivan Lozano1921e802021-05-20 13:39:16 -04001211 // rlibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001212 actx.AddVariationDependencies(
Ivan Lozano2b081132020-09-08 12:46:52 -04001213 append(rlibDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001214 {Mutator: "rust_libraries", Variation: rlibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001215 rlibDepTag, deps.Rlibs...)
Ivan Lozano1921e802021-05-20 13:39:16 -04001216
1217 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001218 actx.AddVariationDependencies(
1219 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001220 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001221 dylibDepTag, deps.Dylibs...)
1222
Ivan Lozano1921e802021-05-20 13:39:16 -04001223 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001224 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1225 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001226 if autoDep.depTag == rlibDepTag {
1227 actx.AddVariationDependencies(
1228 append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1229 autoDep.depTag, deps.Rustlibs...)
1230 } else {
1231 actx.AddVariationDependencies(
1232 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1233 autoDep.depTag, deps.Rustlibs...)
1234 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001235 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001236
1237 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001238 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001239 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001240 actx.AddVariationDependencies(
Ivan Lozano1921e802021-05-20 13:39:16 -04001241 append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
Ivan Lozano2b081132020-09-08 12:46:52 -04001242 rlibDepTag, deps.Stdlibs...)
1243 } else {
1244 actx.AddVariationDependencies(
1245 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1246 dylibDepTag, deps.Stdlibs...)
1247 }
1248 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001249
1250 for _, lib := range deps.SharedLibs {
1251 depTag := cc.SharedDepTag()
1252 name, version := cc.StubsLibNameAndVersion(lib)
1253
1254 variations := []blueprint.Variation{
1255 {Mutator: "link", Variation: "shared"},
1256 }
1257 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1258 }
1259
1260 for _, lib := range deps.WholeStaticLibs {
1261 depTag := cc.StaticDepTag(true)
1262 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1263
1264 actx.AddVariationDependencies([]blueprint.Variation{
1265 {Mutator: "link", Variation: "static"},
1266 }, depTag, lib)
1267 }
1268
1269 for _, lib := range deps.StaticLibs {
1270 depTag := cc.StaticDepTag(false)
1271 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1272
1273 actx.AddVariationDependencies([]blueprint.Variation{
1274 {Mutator: "link", Variation: "static"},
1275 }, depTag, lib)
1276 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001277
Zach Johnson3df4e632020-11-06 11:56:27 -08001278 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1279
Colin Cross565cafd2020-09-25 18:47:38 -07001280 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001281 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001282 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1283 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001284 }
1285 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001286 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1287 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001288 }
1289
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001290 if mod.sourceProvider != nil {
1291 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1292 bindgen.Properties.Custom_bindgen != "" {
1293 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1294 bindgen.Properties.Custom_bindgen)
1295 }
1296 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001297
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001298 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001299 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001300}
1301
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001302func BeginMutator(ctx android.BottomUpMutatorContext) {
1303 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1304 mod.beginMutator(ctx)
1305 }
1306}
1307
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001308func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1309 ctx := &baseModuleContext{
1310 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001311 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001312
1313 mod.begin(ctx)
1314}
1315
Ivan Lozanoffee3342019-08-27 12:03:00 -07001316func (mod *Module) Name() string {
1317 name := mod.ModuleBase.Name()
1318 if p, ok := mod.compiler.(interface {
1319 Name(string) string
1320 }); ok {
1321 name = p.Name(name)
1322 }
1323 return name
1324}
1325
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001326func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001327 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001328 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001329 }
1330}
1331
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001332var _ android.HostToolProvider = (*Module)(nil)
1333
1334func (mod *Module) HostToolPath() android.OptionalPath {
1335 if !mod.Host() {
1336 return android.OptionalPath{}
1337 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001338 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1339 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001340 }
1341 return android.OptionalPath{}
1342}
1343
Jiyong Park99644e92020-11-17 22:21:02 +09001344var _ android.ApexModule = (*Module)(nil)
1345
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001346func (mod *Module) minSdkVersion() string {
1347 return String(mod.Properties.Min_sdk_version)
1348}
1349
Jiyong Park45bf82e2020-12-15 22:29:02 +09001350var _ android.ApexModule = (*Module)(nil)
1351
1352// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001353func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001354 minSdkVersion := mod.minSdkVersion()
1355 if minSdkVersion == "apex_inherit" {
1356 return nil
1357 }
1358 if minSdkVersion == "" {
1359 return fmt.Errorf("min_sdk_version is not specificed")
1360 }
1361
1362 // Not using nativeApiLevelFromUser because the context here is not
1363 // necessarily a native context.
1364 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1365 if err != nil {
1366 return err
1367 }
1368
1369 if ver.GreaterThan(sdkVersion) {
1370 return fmt.Errorf("newer SDK(%v)", ver)
1371 }
Jiyong Park99644e92020-11-17 22:21:02 +09001372 return nil
1373}
1374
Jiyong Park45bf82e2020-12-15 22:29:02 +09001375// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001376func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1377 depTag := ctx.OtherModuleDependencyTag(dep)
1378
1379 if ccm, ok := dep.(*cc.Module); ok {
1380 if ccm.HasStubsVariants() {
1381 if cc.IsSharedDepTag(depTag) {
1382 // dynamic dep to a stubs lib crosses APEX boundary
1383 return false
1384 }
1385 if cc.IsRuntimeDepTag(depTag) {
1386 // runtime dep to a stubs lib also crosses APEX boundary
1387 return false
1388 }
1389
1390 if cc.IsHeaderDepTag(depTag) {
1391 return false
1392 }
1393 }
1394 if mod.Static() && cc.IsSharedDepTag(depTag) {
1395 // shared_lib dependency from a static lib is considered as crossing
1396 // the APEX boundary because the dependency doesn't actually is
1397 // linked; the dependency is used only during the compilation phase.
1398 return false
1399 }
1400 }
1401
1402 if depTag == procMacroDepTag {
1403 return false
1404 }
1405
1406 return true
1407}
1408
1409// Overrides ApexModule.IsInstallabeToApex()
1410func (mod *Module) IsInstallableToApex() bool {
1411 if mod.compiler != nil {
1412 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1413 return true
1414 }
1415 if _, ok := mod.compiler.(*binaryDecorator); ok {
1416 return true
1417 }
1418 }
1419 return false
1420}
1421
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001422// If a library file has a "lib" prefix, extract the library name without the prefix.
1423func libNameFromFilePath(filepath android.Path) (string, bool) {
1424 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1425 if strings.HasPrefix(libName, "lib") {
1426 libName = libName[3:]
1427 return libName, true
1428 }
1429 return "", false
1430}
1431
Ivan Lozanoffee3342019-08-27 12:03:00 -07001432var Bool = proptools.Bool
1433var BoolDefault = proptools.BoolDefault
1434var String = proptools.String
1435var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001436
1437var _ android.OutputFileProducer = (*Module)(nil)