blob: 24c8634ac90df791795d8c1279abe649a0bdb0c3 [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"
hamzehc0a671f2021-07-22 12:05:08 -070028 "android/soong/fuzz"
Ivan Lozanoffee3342019-08-27 12:03:00 -070029 "android/soong/rust/config"
30)
31
32var pctx = android.NewPackageContext("android/soong/rust")
33
34func init() {
35 // Only allow rust modules to be defined for certain projects
Ivan Lozanoffee3342019-08-27 12:03:00 -070036
37 android.AddNeverAllowRules(
38 android.NeverAllow().
Ivan Lozano03a94c42021-06-28 11:27:11 -040039 NotIn(append(config.RustAllowedPaths, config.DownstreamRustAllowedPaths...)...).
Ivan Lozanoe169ad72019-09-18 08:42:54 -070040 ModuleType(config.RustModuleTypes...))
Ivan Lozanoffee3342019-08-27 12:03:00 -070041
42 android.RegisterModuleType("rust_defaults", defaultsFactory)
43 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040045 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040046 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050047
48 })
49 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
50 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070051 })
52 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020053 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanoffee3342019-08-27 12:03:00 -070054}
55
56type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040057 GlobalRustFlags []string // Flags that apply globally to rust
58 GlobalLinkFlags []string // Flags that apply globally to linker
59 RustFlags []string // Flags that apply to rust
60 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020061 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070062 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070063 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040064 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020065 Clippy bool
Ivan Lozanoffee3342019-08-27 12:03:00 -070066}
67
68type BaseProperties struct {
69 AndroidMkRlibs []string
70 AndroidMkDylibs []string
71 AndroidMkProcMacroLibs []string
72 AndroidMkSharedLibs []string
73 AndroidMkStaticLibs []string
Ivan Lozano43845682020-07-09 21:03:28 -040074
Ivan Lozano6a884432020-12-02 09:15:16 -050075 ImageVariationPrefix string `blueprint:"mutated"`
76 VndkVersion string `blueprint:"mutated"`
77 SubName string `blueprint:"mutated"`
78
Ivan Lozanoc08897c2021-04-02 12:41:32 -040079 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
80 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
81 // appended before SubName.
82 RustSubName string `blueprint:"mutated"`
83
Ivan Lozano6a884432020-12-02 09:15:16 -050084 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 CoreVariantNeeded bool `blueprint:"mutated"`
86 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080087 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050088 ExtraVariants []string `blueprint:"mutated"`
89
Ivan Lozanoa2268632021-07-22 10:52:06 -040090 // Allows this module to use non-APEX version of libraries. Useful
91 // for building binaries that are started before APEXes are activated.
92 Bootstrap *bool
93
Ivan Lozano1921e802021-05-20 13:39:16 -040094 // Used by vendor snapshot to record dependencies from snapshot modules.
95 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090096 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040097
Ivan Lozanoe6d30982021-02-05 10:57:43 -050098 // Make this module available when building for vendor ramdisk.
99 // On device without a dedicated recovery partition, the module is only
100 // available after switching root into
101 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800102 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500103 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400104
Ivan Lozano1921e802021-05-20 13:39:16 -0400105 // 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 vendor snapshot.
110 Exclude_from_vendor_snapshot *bool
111
112 // Normally Soong uses the directory structure to decide which modules
113 // should be included (framework) or excluded (non-framework) from the
114 // different snapshots (vendor, recovery, etc.), but this property
115 // allows a partner to exclude a module normally thought of as a
116 // framework module from the recovery snapshot.
117 Exclude_from_recovery_snapshot *bool
118
Matthew Maurer460ee942021-02-11 12:31:46 -0800119 // Make this module available when building for recovery
120 Recovery_available *bool
121
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500122 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
123 Min_sdk_version *string
124
Ivan Lozano43845682020-07-09 21:03:28 -0400125 PreventInstall bool
126 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400127 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128}
129
130type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700131 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700132
Ivan Lozano6a884432020-12-02 09:15:16 -0500133 VendorProperties cc.VendorProperties
134
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135 Properties BaseProperties
136
137 hod android.HostOrDeviceSupported
138 multilib android.Multilib
139
Ivan Lozano6a884432020-12-02 09:15:16 -0500140 makeLinkType string
141
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400143 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200144 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500145 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700146 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400147 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700148 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400149
Jiyong Parke54f07e2021-04-07 15:08:04 +0900150 // Unstripped output. This is usually used when this module is linked to another module
151 // as a library. The stripped output which is used for installation can be found via
152 // compiler.strippedOutputFile if it exists.
153 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700154 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900155
156 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157}
158
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500159func (mod *Module) Header() bool {
160 //TODO: If Rust libraries provide header variants, this needs to be updated.
161 return false
162}
163
164func (mod *Module) SetPreventInstall() {
165 mod.Properties.PreventInstall = true
166}
167
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500168func (mod *Module) SetHideFromMake() {
169 mod.Properties.HideFromMake = true
170}
171
Ivan Lozanod7586b62021-04-01 09:49:36 -0400172func (c *Module) HiddenFromMake() bool {
173 return c.Properties.HideFromMake
174}
175
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500176func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500177 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
178 // nil since we need compiler to actually sanitize.
179 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500180}
181
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500182func (mod *Module) IsPrebuilt() bool {
183 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
184 return true
185 }
186 return false
187}
188
Ivan Lozano43845682020-07-09 21:03:28 -0400189func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
190 switch tag {
191 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700192 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400193 return mod.sourceProvider.Srcs(), nil
194 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900195 if mod.OutputFile().Valid() {
196 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400197 }
198 return android.Paths{}, nil
199 }
200 default:
201 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
202 }
203}
204
Ivan Lozano52767be2019-10-18 14:49:46 -0700205func (mod *Module) SelectedStl() string {
206 return ""
207}
208
Ivan Lozano2b262972019-11-21 12:30:50 -0800209func (mod *Module) NonCcVariants() bool {
210 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400211 if _, ok := mod.compiler.(libraryInterface); ok {
212 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800213 }
214 }
215 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
216}
217
Ivan Lozano52767be2019-10-18 14:49:46 -0700218func (mod *Module) Static() bool {
219 if mod.compiler != nil {
220 if library, ok := mod.compiler.(libraryInterface); ok {
221 return library.static()
222 }
223 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400224 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700225}
226
227func (mod *Module) Shared() bool {
228 if mod.compiler != nil {
229 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400230 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700231 }
232 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400233 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700234}
235
Ivan Lozanod7586b62021-04-01 09:49:36 -0400236func (mod *Module) Dylib() bool {
237 if mod.compiler != nil {
238 if library, ok := mod.compiler.(libraryInterface); ok {
239 return library.dylib()
240 }
241 }
242 return false
243}
244
245func (mod *Module) Rlib() bool {
246 if mod.compiler != nil {
247 if library, ok := mod.compiler.(libraryInterface); ok {
248 return library.rlib()
249 }
250 }
251 return false
252}
253
254func (mod *Module) Binary() bool {
255 if mod.compiler != nil {
256 if _, ok := mod.compiler.(*binaryDecorator); ok {
257 return true
258 }
259 }
260 return false
261}
262
Justin Yun5e035862021-06-29 20:50:37 +0900263func (mod *Module) StaticExecutable() bool {
264 if !mod.Binary() {
265 return false
266 }
267 return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
268}
269
Ivan Lozanod7586b62021-04-01 09:49:36 -0400270func (mod *Module) Object() bool {
271 // Rust has no modules which produce only object files.
272 return false
273}
274
Ivan Lozano52767be2019-10-18 14:49:46 -0700275func (mod *Module) Toc() android.OptionalPath {
276 if mod.compiler != nil {
277 if _, ok := mod.compiler.(libraryInterface); ok {
278 return android.OptionalPath{}
279 }
280 }
281 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
282}
283
Colin Crossc511bc52020-04-07 16:50:32 +0000284func (mod *Module) UseSdk() bool {
285 return false
286}
287
Ivan Lozanod7586b62021-04-01 09:49:36 -0400288func (mod *Module) RelativeInstallPath() string {
289 if mod.compiler != nil {
290 return mod.compiler.relativeInstallPath()
291 }
292 return ""
293}
294
Ivan Lozano52767be2019-10-18 14:49:46 -0700295func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500296 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700297}
298
Jiyong Park7d55b612021-06-11 17:22:09 +0900299func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400300 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900301}
302
Ivan Lozano52767be2019-10-18 14:49:46 -0700303func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400304 return true
305}
306
307func (mod *Module) SubName() string {
308 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700309}
310
311func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500312 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700313 return false
314}
315
Ivan Lozanof9e21722020-12-02 09:00:51 -0500316func (mod *Module) IsVndkExt() bool {
317 return false
318}
319
Ivan Lozanod7586b62021-04-01 09:49:36 -0400320func (mod *Module) IsVndkSp() bool {
321 return false
322}
323
Colin Cross127bb8b2020-12-16 16:46:01 -0800324func (c *Module) IsVndkPrivate() bool {
325 return false
326}
327
328func (c *Module) IsLlndk() bool {
329 return false
330}
331
332func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500333 return false
334}
335
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400336func (mod *Module) KernelHeadersDecorator() bool {
337 return false
338}
339
Colin Cross1f3f1302021-04-26 18:37:44 -0700340func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400341 return false
342}
343
Colin Cross5271fea2021-04-27 13:06:04 -0700344func (m *Module) NeedsVendorPublicLibraryVariants() bool {
345 return false
346}
347
Ivan Lozanod7586b62021-04-01 09:49:36 -0400348func (mod *Module) HasLlndkStubs() bool {
349 return false
350}
351
352func (mod *Module) StubsVersion() string {
353 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
354}
355
Ivan Lozano52767be2019-10-18 14:49:46 -0700356func (mod *Module) SdkVersion() string {
357 return ""
358}
359
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900360func (mod *Module) MinSdkVersion() string {
361 return ""
362}
363
Colin Crossc511bc52020-04-07 16:50:32 +0000364func (mod *Module) AlwaysSdk() bool {
365 return false
366}
367
Jiyong Park2286afd2020-06-16 21:58:53 +0900368func (mod *Module) IsSdkVariant() bool {
369 return false
370}
371
Colin Cross1348ce32020-10-01 13:37:16 -0700372func (mod *Module) SplitPerApiLevel() bool {
373 return false
374}
375
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400377 Dylibs []string
378 Rlibs []string
379 Rustlibs []string
380 Stdlibs []string
381 ProcMacros []string
382 SharedLibs []string
383 StaticLibs []string
384 WholeStaticLibs []string
385 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700386
387 CrtBegin, CrtEnd string
388}
389
390type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500391 DyLibs RustLibraries
392 RLibs RustLibraries
393 SharedLibs android.Paths
394 SharedLibDeps android.Paths
395 StaticLibs android.Paths
396 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500397
398 // depFlags and depLinkFlags are rustc and linker (clang) flags.
399 depFlags []string
400 depLinkFlags []string
401
402 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
403 // Both of these are exported and propagate to dependencies.
404 linkDirs []string
405 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700406
Ivan Lozano45901ed2020-07-24 16:05:01 -0400407 // Used by bindgen modules which call clang
408 depClangFlags []string
409 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400410 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400411 depSystemIncludePaths android.Paths
412
Ivan Lozanof1c84332019-09-20 11:00:37 -0700413 CrtBegin android.OptionalPath
414 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700415
416 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500417 SrcDeps android.Paths
418 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700419}
420
421type RustLibraries []RustLibrary
422
423type RustLibrary struct {
424 Path android.Path
425 CrateName string
426}
427
428type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100429 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430 compilerFlags(ctx ModuleContext, flags Flags) Flags
431 compilerProps() []interface{}
432 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
433 compilerDeps(ctx DepsContext, deps Deps) Deps
434 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700435 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700436
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100437 // Output directory in which source-generated code from dependencies is
438 // copied. This is equivalent to Cargo's OUT_DIR variable.
439 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700440
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400441 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
442 CargoPkgVersion() string
443
444 // CargoEnvCompat returns whether Cargo environment variables should be used.
445 CargoEnvCompat() bool
446
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800447 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200448 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400450 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400451
452 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400453
454 Disabled() bool
455 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400456
Ivan Lozanodd055472020-09-28 13:22:45 -0400457 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900458
459 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400460}
461
Matthew Maurerbb3add12020-06-25 09:34:12 -0700462type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700463 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400464 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700465}
466
467type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400468 linkDirs []string
469 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700470}
471
Matthew Maurerbb3add12020-06-25 09:34:12 -0700472func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
473 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
474}
475
Ivan Lozano2093af22020-08-25 12:48:19 -0400476func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
477 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
478}
479
Colin Cross0de8a1e2020-09-18 14:15:30 -0700480func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
481 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700482 LinkDirs: flagExporter.linkDirs,
483 LinkObjects: flagExporter.linkObjects,
484 })
485}
486
Matthew Maurerbb3add12020-06-25 09:34:12 -0700487var _ exportedFlagsProducer = (*flagExporter)(nil)
488
489func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700490 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700491}
492
Colin Cross0de8a1e2020-09-18 14:15:30 -0700493type FlagExporterInfo struct {
494 Flags []string
495 LinkDirs []string // TODO: this should be android.Paths
496 LinkObjects []string // TODO: this should be android.Paths
497}
498
499var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
500
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400501func (mod *Module) isCoverageVariant() bool {
502 return mod.coverage.Properties.IsCoverageVariant
503}
504
505var _ cc.Coverage = (*Module)(nil)
506
507func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
508 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
509}
510
Ivan Lozanod7586b62021-04-01 09:49:36 -0400511func (mod *Module) VndkVersion() string {
512 return mod.Properties.VndkVersion
513}
514
515func (mod *Module) PreventInstall() bool {
516 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400517}
518
519func (mod *Module) HideFromMake() {
520 mod.Properties.HideFromMake = true
521}
522
523func (mod *Module) MarkAsCoverageVariant(coverage bool) {
524 mod.coverage.Properties.IsCoverageVariant = coverage
525}
526
527func (mod *Module) EnableCoverageIfNeeded() {
528 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700529}
530
531func defaultsFactory() android.Module {
532 return DefaultsFactory()
533}
534
535type Defaults struct {
536 android.ModuleBase
537 android.DefaultsModuleBase
538}
539
540func DefaultsFactory(props ...interface{}) android.Module {
541 module := &Defaults{}
542
543 module.AddProperties(props...)
544 module.AddProperties(
545 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500546 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100547 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400548 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700549 &BaseCompilerProperties{},
550 &BinaryCompilerProperties{},
551 &LibraryCompilerProperties{},
552 &ProcMacroCompilerProperties{},
553 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400554 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700555 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400556 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400557 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200558 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500559 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700560 )
561
562 android.InitDefaultsModule(module)
563 return module
564}
565
566func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700567 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700568}
569
Ivan Lozano183a3212019-10-18 14:18:45 -0700570func (mod *Module) CcLibrary() bool {
571 if mod.compiler != nil {
572 if _, ok := mod.compiler.(*libraryDecorator); ok {
573 return true
574 }
575 }
576 return false
577}
578
579func (mod *Module) CcLibraryInterface() bool {
580 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400581 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
582 // VariantIs{Static,Shared} is set.
583 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700584 return true
585 }
586 }
587 return false
588}
589
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800590func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700591 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700592 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800593 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700594 }
595 }
596 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
597}
598
599func (mod *Module) SetStatic() {
600 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700601 if library, ok := mod.compiler.(libraryInterface); ok {
602 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700603 return
604 }
605 }
606 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
607}
608
609func (mod *Module) SetShared() {
610 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700611 if library, ok := mod.compiler.(libraryInterface); ok {
612 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700613 return
614 }
615 }
616 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
617}
618
Ivan Lozano183a3212019-10-18 14:18:45 -0700619func (mod *Module) BuildStaticVariant() bool {
620 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700621 if library, ok := mod.compiler.(libraryInterface); ok {
622 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700623 }
624 }
625 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
626}
627
628func (mod *Module) BuildSharedVariant() bool {
629 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700630 if library, ok := mod.compiler.(libraryInterface); ok {
631 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700632 }
633 }
634 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
635}
636
Ivan Lozano183a3212019-10-18 14:18:45 -0700637func (mod *Module) Module() android.Module {
638 return mod
639}
640
Ivan Lozano183a3212019-10-18 14:18:45 -0700641func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900642 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
643 return mod.compiler.strippedOutputFilePath()
644 }
645 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700646}
647
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400648func (mod *Module) CoverageFiles() android.Paths {
649 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800650 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400651 }
652 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
653}
654
Jiyong Park459feca2020-12-15 11:02:21 +0900655func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900656 if !mod.EverInstallable() {
657 return false
658 }
659
Jiyong Park459feca2020-12-15 11:02:21 +0900660 // The apex variant is not installable because it is included in the APEX and won't appear
661 // in the system partition as a standalone file.
662 if !apexInfo.IsForPlatform() {
663 return false
664 }
665
Jiyong Parke54f07e2021-04-07 15:08:04 +0900666 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900667}
668
Ivan Lozano183a3212019-10-18 14:18:45 -0700669var _ cc.LinkableInterface = (*Module)(nil)
670
Ivan Lozanoffee3342019-08-27 12:03:00 -0700671func (mod *Module) Init() android.Module {
672 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500673 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700674
675 if mod.compiler != nil {
676 mod.AddProperties(mod.compiler.compilerProps()...)
677 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400678 if mod.coverage != nil {
679 mod.AddProperties(mod.coverage.props()...)
680 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200681 if mod.clippy != nil {
682 mod.AddProperties(mod.clippy.props()...)
683 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400684 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700685 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400686 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500687 if mod.sanitize != nil {
688 mod.AddProperties(mod.sanitize.props()...)
689 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400690
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900692 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700693
694 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700695 return mod
696}
697
698func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
699 return &Module{
700 hod: hod,
701 multilib: multilib,
702 }
703}
704func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
705 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400706 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200707 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500708 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700709 return module
710}
711
712type ModuleContext interface {
713 android.ModuleContext
714 ModuleContextIntf
715}
716
717type BaseModuleContext interface {
718 android.BaseModuleContext
719 ModuleContextIntf
720}
721
722type DepsContext interface {
723 android.BottomUpMutatorContext
724 ModuleContextIntf
725}
726
727type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200728 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730}
731
732type depsContext struct {
733 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734}
735
736type moduleContext struct {
737 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700738}
739
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200740type baseModuleContext struct {
741 android.BaseModuleContext
742}
743
744func (ctx *moduleContext) RustModule() *Module {
745 return ctx.Module().(*Module)
746}
747
748func (ctx *moduleContext) toolchain() config.Toolchain {
749 return ctx.RustModule().toolchain(ctx)
750}
751
752func (ctx *depsContext) RustModule() *Module {
753 return ctx.Module().(*Module)
754}
755
756func (ctx *depsContext) toolchain() config.Toolchain {
757 return ctx.RustModule().toolchain(ctx)
758}
759
760func (ctx *baseModuleContext) RustModule() *Module {
761 return ctx.Module().(*Module)
762}
763
764func (ctx *baseModuleContext) toolchain() config.Toolchain {
765 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400766}
767
768func (mod *Module) nativeCoverage() bool {
769 return mod.compiler != nil && mod.compiler.nativeCoverage()
770}
771
Ivan Lozanod7586b62021-04-01 09:49:36 -0400772func (mod *Module) EverInstallable() bool {
773 return mod.compiler != nil &&
774 // Check to see whether the module is actually ever installable.
775 mod.compiler.everInstallable()
776}
777
778func (mod *Module) Installable() *bool {
779 return mod.Properties.Installable
780}
781
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
783 if mod.cachedToolchain == nil {
784 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
785 }
786 return mod.cachedToolchain
787}
788
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200789func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
790 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
791}
792
Ivan Lozanoffee3342019-08-27 12:03:00 -0700793func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
794}
795
796func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
797 ctx := &moduleContext{
798 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700799 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700800
Jiyong Park99644e92020-11-17 22:21:02 +0900801 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
802 if !apexInfo.IsForPlatform() {
803 mod.hideApexVariantFromMake = true
804 }
805
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500807 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
808
809 // Differentiate static libraries that are vendor available
810 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700811 if mod.InProduct() && !mod.OnlyInProduct() {
812 mod.Properties.SubName += cc.ProductSuffix
813 } else {
814 mod.Properties.SubName += cc.VendorSuffix
815 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500816 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
817 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800818 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
819 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500820 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700821
822 if !toolchain.Supported() {
823 // This toolchain's unsupported, there's nothing to do for this mod.
824 return
825 }
826
827 deps := mod.depsToPaths(ctx)
828 flags := Flags{
829 Toolchain: toolchain,
830 }
831
832 if mod.compiler != nil {
833 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400834 }
835 if mod.coverage != nil {
836 flags, deps = mod.coverage.flags(ctx, flags, deps)
837 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200838 if mod.clippy != nil {
839 flags, deps = mod.clippy.flags(ctx, flags, deps)
840 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500841 if mod.sanitize != nil {
842 flags, deps = mod.sanitize.flags(ctx, flags, deps)
843 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400844
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200845 // SourceProvider needs to call GenerateSource() before compiler calls
846 // compile() so it can provide the source. A SourceProvider has
847 // multiple variants (e.g. source, rlib, dylib). Only the "source"
848 // variant is responsible for effectively generating the source. The
849 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400850 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200851 if mod.compiler.(libraryInterface).source() {
852 mod.sourceProvider.GenerateSource(ctx, deps)
853 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
854 } else {
855 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
856 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700857 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200858 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400859 }
860
861 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100862 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900863 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900864 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200865 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900866
Dan Albert06feee92021-03-19 15:06:02 -0700867 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
868
Ivan Lozano1921e802021-05-20 13:39:16 -0400869 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
870 // RECOVERY_SNAPSHOT_VERSION is current.
871 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
872 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
873 lib.collectHeadersForSnapshot(ctx, deps)
874 }
875 }
876
Jiyong Park459feca2020-12-15 11:02:21 +0900877 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
878 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200879 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400880 }
Chris Wailes74be7642021-07-22 16:20:28 -0700881
882 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700883 }
884}
885
886func (mod *Module) deps(ctx DepsContext) Deps {
887 deps := Deps{}
888
889 if mod.compiler != nil {
890 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400891 }
892 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700893 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 }
895
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400896 if mod.coverage != nil {
897 deps = mod.coverage.deps(ctx, deps)
898 }
899
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500900 if mod.sanitize != nil {
901 deps = mod.sanitize.deps(ctx, deps)
902 }
903
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
905 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700906 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
908 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
909 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400910 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911 return deps
912
913}
914
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915type dependencyTag struct {
916 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800917 name string
918 library bool
919 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920}
921
Jiyong Park65b62242020-11-25 12:44:59 +0900922// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
923// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
924func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800925 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900926}
927
928var _ android.InstallNeededDependencyTag = dependencyTag{}
929
Ivan Lozanoffee3342019-08-27 12:03:00 -0700930var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400931 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
932 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
933 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800934 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400935 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200936 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700937)
938
Jiyong Park99644e92020-11-17 22:21:02 +0900939func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
940 tag, ok := depTag.(dependencyTag)
941 return ok && tag == dylibDepTag
942}
943
Jiyong Park94e22fd2021-04-08 18:19:15 +0900944func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
945 tag, ok := depTag.(dependencyTag)
946 return ok && tag == rlibDepTag
947}
948
Matthew Maurer0f003b12020-06-29 14:34:06 -0700949type autoDep struct {
950 variation string
951 depTag dependencyTag
952}
953
954var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200955 rlibVariation = "rlib"
956 dylibVariation = "dylib"
957 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
958 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700959)
960
961type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500962 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700963}
964
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400965func (mod *Module) begin(ctx BaseModuleContext) {
966 if mod.coverage != nil {
967 mod.coverage.begin(ctx)
968 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500969 if mod.sanitize != nil {
970 mod.sanitize.begin(ctx)
971 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400972}
973
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
975 var depPaths PathDeps
976
977 directRlibDeps := []*Module{}
978 directDylibDeps := []*Module{}
979 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900980 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700981 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400982 directSrcProvidersDeps := []*Module{}
983 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700984
985 ctx.VisitDirectDeps(func(dep android.Module) {
986 depName := ctx.OtherModuleName(dep)
987 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400988
Ivan Lozano89435d12020-07-31 11:01:18 -0400989 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700990 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400991 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700992
Ivan Lozanoffee3342019-08-27 12:03:00 -0700993 switch depTag {
994 case dylibDepTag:
995 dylib, ok := rustDep.compiler.(libraryInterface)
996 if !ok || !dylib.dylib() {
997 ctx.ModuleErrorf("mod %q not an dylib library", depName)
998 return
999 }
1000 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001001 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001002 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001003
Ivan Lozanoffee3342019-08-27 12:03:00 -07001004 rlib, ok := rustDep.compiler.(libraryInterface)
1005 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001006 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001007 return
1008 }
1009 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001010 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001011 case procMacroDepTag:
1012 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001013 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001014 }
1015
1016 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001017 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1018 // OS/Arch variant is used.
1019 var helper string
1020 if ctx.Host() {
1021 helper = "missing 'host_supported'?"
1022 } else {
1023 helper = "device module defined?"
1024 }
1025
1026 if dep.Target().Os != ctx.Os() {
1027 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1028 return
1029 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1030 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1031 return
1032 }
1033 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034 }
1035
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001036 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001037 if depTag != procMacroDepTag {
1038 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1039 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1040 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1041 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042 }
1043
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001045 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001046 if !linkFile.Valid() {
1047 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1048 depName, ctx.ModuleName())
1049 return
1050 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001051 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001052 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1053 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001054 }
1055 }
1056
Ivan Lozano89435d12020-07-31 11:01:18 -04001057 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001058 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001059 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001060 if _, ok := ccDep.(*Module); !ok {
1061 if ccDep.Module().Target().Os != ctx.Os() {
1062 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1063 return
1064 }
1065 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1066 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1067 return
1068 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001069 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001070 linkObject := ccDep.OutputFile()
1071 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001072
Ivan Lozano2093af22020-08-25 12:48:19 -04001073 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001074 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1075 }
1076
1077 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001078 switch {
1079 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001080 if cc.IsWholeStaticLib(depTag) {
1081 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1082 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001083 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1084 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001085 } else {
1086 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 -05001087 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001088 }
1089
1090 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1091 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001092 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001093 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1094
Colin Cross0de8a1e2020-09-18 14:15:30 -07001095 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1096 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1097 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1098 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1099 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001100 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001101 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001102 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001103 // For the shared lib dependencies, we may link to the stub variant
1104 // of the dependency depending on the context (e.g. if this
1105 // dependency crosses the APEX boundaries).
1106 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1107
1108 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1109 // object (either from stub or non-stub) to use.
1110 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1111 linkPath = linkPathFromFilePath(linkObject.Path())
1112
Ivan Lozanoffee3342019-08-27 12:03:00 -07001113 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001114 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001115 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1116 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1117 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1118 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001119 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001120
1121 // Record baseLibName for snapshots.
1122 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001123 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001124
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001125 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001126 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001127 case cc.IsHeaderDepTag(depTag):
1128 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1129 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1130 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1131 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001132 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001133 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001134 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001135 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001136 }
1137
1138 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001139 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1140 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001141 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001142 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001143 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001144
1145 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001146 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001147 // These are usually genrules which don't have per-target variants.
1148 directSrcDeps = append(directSrcDeps, srcDep)
1149 }
1150 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001151 })
1152
1153 var rlibDepFiles RustLibraries
1154 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001155 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001156 }
1157 var dylibDepFiles RustLibraries
1158 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001159 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001160 }
1161 var procMacroDepFiles RustLibraries
1162 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001163 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001164 }
1165
1166 var staticLibDepFiles android.Paths
1167 for _, dep := range directStaticLibDeps {
1168 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1169 }
1170
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001171 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001172 var sharedLibDepFiles android.Paths
1173 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001174 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1175 if dep.TableOfContents.Valid() {
1176 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001177 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001178 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001179 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001180 }
1181
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001182 var srcProviderDepFiles android.Paths
1183 for _, dep := range directSrcProvidersDeps {
1184 srcs, _ := dep.OutputFiles("")
1185 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1186 }
1187 for _, dep := range directSrcDeps {
1188 srcs := dep.Srcs()
1189 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1190 }
1191
Ivan Lozanoffee3342019-08-27 12:03:00 -07001192 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1193 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1194 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001195 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001196 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1197 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001198 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001199
1200 // Dedup exported flags from dependencies
1201 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001202 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001203 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001204 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1205 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1206 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207
1208 return depPaths
1209}
1210
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001211func (mod *Module) InstallInData() bool {
1212 if mod.compiler == nil {
1213 return false
1214 }
1215 return mod.compiler.inData()
1216}
1217
Ivan Lozanoffee3342019-08-27 12:03:00 -07001218func linkPathFromFilePath(filepath android.Path) string {
1219 return strings.Split(filepath.String(), filepath.Base())[0]
1220}
Ivan Lozanod648c432020-02-06 12:05:10 -05001221
Ivan Lozanoffee3342019-08-27 12:03:00 -07001222func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1223 ctx := &depsContext{
1224 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001225 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001226
1227 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001228 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001229 var snapshotInfo *cc.SnapshotInfo
1230
1231 if ctx.Os() == android.Android {
1232 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1233 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001234
Ivan Lozano2b081132020-09-08 12:46:52 -04001235 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001236 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001237 stdLinkage = "rlib-std"
1238 }
1239
1240 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001241
Ivan Lozano2b081132020-09-08 12:46:52 -04001242 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1243 rlibDepVariations = append(rlibDepVariations,
1244 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1245 }
1246
Ivan Lozano1921e802021-05-20 13:39:16 -04001247 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001248 for _, lib := range deps.Rlibs {
1249 depTag := rlibDepTag
1250 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1251
1252 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1253 {Mutator: "rust_libraries", Variation: rlibVariation},
1254 }...), depTag, lib)
1255 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001256
1257 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001258 actx.AddVariationDependencies(
1259 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001260 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001261 dylibDepTag, deps.Dylibs...)
1262
Ivan Lozano1921e802021-05-20 13:39:16 -04001263 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001264 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1265 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001266 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001267 for _, lib := range deps.Rustlibs {
1268 depTag := autoDep.depTag
1269 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1270 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1271 {Mutator: "rust_libraries", Variation: autoDep.variation},
1272 }...), depTag, lib)
1273 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001274 } else {
1275 actx.AddVariationDependencies(
1276 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1277 autoDep.depTag, deps.Rustlibs...)
1278 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001279 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001280
1281 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001282 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001283 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001284 for _, lib := range deps.Stdlibs {
1285 depTag := rlibDepTag
1286 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1287
1288 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1289 depTag, lib)
1290 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001291 } else {
1292 actx.AddVariationDependencies(
1293 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1294 dylibDepTag, deps.Stdlibs...)
1295 }
1296 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001297
1298 for _, lib := range deps.SharedLibs {
1299 depTag := cc.SharedDepTag()
1300 name, version := cc.StubsLibNameAndVersion(lib)
1301
1302 variations := []blueprint.Variation{
1303 {Mutator: "link", Variation: "shared"},
1304 }
1305 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1306 }
1307
1308 for _, lib := range deps.WholeStaticLibs {
1309 depTag := cc.StaticDepTag(true)
1310 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1311
1312 actx.AddVariationDependencies([]blueprint.Variation{
1313 {Mutator: "link", Variation: "static"},
1314 }, depTag, lib)
1315 }
1316
1317 for _, lib := range deps.StaticLibs {
1318 depTag := cc.StaticDepTag(false)
1319 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1320
1321 actx.AddVariationDependencies([]blueprint.Variation{
1322 {Mutator: "link", Variation: "static"},
1323 }, depTag, lib)
1324 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001325
Zach Johnson3df4e632020-11-06 11:56:27 -08001326 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1327
Colin Cross565cafd2020-09-25 18:47:38 -07001328 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001329 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001330 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1331 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001332 }
1333 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001334 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1335 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001336 }
1337
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001338 if mod.sourceProvider != nil {
1339 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1340 bindgen.Properties.Custom_bindgen != "" {
1341 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1342 bindgen.Properties.Custom_bindgen)
1343 }
1344 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001345
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001346 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001347 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001348}
1349
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001350func BeginMutator(ctx android.BottomUpMutatorContext) {
1351 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1352 mod.beginMutator(ctx)
1353 }
1354}
1355
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001356func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1357 ctx := &baseModuleContext{
1358 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001359 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001360
1361 mod.begin(ctx)
1362}
1363
Ivan Lozanoffee3342019-08-27 12:03:00 -07001364func (mod *Module) Name() string {
1365 name := mod.ModuleBase.Name()
1366 if p, ok := mod.compiler.(interface {
1367 Name(string) string
1368 }); ok {
1369 name = p.Name(name)
1370 }
1371 return name
1372}
1373
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001374func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001375 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001376 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001377 }
1378}
1379
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001380var _ android.HostToolProvider = (*Module)(nil)
1381
1382func (mod *Module) HostToolPath() android.OptionalPath {
1383 if !mod.Host() {
1384 return android.OptionalPath{}
1385 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001386 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1387 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001388 }
1389 return android.OptionalPath{}
1390}
1391
Jiyong Park99644e92020-11-17 22:21:02 +09001392var _ android.ApexModule = (*Module)(nil)
1393
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001394func (mod *Module) minSdkVersion() string {
1395 return String(mod.Properties.Min_sdk_version)
1396}
1397
Jiyong Park45bf82e2020-12-15 22:29:02 +09001398var _ android.ApexModule = (*Module)(nil)
1399
1400// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001401func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001402 minSdkVersion := mod.minSdkVersion()
1403 if minSdkVersion == "apex_inherit" {
1404 return nil
1405 }
1406 if minSdkVersion == "" {
1407 return fmt.Errorf("min_sdk_version is not specificed")
1408 }
1409
1410 // Not using nativeApiLevelFromUser because the context here is not
1411 // necessarily a native context.
1412 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1413 if err != nil {
1414 return err
1415 }
1416
1417 if ver.GreaterThan(sdkVersion) {
1418 return fmt.Errorf("newer SDK(%v)", ver)
1419 }
Jiyong Park99644e92020-11-17 22:21:02 +09001420 return nil
1421}
1422
Jiyong Park45bf82e2020-12-15 22:29:02 +09001423// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001424func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1425 depTag := ctx.OtherModuleDependencyTag(dep)
1426
1427 if ccm, ok := dep.(*cc.Module); ok {
1428 if ccm.HasStubsVariants() {
1429 if cc.IsSharedDepTag(depTag) {
1430 // dynamic dep to a stubs lib crosses APEX boundary
1431 return false
1432 }
1433 if cc.IsRuntimeDepTag(depTag) {
1434 // runtime dep to a stubs lib also crosses APEX boundary
1435 return false
1436 }
1437
1438 if cc.IsHeaderDepTag(depTag) {
1439 return false
1440 }
1441 }
1442 if mod.Static() && cc.IsSharedDepTag(depTag) {
1443 // shared_lib dependency from a static lib is considered as crossing
1444 // the APEX boundary because the dependency doesn't actually is
1445 // linked; the dependency is used only during the compilation phase.
1446 return false
1447 }
1448 }
1449
1450 if depTag == procMacroDepTag {
1451 return false
1452 }
1453
1454 return true
1455}
1456
1457// Overrides ApexModule.IsInstallabeToApex()
1458func (mod *Module) IsInstallableToApex() bool {
1459 if mod.compiler != nil {
1460 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1461 return true
1462 }
1463 if _, ok := mod.compiler.(*binaryDecorator); ok {
1464 return true
1465 }
1466 }
1467 return false
1468}
1469
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001470// If a library file has a "lib" prefix, extract the library name without the prefix.
1471func libNameFromFilePath(filepath android.Path) (string, bool) {
1472 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1473 if strings.HasPrefix(libName, "lib") {
1474 libName = libName[3:]
1475 return libName, true
1476 }
1477 return "", false
1478}
1479
Ivan Lozanoffee3342019-08-27 12:03:00 -07001480var Bool = proptools.Bool
1481var BoolDefault = proptools.BoolDefault
1482var String = proptools.String
1483var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001484
1485var _ android.OutputFileProducer = (*Module)(nil)