blob: a200617ded24f503bec2add35dc701eb5887e1df [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 (
Sasha Smundaka76acba2022-04-18 20:12:56 -070018 "android/soong/bloaty"
Ivan Lozano183a3212019-10-18 14:18:45 -070019 "fmt"
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "strings"
21
22 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
26 "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"
Ivan Lozano872d5792022-03-23 17:31:39 -040030 "android/soong/snapshot"
Ivan Lozanoffee3342019-08-27 12:03:00 -070031)
32
33var pctx = android.NewPackageContext("android/soong/rust")
34
35func init() {
Ivan Lozanoffee3342019-08-27 12:03:00 -070036 android.RegisterModuleType("rust_defaults", defaultsFactory)
37 android.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
38 ctx.BottomUp("rust_libraries", LibraryMutator).Parallel()
Ivan Lozano2b081132020-09-08 12:46:52 -040039 ctx.BottomUp("rust_stdlinkage", LibstdMutator).Parallel()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040040 ctx.BottomUp("rust_begin", BeginMutator).Parallel()
Ivan Lozano6cd99e62020-02-11 08:24:25 -050041
42 })
43 android.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
44 ctx.BottomUp("rust_sanitizers", rustSanitizerRuntimeMutator).Parallel()
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 })
46 pctx.Import("android/soong/rust/config")
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020047 pctx.ImportAs("cc_config", "android/soong/cc/config")
Sasha Smundaka76acba2022-04-18 20:12:56 -070048 android.InitRegistrationContext.RegisterSingletonType("kythe_rust_extract", kytheExtractRustFactory)
Ivan Lozanoffee3342019-08-27 12:03:00 -070049}
50
51type Flags struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040052 GlobalRustFlags []string // Flags that apply globally to rust
53 GlobalLinkFlags []string // Flags that apply globally to linker
54 RustFlags []string // Flags that apply to rust
55 LinkFlags []string // Flags that apply to linker
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020056 ClippyFlags []string // Flags that apply to clippy-driver, during the linting
Dan Albert06feee92021-03-19 15:06:02 -070057 RustdocFlags []string // Flags that apply to rustdoc
Ivan Lozanof1c84332019-09-20 11:00:37 -070058 Toolchain config.Toolchain
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040059 Coverage bool
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 Clippy bool
Sasha Smundaka76acba2022-04-18 20:12:56 -070061 EmitXrefs bool // If true, emit rules to aid cross-referencing
Ivan Lozanoffee3342019-08-27 12:03:00 -070062}
63
64type BaseProperties struct {
Liz Kammer884fe9e2023-02-28 14:29:13 -050065 AndroidMkRlibs []string `blueprint:"mutated"`
66 AndroidMkDylibs []string `blueprint:"mutated"`
67 AndroidMkProcMacroLibs []string `blueprint:"mutated"`
68 AndroidMkSharedLibs []string `blueprint:"mutated"`
69 AndroidMkStaticLibs []string `blueprint:"mutated"`
Ivan Lozano43845682020-07-09 21:03:28 -040070
Ivan Lozano6a884432020-12-02 09:15:16 -050071 ImageVariationPrefix string `blueprint:"mutated"`
72 VndkVersion string `blueprint:"mutated"`
73 SubName string `blueprint:"mutated"`
74
Ivan Lozanoc08897c2021-04-02 12:41:32 -040075 // SubName is used by CC for tracking image variants / SDK versions. RustSubName is used for Rust-specific
76 // subnaming which shouldn't be visible to CC modules (such as the rlib stdlinkage subname). This should be
77 // appended before SubName.
78 RustSubName string `blueprint:"mutated"`
79
Ivan Lozano6a884432020-12-02 09:15:16 -050080 // Set by imageMutator
Ivan Lozanoe6d30982021-02-05 10:57:43 -050081 CoreVariantNeeded bool `blueprint:"mutated"`
82 VendorRamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurerc6868382021-07-13 14:12:37 -070083 RamdiskVariantNeeded bool `blueprint:"mutated"`
Matthew Maurer460ee942021-02-11 12:31:46 -080084 RecoveryVariantNeeded bool `blueprint:"mutated"`
Ivan Lozanoe6d30982021-02-05 10:57:43 -050085 ExtraVariants []string `blueprint:"mutated"`
86
Ivan Lozanoa2268632021-07-22 10:52:06 -040087 // Allows this module to use non-APEX version of libraries. Useful
88 // for building binaries that are started before APEXes are activated.
89 Bootstrap *bool
90
Ivan Lozano1921e802021-05-20 13:39:16 -040091 // Used by vendor snapshot to record dependencies from snapshot modules.
92 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090093 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040094
Matthew Maurerc6868382021-07-13 14:12:37 -070095 // Make this module available when building for ramdisk.
96 // On device without a dedicated recovery partition, the module is only
97 // available after switching root into
98 // /first_stage_ramdisk. To expose the module before switching root, install
99 // the recovery variant instead.
100 Ramdisk_available *bool
101
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500102 // Make this module available when building for vendor ramdisk.
103 // On device without a dedicated recovery partition, the module is only
104 // available after switching root into
105 // /first_stage_ramdisk. To expose the module before switching root, install
Matthew Maurer460ee942021-02-11 12:31:46 -0800106 // the recovery variant instead
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500107 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400108
Ivan Lozano1921e802021-05-20 13:39:16 -0400109 // Normally Soong uses the directory structure to decide which modules
110 // should be included (framework) or excluded (non-framework) from the
111 // different snapshots (vendor, recovery, etc.), but this property
112 // allows a partner to exclude a module normally thought of as a
113 // framework module from the vendor snapshot.
114 Exclude_from_vendor_snapshot *bool
115
116 // Normally Soong uses the directory structure to decide which modules
117 // should be included (framework) or excluded (non-framework) from the
118 // different snapshots (vendor, recovery, etc.), but this property
119 // allows a partner to exclude a module normally thought of as a
120 // framework module from the recovery snapshot.
121 Exclude_from_recovery_snapshot *bool
122
Matthew Maurer460ee942021-02-11 12:31:46 -0800123 // Make this module available when building for recovery
124 Recovery_available *bool
125
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500126 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
127 Min_sdk_version *string
128
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900129 HideFromMake bool `blueprint:"mutated"`
130 PreventInstall bool `blueprint:"mutated"`
131
132 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133}
134
135type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700136 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137
Ivan Lozano6a884432020-12-02 09:15:16 -0500138 VendorProperties cc.VendorProperties
139
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 Properties BaseProperties
141
142 hod android.HostOrDeviceSupported
143 multilib android.Multilib
144
Ivan Lozano6a884432020-12-02 09:15:16 -0500145 makeLinkType string
146
Yi Kong46c6e592022-01-20 22:55:00 +0800147 afdo *afdo
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400149 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200150 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500151 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700152 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400153 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700154 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400155
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400156 // Output file to be installed, may be stripped or unstripped.
157 outputFile android.OptionalPath
158
Sasha Smundaka76acba2022-04-18 20:12:56 -0700159 // Cross-reference input file
160 kytheFiles android.Paths
161
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400162 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 }
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500211 case "unstripped":
212 if mod.compiler != nil {
213 return android.PathsIfNonNil(mod.compiler.unstrippedOutputFilePath()), nil
214 }
215 return nil, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400216 default:
217 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
218 }
219}
220
Ivan Lozano52767be2019-10-18 14:49:46 -0700221func (mod *Module) SelectedStl() string {
222 return ""
223}
224
Ivan Lozano2b262972019-11-21 12:30:50 -0800225func (mod *Module) NonCcVariants() bool {
226 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400227 if _, ok := mod.compiler.(libraryInterface); ok {
228 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800229 }
230 }
231 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
232}
233
Ivan Lozano52767be2019-10-18 14:49:46 -0700234func (mod *Module) Static() bool {
235 if mod.compiler != nil {
236 if library, ok := mod.compiler.(libraryInterface); ok {
237 return library.static()
238 }
239 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400240 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700241}
242
243func (mod *Module) Shared() bool {
244 if mod.compiler != nil {
245 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400246 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700247 }
248 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400249 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700250}
251
Ivan Lozanod7586b62021-04-01 09:49:36 -0400252func (mod *Module) Dylib() bool {
253 if mod.compiler != nil {
254 if library, ok := mod.compiler.(libraryInterface); ok {
255 return library.dylib()
256 }
257 }
258 return false
259}
260
261func (mod *Module) Rlib() bool {
262 if mod.compiler != nil {
263 if library, ok := mod.compiler.(libraryInterface); ok {
264 return library.rlib()
265 }
266 }
267 return false
268}
269
270func (mod *Module) Binary() bool {
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400271 if binary, ok := mod.compiler.(binaryInterface); ok {
272 return binary.binary()
Ivan Lozanod7586b62021-04-01 09:49:36 -0400273 }
274 return false
275}
276
Justin Yun5e035862021-06-29 20:50:37 +0900277func (mod *Module) StaticExecutable() bool {
278 if !mod.Binary() {
279 return false
280 }
Ivan Lozano21fa0a52021-11-01 09:19:45 -0400281 return mod.StaticallyLinked()
Justin Yun5e035862021-06-29 20:50:37 +0900282}
283
Ivan Lozanod7586b62021-04-01 09:49:36 -0400284func (mod *Module) Object() bool {
285 // Rust has no modules which produce only object files.
286 return false
287}
288
Ivan Lozano52767be2019-10-18 14:49:46 -0700289func (mod *Module) Toc() android.OptionalPath {
290 if mod.compiler != nil {
Ivan Lozano7b0781d2021-11-03 15:30:18 -0400291 if lib, ok := mod.compiler.(libraryInterface); ok {
292 return lib.toc()
Ivan Lozano52767be2019-10-18 14:49:46 -0700293 }
294 }
295 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
296}
297
Colin Crossc511bc52020-04-07 16:50:32 +0000298func (mod *Module) UseSdk() bool {
299 return false
300}
301
Ivan Lozanod7586b62021-04-01 09:49:36 -0400302func (mod *Module) RelativeInstallPath() string {
303 if mod.compiler != nil {
304 return mod.compiler.relativeInstallPath()
305 }
306 return ""
307}
308
Ivan Lozano52767be2019-10-18 14:49:46 -0700309func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500310 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700311}
312
Jiyong Park7d55b612021-06-11 17:22:09 +0900313func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400314 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900315}
316
Ivan Lozano52767be2019-10-18 14:49:46 -0700317func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400318 return true
319}
320
321func (mod *Module) SubName() string {
322 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700323}
324
325func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500326 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700327 return false
328}
329
Ivan Lozanof9e21722020-12-02 09:00:51 -0500330func (mod *Module) IsVndkExt() bool {
331 return false
332}
333
Ivan Lozanod7586b62021-04-01 09:49:36 -0400334func (mod *Module) IsVndkSp() bool {
335 return false
336}
337
Ivan Lozanof1868af2022-04-12 13:08:36 -0400338func (mod *Module) IsVndkPrebuiltLibrary() bool {
339 // Rust modules do not provide VNDK prebuilts
340 return false
341}
342
343func (mod *Module) IsVendorPublicLibrary() bool {
344 return mod.VendorProperties.IsVendorPublicLibrary
345}
346
347func (mod *Module) SdkAndPlatformVariantVisibleToMake() bool {
348 // Rust modules to not provide Sdk variants
349 return false
350}
351
Colin Cross127bb8b2020-12-16 16:46:01 -0800352func (c *Module) IsVndkPrivate() bool {
353 return false
354}
355
356func (c *Module) IsLlndk() bool {
357 return false
358}
359
360func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500361 return false
362}
363
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400364func (mod *Module) KernelHeadersDecorator() bool {
365 return false
366}
367
Colin Cross1f3f1302021-04-26 18:37:44 -0700368func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400369 return false
370}
371
Colin Cross5271fea2021-04-27 13:06:04 -0700372func (m *Module) NeedsVendorPublicLibraryVariants() bool {
373 return false
374}
375
Ivan Lozanod7586b62021-04-01 09:49:36 -0400376func (mod *Module) HasLlndkStubs() bool {
377 return false
378}
379
380func (mod *Module) StubsVersion() string {
381 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
382}
383
Ivan Lozano52767be2019-10-18 14:49:46 -0700384func (mod *Module) SdkVersion() string {
385 return ""
386}
387
Colin Crossc511bc52020-04-07 16:50:32 +0000388func (mod *Module) AlwaysSdk() bool {
389 return false
390}
391
Jiyong Park2286afd2020-06-16 21:58:53 +0900392func (mod *Module) IsSdkVariant() bool {
393 return false
394}
395
Colin Cross1348ce32020-10-01 13:37:16 -0700396func (mod *Module) SplitPerApiLevel() bool {
397 return false
398}
399
Sasha Smundaka76acba2022-04-18 20:12:56 -0700400func (mod *Module) XrefRustFiles() android.Paths {
401 return mod.kytheFiles
402}
403
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400405 Dylibs []string
406 Rlibs []string
407 Rustlibs []string
408 Stdlibs []string
409 ProcMacros []string
410 SharedLibs []string
411 StaticLibs []string
412 WholeStaticLibs []string
413 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700414
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400415 // Used for data dependencies adjacent to tests
416 DataLibs []string
417 DataBins []string
418
Colin Crossfe605e12022-01-23 20:46:16 -0800419 CrtBegin, CrtEnd []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700420}
421
422type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500423 DyLibs RustLibraries
424 RLibs RustLibraries
425 SharedLibs android.Paths
426 SharedLibDeps android.Paths
427 StaticLibs android.Paths
428 ProcMacros RustLibraries
Yi Kong46c6e592022-01-20 22:55:00 +0800429 AfdoProfiles android.Paths
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500430
431 // depFlags and depLinkFlags are rustc and linker (clang) flags.
432 depFlags []string
433 depLinkFlags []string
434
435 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
436 // Both of these are exported and propagate to dependencies.
437 linkDirs []string
438 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700439
Ivan Lozano45901ed2020-07-24 16:05:01 -0400440 // Used by bindgen modules which call clang
441 depClangFlags []string
442 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400443 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400444 depSystemIncludePaths android.Paths
445
Colin Crossfe605e12022-01-23 20:46:16 -0800446 CrtBegin android.Paths
447 CrtEnd android.Paths
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700448
449 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500450 SrcDeps android.Paths
451 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700452}
453
454type RustLibraries []RustLibrary
455
456type RustLibrary struct {
457 Path android.Path
458 CrateName string
459}
460
461type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100462 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700463 compilerFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozano67eada32021-09-23 11:50:33 -0400464 cfgFlags(ctx ModuleContext, flags Flags) Flags
465 featureFlags(ctx ModuleContext, flags Flags) Flags
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466 compilerProps() []interface{}
Sasha Smundaka76acba2022-04-18 20:12:56 -0700467 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468 compilerDeps(ctx DepsContext, deps Deps) Deps
469 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700470 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100472 // Output directory in which source-generated code from dependencies is
473 // copied. This is equivalent to Cargo's OUT_DIR variable.
474 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700475
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400476 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
477 CargoPkgVersion() string
478
479 // CargoEnvCompat returns whether Cargo environment variables should be used.
480 CargoEnvCompat() bool
481
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800482 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200483 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400485 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400486
487 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400488
489 Disabled() bool
490 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400491
Ivan Lozanodd055472020-09-28 13:22:45 -0400492 stdLinkage(ctx *depsContext) RustLinkage
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500493 noStdlibs() bool
Jiyong Parke54f07e2021-04-07 15:08:04 +0900494
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400495 unstrippedOutputFilePath() android.Path
Jiyong Parke54f07e2021-04-07 15:08:04 +0900496 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400497}
498
Matthew Maurerbb3add12020-06-25 09:34:12 -0700499type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700500 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400501 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700502}
503
Sasha Smundaka76acba2022-04-18 20:12:56 -0700504type xref interface {
505 XrefRustFiles() android.Paths
506}
507
Matthew Maurerbb3add12020-06-25 09:34:12 -0700508type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400509 linkDirs []string
510 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700511}
512
Matthew Maurerbb3add12020-06-25 09:34:12 -0700513func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
514 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
515}
516
Ivan Lozano2093af22020-08-25 12:48:19 -0400517func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
518 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
519}
520
Colin Cross0de8a1e2020-09-18 14:15:30 -0700521func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
522 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700523 LinkDirs: flagExporter.linkDirs,
524 LinkObjects: flagExporter.linkObjects,
525 })
526}
527
Matthew Maurerbb3add12020-06-25 09:34:12 -0700528var _ exportedFlagsProducer = (*flagExporter)(nil)
529
530func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700531 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700532}
533
Colin Cross0de8a1e2020-09-18 14:15:30 -0700534type FlagExporterInfo struct {
535 Flags []string
536 LinkDirs []string // TODO: this should be android.Paths
537 LinkObjects []string // TODO: this should be android.Paths
538}
539
540var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
541
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400542func (mod *Module) isCoverageVariant() bool {
543 return mod.coverage.Properties.IsCoverageVariant
544}
545
546var _ cc.Coverage = (*Module)(nil)
547
548func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
549 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
550}
551
Ivan Lozanod7586b62021-04-01 09:49:36 -0400552func (mod *Module) VndkVersion() string {
553 return mod.Properties.VndkVersion
554}
555
556func (mod *Module) PreventInstall() bool {
557 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400558}
559
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400560func (mod *Module) MarkAsCoverageVariant(coverage bool) {
561 mod.coverage.Properties.IsCoverageVariant = coverage
562}
563
564func (mod *Module) EnableCoverageIfNeeded() {
565 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700566}
567
568func defaultsFactory() android.Module {
569 return DefaultsFactory()
570}
571
572type Defaults struct {
573 android.ModuleBase
574 android.DefaultsModuleBase
575}
576
577func DefaultsFactory(props ...interface{}) android.Module {
578 module := &Defaults{}
579
580 module.AddProperties(props...)
581 module.AddProperties(
582 &BaseProperties{},
Yi Kong46c6e592022-01-20 22:55:00 +0800583 &cc.AfdoProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500584 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100585 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400586 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700587 &BaseCompilerProperties{},
588 &BinaryCompilerProperties{},
589 &LibraryCompilerProperties{},
590 &ProcMacroCompilerProperties{},
591 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400592 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700593 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400594 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400595 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200596 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500597 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700598 )
599
600 android.InitDefaultsModule(module)
601 return module
602}
603
604func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700605 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700606}
607
Ivan Lozano183a3212019-10-18 14:18:45 -0700608func (mod *Module) CcLibrary() bool {
609 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -0500610 if _, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano183a3212019-10-18 14:18:45 -0700611 return true
612 }
613 }
614 return false
615}
616
617func (mod *Module) CcLibraryInterface() bool {
618 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400619 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
620 // VariantIs{Static,Shared} is set.
621 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700622 return true
623 }
624 }
625 return false
626}
627
Ivan Lozano0f9963e2023-02-06 13:31:02 -0500628func (mod *Module) IsFuzzModule() bool {
629 if _, ok := mod.compiler.(*fuzzDecorator); ok {
630 return true
631 }
632 return false
633}
634
635func (mod *Module) FuzzModuleStruct() fuzz.FuzzModule {
636 return mod.FuzzModule
637}
638
639func (mod *Module) FuzzPackagedModule() fuzz.FuzzPackagedModule {
640 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
641 return fuzzer.fuzzPackagedModule
642 }
643 panic(fmt.Errorf("FuzzPackagedModule called on non-fuzz module: %q", mod.BaseModuleName()))
644}
645
646func (mod *Module) FuzzSharedLibraries() android.Paths {
647 if fuzzer, ok := mod.compiler.(*fuzzDecorator); ok {
648 return fuzzer.sharedLibraries
649 }
650 panic(fmt.Errorf("FuzzSharedLibraries called on non-fuzz module: %q", mod.BaseModuleName()))
651}
652
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400653func (mod *Module) UnstrippedOutputFile() android.Path {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400654 if mod.compiler != nil {
655 return mod.compiler.unstrippedOutputFilePath()
Ivan Lozano39b0bf02021-10-14 12:22:09 -0400656 }
657 return nil
658}
659
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800660func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700661 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700662 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800663 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700664 }
665 }
666 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
667}
668
669func (mod *Module) SetStatic() {
670 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700671 if library, ok := mod.compiler.(libraryInterface); ok {
672 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700673 return
674 }
675 }
676 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
677}
678
679func (mod *Module) SetShared() {
680 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700681 if library, ok := mod.compiler.(libraryInterface); ok {
682 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700683 return
684 }
685 }
686 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
687}
688
Ivan Lozano183a3212019-10-18 14:18:45 -0700689func (mod *Module) BuildStaticVariant() bool {
690 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700691 if library, ok := mod.compiler.(libraryInterface); ok {
692 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700693 }
694 }
695 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
696}
697
698func (mod *Module) BuildSharedVariant() bool {
699 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700700 if library, ok := mod.compiler.(libraryInterface); ok {
701 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700702 }
703 }
704 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
705}
706
Ivan Lozano183a3212019-10-18 14:18:45 -0700707func (mod *Module) Module() android.Module {
708 return mod
709}
710
Ivan Lozano183a3212019-10-18 14:18:45 -0700711func (mod *Module) OutputFile() android.OptionalPath {
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400712 return mod.outputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700713}
714
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400715func (mod *Module) CoverageFiles() android.Paths {
716 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800717 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400718 }
719 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
720}
721
Ivan Lozano7f67c2a2022-06-27 16:00:26 -0400722// Rust does not produce gcno files, and therefore does not produce a coverage archive.
723func (mod *Module) CoverageOutputFile() android.OptionalPath {
724 return android.OptionalPath{}
725}
726
727func (mod *Module) IsNdk(config android.Config) bool {
728 return false
729}
730
731func (mod *Module) IsStubs() bool {
732 return false
733}
734
Jiyong Park459feca2020-12-15 11:02:21 +0900735func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Park2811e072021-09-30 17:25:21 +0900736 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900737 return false
738 }
739
Jiyong Park459feca2020-12-15 11:02:21 +0900740 // The apex variant is not installable because it is included in the APEX and won't appear
741 // in the system partition as a standalone file.
742 if !apexInfo.IsForPlatform() {
743 return false
744 }
745
Jiyong Parke54f07e2021-04-07 15:08:04 +0900746 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900747}
748
Ivan Lozanoe950cda2021-11-09 11:26:04 -0500749func (ctx moduleContext) apexVariationName() string {
750 return ctx.Provider(android.ApexInfoProvider).(android.ApexInfo).ApexVariationName
751}
752
Ivan Lozano183a3212019-10-18 14:18:45 -0700753var _ cc.LinkableInterface = (*Module)(nil)
754
Ivan Lozanoffee3342019-08-27 12:03:00 -0700755func (mod *Module) Init() android.Module {
756 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500757 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700758
Yi Kong46c6e592022-01-20 22:55:00 +0800759 if mod.afdo != nil {
760 mod.AddProperties(mod.afdo.props()...)
761 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700762 if mod.compiler != nil {
763 mod.AddProperties(mod.compiler.compilerProps()...)
764 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400765 if mod.coverage != nil {
766 mod.AddProperties(mod.coverage.props()...)
767 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200768 if mod.clippy != nil {
769 mod.AddProperties(mod.clippy.props()...)
770 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400771 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700772 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400773 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500774 if mod.sanitize != nil {
775 mod.AddProperties(mod.sanitize.props()...)
776 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900779 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700780
781 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700782 return mod
783}
784
785func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
786 return &Module{
787 hod: hod,
788 multilib: multilib,
789 }
790}
791func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
792 module := newBaseModule(hod, multilib)
Yi Kong46c6e592022-01-20 22:55:00 +0800793 module.afdo = &afdo{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400794 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200795 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500796 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700797 return module
798}
799
800type ModuleContext interface {
801 android.ModuleContext
802 ModuleContextIntf
803}
804
805type BaseModuleContext interface {
806 android.BaseModuleContext
807 ModuleContextIntf
808}
809
810type DepsContext interface {
811 android.BottomUpMutatorContext
812 ModuleContextIntf
813}
814
815type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200816 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700817 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700818}
819
820type depsContext struct {
821 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700822}
823
824type moduleContext struct {
825 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700826}
827
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200828type baseModuleContext struct {
829 android.BaseModuleContext
830}
831
832func (ctx *moduleContext) RustModule() *Module {
833 return ctx.Module().(*Module)
834}
835
836func (ctx *moduleContext) toolchain() config.Toolchain {
837 return ctx.RustModule().toolchain(ctx)
838}
839
840func (ctx *depsContext) RustModule() *Module {
841 return ctx.Module().(*Module)
842}
843
844func (ctx *depsContext) toolchain() config.Toolchain {
845 return ctx.RustModule().toolchain(ctx)
846}
847
848func (ctx *baseModuleContext) RustModule() *Module {
849 return ctx.Module().(*Module)
850}
851
852func (ctx *baseModuleContext) toolchain() config.Toolchain {
853 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400854}
855
856func (mod *Module) nativeCoverage() bool {
Matthew Maurera61e31f2021-05-27 11:09:11 -0700857 // Bug: http://b/137883967 - native-bridge modules do not currently work with coverage
858 if mod.Target().NativeBridge == android.NativeBridgeEnabled {
859 return false
860 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400861 return mod.compiler != nil && mod.compiler.nativeCoverage()
862}
863
Ivan Lozanod7586b62021-04-01 09:49:36 -0400864func (mod *Module) EverInstallable() bool {
865 return mod.compiler != nil &&
866 // Check to see whether the module is actually ever installable.
867 mod.compiler.everInstallable()
868}
869
870func (mod *Module) Installable() *bool {
871 return mod.Properties.Installable
872}
873
Ivan Lozano872d5792022-03-23 17:31:39 -0400874func (mod *Module) ProcMacro() bool {
875 if pm, ok := mod.compiler.(procMacroInterface); ok {
876 return pm.ProcMacro()
877 }
878 return false
879}
880
Ivan Lozanoffee3342019-08-27 12:03:00 -0700881func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
882 if mod.cachedToolchain == nil {
883 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
884 }
885 return mod.cachedToolchain
886}
887
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200888func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
889 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
890}
891
Ivan Lozanoffee3342019-08-27 12:03:00 -0700892func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
893}
894
895func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
896 ctx := &moduleContext{
897 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700898 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899
Jiyong Park99644e92020-11-17 22:21:02 +0900900 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
901 if !apexInfo.IsForPlatform() {
902 mod.hideApexVariantFromMake = true
903 }
904
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500906 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
907
Ivan Lozanof1868af2022-04-12 13:08:36 -0400908 mod.Properties.SubName = cc.GetSubnameProperty(actx, mod)
Matthew Maurera61e31f2021-05-27 11:09:11 -0700909
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910 if !toolchain.Supported() {
911 // This toolchain's unsupported, there's nothing to do for this mod.
912 return
913 }
914
915 deps := mod.depsToPaths(ctx)
916 flags := Flags{
917 Toolchain: toolchain,
918 }
919
Ivan Lozano67eada32021-09-23 11:50:33 -0400920 // Calculate rustc flags
Yi Kong46c6e592022-01-20 22:55:00 +0800921 if mod.afdo != nil {
922 flags, deps = mod.afdo.flags(ctx, flags, deps)
923 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700924 if mod.compiler != nil {
925 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozano67eada32021-09-23 11:50:33 -0400926 flags = mod.compiler.cfgFlags(ctx, flags)
927 flags = mod.compiler.featureFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400928 }
929 if mod.coverage != nil {
930 flags, deps = mod.coverage.flags(ctx, flags, deps)
931 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200932 if mod.clippy != nil {
933 flags, deps = mod.clippy.flags(ctx, flags, deps)
934 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500935 if mod.sanitize != nil {
936 flags, deps = mod.sanitize.flags(ctx, flags, deps)
937 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400938
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200939 // SourceProvider needs to call GenerateSource() before compiler calls
940 // compile() so it can provide the source. A SourceProvider has
941 // multiple variants (e.g. source, rlib, dylib). Only the "source"
942 // variant is responsible for effectively generating the source. The
943 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400944 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200945 if mod.compiler.(libraryInterface).source() {
946 mod.sourceProvider.GenerateSource(ctx, deps)
947 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
948 } else {
949 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
950 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700951 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200952 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400953 }
954
955 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100956 mod.compiler.initialize(ctx)
Sasha Smundaka76acba2022-04-18 20:12:56 -0700957 buildOutput := mod.compiler.compile(ctx, flags, deps)
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400958 if ctx.Failed() {
959 return
960 }
Sasha Smundaka76acba2022-04-18 20:12:56 -0700961 mod.outputFile = android.OptionalPathForPath(buildOutput.outputFile)
962 if buildOutput.kytheFile != nil {
963 mod.kytheFiles = append(mod.kytheFiles, buildOutput.kytheFile)
964 }
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400965 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), android.OptionalPathForPath(mod.compiler.unstrippedOutputFilePath()))
Jiyong Park459feca2020-12-15 11:02:21 +0900966
Dan Albert06feee92021-03-19 15:06:02 -0700967 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
Matthew Maurere3803632021-12-10 21:57:21 +0000968 if mod.docTimestampFile.Valid() {
969 ctx.CheckbuildFile(mod.docTimestampFile.Path())
970 }
Dan Albert06feee92021-03-19 15:06:02 -0700971
Ivan Lozano1921e802021-05-20 13:39:16 -0400972 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
973 // RECOVERY_SNAPSHOT_VERSION is current.
974 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
975 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
976 lib.collectHeadersForSnapshot(ctx, deps)
977 }
978 }
979
Jiyong Park459feca2020-12-15 11:02:21 +0900980 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
Ivan Lozano872d5792022-03-23 17:31:39 -0400981 if !proptools.BoolDefault(mod.Installable(), mod.EverInstallable()) && !mod.ProcMacro() {
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900982 // If the module has been specifically configure to not be installed then
983 // hide from make as otherwise it will break when running inside make as the
984 // output path to install will not be specified. Not all uninstallable
985 // modules can be hidden from make as some are needed for resolving make
Ivan Lozano872d5792022-03-23 17:31:39 -0400986 // side dependencies. In particular, proc-macros need to be captured in the
987 // host snapshot.
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900988 mod.HideFromMake()
989 } else if !mod.installable(apexInfo) {
990 mod.SkipInstall()
991 }
992
993 // Still call install though, the installs will be stored as PackageSpecs to allow
994 // using the outputs in a genrule.
995 if mod.OutputFile().Valid() {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200996 mod.compiler.install(ctx)
Jiyong Parkd1e366a2021-10-05 09:12:41 +0900997 if ctx.Failed() {
998 return
999 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001000 }
Chris Wailes74be7642021-07-22 16:20:28 -07001001
1002 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001003 }
1004}
1005
1006func (mod *Module) deps(ctx DepsContext) Deps {
1007 deps := Deps{}
1008
1009 if mod.compiler != nil {
1010 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001011 }
1012 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -07001013 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001014 }
1015
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001016 if mod.coverage != nil {
1017 deps = mod.coverage.deps(ctx, deps)
1018 }
1019
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001020 if mod.sanitize != nil {
1021 deps = mod.sanitize.deps(ctx, deps)
1022 }
1023
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
1025 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -07001026 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001027 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
1028 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
1029 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Andrew Walbran797e4be2022-03-07 15:41:53 +00001030 deps.Stdlibs = android.LastUniqueStrings(deps.Stdlibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001031 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032 return deps
1033
1034}
1035
Ivan Lozanoffee3342019-08-27 12:03:00 -07001036type dependencyTag struct {
1037 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001038 name string
1039 library bool
1040 procMacro bool
Colin Cross65cb3142021-12-10 23:05:02 +00001041 dynamic bool
Ivan Lozanoffee3342019-08-27 12:03:00 -07001042}
1043
Jiyong Park65b62242020-11-25 12:44:59 +09001044// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
1045// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
1046func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001047 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +09001048}
1049
1050var _ android.InstallNeededDependencyTag = dependencyTag{}
1051
Colin Cross65cb3142021-12-10 23:05:02 +00001052func (d dependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
1053 if d.library && d.dynamic {
1054 return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
1055 }
1056 return nil
1057}
1058
1059var _ android.LicenseAnnotationsDependencyTag = dependencyTag{}
1060
Ivan Lozanoffee3342019-08-27 12:03:00 -07001061var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001062 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
1063 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
Colin Cross65cb3142021-12-10 23:05:02 +00001064 dylibDepTag = dependencyTag{name: "dylib", library: true, dynamic: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001065 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001066 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001067 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001068 dataLibDepTag = dependencyTag{name: "data lib"}
1069 dataBinDepTag = dependencyTag{name: "data bin"}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001070)
1071
Jiyong Park99644e92020-11-17 22:21:02 +09001072func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
1073 tag, ok := depTag.(dependencyTag)
1074 return ok && tag == dylibDepTag
1075}
1076
Jiyong Park94e22fd2021-04-08 18:19:15 +09001077func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
1078 tag, ok := depTag.(dependencyTag)
1079 return ok && tag == rlibDepTag
1080}
1081
Matthew Maurer0f003b12020-06-29 14:34:06 -07001082type autoDep struct {
1083 variation string
1084 depTag dependencyTag
1085}
1086
1087var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001088 rlibVariation = "rlib"
1089 dylibVariation = "dylib"
1090 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
1091 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -07001092)
1093
1094type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -05001095 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -07001096}
1097
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001098func (mod *Module) begin(ctx BaseModuleContext) {
1099 if mod.coverage != nil {
1100 mod.coverage.begin(ctx)
1101 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -05001102 if mod.sanitize != nil {
1103 mod.sanitize.begin(ctx)
1104 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001105}
1106
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001107func (mod *Module) Prebuilt() *android.Prebuilt {
Ivan Lozano872d5792022-03-23 17:31:39 -04001108 if p, ok := mod.compiler.(rustPrebuilt); ok {
Ivan Lozanofba2aa22021-11-11 09:29:07 -05001109 return p.prebuilt()
1110 }
1111 return nil
1112}
1113
Justin Yun24b246a2023-03-16 10:36:16 +09001114func rustMakeLibName(ctx android.ModuleContext, c cc.LinkableInterface, dep cc.LinkableInterface, depName string) string {
1115 if rustDep, ok := dep.(*Module); ok {
1116 // Use base module name for snapshots when exporting to Makefile.
1117 if snapshotPrebuilt, ok := rustDep.compiler.(cc.SnapshotInterface); ok {
1118 baseName := rustDep.BaseModuleName()
1119 return baseName + snapshotPrebuilt.SnapshotAndroidMkSuffix() + rustDep.AndroidMkSuffix()
1120 }
1121 }
1122 return cc.MakeLibName(ctx, c, dep, depName)
1123}
1124
Ivan Lozanoffee3342019-08-27 12:03:00 -07001125func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
1126 var depPaths PathDeps
1127
1128 directRlibDeps := []*Module{}
1129 directDylibDeps := []*Module{}
1130 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +09001131 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -07001132 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001133 directSrcProvidersDeps := []*Module{}
1134 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -07001135
Ivan Lozanoe950cda2021-11-09 11:26:04 -05001136 // For the dependency from platform to apex, use the latest stubs
1137 mod.apexSdkVersion = android.FutureApiLevel
1138 apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
1139 if !apexInfo.IsForPlatform() {
1140 mod.apexSdkVersion = apexInfo.MinSdkVersion
1141 }
1142
1143 if android.InList("hwaddress", ctx.Config().SanitizeDevice()) {
1144 // In hwasan build, we override apexSdkVersion to the FutureApiLevel(10000)
1145 // so that even Q(29/Android10) apexes could use the dynamic unwinder by linking the newer stubs(e.g libc(R+)).
1146 // (b/144430859)
1147 mod.apexSdkVersion = android.FutureApiLevel
1148 }
1149
Ivan Lozanoffee3342019-08-27 12:03:00 -07001150 ctx.VisitDirectDeps(func(dep android.Module) {
1151 depName := ctx.OtherModuleName(dep)
1152 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001153
Ivan Lozano89435d12020-07-31 11:01:18 -04001154 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001155 //Handle Rust Modules
Justin Yun24b246a2023-03-16 10:36:16 +09001156 makeLibName := rustMakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -07001157
Ivan Lozanoffee3342019-08-27 12:03:00 -07001158 switch depTag {
1159 case dylibDepTag:
1160 dylib, ok := rustDep.compiler.(libraryInterface)
1161 if !ok || !dylib.dylib() {
1162 ctx.ModuleErrorf("mod %q not an dylib library", depName)
1163 return
1164 }
1165 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001166 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001167 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -04001168
Ivan Lozanoffee3342019-08-27 12:03:00 -07001169 rlib, ok := rustDep.compiler.(libraryInterface)
1170 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001171 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001172 return
1173 }
1174 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001175 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001176 case procMacroDepTag:
1177 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001178 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001179 }
1180
1181 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001182 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1183 // OS/Arch variant is used.
1184 var helper string
1185 if ctx.Host() {
1186 helper = "missing 'host_supported'?"
1187 } else {
1188 helper = "device module defined?"
1189 }
1190
1191 if dep.Target().Os != ctx.Os() {
1192 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1193 return
1194 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1195 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1196 return
1197 }
1198 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001199 }
1200
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001201 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001202 if depTag != procMacroDepTag {
1203 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1204 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1205 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1206 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001207 }
1208
Ivan Lozanoffee3342019-08-27 12:03:00 -07001209 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001210 linkFile := rustDep.UnstrippedOutputFile()
1211 linkDir := linkPathFromFilePath(linkFile)
Matthew Maurerbb3add12020-06-25 09:34:12 -07001212 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1213 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001214 }
1215 }
1216
Ivan Lozano89435d12020-07-31 11:01:18 -04001217 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001218 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001219 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001220 if _, ok := ccDep.(*Module); !ok {
1221 if ccDep.Module().Target().Os != ctx.Os() {
1222 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1223 return
1224 }
1225 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1226 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1227 return
1228 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001229 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001230 linkObject := ccDep.OutputFile()
1231 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001232
Ivan Lozano2093af22020-08-25 12:48:19 -04001233 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001234 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1235 }
1236
1237 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001238 switch {
1239 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001240 if cc.IsWholeStaticLib(depTag) {
1241 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1242 // if the library is not prefixed by "lib".
Ivan Lozanofdadcd72021-11-01 09:04:23 -04001243 if mod.Binary() {
1244 // Binaries may sometimes need to link whole static libraries that don't start with 'lib'.
1245 // Since binaries don't need to 'rebundle' these like libraries and only use these for the
1246 // final linkage, pass the args directly to the linker to handle these cases.
1247 depPaths.depLinkFlags = append(depPaths.depLinkFlags, []string{"-Wl,--whole-archive", linkObject.Path().String(), "-Wl,--no-whole-archive"}...)
1248 } else if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001249 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001250 } else {
1251 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 -05001252 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001253 }
1254
1255 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1256 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001257 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001258 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1259
Colin Cross0de8a1e2020-09-18 14:15:30 -07001260 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1261 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1262 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1263 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1264 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001265 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Justin Yun2b3ed642022-02-16 08:15:07 +09001266
1267 // Record baseLibName for snapshots.
1268 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
1269
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001270 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001271 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001272 // For the shared lib dependencies, we may link to the stub variant
1273 // of the dependency depending on the context (e.g. if this
1274 // dependency crosses the APEX boundaries).
1275 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1276
1277 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1278 // object (either from stub or non-stub) to use.
1279 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1280 linkPath = linkPathFromFilePath(linkObject.Path())
1281
Ivan Lozanoffee3342019-08-27 12:03:00 -07001282 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001283 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001284 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1285 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1286 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1287 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001288 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001289
1290 // Record baseLibName for snapshots.
1291 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
1292
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001293 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001294 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001295 case cc.IsHeaderDepTag(depTag):
1296 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1297 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1298 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1299 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001300 case depTag == cc.CrtBeginDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001301 depPaths.CrtBegin = append(depPaths.CrtBegin, linkObject.Path())
Colin Cross6e511a92020-07-27 21:26:48 -07001302 case depTag == cc.CrtEndDepTag:
Colin Crossfe605e12022-01-23 20:46:16 -08001303 depPaths.CrtEnd = append(depPaths.CrtEnd, linkObject.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001304 }
1305
1306 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001307 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1308 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001309 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001310 }
Colin Cross018cbeb2022-01-24 17:22:45 -08001311 } else {
1312 switch {
1313 case depTag == cc.CrtBeginDepTag:
1314 depPaths.CrtBegin = append(depPaths.CrtBegin, android.OutputFileForModule(ctx, dep, ""))
1315 case depTag == cc.CrtEndDepTag:
1316 depPaths.CrtEnd = append(depPaths.CrtEnd, android.OutputFileForModule(ctx, dep, ""))
1317 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001318 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001319
1320 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001321 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001322 // These are usually genrules which don't have per-target variants.
1323 directSrcDeps = append(directSrcDeps, srcDep)
1324 }
1325 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001326 })
1327
1328 var rlibDepFiles RustLibraries
1329 for _, dep := range directRlibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001330 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001331 }
1332 var dylibDepFiles RustLibraries
1333 for _, dep := range directDylibDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001334 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001335 }
1336 var procMacroDepFiles RustLibraries
1337 for _, dep := range directProcMacroDeps {
Ivan Lozano8d10fc32021-11-05 16:36:47 -04001338 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.UnstrippedOutputFile(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001339 }
1340
1341 var staticLibDepFiles android.Paths
1342 for _, dep := range directStaticLibDeps {
1343 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1344 }
1345
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001346 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001347 var sharedLibDepFiles android.Paths
1348 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001349 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1350 if dep.TableOfContents.Valid() {
1351 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001352 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001353 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001354 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001355 }
1356
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001357 var srcProviderDepFiles android.Paths
1358 for _, dep := range directSrcProvidersDeps {
1359 srcs, _ := dep.OutputFiles("")
1360 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1361 }
1362 for _, dep := range directSrcDeps {
1363 srcs := dep.Srcs()
1364 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1365 }
1366
Ivan Lozanoffee3342019-08-27 12:03:00 -07001367 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1368 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1369 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001370 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001371 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1372 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001373 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001374
1375 // Dedup exported flags from dependencies
1376 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001377 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001378 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001379 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1380 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1381 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001382
1383 return depPaths
1384}
1385
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001386func (mod *Module) InstallInData() bool {
1387 if mod.compiler == nil {
1388 return false
1389 }
1390 return mod.compiler.inData()
1391}
1392
Matthew Maurer9f59e8d2021-08-19 13:10:05 -07001393func (mod *Module) InstallInRamdisk() bool {
1394 return mod.InRamdisk()
1395}
1396
1397func (mod *Module) InstallInVendorRamdisk() bool {
1398 return mod.InVendorRamdisk()
1399}
1400
1401func (mod *Module) InstallInRecovery() bool {
1402 return mod.InRecovery()
1403}
1404
Ivan Lozanoffee3342019-08-27 12:03:00 -07001405func linkPathFromFilePath(filepath android.Path) string {
1406 return strings.Split(filepath.String(), filepath.Base())[0]
1407}
Ivan Lozanod648c432020-02-06 12:05:10 -05001408
Ivan Lozanoffee3342019-08-27 12:03:00 -07001409func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1410 ctx := &depsContext{
1411 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001412 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001413
1414 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001415 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001416 var snapshotInfo *cc.SnapshotInfo
1417
Kiyoung Kim487689e2022-07-26 09:48:22 +09001418 apiImportInfo := cc.GetApiImports(mod, actx)
1419 for idx, lib := range deps.SharedLibs {
1420 deps.SharedLibs[idx] = cc.GetReplaceModuleName(lib, apiImportInfo.SharedLibs)
1421 }
1422
Ivan Lozano1921e802021-05-20 13:39:16 -04001423 if ctx.Os() == android.Android {
1424 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1425 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001426
Ivan Lozano2b081132020-09-08 12:46:52 -04001427 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001428 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001429 stdLinkage = "rlib-std"
1430 }
1431
1432 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001433
Ivan Lozano2b081132020-09-08 12:46:52 -04001434 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1435 rlibDepVariations = append(rlibDepVariations,
1436 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1437 }
1438
Ivan Lozano1921e802021-05-20 13:39:16 -04001439 // rlibs
Ivan Lozano2d407632022-04-07 12:59:11 -04001440 rlibDepVariations = append(rlibDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: rlibVariation})
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001441 for _, lib := range deps.Rlibs {
1442 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001443 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001444
Ivan Lozano2d407632022-04-07 12:59:11 -04001445 actx.AddVariationDependencies(rlibDepVariations, depTag, lib)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001446 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001447
1448 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001449 actx.AddVariationDependencies(
1450 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001451 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001452 dylibDepTag, deps.Dylibs...)
1453
Ivan Lozano1921e802021-05-20 13:39:16 -04001454 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001455 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1456 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2d407632022-04-07 12:59:11 -04001457 for _, lib := range deps.Rustlibs {
1458 if autoDep.depTag == rlibDepTag {
1459 // Handle the rlib deptag case
1460 addRlibDependency(actx, lib, mod, snapshotInfo, rlibDepVariations)
1461 } else {
1462 // autoDep.depTag is a dylib depTag. Not all rustlibs may be available as a dylib however.
1463 // Check for the existence of the dylib deptag variant. Select it if available,
1464 // otherwise select the rlib variant.
1465 autoDepVariations := append(commonDepVariations,
1466 blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation})
1467 if actx.OtherModuleDependencyVariantExists(autoDepVariations, lib) {
1468 actx.AddVariationDependencies(autoDepVariations, autoDep.depTag, lib)
1469 } else {
1470 // If there's no dylib dependency available, try to add the rlib dependency instead.
1471 addRlibDependency(actx, lib, mod, snapshotInfo, rlibDepVariations)
1472 }
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001473 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001474 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001475 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001476 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001477 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001478 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001479 for _, lib := range deps.Stdlibs {
1480 depTag := rlibDepTag
Kiyoung Kim487689e2022-07-26 09:48:22 +09001481 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001482
1483 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1484 depTag, lib)
1485 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001486 } else {
1487 actx.AddVariationDependencies(
1488 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1489 dylibDepTag, deps.Stdlibs...)
1490 }
1491 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001492
1493 for _, lib := range deps.SharedLibs {
1494 depTag := cc.SharedDepTag()
1495 name, version := cc.StubsLibNameAndVersion(lib)
1496
1497 variations := []blueprint.Variation{
1498 {Mutator: "link", Variation: "shared"},
1499 }
1500 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1501 }
1502
1503 for _, lib := range deps.WholeStaticLibs {
1504 depTag := cc.StaticDepTag(true)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001505 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001506
1507 actx.AddVariationDependencies([]blueprint.Variation{
1508 {Mutator: "link", Variation: "static"},
1509 }, depTag, lib)
1510 }
1511
1512 for _, lib := range deps.StaticLibs {
1513 depTag := cc.StaticDepTag(false)
Kiyoung Kim487689e2022-07-26 09:48:22 +09001514 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
Ivan Lozano1921e802021-05-20 13:39:16 -04001515
1516 actx.AddVariationDependencies([]blueprint.Variation{
1517 {Mutator: "link", Variation: "static"},
1518 }, depTag, lib)
1519 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001520
Zach Johnson3df4e632020-11-06 11:56:27 -08001521 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1522
Colin Cross565cafd2020-09-25 18:47:38 -07001523 crtVariations := cc.GetCrtVariations(ctx, mod)
Colin Crossfe605e12022-01-23 20:46:16 -08001524 for _, crt := range deps.CrtBegin {
Ivan Lozano1921e802021-05-20 13:39:16 -04001525 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001526 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001527 }
Colin Crossfe605e12022-01-23 20:46:16 -08001528 for _, crt := range deps.CrtEnd {
Ivan Lozano1921e802021-05-20 13:39:16 -04001529 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
Kiyoung Kim487689e2022-07-26 09:48:22 +09001530 cc.GetReplaceModuleName(crt, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001531 }
1532
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001533 if mod.sourceProvider != nil {
1534 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1535 bindgen.Properties.Custom_bindgen != "" {
1536 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1537 bindgen.Properties.Custom_bindgen)
1538 }
1539 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001540
Ivan Lozano4e5f07d2021-11-04 14:09:38 -04001541 actx.AddVariationDependencies([]blueprint.Variation{
1542 {Mutator: "link", Variation: "shared"},
1543 }, dataLibDepTag, deps.DataLibs...)
1544
1545 actx.AddVariationDependencies(nil, dataBinDepTag, deps.DataBins...)
1546
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001547 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001548 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001549}
1550
Ivan Lozano2d407632022-04-07 12:59:11 -04001551// addRlibDependency will add an rlib dependency, rewriting to the snapshot library if available.
1552func addRlibDependency(actx android.BottomUpMutatorContext, lib string, mod *Module, snapshotInfo *cc.SnapshotInfo, variations []blueprint.Variation) {
Kiyoung Kim487689e2022-07-26 09:48:22 +09001553 lib = cc.GetReplaceModuleName(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
Ivan Lozano2d407632022-04-07 12:59:11 -04001554 actx.AddVariationDependencies(variations, rlibDepTag, lib)
1555}
1556
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001557func BeginMutator(ctx android.BottomUpMutatorContext) {
1558 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1559 mod.beginMutator(ctx)
1560 }
1561}
1562
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001563func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1564 ctx := &baseModuleContext{
1565 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001566 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001567
1568 mod.begin(ctx)
1569}
1570
Ivan Lozanoffee3342019-08-27 12:03:00 -07001571func (mod *Module) Name() string {
1572 name := mod.ModuleBase.Name()
1573 if p, ok := mod.compiler.(interface {
1574 Name(string) string
1575 }); ok {
1576 name = p.Name(name)
1577 }
1578 return name
1579}
1580
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001581func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001582 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001583 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001584 }
1585}
1586
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001587var _ android.HostToolProvider = (*Module)(nil)
Ivan Lozano872d5792022-03-23 17:31:39 -04001588var _ snapshot.RelativeInstallPath = (*Module)(nil)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001589
1590func (mod *Module) HostToolPath() android.OptionalPath {
1591 if !mod.Host() {
1592 return android.OptionalPath{}
1593 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001594 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1595 return android.OptionalPathForPath(binary.baseCompiler.path)
Ivan Lozano872d5792022-03-23 17:31:39 -04001596 } else if pm, ok := mod.compiler.(*procMacroDecorator); ok {
1597 // Even though proc-macros aren't strictly "tools", since they target the compiler
1598 // and act as compiler plugins, we treat them similarly.
1599 return android.OptionalPathForPath(pm.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001600 }
1601 return android.OptionalPath{}
1602}
1603
Jiyong Park99644e92020-11-17 22:21:02 +09001604var _ android.ApexModule = (*Module)(nil)
1605
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001606func (mod *Module) MinSdkVersion() string {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001607 return String(mod.Properties.Min_sdk_version)
1608}
1609
Jiyong Park45bf82e2020-12-15 22:29:02 +09001610// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001611func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozanoa91ba252022-01-11 12:02:06 -05001612 minSdkVersion := mod.MinSdkVersion()
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001613 if minSdkVersion == "apex_inherit" {
1614 return nil
1615 }
1616 if minSdkVersion == "" {
1617 return fmt.Errorf("min_sdk_version is not specificed")
1618 }
1619
1620 // Not using nativeApiLevelFromUser because the context here is not
1621 // necessarily a native context.
1622 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1623 if err != nil {
1624 return err
1625 }
1626
1627 if ver.GreaterThan(sdkVersion) {
1628 return fmt.Errorf("newer SDK(%v)", ver)
1629 }
Jiyong Park99644e92020-11-17 22:21:02 +09001630 return nil
1631}
1632
Jiyong Park45bf82e2020-12-15 22:29:02 +09001633// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001634func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1635 depTag := ctx.OtherModuleDependencyTag(dep)
1636
1637 if ccm, ok := dep.(*cc.Module); ok {
1638 if ccm.HasStubsVariants() {
1639 if cc.IsSharedDepTag(depTag) {
1640 // dynamic dep to a stubs lib crosses APEX boundary
1641 return false
1642 }
1643 if cc.IsRuntimeDepTag(depTag) {
1644 // runtime dep to a stubs lib also crosses APEX boundary
1645 return false
1646 }
1647
1648 if cc.IsHeaderDepTag(depTag) {
1649 return false
1650 }
1651 }
1652 if mod.Static() && cc.IsSharedDepTag(depTag) {
1653 // shared_lib dependency from a static lib is considered as crossing
1654 // the APEX boundary because the dependency doesn't actually is
1655 // linked; the dependency is used only during the compilation phase.
1656 return false
1657 }
1658 }
1659
Matthew Maurer581b6d82022-09-29 16:46:25 -07001660 if depTag == procMacroDepTag || depTag == customBindgenDepTag {
Jiyong Park99644e92020-11-17 22:21:02 +09001661 return false
1662 }
1663
1664 return true
1665}
1666
1667// Overrides ApexModule.IsInstallabeToApex()
1668func (mod *Module) IsInstallableToApex() bool {
1669 if mod.compiler != nil {
Ivan Lozano45e0e5b2021-11-13 07:42:36 -05001670 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.shared() || lib.dylib()) {
Jiyong Park99644e92020-11-17 22:21:02 +09001671 return true
1672 }
1673 if _, ok := mod.compiler.(*binaryDecorator); ok {
1674 return true
1675 }
1676 }
1677 return false
1678}
1679
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001680// If a library file has a "lib" prefix, extract the library name without the prefix.
1681func libNameFromFilePath(filepath android.Path) (string, bool) {
1682 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1683 if strings.HasPrefix(libName, "lib") {
1684 libName = libName[3:]
1685 return libName, true
1686 }
1687 return "", false
1688}
1689
Sasha Smundaka76acba2022-04-18 20:12:56 -07001690func kytheExtractRustFactory() android.Singleton {
1691 return &kytheExtractRustSingleton{}
1692}
1693
1694type kytheExtractRustSingleton struct {
1695}
1696
1697func (k kytheExtractRustSingleton) GenerateBuildActions(ctx android.SingletonContext) {
1698 var xrefTargets android.Paths
1699 ctx.VisitAllModules(func(module android.Module) {
1700 if rustModule, ok := module.(xref); ok {
1701 xrefTargets = append(xrefTargets, rustModule.XrefRustFiles()...)
1702 }
1703 })
1704 if len(xrefTargets) > 0 {
1705 ctx.Phony("xref_rust", xrefTargets...)
1706 }
1707}
1708
Jihoon Kangf78a8902022-09-01 22:47:07 +00001709func (c *Module) Partition() string {
1710 return ""
1711}
1712
Ivan Lozanoffee3342019-08-27 12:03:00 -07001713var Bool = proptools.Bool
1714var BoolDefault = proptools.BoolDefault
1715var String = proptools.String
1716var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001717
1718var _ android.OutputFileProducer = (*Module)(nil)