blob: 79a5febf0208426d62278d76a507389a6a798894 [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() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500811 mod.Properties.SubName += cc.VendorSuffix
812 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
813 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800814 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
815 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500816 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817
818 if !toolchain.Supported() {
819 // This toolchain's unsupported, there's nothing to do for this mod.
820 return
821 }
822
823 deps := mod.depsToPaths(ctx)
824 flags := Flags{
825 Toolchain: toolchain,
826 }
827
828 if mod.compiler != nil {
829 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400830 }
831 if mod.coverage != nil {
832 flags, deps = mod.coverage.flags(ctx, flags, deps)
833 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200834 if mod.clippy != nil {
835 flags, deps = mod.clippy.flags(ctx, flags, deps)
836 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500837 if mod.sanitize != nil {
838 flags, deps = mod.sanitize.flags(ctx, flags, deps)
839 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400840
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200841 // SourceProvider needs to call GenerateSource() before compiler calls
842 // compile() so it can provide the source. A SourceProvider has
843 // multiple variants (e.g. source, rlib, dylib). Only the "source"
844 // variant is responsible for effectively generating the source. The
845 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400846 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200847 if mod.compiler.(libraryInterface).source() {
848 mod.sourceProvider.GenerateSource(ctx, deps)
849 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
850 } else {
851 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
852 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700853 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200854 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400855 }
856
857 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100858 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900859 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900860 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200861 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900862
Dan Albert06feee92021-03-19 15:06:02 -0700863 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
864
Ivan Lozano1921e802021-05-20 13:39:16 -0400865 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
866 // RECOVERY_SNAPSHOT_VERSION is current.
867 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
868 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
869 lib.collectHeadersForSnapshot(ctx, deps)
870 }
871 }
872
Jiyong Park459feca2020-12-15 11:02:21 +0900873 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
874 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200875 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400876 }
Chris Wailes74be7642021-07-22 16:20:28 -0700877
878 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700879 }
880}
881
882func (mod *Module) deps(ctx DepsContext) Deps {
883 deps := Deps{}
884
885 if mod.compiler != nil {
886 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400887 }
888 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700889 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700890 }
891
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400892 if mod.coverage != nil {
893 deps = mod.coverage.deps(ctx, deps)
894 }
895
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500896 if mod.sanitize != nil {
897 deps = mod.sanitize.deps(ctx, deps)
898 }
899
Ivan Lozanoffee3342019-08-27 12:03:00 -0700900 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
901 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700902 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700903 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
904 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
905 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400906 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700907 return deps
908
909}
910
Ivan Lozanoffee3342019-08-27 12:03:00 -0700911type dependencyTag struct {
912 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800913 name string
914 library bool
915 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700916}
917
Jiyong Park65b62242020-11-25 12:44:59 +0900918// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
919// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
920func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800921 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900922}
923
924var _ android.InstallNeededDependencyTag = dependencyTag{}
925
Ivan Lozanoffee3342019-08-27 12:03:00 -0700926var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400927 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
928 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
929 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800930 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400931 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200932 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700933)
934
Jiyong Park99644e92020-11-17 22:21:02 +0900935func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
936 tag, ok := depTag.(dependencyTag)
937 return ok && tag == dylibDepTag
938}
939
Jiyong Park94e22fd2021-04-08 18:19:15 +0900940func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
941 tag, ok := depTag.(dependencyTag)
942 return ok && tag == rlibDepTag
943}
944
Matthew Maurer0f003b12020-06-29 14:34:06 -0700945type autoDep struct {
946 variation string
947 depTag dependencyTag
948}
949
950var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200951 rlibVariation = "rlib"
952 dylibVariation = "dylib"
953 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
954 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700955)
956
957type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500958 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700959}
960
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400961func (mod *Module) begin(ctx BaseModuleContext) {
962 if mod.coverage != nil {
963 mod.coverage.begin(ctx)
964 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500965 if mod.sanitize != nil {
966 mod.sanitize.begin(ctx)
967 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400968}
969
Ivan Lozanoffee3342019-08-27 12:03:00 -0700970func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
971 var depPaths PathDeps
972
973 directRlibDeps := []*Module{}
974 directDylibDeps := []*Module{}
975 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900976 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700977 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400978 directSrcProvidersDeps := []*Module{}
979 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700980
981 ctx.VisitDirectDeps(func(dep android.Module) {
982 depName := ctx.OtherModuleName(dep)
983 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400984
Ivan Lozano89435d12020-07-31 11:01:18 -0400985 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400987 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700988
Ivan Lozanoffee3342019-08-27 12:03:00 -0700989 switch depTag {
990 case dylibDepTag:
991 dylib, ok := rustDep.compiler.(libraryInterface)
992 if !ok || !dylib.dylib() {
993 ctx.ModuleErrorf("mod %q not an dylib library", depName)
994 return
995 }
996 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400997 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700998 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400999
Ivan Lozanoffee3342019-08-27 12:03:00 -07001000 rlib, ok := rustDep.compiler.(libraryInterface)
1001 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001002 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001003 return
1004 }
1005 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001006 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001007 case procMacroDepTag:
1008 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001009 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001010 }
1011
1012 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001013 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1014 // OS/Arch variant is used.
1015 var helper string
1016 if ctx.Host() {
1017 helper = "missing 'host_supported'?"
1018 } else {
1019 helper = "device module defined?"
1020 }
1021
1022 if dep.Target().Os != ctx.Os() {
1023 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1024 return
1025 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1026 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1027 return
1028 }
1029 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001030 }
1031
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001032 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001033 if depTag != procMacroDepTag {
1034 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1035 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1036 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1037 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038 }
1039
Ivan Lozanoffee3342019-08-27 12:03:00 -07001040 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001041 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001042 if !linkFile.Valid() {
1043 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1044 depName, ctx.ModuleName())
1045 return
1046 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001047 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001048 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1049 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001050 }
1051 }
1052
Ivan Lozano89435d12020-07-31 11:01:18 -04001053 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001054 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001055 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001056 if _, ok := ccDep.(*Module); !ok {
1057 if ccDep.Module().Target().Os != ctx.Os() {
1058 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1059 return
1060 }
1061 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1062 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1063 return
1064 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001065 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001066 linkObject := ccDep.OutputFile()
1067 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001068
Ivan Lozano2093af22020-08-25 12:48:19 -04001069 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1071 }
1072
1073 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001074 switch {
1075 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001076 if cc.IsWholeStaticLib(depTag) {
1077 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1078 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001079 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1080 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001081 } else {
1082 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 -05001083 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001084 }
1085
1086 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1087 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001088 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001089 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1090
Colin Cross0de8a1e2020-09-18 14:15:30 -07001091 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1092 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1093 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1094 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1095 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001096 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001097 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001098 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001099 // For the shared lib dependencies, we may link to the stub variant
1100 // of the dependency depending on the context (e.g. if this
1101 // dependency crosses the APEX boundaries).
1102 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1103
1104 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1105 // object (either from stub or non-stub) to use.
1106 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1107 linkPath = linkPathFromFilePath(linkObject.Path())
1108
Ivan Lozanoffee3342019-08-27 12:03:00 -07001109 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001110 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001111 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1112 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1113 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1114 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001115 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001116
1117 // Record baseLibName for snapshots.
1118 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001119 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001120
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001121 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001122 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001123 case cc.IsHeaderDepTag(depTag):
1124 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1125 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1126 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1127 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001128 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001129 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001130 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001131 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001132 }
1133
1134 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001135 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1136 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001137 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001138 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001139 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001140
1141 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001142 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001143 // These are usually genrules which don't have per-target variants.
1144 directSrcDeps = append(directSrcDeps, srcDep)
1145 }
1146 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001147 })
1148
1149 var rlibDepFiles RustLibraries
1150 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001151 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001152 }
1153 var dylibDepFiles RustLibraries
1154 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001155 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001156 }
1157 var procMacroDepFiles RustLibraries
1158 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001159 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001160 }
1161
1162 var staticLibDepFiles android.Paths
1163 for _, dep := range directStaticLibDeps {
1164 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1165 }
1166
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001167 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001168 var sharedLibDepFiles android.Paths
1169 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001170 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1171 if dep.TableOfContents.Valid() {
1172 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001173 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001174 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001175 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001176 }
1177
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001178 var srcProviderDepFiles android.Paths
1179 for _, dep := range directSrcProvidersDeps {
1180 srcs, _ := dep.OutputFiles("")
1181 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1182 }
1183 for _, dep := range directSrcDeps {
1184 srcs := dep.Srcs()
1185 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1186 }
1187
Ivan Lozanoffee3342019-08-27 12:03:00 -07001188 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1189 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1190 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001191 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001192 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1193 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001194 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001195
1196 // Dedup exported flags from dependencies
1197 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001198 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001199 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001200 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1201 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1202 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001203
1204 return depPaths
1205}
1206
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001207func (mod *Module) InstallInData() bool {
1208 if mod.compiler == nil {
1209 return false
1210 }
1211 return mod.compiler.inData()
1212}
1213
Ivan Lozanoffee3342019-08-27 12:03:00 -07001214func linkPathFromFilePath(filepath android.Path) string {
1215 return strings.Split(filepath.String(), filepath.Base())[0]
1216}
Ivan Lozanod648c432020-02-06 12:05:10 -05001217
Ivan Lozanoffee3342019-08-27 12:03:00 -07001218func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1219 ctx := &depsContext{
1220 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001221 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001222
1223 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001224 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001225 var snapshotInfo *cc.SnapshotInfo
1226
1227 if ctx.Os() == android.Android {
1228 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1229 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001230
Ivan Lozano2b081132020-09-08 12:46:52 -04001231 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001232 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001233 stdLinkage = "rlib-std"
1234 }
1235
1236 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001237
Ivan Lozano2b081132020-09-08 12:46:52 -04001238 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1239 rlibDepVariations = append(rlibDepVariations,
1240 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1241 }
1242
Ivan Lozano1921e802021-05-20 13:39:16 -04001243 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001244 for _, lib := range deps.Rlibs {
1245 depTag := rlibDepTag
1246 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1247
1248 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1249 {Mutator: "rust_libraries", Variation: rlibVariation},
1250 }...), depTag, lib)
1251 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001252
1253 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001254 actx.AddVariationDependencies(
1255 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001256 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001257 dylibDepTag, deps.Dylibs...)
1258
Ivan Lozano1921e802021-05-20 13:39:16 -04001259 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001260 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1261 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001262 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001263 for _, lib := range deps.Rustlibs {
1264 depTag := autoDep.depTag
1265 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1266 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1267 {Mutator: "rust_libraries", Variation: autoDep.variation},
1268 }...), depTag, lib)
1269 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001270 } else {
1271 actx.AddVariationDependencies(
1272 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1273 autoDep.depTag, deps.Rustlibs...)
1274 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001275 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001276
1277 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001278 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001279 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001280 for _, lib := range deps.Stdlibs {
1281 depTag := rlibDepTag
1282 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1283
1284 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1285 depTag, lib)
1286 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001287 } else {
1288 actx.AddVariationDependencies(
1289 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1290 dylibDepTag, deps.Stdlibs...)
1291 }
1292 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001293
1294 for _, lib := range deps.SharedLibs {
1295 depTag := cc.SharedDepTag()
1296 name, version := cc.StubsLibNameAndVersion(lib)
1297
1298 variations := []blueprint.Variation{
1299 {Mutator: "link", Variation: "shared"},
1300 }
1301 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1302 }
1303
1304 for _, lib := range deps.WholeStaticLibs {
1305 depTag := cc.StaticDepTag(true)
1306 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1307
1308 actx.AddVariationDependencies([]blueprint.Variation{
1309 {Mutator: "link", Variation: "static"},
1310 }, depTag, lib)
1311 }
1312
1313 for _, lib := range deps.StaticLibs {
1314 depTag := cc.StaticDepTag(false)
1315 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1316
1317 actx.AddVariationDependencies([]blueprint.Variation{
1318 {Mutator: "link", Variation: "static"},
1319 }, depTag, lib)
1320 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001321
Zach Johnson3df4e632020-11-06 11:56:27 -08001322 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1323
Colin Cross565cafd2020-09-25 18:47:38 -07001324 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001325 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001326 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1327 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001328 }
1329 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001330 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1331 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001332 }
1333
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001334 if mod.sourceProvider != nil {
1335 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1336 bindgen.Properties.Custom_bindgen != "" {
1337 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1338 bindgen.Properties.Custom_bindgen)
1339 }
1340 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001341
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001342 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001343 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001344}
1345
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001346func BeginMutator(ctx android.BottomUpMutatorContext) {
1347 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1348 mod.beginMutator(ctx)
1349 }
1350}
1351
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001352func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1353 ctx := &baseModuleContext{
1354 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001355 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001356
1357 mod.begin(ctx)
1358}
1359
Ivan Lozanoffee3342019-08-27 12:03:00 -07001360func (mod *Module) Name() string {
1361 name := mod.ModuleBase.Name()
1362 if p, ok := mod.compiler.(interface {
1363 Name(string) string
1364 }); ok {
1365 name = p.Name(name)
1366 }
1367 return name
1368}
1369
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001370func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001371 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001372 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001373 }
1374}
1375
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001376var _ android.HostToolProvider = (*Module)(nil)
1377
1378func (mod *Module) HostToolPath() android.OptionalPath {
1379 if !mod.Host() {
1380 return android.OptionalPath{}
1381 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001382 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1383 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001384 }
1385 return android.OptionalPath{}
1386}
1387
Jiyong Park99644e92020-11-17 22:21:02 +09001388var _ android.ApexModule = (*Module)(nil)
1389
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001390func (mod *Module) minSdkVersion() string {
1391 return String(mod.Properties.Min_sdk_version)
1392}
1393
Jiyong Park45bf82e2020-12-15 22:29:02 +09001394var _ android.ApexModule = (*Module)(nil)
1395
1396// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001397func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001398 minSdkVersion := mod.minSdkVersion()
1399 if minSdkVersion == "apex_inherit" {
1400 return nil
1401 }
1402 if minSdkVersion == "" {
1403 return fmt.Errorf("min_sdk_version is not specificed")
1404 }
1405
1406 // Not using nativeApiLevelFromUser because the context here is not
1407 // necessarily a native context.
1408 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1409 if err != nil {
1410 return err
1411 }
1412
1413 if ver.GreaterThan(sdkVersion) {
1414 return fmt.Errorf("newer SDK(%v)", ver)
1415 }
Jiyong Park99644e92020-11-17 22:21:02 +09001416 return nil
1417}
1418
Jiyong Park45bf82e2020-12-15 22:29:02 +09001419// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001420func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1421 depTag := ctx.OtherModuleDependencyTag(dep)
1422
1423 if ccm, ok := dep.(*cc.Module); ok {
1424 if ccm.HasStubsVariants() {
1425 if cc.IsSharedDepTag(depTag) {
1426 // dynamic dep to a stubs lib crosses APEX boundary
1427 return false
1428 }
1429 if cc.IsRuntimeDepTag(depTag) {
1430 // runtime dep to a stubs lib also crosses APEX boundary
1431 return false
1432 }
1433
1434 if cc.IsHeaderDepTag(depTag) {
1435 return false
1436 }
1437 }
1438 if mod.Static() && cc.IsSharedDepTag(depTag) {
1439 // shared_lib dependency from a static lib is considered as crossing
1440 // the APEX boundary because the dependency doesn't actually is
1441 // linked; the dependency is used only during the compilation phase.
1442 return false
1443 }
1444 }
1445
1446 if depTag == procMacroDepTag {
1447 return false
1448 }
1449
1450 return true
1451}
1452
1453// Overrides ApexModule.IsInstallabeToApex()
1454func (mod *Module) IsInstallableToApex() bool {
1455 if mod.compiler != nil {
1456 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1457 return true
1458 }
1459 if _, ok := mod.compiler.(*binaryDecorator); ok {
1460 return true
1461 }
1462 }
1463 return false
1464}
1465
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001466// If a library file has a "lib" prefix, extract the library name without the prefix.
1467func libNameFromFilePath(filepath android.Path) (string, bool) {
1468 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1469 if strings.HasPrefix(libName, "lib") {
1470 libName = libName[3:]
1471 return libName, true
1472 }
1473 return "", false
1474}
1475
Ivan Lozanoffee3342019-08-27 12:03:00 -07001476var Bool = proptools.Bool
1477var BoolDefault = proptools.BoolDefault
1478var String = proptools.String
1479var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001480
1481var _ android.OutputFileProducer = (*Module)(nil)