blob: 3ec550b1054a5c305342311faa3ffdbd4af07bf4 [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 Lozano03a94c42021-06-28 11:27:11 -040038 NotIn(append(config.RustAllowedPaths, config.DownstreamRustAllowedPaths...)...).
Ivan Lozanoe169ad72019-09-18 08:42:54 -070039 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 Lozanoa2268632021-07-22 10:52:06 -040088 // Allows this module to use non-APEX version of libraries. Useful
89 // for building binaries that are started before APEXes are activated.
90 Bootstrap *bool
91
Ivan Lozano1921e802021-05-20 13:39:16 -040092 // Used by vendor snapshot to record dependencies from snapshot modules.
93 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090094 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040095
Ivan Lozanoe6d30982021-02-05 10:57:43 -050096 // Make this module available when building for vendor ramdisk.
97 // On device without a dedicated recovery partition, the module is only
98 // available after switching root into
99 // /first_stage_ramdisk. To expose the module before switching root, install
100 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
101 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400102
Ivan Lozano1921e802021-05-20 13:39:16 -0400103 // Normally Soong uses the directory structure to decide which modules
104 // should be included (framework) or excluded (non-framework) from the
105 // different snapshots (vendor, recovery, etc.), but this property
106 // allows a partner to exclude a module normally thought of as a
107 // framework module from the vendor snapshot.
108 Exclude_from_vendor_snapshot *bool
109
110 // Normally Soong uses the directory structure to decide which modules
111 // should be included (framework) or excluded (non-framework) from the
112 // different snapshots (vendor, recovery, etc.), but this property
113 // allows a partner to exclude a module normally thought of as a
114 // framework module from the recovery snapshot.
115 Exclude_from_recovery_snapshot *bool
116
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500117 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
118 Min_sdk_version *string
119
Ivan Lozano43845682020-07-09 21:03:28 -0400120 PreventInstall bool
121 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400122 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123}
124
125type Module struct {
hamzeh41ad8812021-07-07 14:00:07 -0700126 cc.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700127
Ivan Lozano6a884432020-12-02 09:15:16 -0500128 VendorProperties cc.VendorProperties
129
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130 Properties BaseProperties
131
132 hod android.HostOrDeviceSupported
133 multilib android.Multilib
134
Ivan Lozano6a884432020-12-02 09:15:16 -0500135 makeLinkType string
136
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400138 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200139 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500140 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400142 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700143 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400144
Jiyong Parke54f07e2021-04-07 15:08:04 +0900145 // Unstripped output. This is usually used when this module is linked to another module
146 // as a library. The stripped output which is used for installation can be found via
147 // compiler.strippedOutputFile if it exists.
148 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700149 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900150
151 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152}
153
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500154func (mod *Module) Header() bool {
155 //TODO: If Rust libraries provide header variants, this needs to be updated.
156 return false
157}
158
159func (mod *Module) SetPreventInstall() {
160 mod.Properties.PreventInstall = true
161}
162
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500163func (mod *Module) SetHideFromMake() {
164 mod.Properties.HideFromMake = true
165}
166
Ivan Lozanod7586b62021-04-01 09:49:36 -0400167func (c *Module) HiddenFromMake() bool {
168 return c.Properties.HideFromMake
169}
170
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500171func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500172 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
173 // nil since we need compiler to actually sanitize.
174 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500175}
176
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500177func (mod *Module) IsPrebuilt() bool {
178 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
179 return true
180 }
181 return false
182}
183
Ivan Lozano43845682020-07-09 21:03:28 -0400184func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
185 switch tag {
186 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700187 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400188 return mod.sourceProvider.Srcs(), nil
189 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900190 if mod.OutputFile().Valid() {
191 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400192 }
193 return android.Paths{}, nil
194 }
195 default:
196 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
197 }
198}
199
Ivan Lozano52767be2019-10-18 14:49:46 -0700200func (mod *Module) SelectedStl() string {
201 return ""
202}
203
Ivan Lozano2b262972019-11-21 12:30:50 -0800204func (mod *Module) NonCcVariants() bool {
205 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400206 if _, ok := mod.compiler.(libraryInterface); ok {
207 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800208 }
209 }
210 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
211}
212
Ivan Lozano52767be2019-10-18 14:49:46 -0700213func (mod *Module) Static() bool {
214 if mod.compiler != nil {
215 if library, ok := mod.compiler.(libraryInterface); ok {
216 return library.static()
217 }
218 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400219 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700220}
221
222func (mod *Module) Shared() bool {
223 if mod.compiler != nil {
224 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400225 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700226 }
227 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400228 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700229}
230
Ivan Lozanod7586b62021-04-01 09:49:36 -0400231func (mod *Module) Dylib() bool {
232 if mod.compiler != nil {
233 if library, ok := mod.compiler.(libraryInterface); ok {
234 return library.dylib()
235 }
236 }
237 return false
238}
239
240func (mod *Module) Rlib() bool {
241 if mod.compiler != nil {
242 if library, ok := mod.compiler.(libraryInterface); ok {
243 return library.rlib()
244 }
245 }
246 return false
247}
248
249func (mod *Module) Binary() bool {
250 if mod.compiler != nil {
251 if _, ok := mod.compiler.(*binaryDecorator); ok {
252 return true
253 }
254 }
255 return false
256}
257
Justin Yun5e035862021-06-29 20:50:37 +0900258func (mod *Module) StaticExecutable() bool {
259 if !mod.Binary() {
260 return false
261 }
262 return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
263}
264
Ivan Lozanod7586b62021-04-01 09:49:36 -0400265func (mod *Module) Object() bool {
266 // Rust has no modules which produce only object files.
267 return false
268}
269
Ivan Lozano52767be2019-10-18 14:49:46 -0700270func (mod *Module) Toc() android.OptionalPath {
271 if mod.compiler != nil {
272 if _, ok := mod.compiler.(libraryInterface); ok {
273 return android.OptionalPath{}
274 }
275 }
276 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
277}
278
Colin Crossc511bc52020-04-07 16:50:32 +0000279func (mod *Module) UseSdk() bool {
280 return false
281}
282
Ivan Lozanod7586b62021-04-01 09:49:36 -0400283func (mod *Module) RelativeInstallPath() string {
284 if mod.compiler != nil {
285 return mod.compiler.relativeInstallPath()
286 }
287 return ""
288}
289
Ivan Lozano52767be2019-10-18 14:49:46 -0700290func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500291 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700292}
293
Jiyong Park7d55b612021-06-11 17:22:09 +0900294func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400295 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900296}
297
Ivan Lozano52767be2019-10-18 14:49:46 -0700298func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400299 return true
300}
301
302func (mod *Module) SubName() string {
303 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700304}
305
306func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500307 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700308 return false
309}
310
Ivan Lozanof9e21722020-12-02 09:00:51 -0500311func (mod *Module) IsVndkExt() bool {
312 return false
313}
314
Ivan Lozanod7586b62021-04-01 09:49:36 -0400315func (mod *Module) IsVndkSp() bool {
316 return false
317}
318
Colin Cross127bb8b2020-12-16 16:46:01 -0800319func (c *Module) IsVndkPrivate() bool {
320 return false
321}
322
323func (c *Module) IsLlndk() bool {
324 return false
325}
326
327func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500328 return false
329}
330
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400331func (mod *Module) KernelHeadersDecorator() bool {
332 return false
333}
334
Colin Cross1f3f1302021-04-26 18:37:44 -0700335func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400336 return false
337}
338
Colin Cross5271fea2021-04-27 13:06:04 -0700339func (m *Module) NeedsVendorPublicLibraryVariants() bool {
340 return false
341}
342
Ivan Lozanod7586b62021-04-01 09:49:36 -0400343func (mod *Module) HasLlndkStubs() bool {
344 return false
345}
346
347func (mod *Module) StubsVersion() string {
348 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
349}
350
Ivan Lozano52767be2019-10-18 14:49:46 -0700351func (mod *Module) SdkVersion() string {
352 return ""
353}
354
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900355func (mod *Module) MinSdkVersion() string {
356 return ""
357}
358
Colin Crossc511bc52020-04-07 16:50:32 +0000359func (mod *Module) AlwaysSdk() bool {
360 return false
361}
362
Jiyong Park2286afd2020-06-16 21:58:53 +0900363func (mod *Module) IsSdkVariant() bool {
364 return false
365}
366
Colin Cross1348ce32020-10-01 13:37:16 -0700367func (mod *Module) SplitPerApiLevel() bool {
368 return false
369}
370
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400372 Dylibs []string
373 Rlibs []string
374 Rustlibs []string
375 Stdlibs []string
376 ProcMacros []string
377 SharedLibs []string
378 StaticLibs []string
379 WholeStaticLibs []string
380 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700381
382 CrtBegin, CrtEnd string
383}
384
385type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500386 DyLibs RustLibraries
387 RLibs RustLibraries
388 SharedLibs android.Paths
389 SharedLibDeps android.Paths
390 StaticLibs android.Paths
391 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500392
393 // depFlags and depLinkFlags are rustc and linker (clang) flags.
394 depFlags []string
395 depLinkFlags []string
396
397 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
398 // Both of these are exported and propagate to dependencies.
399 linkDirs []string
400 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700401
Ivan Lozano45901ed2020-07-24 16:05:01 -0400402 // Used by bindgen modules which call clang
403 depClangFlags []string
404 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400405 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400406 depSystemIncludePaths android.Paths
407
Ivan Lozanof1c84332019-09-20 11:00:37 -0700408 CrtBegin android.OptionalPath
409 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700410
411 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500412 SrcDeps android.Paths
413 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414}
415
416type RustLibraries []RustLibrary
417
418type RustLibrary struct {
419 Path android.Path
420 CrateName string
421}
422
423type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100424 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425 compilerFlags(ctx ModuleContext, flags Flags) Flags
426 compilerProps() []interface{}
427 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
428 compilerDeps(ctx DepsContext, deps Deps) Deps
429 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700430 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700431
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100432 // Output directory in which source-generated code from dependencies is
433 // copied. This is equivalent to Cargo's OUT_DIR variable.
434 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700435
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400436 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
437 CargoPkgVersion() string
438
439 // CargoEnvCompat returns whether Cargo environment variables should be used.
440 CargoEnvCompat() bool
441
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800442 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200443 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700444 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400445 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400446
447 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400448
449 Disabled() bool
450 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400451
Ivan Lozanodd055472020-09-28 13:22:45 -0400452 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900453
454 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400455}
456
Matthew Maurerbb3add12020-06-25 09:34:12 -0700457type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700458 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400459 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700460}
461
462type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400463 linkDirs []string
464 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700465}
466
Matthew Maurerbb3add12020-06-25 09:34:12 -0700467func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
468 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
469}
470
Ivan Lozano2093af22020-08-25 12:48:19 -0400471func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
472 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
473}
474
Colin Cross0de8a1e2020-09-18 14:15:30 -0700475func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
476 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700477 LinkDirs: flagExporter.linkDirs,
478 LinkObjects: flagExporter.linkObjects,
479 })
480}
481
Matthew Maurerbb3add12020-06-25 09:34:12 -0700482var _ exportedFlagsProducer = (*flagExporter)(nil)
483
484func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700485 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700486}
487
Colin Cross0de8a1e2020-09-18 14:15:30 -0700488type FlagExporterInfo struct {
489 Flags []string
490 LinkDirs []string // TODO: this should be android.Paths
491 LinkObjects []string // TODO: this should be android.Paths
492}
493
494var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
495
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400496func (mod *Module) isCoverageVariant() bool {
497 return mod.coverage.Properties.IsCoverageVariant
498}
499
500var _ cc.Coverage = (*Module)(nil)
501
502func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
503 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
504}
505
Ivan Lozanod7586b62021-04-01 09:49:36 -0400506func (mod *Module) VndkVersion() string {
507 return mod.Properties.VndkVersion
508}
509
510func (mod *Module) PreventInstall() bool {
511 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400512}
513
514func (mod *Module) HideFromMake() {
515 mod.Properties.HideFromMake = true
516}
517
518func (mod *Module) MarkAsCoverageVariant(coverage bool) {
519 mod.coverage.Properties.IsCoverageVariant = coverage
520}
521
522func (mod *Module) EnableCoverageIfNeeded() {
523 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700524}
525
526func defaultsFactory() android.Module {
527 return DefaultsFactory()
528}
529
530type Defaults struct {
531 android.ModuleBase
532 android.DefaultsModuleBase
533}
534
535func DefaultsFactory(props ...interface{}) android.Module {
536 module := &Defaults{}
537
538 module.AddProperties(props...)
539 module.AddProperties(
540 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500541 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100542 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400543 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700544 &BaseCompilerProperties{},
545 &BinaryCompilerProperties{},
546 &LibraryCompilerProperties{},
547 &ProcMacroCompilerProperties{},
548 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400549 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700550 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400551 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400552 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200553 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500554 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700555 )
556
557 android.InitDefaultsModule(module)
558 return module
559}
560
561func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700562 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700563}
564
Ivan Lozano183a3212019-10-18 14:18:45 -0700565func (mod *Module) CcLibrary() bool {
566 if mod.compiler != nil {
567 if _, ok := mod.compiler.(*libraryDecorator); ok {
568 return true
569 }
570 }
571 return false
572}
573
574func (mod *Module) CcLibraryInterface() bool {
575 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400576 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
577 // VariantIs{Static,Shared} is set.
578 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700579 return true
580 }
581 }
582 return false
583}
584
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800585func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700586 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700587 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800588 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700589 }
590 }
591 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
592}
593
594func (mod *Module) SetStatic() {
595 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700596 if library, ok := mod.compiler.(libraryInterface); ok {
597 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700598 return
599 }
600 }
601 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
602}
603
604func (mod *Module) SetShared() {
605 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700606 if library, ok := mod.compiler.(libraryInterface); ok {
607 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700608 return
609 }
610 }
611 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
612}
613
Ivan Lozano183a3212019-10-18 14:18:45 -0700614func (mod *Module) BuildStaticVariant() bool {
615 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700616 if library, ok := mod.compiler.(libraryInterface); ok {
617 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700618 }
619 }
620 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
621}
622
623func (mod *Module) BuildSharedVariant() bool {
624 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700625 if library, ok := mod.compiler.(libraryInterface); ok {
626 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700627 }
628 }
629 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
630}
631
Ivan Lozano183a3212019-10-18 14:18:45 -0700632func (mod *Module) Module() android.Module {
633 return mod
634}
635
Ivan Lozano183a3212019-10-18 14:18:45 -0700636func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900637 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
638 return mod.compiler.strippedOutputFilePath()
639 }
640 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700641}
642
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400643func (mod *Module) CoverageFiles() android.Paths {
644 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800645 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400646 }
647 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
648}
649
Jiyong Park459feca2020-12-15 11:02:21 +0900650func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900651 if !mod.EverInstallable() {
652 return false
653 }
654
Jiyong Park459feca2020-12-15 11:02:21 +0900655 // The apex variant is not installable because it is included in the APEX and won't appear
656 // in the system partition as a standalone file.
657 if !apexInfo.IsForPlatform() {
658 return false
659 }
660
Jiyong Parke54f07e2021-04-07 15:08:04 +0900661 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900662}
663
Ivan Lozano183a3212019-10-18 14:18:45 -0700664var _ cc.LinkableInterface = (*Module)(nil)
665
Ivan Lozanoffee3342019-08-27 12:03:00 -0700666func (mod *Module) Init() android.Module {
667 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500668 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700669
670 if mod.compiler != nil {
671 mod.AddProperties(mod.compiler.compilerProps()...)
672 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400673 if mod.coverage != nil {
674 mod.AddProperties(mod.coverage.props()...)
675 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200676 if mod.clippy != nil {
677 mod.AddProperties(mod.clippy.props()...)
678 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400679 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700680 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400681 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500682 if mod.sanitize != nil {
683 mod.AddProperties(mod.sanitize.props()...)
684 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400685
Ivan Lozanoffee3342019-08-27 12:03:00 -0700686 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900687 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700688
689 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700690 return mod
691}
692
693func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
694 return &Module{
695 hod: hod,
696 multilib: multilib,
697 }
698}
699func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
700 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400701 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200702 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500703 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700704 return module
705}
706
707type ModuleContext interface {
708 android.ModuleContext
709 ModuleContextIntf
710}
711
712type BaseModuleContext interface {
713 android.BaseModuleContext
714 ModuleContextIntf
715}
716
717type DepsContext interface {
718 android.BottomUpMutatorContext
719 ModuleContextIntf
720}
721
722type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200723 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725}
726
727type depsContext struct {
728 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729}
730
731type moduleContext struct {
732 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700733}
734
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200735type baseModuleContext struct {
736 android.BaseModuleContext
737}
738
739func (ctx *moduleContext) RustModule() *Module {
740 return ctx.Module().(*Module)
741}
742
743func (ctx *moduleContext) toolchain() config.Toolchain {
744 return ctx.RustModule().toolchain(ctx)
745}
746
747func (ctx *depsContext) RustModule() *Module {
748 return ctx.Module().(*Module)
749}
750
751func (ctx *depsContext) toolchain() config.Toolchain {
752 return ctx.RustModule().toolchain(ctx)
753}
754
755func (ctx *baseModuleContext) RustModule() *Module {
756 return ctx.Module().(*Module)
757}
758
759func (ctx *baseModuleContext) toolchain() config.Toolchain {
760 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400761}
762
763func (mod *Module) nativeCoverage() bool {
764 return mod.compiler != nil && mod.compiler.nativeCoverage()
765}
766
Ivan Lozanod7586b62021-04-01 09:49:36 -0400767func (mod *Module) EverInstallable() bool {
768 return mod.compiler != nil &&
769 // Check to see whether the module is actually ever installable.
770 mod.compiler.everInstallable()
771}
772
773func (mod *Module) Installable() *bool {
774 return mod.Properties.Installable
775}
776
Ivan Lozanoffee3342019-08-27 12:03:00 -0700777func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
778 if mod.cachedToolchain == nil {
779 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
780 }
781 return mod.cachedToolchain
782}
783
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200784func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
785 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
786}
787
Ivan Lozanoffee3342019-08-27 12:03:00 -0700788func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
789}
790
791func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
792 ctx := &moduleContext{
793 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700794 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795
Jiyong Park99644e92020-11-17 22:21:02 +0900796 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
797 if !apexInfo.IsForPlatform() {
798 mod.hideApexVariantFromMake = true
799 }
800
Ivan Lozanoffee3342019-08-27 12:03:00 -0700801 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500802 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
803
804 // Differentiate static libraries that are vendor available
805 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500806 mod.Properties.SubName += cc.VendorSuffix
807 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
808 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500809 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810
811 if !toolchain.Supported() {
812 // This toolchain's unsupported, there's nothing to do for this mod.
813 return
814 }
815
816 deps := mod.depsToPaths(ctx)
817 flags := Flags{
818 Toolchain: toolchain,
819 }
820
821 if mod.compiler != nil {
822 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400823 }
824 if mod.coverage != nil {
825 flags, deps = mod.coverage.flags(ctx, flags, deps)
826 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200827 if mod.clippy != nil {
828 flags, deps = mod.clippy.flags(ctx, flags, deps)
829 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500830 if mod.sanitize != nil {
831 flags, deps = mod.sanitize.flags(ctx, flags, deps)
832 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400833
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200834 // SourceProvider needs to call GenerateSource() before compiler calls
835 // compile() so it can provide the source. A SourceProvider has
836 // multiple variants (e.g. source, rlib, dylib). Only the "source"
837 // variant is responsible for effectively generating the source. The
838 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400839 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200840 if mod.compiler.(libraryInterface).source() {
841 mod.sourceProvider.GenerateSource(ctx, deps)
842 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
843 } else {
844 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
845 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700846 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200847 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400848 }
849
850 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100851 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900852 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900853 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200854 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900855
Dan Albert06feee92021-03-19 15:06:02 -0700856 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
857
Ivan Lozano1921e802021-05-20 13:39:16 -0400858 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
859 // RECOVERY_SNAPSHOT_VERSION is current.
860 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
861 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
862 lib.collectHeadersForSnapshot(ctx, deps)
863 }
864 }
865
Jiyong Park459feca2020-12-15 11:02:21 +0900866 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
867 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200868 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400869 }
Chris Wailes74be7642021-07-22 16:20:28 -0700870
871 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700872 }
873}
874
875func (mod *Module) deps(ctx DepsContext) Deps {
876 deps := Deps{}
877
878 if mod.compiler != nil {
879 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400880 }
881 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700882 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 }
884
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400885 if mod.coverage != nil {
886 deps = mod.coverage.deps(ctx, deps)
887 }
888
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500889 if mod.sanitize != nil {
890 deps = mod.sanitize.deps(ctx, deps)
891 }
892
Ivan Lozanoffee3342019-08-27 12:03:00 -0700893 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
894 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700895 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700896 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
897 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
898 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400899 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 return deps
901
902}
903
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904type dependencyTag struct {
905 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800906 name string
907 library bool
908 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700909}
910
Jiyong Park65b62242020-11-25 12:44:59 +0900911// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
912// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
913func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800914 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900915}
916
917var _ android.InstallNeededDependencyTag = dependencyTag{}
918
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400920 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
921 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
922 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800923 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400924 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200925 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926)
927
Jiyong Park99644e92020-11-17 22:21:02 +0900928func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
929 tag, ok := depTag.(dependencyTag)
930 return ok && tag == dylibDepTag
931}
932
Jiyong Park94e22fd2021-04-08 18:19:15 +0900933func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
934 tag, ok := depTag.(dependencyTag)
935 return ok && tag == rlibDepTag
936}
937
Matthew Maurer0f003b12020-06-29 14:34:06 -0700938type autoDep struct {
939 variation string
940 depTag dependencyTag
941}
942
943var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200944 rlibVariation = "rlib"
945 dylibVariation = "dylib"
946 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
947 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700948)
949
950type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500951 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700952}
953
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400954func (mod *Module) begin(ctx BaseModuleContext) {
955 if mod.coverage != nil {
956 mod.coverage.begin(ctx)
957 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500958 if mod.sanitize != nil {
959 mod.sanitize.begin(ctx)
960 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400961}
962
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
964 var depPaths PathDeps
965
966 directRlibDeps := []*Module{}
967 directDylibDeps := []*Module{}
968 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900969 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700970 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400971 directSrcProvidersDeps := []*Module{}
972 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700973
974 ctx.VisitDirectDeps(func(dep android.Module) {
975 depName := ctx.OtherModuleName(dep)
976 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400977
Ivan Lozano89435d12020-07-31 11:01:18 -0400978 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700979 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400980 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700981
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982 switch depTag {
983 case dylibDepTag:
984 dylib, ok := rustDep.compiler.(libraryInterface)
985 if !ok || !dylib.dylib() {
986 ctx.ModuleErrorf("mod %q not an dylib library", depName)
987 return
988 }
989 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400990 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700991 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400992
Ivan Lozanoffee3342019-08-27 12:03:00 -0700993 rlib, ok := rustDep.compiler.(libraryInterface)
994 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400995 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700996 return
997 }
998 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400999 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001000 case procMacroDepTag:
1001 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001002 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001003 }
1004
1005 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001006 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1007 // OS/Arch variant is used.
1008 var helper string
1009 if ctx.Host() {
1010 helper = "missing 'host_supported'?"
1011 } else {
1012 helper = "device module defined?"
1013 }
1014
1015 if dep.Target().Os != ctx.Os() {
1016 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1017 return
1018 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1019 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1020 return
1021 }
1022 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001023 }
1024
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001025 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001026 if depTag != procMacroDepTag {
1027 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1028 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1029 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1030 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001031 }
1032
Ivan Lozanoffee3342019-08-27 12:03:00 -07001033 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001034 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001035 if !linkFile.Valid() {
1036 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1037 depName, ctx.ModuleName())
1038 return
1039 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001041 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1042 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001043 }
1044 }
1045
Ivan Lozano89435d12020-07-31 11:01:18 -04001046 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001047 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001048 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001049 if _, ok := ccDep.(*Module); !ok {
1050 if ccDep.Module().Target().Os != ctx.Os() {
1051 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1052 return
1053 }
1054 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1055 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1056 return
1057 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001058 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001059 linkObject := ccDep.OutputFile()
1060 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001061
Ivan Lozano2093af22020-08-25 12:48:19 -04001062 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001063 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1064 }
1065
1066 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001067 switch {
1068 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001069 if cc.IsWholeStaticLib(depTag) {
1070 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1071 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001072 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1073 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001074 } else {
1075 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 -05001076 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001077 }
1078
1079 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1080 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001081 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001082 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1083
Colin Cross0de8a1e2020-09-18 14:15:30 -07001084 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1085 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1086 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1087 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1088 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001090 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001091 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001092 // For the shared lib dependencies, we may link to the stub variant
1093 // of the dependency depending on the context (e.g. if this
1094 // dependency crosses the APEX boundaries).
1095 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1096
1097 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1098 // object (either from stub or non-stub) to use.
1099 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1100 linkPath = linkPathFromFilePath(linkObject.Path())
1101
Ivan Lozanoffee3342019-08-27 12:03:00 -07001102 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001103 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001104 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1105 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1106 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1107 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001108 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001109
1110 // Record baseLibName for snapshots.
1111 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001112 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001113
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001114 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001115 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001116 case cc.IsHeaderDepTag(depTag):
1117 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1118 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1119 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1120 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001121 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001122 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001123 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001124 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001125 }
1126
1127 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001128 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1129 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001130 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001131 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001132 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001133
1134 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001135 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001136 // These are usually genrules which don't have per-target variants.
1137 directSrcDeps = append(directSrcDeps, srcDep)
1138 }
1139 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001140 })
1141
1142 var rlibDepFiles RustLibraries
1143 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001144 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001145 }
1146 var dylibDepFiles RustLibraries
1147 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001148 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001149 }
1150 var procMacroDepFiles RustLibraries
1151 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001152 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001153 }
1154
1155 var staticLibDepFiles android.Paths
1156 for _, dep := range directStaticLibDeps {
1157 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1158 }
1159
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001160 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001161 var sharedLibDepFiles android.Paths
1162 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001163 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1164 if dep.TableOfContents.Valid() {
1165 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001166 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001167 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001168 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001169 }
1170
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001171 var srcProviderDepFiles android.Paths
1172 for _, dep := range directSrcProvidersDeps {
1173 srcs, _ := dep.OutputFiles("")
1174 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1175 }
1176 for _, dep := range directSrcDeps {
1177 srcs := dep.Srcs()
1178 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1179 }
1180
Ivan Lozanoffee3342019-08-27 12:03:00 -07001181 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1182 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1183 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001184 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001185 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1186 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001187 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001188
1189 // Dedup exported flags from dependencies
1190 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001191 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001192 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001193 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1194 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1195 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001196
1197 return depPaths
1198}
1199
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001200func (mod *Module) InstallInData() bool {
1201 if mod.compiler == nil {
1202 return false
1203 }
1204 return mod.compiler.inData()
1205}
1206
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207func linkPathFromFilePath(filepath android.Path) string {
1208 return strings.Split(filepath.String(), filepath.Base())[0]
1209}
Ivan Lozanod648c432020-02-06 12:05:10 -05001210
Ivan Lozanoffee3342019-08-27 12:03:00 -07001211func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1212 ctx := &depsContext{
1213 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001214 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001215
1216 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001217 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001218 var snapshotInfo *cc.SnapshotInfo
1219
1220 if ctx.Os() == android.Android {
1221 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1222 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001223
Ivan Lozano2b081132020-09-08 12:46:52 -04001224 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001225 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001226 stdLinkage = "rlib-std"
1227 }
1228
1229 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001230
Ivan Lozano2b081132020-09-08 12:46:52 -04001231 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1232 rlibDepVariations = append(rlibDepVariations,
1233 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1234 }
1235
Ivan Lozano1921e802021-05-20 13:39:16 -04001236 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001237 for _, lib := range deps.Rlibs {
1238 depTag := rlibDepTag
1239 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1240
1241 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1242 {Mutator: "rust_libraries", Variation: rlibVariation},
1243 }...), depTag, lib)
1244 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001245
1246 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001247 actx.AddVariationDependencies(
1248 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001249 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001250 dylibDepTag, deps.Dylibs...)
1251
Ivan Lozano1921e802021-05-20 13:39:16 -04001252 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001253 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1254 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001255 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001256 for _, lib := range deps.Rustlibs {
1257 depTag := autoDep.depTag
1258 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1259 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1260 {Mutator: "rust_libraries", Variation: autoDep.variation},
1261 }...), depTag, lib)
1262 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001263 } else {
1264 actx.AddVariationDependencies(
1265 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1266 autoDep.depTag, deps.Rustlibs...)
1267 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001268 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001269
1270 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001271 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001272 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001273 for _, lib := range deps.Stdlibs {
1274 depTag := rlibDepTag
1275 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1276
1277 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1278 depTag, lib)
1279 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001280 } else {
1281 actx.AddVariationDependencies(
1282 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1283 dylibDepTag, deps.Stdlibs...)
1284 }
1285 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001286
1287 for _, lib := range deps.SharedLibs {
1288 depTag := cc.SharedDepTag()
1289 name, version := cc.StubsLibNameAndVersion(lib)
1290
1291 variations := []blueprint.Variation{
1292 {Mutator: "link", Variation: "shared"},
1293 }
1294 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1295 }
1296
1297 for _, lib := range deps.WholeStaticLibs {
1298 depTag := cc.StaticDepTag(true)
1299 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1300
1301 actx.AddVariationDependencies([]blueprint.Variation{
1302 {Mutator: "link", Variation: "static"},
1303 }, depTag, lib)
1304 }
1305
1306 for _, lib := range deps.StaticLibs {
1307 depTag := cc.StaticDepTag(false)
1308 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1309
1310 actx.AddVariationDependencies([]blueprint.Variation{
1311 {Mutator: "link", Variation: "static"},
1312 }, depTag, lib)
1313 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001314
Zach Johnson3df4e632020-11-06 11:56:27 -08001315 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1316
Colin Cross565cafd2020-09-25 18:47:38 -07001317 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001318 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001319 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1320 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001321 }
1322 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001323 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1324 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001325 }
1326
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001327 if mod.sourceProvider != nil {
1328 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1329 bindgen.Properties.Custom_bindgen != "" {
1330 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1331 bindgen.Properties.Custom_bindgen)
1332 }
1333 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001334
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001335 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001336 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001337}
1338
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001339func BeginMutator(ctx android.BottomUpMutatorContext) {
1340 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1341 mod.beginMutator(ctx)
1342 }
1343}
1344
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001345func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1346 ctx := &baseModuleContext{
1347 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001348 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001349
1350 mod.begin(ctx)
1351}
1352
Ivan Lozanoffee3342019-08-27 12:03:00 -07001353func (mod *Module) Name() string {
1354 name := mod.ModuleBase.Name()
1355 if p, ok := mod.compiler.(interface {
1356 Name(string) string
1357 }); ok {
1358 name = p.Name(name)
1359 }
1360 return name
1361}
1362
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001363func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001364 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001365 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001366 }
1367}
1368
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001369var _ android.HostToolProvider = (*Module)(nil)
1370
1371func (mod *Module) HostToolPath() android.OptionalPath {
1372 if !mod.Host() {
1373 return android.OptionalPath{}
1374 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001375 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1376 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001377 }
1378 return android.OptionalPath{}
1379}
1380
Jiyong Park99644e92020-11-17 22:21:02 +09001381var _ android.ApexModule = (*Module)(nil)
1382
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001383func (mod *Module) minSdkVersion() string {
1384 return String(mod.Properties.Min_sdk_version)
1385}
1386
Jiyong Park45bf82e2020-12-15 22:29:02 +09001387var _ android.ApexModule = (*Module)(nil)
1388
1389// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001390func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001391 minSdkVersion := mod.minSdkVersion()
1392 if minSdkVersion == "apex_inherit" {
1393 return nil
1394 }
1395 if minSdkVersion == "" {
1396 return fmt.Errorf("min_sdk_version is not specificed")
1397 }
1398
1399 // Not using nativeApiLevelFromUser because the context here is not
1400 // necessarily a native context.
1401 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1402 if err != nil {
1403 return err
1404 }
1405
1406 if ver.GreaterThan(sdkVersion) {
1407 return fmt.Errorf("newer SDK(%v)", ver)
1408 }
Jiyong Park99644e92020-11-17 22:21:02 +09001409 return nil
1410}
1411
Jiyong Park45bf82e2020-12-15 22:29:02 +09001412// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001413func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1414 depTag := ctx.OtherModuleDependencyTag(dep)
1415
1416 if ccm, ok := dep.(*cc.Module); ok {
1417 if ccm.HasStubsVariants() {
1418 if cc.IsSharedDepTag(depTag) {
1419 // dynamic dep to a stubs lib crosses APEX boundary
1420 return false
1421 }
1422 if cc.IsRuntimeDepTag(depTag) {
1423 // runtime dep to a stubs lib also crosses APEX boundary
1424 return false
1425 }
1426
1427 if cc.IsHeaderDepTag(depTag) {
1428 return false
1429 }
1430 }
1431 if mod.Static() && cc.IsSharedDepTag(depTag) {
1432 // shared_lib dependency from a static lib is considered as crossing
1433 // the APEX boundary because the dependency doesn't actually is
1434 // linked; the dependency is used only during the compilation phase.
1435 return false
1436 }
1437 }
1438
1439 if depTag == procMacroDepTag {
1440 return false
1441 }
1442
1443 return true
1444}
1445
1446// Overrides ApexModule.IsInstallabeToApex()
1447func (mod *Module) IsInstallableToApex() bool {
1448 if mod.compiler != nil {
1449 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1450 return true
1451 }
1452 if _, ok := mod.compiler.(*binaryDecorator); ok {
1453 return true
1454 }
1455 }
1456 return false
1457}
1458
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001459// If a library file has a "lib" prefix, extract the library name without the prefix.
1460func libNameFromFilePath(filepath android.Path) (string, bool) {
1461 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1462 if strings.HasPrefix(libName, "lib") {
1463 libName = libName[3:]
1464 return libName, true
1465 }
1466 return "", false
1467}
1468
Ivan Lozanoffee3342019-08-27 12:03:00 -07001469var Bool = proptools.Bool
1470var BoolDefault = proptools.BoolDefault
1471var String = proptools.String
1472var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001473
1474var _ android.OutputFileProducer = (*Module)(nil)