blob: 70721fb2c1544d6bdddcd4e102394dd6483736f9 [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 {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700769 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
770 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
771 return false
772 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400773 return mod.compiler != nil && mod.compiler.nativeCoverage()
774}
775
Ivan Lozanod7586b62021-04-01 09:49:36 -0400776func (mod *Module) EverInstallable() bool {
777 return mod.compiler != nil &&
778 // Check to see whether the module is actually ever installable.
779 mod.compiler.everInstallable()
780}
781
782func (mod *Module) Installable() *bool {
783 return mod.Properties.Installable
784}
785
Ivan Lozanoffee3342019-08-27 12:03:00 -0700786func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
787 if mod.cachedToolchain == nil {
788 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
789 }
790 return mod.cachedToolchain
791}
792
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200793func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
794 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
795}
796
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
798}
799
800func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
801 ctx := &moduleContext{
802 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700803 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700804
Jiyong Park99644e92020-11-17 22:21:02 +0900805 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
806 if !apexInfo.IsForPlatform() {
807 mod.hideApexVariantFromMake = true
808 }
809
Ivan Lozanoffee3342019-08-27 12:03:00 -0700810 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500811 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
812
813 // Differentiate static libraries that are vendor available
814 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700815 if mod.InProduct() && !mod.OnlyInProduct() {
816 mod.Properties.SubName += cc.ProductSuffix
817 } else {
818 mod.Properties.SubName += cc.VendorSuffix
819 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500820 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
821 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800822 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
823 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500824 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700825
Matthew Maurera61e31f2021-05-27 11:09:11 -0700826 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
827 mod.Properties.SubName += cc.NativeBridgeSuffix
828 }
829
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 if !toolchain.Supported() {
831 // This toolchain's unsupported, there's nothing to do for this mod.
832 return
833 }
834
835 deps := mod.depsToPaths(ctx)
836 flags := Flags{
837 Toolchain: toolchain,
838 }
839
840 if mod.compiler != nil {
841 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400842 }
843 if mod.coverage != nil {
844 flags, deps = mod.coverage.flags(ctx, flags, deps)
845 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200846 if mod.clippy != nil {
847 flags, deps = mod.clippy.flags(ctx, flags, deps)
848 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500849 if mod.sanitize != nil {
850 flags, deps = mod.sanitize.flags(ctx, flags, deps)
851 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400852
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200853 // SourceProvider needs to call GenerateSource() before compiler calls
854 // compile() so it can provide the source. A SourceProvider has
855 // multiple variants (e.g. source, rlib, dylib). Only the "source"
856 // variant is responsible for effectively generating the source. The
857 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400858 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200859 if mod.compiler.(libraryInterface).source() {
860 mod.sourceProvider.GenerateSource(ctx, deps)
861 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
862 } else {
863 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
864 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700865 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200866 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400867 }
868
869 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100870 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900871 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900872 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200873 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900874
Dan Albert06feee92021-03-19 15:06:02 -0700875 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
876
Ivan Lozano1921e802021-05-20 13:39:16 -0400877 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
878 // RECOVERY_SNAPSHOT_VERSION is current.
879 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
880 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
881 lib.collectHeadersForSnapshot(ctx, deps)
882 }
883 }
884
Jiyong Park459feca2020-12-15 11:02:21 +0900885 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
886 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200887 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400888 }
Chris Wailes74be7642021-07-22 16:20:28 -0700889
890 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 }
892}
893
894func (mod *Module) deps(ctx DepsContext) Deps {
895 deps := Deps{}
896
897 if mod.compiler != nil {
898 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400899 }
900 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700901 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700902 }
903
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400904 if mod.coverage != nil {
905 deps = mod.coverage.deps(ctx, deps)
906 }
907
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500908 if mod.sanitize != nil {
909 deps = mod.sanitize.deps(ctx, deps)
910 }
911
Ivan Lozanoffee3342019-08-27 12:03:00 -0700912 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
913 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700914 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700915 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
916 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
917 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400918 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700919 return deps
920
921}
922
Ivan Lozanoffee3342019-08-27 12:03:00 -0700923type dependencyTag struct {
924 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800925 name string
926 library bool
927 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700928}
929
Jiyong Park65b62242020-11-25 12:44:59 +0900930// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
931// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
932func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800933 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900934}
935
936var _ android.InstallNeededDependencyTag = dependencyTag{}
937
Ivan Lozanoffee3342019-08-27 12:03:00 -0700938var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400939 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
940 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
941 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800942 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400943 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200944 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700945)
946
Jiyong Park99644e92020-11-17 22:21:02 +0900947func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
948 tag, ok := depTag.(dependencyTag)
949 return ok && tag == dylibDepTag
950}
951
Jiyong Park94e22fd2021-04-08 18:19:15 +0900952func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
953 tag, ok := depTag.(dependencyTag)
954 return ok && tag == rlibDepTag
955}
956
Matthew Maurer0f003b12020-06-29 14:34:06 -0700957type autoDep struct {
958 variation string
959 depTag dependencyTag
960}
961
962var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200963 rlibVariation = "rlib"
964 dylibVariation = "dylib"
965 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
966 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700967)
968
969type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500970 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700971}
972
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400973func (mod *Module) begin(ctx BaseModuleContext) {
974 if mod.coverage != nil {
975 mod.coverage.begin(ctx)
976 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500977 if mod.sanitize != nil {
978 mod.sanitize.begin(ctx)
979 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400980}
981
Ivan Lozanoffee3342019-08-27 12:03:00 -0700982func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
983 var depPaths PathDeps
984
985 directRlibDeps := []*Module{}
986 directDylibDeps := []*Module{}
987 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900988 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700989 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400990 directSrcProvidersDeps := []*Module{}
991 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700992
993 ctx.VisitDirectDeps(func(dep android.Module) {
994 depName := ctx.OtherModuleName(dep)
995 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400996
Ivan Lozano89435d12020-07-31 11:01:18 -0400997 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400999 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001000
Ivan Lozanoffee3342019-08-27 12:03:00 -07001001 switch depTag {
1002 case dylibDepTag:
1003 dylib, ok := rustDep.compiler.(libraryInterface)
1004 if !ok || !dylib.dylib() {
1005 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1006 return
1007 }
1008 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001009 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001010 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001011
Ivan Lozanoffee3342019-08-27 12:03:00 -07001012 rlib, ok := rustDep.compiler.(libraryInterface)
1013 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001014 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001015 return
1016 }
1017 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001018 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001019 case procMacroDepTag:
1020 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001021 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001022 }
1023
1024 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001025 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1026 // OS/Arch variant is used.
1027 var helper string
1028 if ctx.Host() {
1029 helper = "missing 'host_supported'?"
1030 } else {
1031 helper = "device module defined?"
1032 }
1033
1034 if dep.Target().Os != ctx.Os() {
1035 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1036 return
1037 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1038 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1039 return
1040 }
1041 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042 }
1043
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001044 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001045 if depTag != procMacroDepTag {
1046 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1047 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1048 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1049 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050 }
1051
Ivan Lozanoffee3342019-08-27 12:03:00 -07001052 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001053 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001054 if !linkFile.Valid() {
1055 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1056 depName, ctx.ModuleName())
1057 return
1058 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001059 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001060 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1061 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001062 }
1063 }
1064
Ivan Lozano89435d12020-07-31 11:01:18 -04001065 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001066 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001067 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001068 if _, ok := ccDep.(*Module); !ok {
1069 if ccDep.Module().Target().Os != ctx.Os() {
1070 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1071 return
1072 }
1073 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1074 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1075 return
1076 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001077 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001078 linkObject := ccDep.OutputFile()
1079 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001080
Ivan Lozano2093af22020-08-25 12:48:19 -04001081 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001082 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1083 }
1084
1085 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001086 switch {
1087 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001088 if cc.IsWholeStaticLib(depTag) {
1089 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1090 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001091 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1092 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001093 } else {
1094 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 -05001095 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001096 }
1097
1098 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1099 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001100 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001101 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1102
Colin Cross0de8a1e2020-09-18 14:15:30 -07001103 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1104 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...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001108 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001109 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001110 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001111 // For the shared lib dependencies, we may link to the stub variant
1112 // of the dependency depending on the context (e.g. if this
1113 // dependency crosses the APEX boundaries).
1114 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1115
1116 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1117 // object (either from stub or non-stub) to use.
1118 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1119 linkPath = linkPathFromFilePath(linkObject.Path())
1120
Ivan Lozanoffee3342019-08-27 12:03:00 -07001121 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001122 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001123 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1124 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1125 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1126 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001127 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001128
1129 // Record baseLibName for snapshots.
1130 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001131 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001132
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001133 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001135 case cc.IsHeaderDepTag(depTag):
1136 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1137 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1138 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1139 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001140 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001141 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001142 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001143 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001144 }
1145
1146 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001147 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1148 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001149 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001150 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001151 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001152
1153 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001154 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001155 // These are usually genrules which don't have per-target variants.
1156 directSrcDeps = append(directSrcDeps, srcDep)
1157 }
1158 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001159 })
1160
1161 var rlibDepFiles RustLibraries
1162 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001163 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001164 }
1165 var dylibDepFiles RustLibraries
1166 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001167 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001168 }
1169 var procMacroDepFiles RustLibraries
1170 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001171 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001172 }
1173
1174 var staticLibDepFiles android.Paths
1175 for _, dep := range directStaticLibDeps {
1176 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1177 }
1178
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001179 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001180 var sharedLibDepFiles android.Paths
1181 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001182 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1183 if dep.TableOfContents.Valid() {
1184 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001185 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001186 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001187 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001188 }
1189
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001190 var srcProviderDepFiles android.Paths
1191 for _, dep := range directSrcProvidersDeps {
1192 srcs, _ := dep.OutputFiles("")
1193 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1194 }
1195 for _, dep := range directSrcDeps {
1196 srcs := dep.Srcs()
1197 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1198 }
1199
Ivan Lozanoffee3342019-08-27 12:03:00 -07001200 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1201 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1202 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001203 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001204 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1205 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001206 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207
1208 // Dedup exported flags from dependencies
1209 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001210 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001211 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001212 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1213 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1214 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001215
1216 return depPaths
1217}
1218
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001219func (mod *Module) InstallInData() bool {
1220 if mod.compiler == nil {
1221 return false
1222 }
1223 return mod.compiler.inData()
1224}
1225
Ivan Lozanoffee3342019-08-27 12:03:00 -07001226func linkPathFromFilePath(filepath android.Path) string {
1227 return strings.Split(filepath.String(), filepath.Base())[0]
1228}
Ivan Lozanod648c432020-02-06 12:05:10 -05001229
Ivan Lozanoffee3342019-08-27 12:03:00 -07001230func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1231 ctx := &depsContext{
1232 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001233 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001234
1235 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001236 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001237 var snapshotInfo *cc.SnapshotInfo
1238
1239 if ctx.Os() == android.Android {
1240 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1241 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001242
Ivan Lozano2b081132020-09-08 12:46:52 -04001243 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001244 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001245 stdLinkage = "rlib-std"
1246 }
1247
1248 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001249
Ivan Lozano2b081132020-09-08 12:46:52 -04001250 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1251 rlibDepVariations = append(rlibDepVariations,
1252 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1253 }
1254
Ivan Lozano1921e802021-05-20 13:39:16 -04001255 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001256 for _, lib := range deps.Rlibs {
1257 depTag := rlibDepTag
1258 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1259
1260 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1261 {Mutator: "rust_libraries", Variation: rlibVariation},
1262 }...), depTag, lib)
1263 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001264
1265 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001266 actx.AddVariationDependencies(
1267 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001268 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001269 dylibDepTag, deps.Dylibs...)
1270
Ivan Lozano1921e802021-05-20 13:39:16 -04001271 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001272 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1273 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001274 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001275 for _, lib := range deps.Rustlibs {
1276 depTag := autoDep.depTag
1277 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1278 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1279 {Mutator: "rust_libraries", Variation: autoDep.variation},
1280 }...), depTag, lib)
1281 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001282 } else {
1283 actx.AddVariationDependencies(
1284 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1285 autoDep.depTag, deps.Rustlibs...)
1286 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001287 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001288
1289 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001290 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001291 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001292 for _, lib := range deps.Stdlibs {
1293 depTag := rlibDepTag
1294 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1295
1296 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1297 depTag, lib)
1298 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001299 } else {
1300 actx.AddVariationDependencies(
1301 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1302 dylibDepTag, deps.Stdlibs...)
1303 }
1304 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001305
1306 for _, lib := range deps.SharedLibs {
1307 depTag := cc.SharedDepTag()
1308 name, version := cc.StubsLibNameAndVersion(lib)
1309
1310 variations := []blueprint.Variation{
1311 {Mutator: "link", Variation: "shared"},
1312 }
1313 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1314 }
1315
1316 for _, lib := range deps.WholeStaticLibs {
1317 depTag := cc.StaticDepTag(true)
1318 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1319
1320 actx.AddVariationDependencies([]blueprint.Variation{
1321 {Mutator: "link", Variation: "static"},
1322 }, depTag, lib)
1323 }
1324
1325 for _, lib := range deps.StaticLibs {
1326 depTag := cc.StaticDepTag(false)
1327 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1328
1329 actx.AddVariationDependencies([]blueprint.Variation{
1330 {Mutator: "link", Variation: "static"},
1331 }, depTag, lib)
1332 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001333
Zach Johnson3df4e632020-11-06 11:56:27 -08001334 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1335
Colin Cross565cafd2020-09-25 18:47:38 -07001336 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001337 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001338 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1339 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001340 }
1341 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001342 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1343 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001344 }
1345
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001346 if mod.sourceProvider != nil {
1347 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1348 bindgen.Properties.Custom_bindgen != "" {
1349 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1350 bindgen.Properties.Custom_bindgen)
1351 }
1352 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001353
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001354 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001355 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001356}
1357
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001358func BeginMutator(ctx android.BottomUpMutatorContext) {
1359 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1360 mod.beginMutator(ctx)
1361 }
1362}
1363
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001364func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1365 ctx := &baseModuleContext{
1366 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001367 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001368
1369 mod.begin(ctx)
1370}
1371
Ivan Lozanoffee3342019-08-27 12:03:00 -07001372func (mod *Module) Name() string {
1373 name := mod.ModuleBase.Name()
1374 if p, ok := mod.compiler.(interface {
1375 Name(string) string
1376 }); ok {
1377 name = p.Name(name)
1378 }
1379 return name
1380}
1381
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001382func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001383 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001384 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001385 }
1386}
1387
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001388var _ android.HostToolProvider = (*Module)(nil)
1389
1390func (mod *Module) HostToolPath() android.OptionalPath {
1391 if !mod.Host() {
1392 return android.OptionalPath{}
1393 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001394 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1395 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001396 }
1397 return android.OptionalPath{}
1398}
1399
Jiyong Park99644e92020-11-17 22:21:02 +09001400var _ android.ApexModule = (*Module)(nil)
1401
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001402func (mod *Module) minSdkVersion() string {
1403 return String(mod.Properties.Min_sdk_version)
1404}
1405
Jiyong Park45bf82e2020-12-15 22:29:02 +09001406var _ android.ApexModule = (*Module)(nil)
1407
1408// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001409func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001410 minSdkVersion := mod.minSdkVersion()
1411 if minSdkVersion == "apex_inherit" {
1412 return nil
1413 }
1414 if minSdkVersion == "" {
1415 return fmt.Errorf("min_sdk_version is not specificed")
1416 }
1417
1418 // Not using nativeApiLevelFromUser because the context here is not
1419 // necessarily a native context.
1420 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1421 if err != nil {
1422 return err
1423 }
1424
1425 if ver.GreaterThan(sdkVersion) {
1426 return fmt.Errorf("newer SDK(%v)", ver)
1427 }
Jiyong Park99644e92020-11-17 22:21:02 +09001428 return nil
1429}
1430
Jiyong Park45bf82e2020-12-15 22:29:02 +09001431// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001432func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1433 depTag := ctx.OtherModuleDependencyTag(dep)
1434
1435 if ccm, ok := dep.(*cc.Module); ok {
1436 if ccm.HasStubsVariants() {
1437 if cc.IsSharedDepTag(depTag) {
1438 // dynamic dep to a stubs lib crosses APEX boundary
1439 return false
1440 }
1441 if cc.IsRuntimeDepTag(depTag) {
1442 // runtime dep to a stubs lib also crosses APEX boundary
1443 return false
1444 }
1445
1446 if cc.IsHeaderDepTag(depTag) {
1447 return false
1448 }
1449 }
1450 if mod.Static() && cc.IsSharedDepTag(depTag) {
1451 // shared_lib dependency from a static lib is considered as crossing
1452 // the APEX boundary because the dependency doesn't actually is
1453 // linked; the dependency is used only during the compilation phase.
1454 return false
1455 }
1456 }
1457
1458 if depTag == procMacroDepTag {
1459 return false
1460 }
1461
1462 return true
1463}
1464
1465// Overrides ApexModule.IsInstallabeToApex()
1466func (mod *Module) IsInstallableToApex() bool {
1467 if mod.compiler != nil {
1468 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1469 return true
1470 }
1471 if _, ok := mod.compiler.(*binaryDecorator); ok {
1472 return true
1473 }
1474 }
1475 return false
1476}
1477
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001478// If a library file has a "lib" prefix, extract the library name without the prefix.
1479func libNameFromFilePath(filepath android.Path) (string, bool) {
1480 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1481 if strings.HasPrefix(libName, "lib") {
1482 libName = libName[3:]
1483 return libName, true
1484 }
1485 return "", false
1486}
1487
Ivan Lozanoffee3342019-08-27 12:03:00 -07001488var Bool = proptools.Bool
1489var BoolDefault = proptools.BoolDefault
1490var String = proptools.String
1491var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001492
1493var _ android.OutputFileProducer = (*Module)(nil)