blob: 300c0f57a4992e54795f4398953202287bd3e38a [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 Maurerc6868382021-07-13 14:12:37 -070087 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080088 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050089 ExtraVariants []string `blueprint:"mutated"`
90
Ivan Lozanoa2268632021-07-22 10:52:06 -040091 // Allows this module to use non-APEX version of libraries. Useful
92 // for building binaries that are started before APEXes are activated.
93 Bootstrap *bool
94
Ivan Lozano1921e802021-05-20 13:39:16 -040095 // Used by vendor snapshot to record dependencies from snapshot modules.
96 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090097 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040098
Matthew Maurerc6868382021-07-13 14:12:37 -070099 // Make this module available when building for ramdisk.
100 // On device without a dedicated recovery partition, the module is only
101 // available after switching root into
102 // /first_stage_ramdisk. To expose the module before switching root, install
103 // the recovery variant instead.
104 Ramdisk_available *bool
105
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500106 // Make this module available when building for vendor ramdisk.
107 // On device without a dedicated recovery partition, the module is only
108 // available after switching root into
109 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800110 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500111 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400112
Ivan Lozano1921e802021-05-20 13:39:16 -0400113 // Normally Soong uses the directory structure to decide which modules
114 // should be included (framework) or excluded (non-framework) from the
115 // different snapshots (vendor, recovery, etc.), but this property
116 // allows a partner to exclude a module normally thought of as a
117 // framework module from the vendor snapshot.
118 Exclude_from_vendor_snapshot *bool
119
120 // Normally Soong uses the directory structure to decide which modules
121 // should be included (framework) or excluded (non-framework) from the
122 // different snapshots (vendor, recovery, etc.), but this property
123 // allows a partner to exclude a module normally thought of as a
124 // framework module from the recovery snapshot.
125 Exclude_from_recovery_snapshot *bool
126
Matthew Maurer460ee942021-02-11 12:31:46 -0800127 // Make this module available when building for recovery
128 Recovery_available *bool
129
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500130 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
131 Min_sdk_version *string
132
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900133 HideFromMake bool `blueprint:"mutated"`
134 PreventInstall bool `blueprint:"mutated"`
135
136 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137}
138
139type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700140 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
Ivan Lozano6a884432020-12-02 09:15:16 -0500142 VendorProperties cc.VendorProperties
143
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144 Properties BaseProperties
145
146 hod android.HostOrDeviceSupported
147 multilib android.Multilib
148
Ivan Lozano6a884432020-12-02 09:15:16 -0500149 makeLinkType string
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400152 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200153 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500154 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400156 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700157 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400158
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400159 // Output file to be installed, may be stripped or unstripped.
160 outputFile android.OptionalPath
161
162 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900163
164 hideApexVariantFromMake bool
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500165
166 // For apex variants, this is set as apex.min_sdk_version
167 apexSdkVersion android.ApiLevel
Ivan Lozanoffee3342019-08-27 12:03:00 -0700168}
169
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500170func (mod *Module) Header() bool {
171 //TODO: If Rust libraries provide header variants, this needs to be updated.
172 return false
173}
174
175func (mod *Module) SetPreventInstall() {
176 mod.Properties.PreventInstall = true
177}
178
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500179func (mod *Module) SetHideFromMake() {
180 mod.Properties.HideFromMake = true
181}
182
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900183func (mod *Module) HiddenFromMake() bool {
184 return mod.Properties.HideFromMake
Ivan Lozanod7586b62021-04-01 09:49:36 -0400185}
186
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500187func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500188 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
189 // nil since we need compiler to actually sanitize.
190 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500191}
192
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500193func (mod *Module) IsPrebuilt() bool {
194 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
195 return true
196 }
197 return false
198}
199
Ivan Lozano43845682020-07-09 21:03:28 -0400200func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
201 switch tag {
202 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700203 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400204 return mod.sourceProvider.Srcs(), nil
205 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900206 if mod.OutputFile().Valid() {
207 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400208 }
209 return android.Paths{}, nil
210 }
211 default:
212 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
213 }
214}
215
Ivan Lozano52767be2019-10-18 14:49:46 -0700216func (mod *Module) SelectedStl() string {
217 return ""
218}
219
Ivan Lozano2b262972019-11-21 12:30:50 -0800220func (mod *Module) NonCcVariants() bool {
221 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400222 if _, ok := mod.compiler.(libraryInterface); ok {
223 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800224 }
225 }
226 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
227}
228
Ivan Lozano52767be2019-10-18 14:49:46 -0700229func (mod *Module) Static() bool {
230 if mod.compiler != nil {
231 if library, ok := mod.compiler.(libraryInterface); ok {
232 return library.static()
233 }
234 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400235 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700236}
237
238func (mod *Module) Shared() bool {
239 if mod.compiler != nil {
240 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400241 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700242 }
243 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400244 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700245}
246
Ivan Lozanod7586b62021-04-01 09:49:36 -0400247func (mod *Module) Dylib() bool {
248 if mod.compiler != nil {
249 if library, ok := mod.compiler.(libraryInterface); ok {
250 return library.dylib()
251 }
252 }
253 return false
254}
255
256func (mod *Module) Rlib() bool {
257 if mod.compiler != nil {
258 if library, ok := mod.compiler.(libraryInterface); ok {
259 return library.rlib()
260 }
261 }
262 return false
263}
264
265func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400266 if binary, ok := mod.compiler.(binaryInterface); ok {
267 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400268 }
269 return false
270}
271
Justin Yun5e035862021-06-29 20:50:37 +0900272func (mod *Module) StaticExecutable() bool {
273 if !mod.Binary() {
274 return false
275 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400276 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900277}
278
Ivan Lozanod7586b62021-04-01 09:49:36 -0400279func (mod *Module) Object() bool {
280 // Rust has no modules which produce only object files.
281 return false
282}
283
Ivan Lozano52767be2019-10-18 14:49:46 -0700284func (mod *Module) Toc() android.OptionalPath {
285 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400286 if lib, ok := mod.compiler.(libraryInterface); ok {
287 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700288 }
289 }
290 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
291}
292
Colin Crossc511bc52020-04-07 16:50:32 +0000293func (mod *Module) UseSdk() bool {
294 return false
295}
296
Ivan Lozanod7586b62021-04-01 09:49:36 -0400297func (mod *Module) RelativeInstallPath() string {
298 if mod.compiler != nil {
299 return mod.compiler.relativeInstallPath()
300 }
301 return ""
302}
303
Ivan Lozano52767be2019-10-18 14:49:46 -0700304func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500305 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700306}
307
Jiyong Park7d55b612021-06-11 17:22:09 +0900308func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400309 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900310}
311
Ivan Lozano52767be2019-10-18 14:49:46 -0700312func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400313 return true
314}
315
316func (mod *Module) SubName() string {
317 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700318}
319
320func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500321 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700322 return false
323}
324
Ivan Lozanof9e21722020-12-02 09:00:51 -0500325func (mod *Module) IsVndkExt() bool {
326 return false
327}
328
Ivan Lozanod7586b62021-04-01 09:49:36 -0400329func (mod *Module) IsVndkSp() bool {
330 return false
331}
332
Colin Cross127bb8b2020-12-16 16:46:01 -0800333func (c *Module) IsVndkPrivate() bool {
334 return false
335}
336
337func (c *Module) IsLlndk() bool {
338 return false
339}
340
341func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500342 return false
343}
344
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400345func (mod *Module) KernelHeadersDecorator() bool {
346 return false
347}
348
Colin Cross1f3f1302021-04-26 18:37:44 -0700349func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400350 return false
351}
352
Colin Cross5271fea2021-04-27 13:06:04 -0700353func (m *Module) NeedsVendorPublicLibraryVariants() bool {
354 return false
355}
356
Ivan Lozanod7586b62021-04-01 09:49:36 -0400357func (mod *Module) HasLlndkStubs() bool {
358 return false
359}
360
361func (mod *Module) StubsVersion() string {
362 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
363}
364
Ivan Lozano52767be2019-10-18 14:49:46 -0700365func (mod *Module) SdkVersion() string {
366 return ""
367}
368
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900369func (mod *Module) MinSdkVersion() string {
370 return ""
371}
372
Colin Crossc511bc52020-04-07 16:50:32 +0000373func (mod *Module) AlwaysSdk() bool {
374 return false
375}
376
Jiyong Park2286afd2020-06-16 21:58:53 +0900377func (mod *Module) IsSdkVariant() bool {
378 return false
379}
380
Colin Cross1348ce32020-10-01 13:37:16 -0700381func (mod *Module) SplitPerApiLevel() bool {
382 return false
383}
384
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400386 Dylibs []string
387 Rlibs []string
388 Rustlibs []string
389 Stdlibs []string
390 ProcMacros []string
391 SharedLibs []string
392 StaticLibs []string
393 WholeStaticLibs []string
394 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700395
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400396 // Used for data dependencies adjacent to tests
397 DataLibs []string
398 DataBins []string
399
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 CrtBegin, CrtEnd string
401}
402
403type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500404 DyLibs RustLibraries
405 RLibs RustLibraries
406 SharedLibs android.Paths
407 SharedLibDeps android.Paths
408 StaticLibs android.Paths
409 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500410
411 // depFlags and depLinkFlags are rustc and linker (clang) flags.
412 depFlags []string
413 depLinkFlags []string
414
415 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
416 // Both of these are exported and propagate to dependencies.
417 linkDirs []string
418 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700419
Ivan Lozano45901ed2020-07-24 16:05:01 -0400420 // Used by bindgen modules which call clang
421 depClangFlags []string
422 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400423 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400424 depSystemIncludePaths android.Paths
425
Ivan Lozanof1c84332019-09-20 11:00:37 -0700426 CrtBegin android.OptionalPath
427 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700428
429 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500430 SrcDeps android.Paths
431 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432}
433
434type RustLibraries []RustLibrary
435
436type RustLibrary struct {
437 Path android.Path
438 CrateName string
439}
440
441type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100442 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400444 cfgFlags(ctx ModuleContext, flags Flags) Flags
445 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700446 compilerProps() []interface{}
447 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
448 compilerDeps(ctx DepsContext, deps Deps) Deps
449 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700450 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700451
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100452 // Output directory in which source-generated code from dependencies is
453 // copied. This is equivalent to Cargo's OUT_DIR variable.
454 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700455
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400456 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
457 CargoPkgVersion() string
458
459 // CargoEnvCompat returns whether Cargo environment variables should be used.
460 CargoEnvCompat() bool
461
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800462 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200463 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700464 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400465 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400466
467 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400468
469 Disabled() bool
470 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400471
Ivan Lozanodd055472020-09-28 13:22:45 -0400472 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900473
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400474 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900475 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400476}
477
Matthew Maurerbb3add12020-06-25 09:34:12 -0700478type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700479 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400480 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700481}
482
483type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400484 linkDirs []string
485 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700486}
487
Matthew Maurerbb3add12020-06-25 09:34:12 -0700488func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
489 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
490}
491
Ivan Lozano2093af22020-08-25 12:48:19 -0400492func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
493 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
494}
495
Colin Cross0de8a1e2020-09-18 14:15:30 -0700496func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
497 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700498 LinkDirs: flagExporter.linkDirs,
499 LinkObjects: flagExporter.linkObjects,
500 })
501}
502
Matthew Maurerbb3add12020-06-25 09:34:12 -0700503var _ exportedFlagsProducer = (*flagExporter)(nil)
504
505func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700506 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700507}
508
Colin Cross0de8a1e2020-09-18 14:15:30 -0700509type FlagExporterInfo struct {
510 Flags []string
511 LinkDirs []string // TODO: this should be android.Paths
512 LinkObjects []string // TODO: this should be android.Paths
513}
514
515var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
516
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400517func (mod *Module) isCoverageVariant() bool {
518 return mod.coverage.Properties.IsCoverageVariant
519}
520
521var _ cc.Coverage = (*Module)(nil)
522
523func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
524 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
525}
526
Ivan Lozanod7586b62021-04-01 09:49:36 -0400527func (mod *Module) VndkVersion() string {
528 return mod.Properties.VndkVersion
529}
530
531func (mod *Module) PreventInstall() bool {
532 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400533}
534
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400535func (mod *Module) MarkAsCoverageVariant(coverage bool) {
536 mod.coverage.Properties.IsCoverageVariant = coverage
537}
538
539func (mod *Module) EnableCoverageIfNeeded() {
540 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700541}
542
543func defaultsFactory() android.Module {
544 return DefaultsFactory()
545}
546
547type Defaults struct {
548 android.ModuleBase
549 android.DefaultsModuleBase
550}
551
552func DefaultsFactory(props ...interface{}) android.Module {
553 module := &Defaults{}
554
555 module.AddProperties(props...)
556 module.AddProperties(
557 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500558 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100559 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400560 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700561 &BaseCompilerProperties{},
562 &BinaryCompilerProperties{},
563 &LibraryCompilerProperties{},
564 &ProcMacroCompilerProperties{},
565 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400566 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700567 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400568 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400569 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200570 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500571 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700572 )
573
574 android.InitDefaultsModule(module)
575 return module
576}
577
578func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700579 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700580}
581
Ivan Lozano183a3212019-10-18 14:18:45 -0700582func (mod *Module) CcLibrary() bool {
583 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500584 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700585 return true
586 }
587 }
588 return false
589}
590
591func (mod *Module) CcLibraryInterface() bool {
592 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400593 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
594 // VariantIs{Static,Shared} is set.
595 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700596 return true
597 }
598 }
599 return false
600}
601
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400602func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400603 if mod.compiler != nil {
604 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400605 }
606 return nil
607}
608
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800609func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700610 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700611 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800612 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700613 }
614 }
615 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
616}
617
618func (mod *Module) SetStatic() {
619 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700620 if library, ok := mod.compiler.(libraryInterface); ok {
621 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700622 return
623 }
624 }
625 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
626}
627
628func (mod *Module) SetShared() {
629 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700630 if library, ok := mod.compiler.(libraryInterface); ok {
631 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700632 return
633 }
634 }
635 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
636}
637
Ivan Lozano183a3212019-10-18 14:18:45 -0700638func (mod *Module) BuildStaticVariant() bool {
639 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700640 if library, ok := mod.compiler.(libraryInterface); ok {
641 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700642 }
643 }
644 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
645}
646
647func (mod *Module) BuildSharedVariant() bool {
648 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700649 if library, ok := mod.compiler.(libraryInterface); ok {
650 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700651 }
652 }
653 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
654}
655
Ivan Lozano183a3212019-10-18 14:18:45 -0700656func (mod *Module) Module() android.Module {
657 return mod
658}
659
Ivan Lozano183a3212019-10-18 14:18:45 -0700660func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400661 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700662}
663
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400664func (mod *Module) CoverageFiles() android.Paths {
665 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800666 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400667 }
668 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
669}
670
Jiyong Park459feca2020-12-15 11:02:21 +0900671func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900672 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900673 return false
674 }
675
Jiyong Park459feca2020-12-15 11:02:21 +0900676 // The apex variant is not installable because it is included in the APEX and won't appear
677 // in the system partition as a standalone file.
678 if !apexInfo.IsForPlatform() {
679 return false
680 }
681
Jiyong Parke54f07e2021-04-07 15:08:04 +0900682 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900683}
684
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500685func (ctx moduleContext) apexVariationName() string {
686 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
687}
688
Ivan Lozano183a3212019-10-18 14:18:45 -0700689var _ cc.LinkableInterface = (*Module)(nil)
690
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691func (mod *Module) Init() android.Module {
692 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500693 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700694
695 if mod.compiler != nil {
696 mod.AddProperties(mod.compiler.compilerProps()...)
697 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400698 if mod.coverage != nil {
699 mod.AddProperties(mod.coverage.props()...)
700 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200701 if mod.clippy != nil {
702 mod.AddProperties(mod.clippy.props()...)
703 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400704 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700705 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400706 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500707 if mod.sanitize != nil {
708 mod.AddProperties(mod.sanitize.props()...)
709 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400710
Ivan Lozanoffee3342019-08-27 12:03:00 -0700711 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900712 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700713
714 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700715 return mod
716}
717
718func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
719 return &Module{
720 hod: hod,
721 multilib: multilib,
722 }
723}
724func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
725 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400726 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200727 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500728 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700729 return module
730}
731
732type ModuleContext interface {
733 android.ModuleContext
734 ModuleContextIntf
735}
736
737type BaseModuleContext interface {
738 android.BaseModuleContext
739 ModuleContextIntf
740}
741
742type DepsContext interface {
743 android.BottomUpMutatorContext
744 ModuleContextIntf
745}
746
747type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200748 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700749 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700750}
751
752type depsContext struct {
753 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700754}
755
756type moduleContext struct {
757 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758}
759
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200760type baseModuleContext struct {
761 android.BaseModuleContext
762}
763
764func (ctx *moduleContext) RustModule() *Module {
765 return ctx.Module().(*Module)
766}
767
768func (ctx *moduleContext) toolchain() config.Toolchain {
769 return ctx.RustModule().toolchain(ctx)
770}
771
772func (ctx *depsContext) RustModule() *Module {
773 return ctx.Module().(*Module)
774}
775
776func (ctx *depsContext) toolchain() config.Toolchain {
777 return ctx.RustModule().toolchain(ctx)
778}
779
780func (ctx *baseModuleContext) RustModule() *Module {
781 return ctx.Module().(*Module)
782}
783
784func (ctx *baseModuleContext) toolchain() config.Toolchain {
785 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400786}
787
788func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700789 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
790 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
791 return false
792 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400793 return mod.compiler != nil && mod.compiler.nativeCoverage()
794}
795
Ivan Lozanod7586b62021-04-01 09:49:36 -0400796func (mod *Module) EverInstallable() bool {
797 return mod.compiler != nil &&
798 // Check to see whether the module is actually ever installable.
799 mod.compiler.everInstallable()
800}
801
802func (mod *Module) Installable() *bool {
803 return mod.Properties.Installable
804}
805
Ivan Lozanoffee3342019-08-27 12:03:00 -0700806func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
807 if mod.cachedToolchain == nil {
808 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
809 }
810 return mod.cachedToolchain
811}
812
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200813func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
814 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
815}
816
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
818}
819
820func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
821 ctx := &moduleContext{
822 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700823 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700824
Jiyong Park99644e92020-11-17 22:21:02 +0900825 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
826 if !apexInfo.IsForPlatform() {
827 mod.hideApexVariantFromMake = true
828 }
829
Ivan Lozanoffee3342019-08-27 12:03:00 -0700830 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500831 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
832
833 // Differentiate static libraries that are vendor available
834 if mod.UseVndk() {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700835 if mod.InProduct() && !mod.OnlyInProduct() {
836 mod.Properties.SubName += cc.ProductSuffix
837 } else {
838 mod.Properties.SubName += cc.VendorSuffix
839 }
Matthew Maurerc6868382021-07-13 14:12:37 -0700840 } else if mod.InRamdisk() && !mod.OnlyInRamdisk() {
841 mod.Properties.SubName += cc.RamdiskSuffix
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500842 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
843 mod.Properties.SubName += cc.VendorRamdiskSuffix
Matthew Maurer460ee942021-02-11 12:31:46 -0800844 } else if mod.InRecovery() && !mod.OnlyInRecovery() {
845 mod.Properties.SubName += cc.RecoverySuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500846 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700847
Matthew Maurera61e31f2021-05-27 11:09:11 -0700848 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
849 mod.Properties.SubName += cc.NativeBridgeSuffix
850 }
851
Ivan Lozanoffee3342019-08-27 12:03:00 -0700852 if !toolchain.Supported() {
853 // This toolchain's unsupported, there's nothing to do for this mod.
854 return
855 }
856
857 deps := mod.depsToPaths(ctx)
858 flags := Flags{
859 Toolchain: toolchain,
860 }
861
Ivan Lozano67eada32021-09-23 11:50:33 -0400862 // Calculate rustc flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700863 if mod.compiler != nil {
864 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400865 flags = mod.compiler.cfgFlags(ctx, flags)
866 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400867 }
868 if mod.coverage != nil {
869 flags, deps = mod.coverage.flags(ctx, flags, deps)
870 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200871 if mod.clippy != nil {
872 flags, deps = mod.clippy.flags(ctx, flags, deps)
873 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500874 if mod.sanitize != nil {
875 flags, deps = mod.sanitize.flags(ctx, flags, deps)
876 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400877
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200878 // SourceProvider needs to call GenerateSource() before compiler calls
879 // compile() so it can provide the source. A SourceProvider has
880 // multiple variants (e.g. source, rlib, dylib). Only the "source"
881 // variant is responsible for effectively generating the source. The
882 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400883 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200884 if mod.compiler.(libraryInterface).source() {
885 mod.sourceProvider.GenerateSource(ctx, deps)
886 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
887 } else {
888 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
889 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700890 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200891 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400892 }
893
894 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100895 mod.compiler.initialize(ctx)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400896 outputFile := mod.compiler.compile(ctx, flags, deps)
897 if ctx.Failed() {
898 return
899 }
900 mod.outputFile = android.OptionalPathForPath(outputFile)
901 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900902
Dan Albert06feee92021-03-19 15:06:02 -0700903 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
904
Ivan Lozano1921e802021-05-20 13:39:16 -0400905 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
906 // RECOVERY_SNAPSHOT_VERSION is current.
907 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
908 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
909 lib.collectHeadersForSnapshot(ctx, deps)
910 }
911 }
912
Jiyong Park459feca2020-12-15 11:02:21 +0900913 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900914 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
915 // If the module has been specifically configure to not be installed then
916 // hide from make as otherwise it will break when running inside make as the
917 // output path to install will not be specified. Not all uninstallable
918 // modules can be hidden from make as some are needed for resolving make
919 // side dependencies.
920 mod.HideFromMake()
921 } else if !mod.installable(apexInfo) {
922 mod.SkipInstall()
923 }
924
925 // Still call install though, the installs will be stored as PackageSpecs to allow
926 // using the outputs in a genrule.
927 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200928 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900929 if ctx.Failed() {
930 return
931 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400932 }
Chris Wailes74be7642021-07-22 16:20:28 -0700933
934 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700935 }
936}
937
938func (mod *Module) deps(ctx DepsContext) Deps {
939 deps := Deps{}
940
941 if mod.compiler != nil {
942 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400943 }
944 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700945 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700946 }
947
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400948 if mod.coverage != nil {
949 deps = mod.coverage.deps(ctx, deps)
950 }
951
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500952 if mod.sanitize != nil {
953 deps = mod.sanitize.deps(ctx, deps)
954 }
955
Ivan Lozanoffee3342019-08-27 12:03:00 -0700956 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
957 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700958 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700959 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
960 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
961 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400962 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700963 return deps
964
965}
966
Ivan Lozanoffee3342019-08-27 12:03:00 -0700967type dependencyTag struct {
968 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800969 name string
970 library bool
971 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +0000972 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700973}
974
Jiyong Park65b62242020-11-25 12:44:59 +0900975// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
976// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
977func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800978 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900979}
980
981var _ android.InstallNeededDependencyTag = dependencyTag{}
982
Colin Cross65cb3142021-12-10 23:05:02 +0000983func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
984 if d.library && d.dynamic {
985 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
986 }
987 return nil
988}
989
990var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
991
Ivan Lozanoffee3342019-08-27 12:03:00 -0700992var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400993 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
994 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +0000995 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800996 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400997 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200998 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400999 dataLibDepTag = dependencyTag{name: "data lib"}
1000 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001001)
1002
Jiyong Park99644e92020-11-17 22:21:02 +09001003func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1004 tag, ok := depTag.(dependencyTag)
1005 return ok && tag == dylibDepTag
1006}
1007
Jiyong Park94e22fd2021-04-08 18:19:15 +09001008func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1009 tag, ok := depTag.(dependencyTag)
1010 return ok && tag == rlibDepTag
1011}
1012
Matthew Maurer0f003b12020-06-29 14:34:06 -07001013type autoDep struct {
1014 variation string
1015 depTag dependencyTag
1016}
1017
1018var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001019 rlibVariation = "rlib"
1020 dylibVariation = "dylib"
1021 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1022 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001023)
1024
1025type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001026 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001027}
1028
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001029func (mod *Module) begin(ctx BaseModuleContext) {
1030 if mod.coverage != nil {
1031 mod.coverage.begin(ctx)
1032 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001033 if mod.sanitize != nil {
1034 mod.sanitize.begin(ctx)
1035 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001036}
1037
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001038func (mod *Module) Prebuilt() *android.Prebuilt {
1039 if p, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
1040 return p.prebuilt()
1041 }
1042 return nil
1043}
1044
Ivan Lozanoffee3342019-08-27 12:03:00 -07001045func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1046 var depPaths PathDeps
1047
1048 directRlibDeps := []*Module{}
1049 directDylibDeps := []*Module{}
1050 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001051 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001052 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001053 directSrcProvidersDeps := []*Module{}
1054 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001055
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001056 // For the dependency from platform to apex, use the latest stubs
1057 mod.apexSdkVersion = android.FutureApiLevel
1058 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1059 if !apexInfo.IsForPlatform() {
1060 mod.apexSdkVersion = apexInfo.MinSdkVersion
1061 }
1062
1063 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1064 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1065 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1066 // (b/144430859)
1067 mod.apexSdkVersion = android.FutureApiLevel
1068 }
1069
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070 ctx.VisitDirectDeps(func(dep android.Module) {
1071 depName := ctx.OtherModuleName(dep)
1072 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001073
Ivan Lozano89435d12020-07-31 11:01:18 -04001074 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001075 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001076 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001077
Ivan Lozanoffee3342019-08-27 12:03:00 -07001078 switch depTag {
1079 case dylibDepTag:
1080 dylib, ok := rustDep.compiler.(libraryInterface)
1081 if !ok || !dylib.dylib() {
1082 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1083 return
1084 }
1085 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001086 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001087 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001088
Ivan Lozanoffee3342019-08-27 12:03:00 -07001089 rlib, ok := rustDep.compiler.(libraryInterface)
1090 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001091 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001092 return
1093 }
1094 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001095 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001096 case procMacroDepTag:
1097 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001098 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001099 }
1100
1101 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001102 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1103 // OS/Arch variant is used.
1104 var helper string
1105 if ctx.Host() {
1106 helper = "missing 'host_supported'?"
1107 } else {
1108 helper = "device module defined?"
1109 }
1110
1111 if dep.Target().Os != ctx.Os() {
1112 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1113 return
1114 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1115 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1116 return
1117 }
1118 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001119 }
1120
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001121 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001122 if depTag != procMacroDepTag {
1123 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1124 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1125 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1126 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001127 }
1128
Ivan Lozanoffee3342019-08-27 12:03:00 -07001129 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001130 linkFile := rustDep.UnstrippedOutputFile()
1131 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001132 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1133 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001134 }
1135 }
1136
Ivan Lozano89435d12020-07-31 11:01:18 -04001137 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001138 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001139 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001140 if _, ok := ccDep.(*Module); !ok {
1141 if ccDep.Module().Target().Os != ctx.Os() {
1142 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1143 return
1144 }
1145 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1146 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1147 return
1148 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001149 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001150 linkObject := ccDep.OutputFile()
1151 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001152
Ivan Lozano2093af22020-08-25 12:48:19 -04001153 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001154 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1155 }
1156
1157 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001158 switch {
1159 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001160 if cc.IsWholeStaticLib(depTag) {
1161 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1162 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001163 if mod.Binary() {
1164 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1165 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1166 // final linkage, pass the args directly to the linker to handle these cases.
1167 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1168 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001169 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001170 } else {
1171 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 -05001172 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001173 }
1174
1175 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1176 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001177 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001178 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1179
Colin Cross0de8a1e2020-09-18 14:15:30 -07001180 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1181 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1182 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1183 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1184 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001185 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001186 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001187 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001188 // For the shared lib dependencies, we may link to the stub variant
1189 // of the dependency depending on the context (e.g. if this
1190 // dependency crosses the APEX boundaries).
1191 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1192
1193 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1194 // object (either from stub or non-stub) to use.
1195 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1196 linkPath = linkPathFromFilePath(linkObject.Path())
1197
Ivan Lozanoffee3342019-08-27 12:03:00 -07001198 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001199 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001200 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1201 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1202 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1203 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001204 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001205
1206 // Record baseLibName for snapshots.
1207 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001208 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001209
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001210 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001211 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001212 case cc.IsHeaderDepTag(depTag):
1213 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1214 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1215 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1216 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001217 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001218 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001219 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001220 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001221 }
1222
1223 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001224 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1225 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001226 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001227 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001228 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001229
1230 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001231 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001232 // These are usually genrules which don't have per-target variants.
1233 directSrcDeps = append(directSrcDeps, srcDep)
1234 }
1235 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001236 })
1237
1238 var rlibDepFiles RustLibraries
1239 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001240 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001241 }
1242 var dylibDepFiles RustLibraries
1243 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001244 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001245 }
1246 var procMacroDepFiles RustLibraries
1247 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001248 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001249 }
1250
1251 var staticLibDepFiles android.Paths
1252 for _, dep := range directStaticLibDeps {
1253 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1254 }
1255
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001256 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001257 var sharedLibDepFiles android.Paths
1258 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001259 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1260 if dep.TableOfContents.Valid() {
1261 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001262 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001263 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001264 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001265 }
1266
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001267 var srcProviderDepFiles android.Paths
1268 for _, dep := range directSrcProvidersDeps {
1269 srcs, _ := dep.OutputFiles("")
1270 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1271 }
1272 for _, dep := range directSrcDeps {
1273 srcs := dep.Srcs()
1274 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1275 }
1276
Ivan Lozanoffee3342019-08-27 12:03:00 -07001277 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1278 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1279 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001280 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001281 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1282 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001283 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001284
1285 // Dedup exported flags from dependencies
1286 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001287 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001288 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001289 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1290 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1291 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001292
1293 return depPaths
1294}
1295
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001296func (mod *Module) InstallInData() bool {
1297 if mod.compiler == nil {
1298 return false
1299 }
1300 return mod.compiler.inData()
1301}
1302
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001303func (mod *Module) InstallInRamdisk() bool {
1304 return mod.InRamdisk()
1305}
1306
1307func (mod *Module) InstallInVendorRamdisk() bool {
1308 return mod.InVendorRamdisk()
1309}
1310
1311func (mod *Module) InstallInRecovery() bool {
1312 return mod.InRecovery()
1313}
1314
Colin Cross77435572021-11-08 12:37:01 -08001315func (mod *Module) InstallBypassMake() bool {
1316 return true
1317}
1318
Ivan Lozanoffee3342019-08-27 12:03:00 -07001319func linkPathFromFilePath(filepath android.Path) string {
1320 return strings.Split(filepath.String(), filepath.Base())[0]
1321}
Ivan Lozanod648c432020-02-06 12:05:10 -05001322
Ivan Lozanoffee3342019-08-27 12:03:00 -07001323func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1324 ctx := &depsContext{
1325 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001326 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001327
1328 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001329 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001330 var snapshotInfo *cc.SnapshotInfo
1331
1332 if ctx.Os() == android.Android {
1333 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1334 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001335
Ivan Lozano2b081132020-09-08 12:46:52 -04001336 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001337 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001338 stdLinkage = "rlib-std"
1339 }
1340
1341 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001342
Ivan Lozano2b081132020-09-08 12:46:52 -04001343 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1344 rlibDepVariations = append(rlibDepVariations,
1345 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1346 }
1347
Ivan Lozano1921e802021-05-20 13:39:16 -04001348 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001349 for _, lib := range deps.Rlibs {
1350 depTag := rlibDepTag
1351 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1352
1353 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1354 {Mutator: "rust_libraries", Variation: rlibVariation},
1355 }...), depTag, lib)
1356 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001357
1358 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001359 actx.AddVariationDependencies(
1360 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001361 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001362 dylibDepTag, deps.Dylibs...)
1363
Ivan Lozano1921e802021-05-20 13:39:16 -04001364 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001365 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1366 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001367 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001368 for _, lib := range deps.Rustlibs {
1369 depTag := autoDep.depTag
1370 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1371 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1372 {Mutator: "rust_libraries", Variation: autoDep.variation},
1373 }...), depTag, lib)
1374 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001375 } else {
1376 actx.AddVariationDependencies(
1377 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1378 autoDep.depTag, deps.Rustlibs...)
1379 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001380 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001381
1382 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001383 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001384 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001385 for _, lib := range deps.Stdlibs {
1386 depTag := rlibDepTag
1387 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1388
1389 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1390 depTag, lib)
1391 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001392 } else {
1393 actx.AddVariationDependencies(
1394 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1395 dylibDepTag, deps.Stdlibs...)
1396 }
1397 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001398
1399 for _, lib := range deps.SharedLibs {
1400 depTag := cc.SharedDepTag()
1401 name, version := cc.StubsLibNameAndVersion(lib)
1402
1403 variations := []blueprint.Variation{
1404 {Mutator: "link", Variation: "shared"},
1405 }
1406 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1407 }
1408
1409 for _, lib := range deps.WholeStaticLibs {
1410 depTag := cc.StaticDepTag(true)
1411 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1412
1413 actx.AddVariationDependencies([]blueprint.Variation{
1414 {Mutator: "link", Variation: "static"},
1415 }, depTag, lib)
1416 }
1417
1418 for _, lib := range deps.StaticLibs {
1419 depTag := cc.StaticDepTag(false)
1420 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1421
1422 actx.AddVariationDependencies([]blueprint.Variation{
1423 {Mutator: "link", Variation: "static"},
1424 }, depTag, lib)
1425 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001426
Zach Johnson3df4e632020-11-06 11:56:27 -08001427 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1428
Colin Cross565cafd2020-09-25 18:47:38 -07001429 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001430 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001431 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1432 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001433 }
1434 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001435 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1436 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001437 }
1438
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001439 if mod.sourceProvider != nil {
1440 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1441 bindgen.Properties.Custom_bindgen != "" {
1442 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1443 bindgen.Properties.Custom_bindgen)
1444 }
1445 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001446
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001447 actx.AddVariationDependencies([]blueprint.Variation{
1448 {Mutator: "link", Variation: "shared"},
1449 }, dataLibDepTag, deps.DataLibs...)
1450
1451 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1452
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001453 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001454 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001455}
1456
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001457func BeginMutator(ctx android.BottomUpMutatorContext) {
1458 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1459 mod.beginMutator(ctx)
1460 }
1461}
1462
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001463func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1464 ctx := &baseModuleContext{
1465 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001466 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001467
1468 mod.begin(ctx)
1469}
1470
Ivan Lozanoffee3342019-08-27 12:03:00 -07001471func (mod *Module) Name() string {
1472 name := mod.ModuleBase.Name()
1473 if p, ok := mod.compiler.(interface {
1474 Name(string) string
1475 }); ok {
1476 name = p.Name(name)
1477 }
1478 return name
1479}
1480
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001481func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001482 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001483 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001484 }
1485}
1486
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001487var _ android.HostToolProvider = (*Module)(nil)
1488
1489func (mod *Module) HostToolPath() android.OptionalPath {
1490 if !mod.Host() {
1491 return android.OptionalPath{}
1492 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001493 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1494 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001495 }
1496 return android.OptionalPath{}
1497}
1498
Jiyong Park99644e92020-11-17 22:21:02 +09001499var _ android.ApexModule = (*Module)(nil)
1500
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001501func (mod *Module) minSdkVersion() string {
1502 return String(mod.Properties.Min_sdk_version)
1503}
1504
Jiyong Park45bf82e2020-12-15 22:29:02 +09001505var _ android.ApexModule = (*Module)(nil)
1506
1507// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001508func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001509 minSdkVersion := mod.minSdkVersion()
1510 if minSdkVersion == "apex_inherit" {
1511 return nil
1512 }
1513 if minSdkVersion == "" {
1514 return fmt.Errorf("min_sdk_version is not specificed")
1515 }
1516
1517 // Not using nativeApiLevelFromUser because the context here is not
1518 // necessarily a native context.
1519 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1520 if err != nil {
1521 return err
1522 }
1523
1524 if ver.GreaterThan(sdkVersion) {
1525 return fmt.Errorf("newer SDK(%v)", ver)
1526 }
Jiyong Park99644e92020-11-17 22:21:02 +09001527 return nil
1528}
1529
Jiyong Park45bf82e2020-12-15 22:29:02 +09001530// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001531func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1532 depTag := ctx.OtherModuleDependencyTag(dep)
1533
1534 if ccm, ok := dep.(*cc.Module); ok {
1535 if ccm.HasStubsVariants() {
1536 if cc.IsSharedDepTag(depTag) {
1537 // dynamic dep to a stubs lib crosses APEX boundary
1538 return false
1539 }
1540 if cc.IsRuntimeDepTag(depTag) {
1541 // runtime dep to a stubs lib also crosses APEX boundary
1542 return false
1543 }
1544
1545 if cc.IsHeaderDepTag(depTag) {
1546 return false
1547 }
1548 }
1549 if mod.Static() && cc.IsSharedDepTag(depTag) {
1550 // shared_lib dependency from a static lib is considered as crossing
1551 // the APEX boundary because the dependency doesn't actually is
1552 // linked; the dependency is used only during the compilation phase.
1553 return false
1554 }
1555 }
1556
1557 if depTag == procMacroDepTag {
1558 return false
1559 }
1560
1561 return true
1562}
1563
1564// Overrides ApexModule.IsInstallabeToApex()
1565func (mod *Module) IsInstallableToApex() bool {
1566 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001567 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001568 return true
1569 }
1570 if _, ok := mod.compiler.(*binaryDecorator); ok {
1571 return true
1572 }
1573 }
1574 return false
1575}
1576
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001577// If a library file has a "lib" prefix, extract the library name without the prefix.
1578func libNameFromFilePath(filepath android.Path) (string, bool) {
1579 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1580 if strings.HasPrefix(libName, "lib") {
1581 libName = libName[3:]
1582 return libName, true
1583 }
1584 return "", false
1585}
1586
Ivan Lozanoffee3342019-08-27 12:03:00 -07001587var Bool = proptools.Bool
1588var BoolDefault = proptools.BoolDefault
1589var String = proptools.String
1590var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001591
1592var _ android.OutputFileProducer = (*Module)(nil)