blob: 4ceeef1d10c82ad9364a210f913ab0eca5b1bd54 [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"`
87 ExtraVariants []string `blueprint:"mutated"`
88
Ivan Lozanoa2268632021-07-22 10:52:06 -040089 // Allows this module to use non-APEX version of libraries. Useful
90 // for building binaries that are started before APEXes are activated.
91 Bootstrap *bool
92
Ivan Lozano1921e802021-05-20 13:39:16 -040093 // Used by vendor snapshot to record dependencies from snapshot modules.
94 SnapshotSharedLibs []string `blueprint:"mutated"`
Justin Yun5e035862021-06-29 20:50:37 +090095 SnapshotStaticLibs []string `blueprint:"mutated"`
Ivan Lozano1921e802021-05-20 13:39:16 -040096
Ivan Lozanoe6d30982021-02-05 10:57:43 -050097 // Make this module available when building for vendor ramdisk.
98 // On device without a dedicated recovery partition, the module is only
99 // available after switching root into
100 // /first_stage_ramdisk. To expose the module before switching root, install
101 // the recovery variant instead (TODO(b/165791368) recovery not yet supported)
102 Vendor_ramdisk_available *bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400103
Ivan Lozano1921e802021-05-20 13:39:16 -0400104 // Normally Soong uses the directory structure to decide which modules
105 // should be included (framework) or excluded (non-framework) from the
106 // different snapshots (vendor, recovery, etc.), but this property
107 // allows a partner to exclude a module normally thought of as a
108 // framework module from the vendor snapshot.
109 Exclude_from_vendor_snapshot *bool
110
111 // Normally Soong uses the directory structure to decide which modules
112 // should be included (framework) or excluded (non-framework) from the
113 // different snapshots (vendor, recovery, etc.), but this property
114 // allows a partner to exclude a module normally thought of as a
115 // framework module from the recovery snapshot.
116 Exclude_from_recovery_snapshot *bool
117
Ivan Lozano3e9f9e42020-12-04 15:05:43 -0500118 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
119 Min_sdk_version *string
120
Ivan Lozano43845682020-07-09 21:03:28 -0400121 PreventInstall bool
122 HideFromMake bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400123 Installable *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124}
125
126type Module struct {
hamzehc0a671f2021-07-22 12:05:08 -0700127 fuzz.FuzzModule
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128
Ivan Lozano6a884432020-12-02 09:15:16 -0500129 VendorProperties cc.VendorProperties
130
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131 Properties BaseProperties
132
133 hod android.HostOrDeviceSupported
134 multilib android.Multilib
135
Ivan Lozano6a884432020-12-02 09:15:16 -0500136 makeLinkType string
137
Ivan Lozanoffee3342019-08-27 12:03:00 -0700138 compiler compiler
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400139 coverage *coverage
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200140 clippy *clippy
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500141 sanitize *sanitize
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142 cachedToolchain config.Toolchain
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400143 sourceProvider SourceProvider
Andrei Homescuc7767922020-08-05 06:36:19 -0700144 subAndroidMkOnce map[SubAndroidMkProvider]bool
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400145
Jiyong Parke54f07e2021-04-07 15:08:04 +0900146 // Unstripped output. This is usually used when this module is linked to another module
147 // as a library. The stripped output which is used for installation can be found via
148 // compiler.strippedOutputFile if it exists.
149 unstrippedOutputFile android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700150 docTimestampFile android.OptionalPath
Jiyong Park99644e92020-11-17 22:21:02 +0900151
152 hideApexVariantFromMake bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153}
154
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500155func (mod *Module) Header() bool {
156 //TODO: If Rust libraries provide header variants, this needs to be updated.
157 return false
158}
159
160func (mod *Module) SetPreventInstall() {
161 mod.Properties.PreventInstall = true
162}
163
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500164func (mod *Module) SetHideFromMake() {
165 mod.Properties.HideFromMake = true
166}
167
Ivan Lozanod7586b62021-04-01 09:49:36 -0400168func (c *Module) HiddenFromMake() bool {
169 return c.Properties.HideFromMake
170}
171
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500172func (mod *Module) SanitizePropDefined() bool {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500173 // Because compiler is not set for some Rust modules where sanitize might be set, check that compiler is also not
174 // nil since we need compiler to actually sanitize.
175 return mod.sanitize != nil && mod.compiler != nil
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500176}
177
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500178func (mod *Module) IsPrebuilt() bool {
179 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
180 return true
181 }
182 return false
183}
184
Ivan Lozano43845682020-07-09 21:03:28 -0400185func (mod *Module) OutputFiles(tag string) (android.Paths, error) {
186 switch tag {
187 case "":
Andrei Homescu5db69cc2020-08-06 15:27:45 -0700188 if mod.sourceProvider != nil && (mod.compiler == nil || mod.compiler.Disabled()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400189 return mod.sourceProvider.Srcs(), nil
190 } else {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900191 if mod.OutputFile().Valid() {
192 return android.Paths{mod.OutputFile().Path()}, nil
Ivan Lozano43845682020-07-09 21:03:28 -0400193 }
194 return android.Paths{}, nil
195 }
196 default:
197 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
198 }
199}
200
Ivan Lozano52767be2019-10-18 14:49:46 -0700201func (mod *Module) SelectedStl() string {
202 return ""
203}
204
Ivan Lozano2b262972019-11-21 12:30:50 -0800205func (mod *Module) NonCcVariants() bool {
206 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400207 if _, ok := mod.compiler.(libraryInterface); ok {
208 return false
Ivan Lozano2b262972019-11-21 12:30:50 -0800209 }
210 }
211 panic(fmt.Errorf("NonCcVariants called on non-library module: %q", mod.BaseModuleName()))
212}
213
Ivan Lozano52767be2019-10-18 14:49:46 -0700214func (mod *Module) Static() bool {
215 if mod.compiler != nil {
216 if library, ok := mod.compiler.(libraryInterface); ok {
217 return library.static()
218 }
219 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400220 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700221}
222
223func (mod *Module) Shared() bool {
224 if mod.compiler != nil {
225 if library, ok := mod.compiler.(libraryInterface); ok {
Ivan Lozano89435d12020-07-31 11:01:18 -0400226 return library.shared()
Ivan Lozano52767be2019-10-18 14:49:46 -0700227 }
228 }
Ivan Lozano89435d12020-07-31 11:01:18 -0400229 return false
Ivan Lozano52767be2019-10-18 14:49:46 -0700230}
231
Ivan Lozanod7586b62021-04-01 09:49:36 -0400232func (mod *Module) Dylib() bool {
233 if mod.compiler != nil {
234 if library, ok := mod.compiler.(libraryInterface); ok {
235 return library.dylib()
236 }
237 }
238 return false
239}
240
241func (mod *Module) Rlib() bool {
242 if mod.compiler != nil {
243 if library, ok := mod.compiler.(libraryInterface); ok {
244 return library.rlib()
245 }
246 }
247 return false
248}
249
250func (mod *Module) Binary() bool {
251 if mod.compiler != nil {
252 if _, ok := mod.compiler.(*binaryDecorator); ok {
253 return true
254 }
255 }
256 return false
257}
258
Justin Yun5e035862021-06-29 20:50:37 +0900259func (mod *Module) StaticExecutable() bool {
260 if !mod.Binary() {
261 return false
262 }
263 return Bool(mod.compiler.(*binaryDecorator).Properties.Static_executable)
264}
265
Ivan Lozanod7586b62021-04-01 09:49:36 -0400266func (mod *Module) Object() bool {
267 // Rust has no modules which produce only object files.
268 return false
269}
270
Ivan Lozano52767be2019-10-18 14:49:46 -0700271func (mod *Module) Toc() android.OptionalPath {
272 if mod.compiler != nil {
273 if _, ok := mod.compiler.(libraryInterface); ok {
274 return android.OptionalPath{}
275 }
276 }
277 panic(fmt.Errorf("Toc() called on non-library module: %q", mod.BaseModuleName()))
278}
279
Colin Crossc511bc52020-04-07 16:50:32 +0000280func (mod *Module) UseSdk() bool {
281 return false
282}
283
Ivan Lozanod7586b62021-04-01 09:49:36 -0400284func (mod *Module) RelativeInstallPath() string {
285 if mod.compiler != nil {
286 return mod.compiler.relativeInstallPath()
287 }
288 return ""
289}
290
Ivan Lozano52767be2019-10-18 14:49:46 -0700291func (mod *Module) UseVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500292 return mod.Properties.VndkVersion != ""
Ivan Lozano52767be2019-10-18 14:49:46 -0700293}
294
Jiyong Park7d55b612021-06-11 17:22:09 +0900295func (mod *Module) Bootstrap() bool {
Ivan Lozanoa2268632021-07-22 10:52:06 -0400296 return Bool(mod.Properties.Bootstrap)
Jiyong Park7d55b612021-06-11 17:22:09 +0900297}
298
Ivan Lozano52767be2019-10-18 14:49:46 -0700299func (mod *Module) MustUseVendorVariant() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400300 return true
301}
302
303func (mod *Module) SubName() string {
304 return mod.Properties.SubName
Ivan Lozano52767be2019-10-18 14:49:46 -0700305}
306
307func (mod *Module) IsVndk() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500308 // TODO(b/165791368)
Ivan Lozano52767be2019-10-18 14:49:46 -0700309 return false
310}
311
Ivan Lozanof9e21722020-12-02 09:00:51 -0500312func (mod *Module) IsVndkExt() bool {
313 return false
314}
315
Ivan Lozanod7586b62021-04-01 09:49:36 -0400316func (mod *Module) IsVndkSp() bool {
317 return false
318}
319
Colin Cross127bb8b2020-12-16 16:46:01 -0800320func (c *Module) IsVndkPrivate() bool {
321 return false
322}
323
324func (c *Module) IsLlndk() bool {
325 return false
326}
327
328func (c *Module) IsLlndkPublic() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -0500329 return false
330}
331
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400332func (mod *Module) KernelHeadersDecorator() bool {
333 return false
334}
335
Colin Cross1f3f1302021-04-26 18:37:44 -0700336func (m *Module) NeedsLlndkVariants() bool {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400337 return false
338}
339
Colin Cross5271fea2021-04-27 13:06:04 -0700340func (m *Module) NeedsVendorPublicLibraryVariants() bool {
341 return false
342}
343
Ivan Lozanod7586b62021-04-01 09:49:36 -0400344func (mod *Module) HasLlndkStubs() bool {
345 return false
346}
347
348func (mod *Module) StubsVersion() string {
349 panic(fmt.Errorf("StubsVersion called on non-versioned module: %q", mod.BaseModuleName()))
350}
351
Ivan Lozano52767be2019-10-18 14:49:46 -0700352func (mod *Module) SdkVersion() string {
353 return ""
354}
355
Jiyong Parkfdaa5f72021-03-19 22:18:04 +0900356func (mod *Module) MinSdkVersion() string {
357 return ""
358}
359
Colin Crossc511bc52020-04-07 16:50:32 +0000360func (mod *Module) AlwaysSdk() bool {
361 return false
362}
363
Jiyong Park2286afd2020-06-16 21:58:53 +0900364func (mod *Module) IsSdkVariant() bool {
365 return false
366}
367
Colin Cross1348ce32020-10-01 13:37:16 -0700368func (mod *Module) SplitPerApiLevel() bool {
369 return false
370}
371
Ivan Lozanoffee3342019-08-27 12:03:00 -0700372type Deps struct {
Ivan Lozano63bb7682021-03-23 15:53:44 -0400373 Dylibs []string
374 Rlibs []string
375 Rustlibs []string
376 Stdlibs []string
377 ProcMacros []string
378 SharedLibs []string
379 StaticLibs []string
380 WholeStaticLibs []string
381 HeaderLibs []string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382
383 CrtBegin, CrtEnd string
384}
385
386type PathDeps struct {
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500387 DyLibs RustLibraries
388 RLibs RustLibraries
389 SharedLibs android.Paths
390 SharedLibDeps android.Paths
391 StaticLibs android.Paths
392 ProcMacros RustLibraries
Ivan Lozano3dfa12d2021-02-04 11:29:41 -0500393
394 // depFlags and depLinkFlags are rustc and linker (clang) flags.
395 depFlags []string
396 depLinkFlags []string
397
398 // linkDirs are link paths passed via -L to rustc. linkObjects are objects passed directly to the linker.
399 // Both of these are exported and propagate to dependencies.
400 linkDirs []string
401 linkObjects []string
Ivan Lozanof1c84332019-09-20 11:00:37 -0700402
Ivan Lozano45901ed2020-07-24 16:05:01 -0400403 // Used by bindgen modules which call clang
404 depClangFlags []string
405 depIncludePaths android.Paths
Ivan Lozanoddd0bdb2020-08-28 17:00:26 -0400406 depGeneratedHeaders android.Paths
Ivan Lozano45901ed2020-07-24 16:05:01 -0400407 depSystemIncludePaths android.Paths
408
Ivan Lozanof1c84332019-09-20 11:00:37 -0700409 CrtBegin android.OptionalPath
410 CrtEnd android.OptionalPath
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700411
412 // Paths to generated source files
Ivan Lozano9d74a522020-12-01 09:25:22 -0500413 SrcDeps android.Paths
414 srcProviderFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415}
416
417type RustLibraries []RustLibrary
418
419type RustLibrary struct {
420 Path android.Path
421 CrateName string
422}
423
424type compiler interface {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100425 initialize(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700426 compilerFlags(ctx ModuleContext, flags Flags) Flags
427 compilerProps() []interface{}
428 compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path
429 compilerDeps(ctx DepsContext, deps Deps) Deps
430 crateName() string
Dan Albert06feee92021-03-19 15:06:02 -0700431 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100433 // Output directory in which source-generated code from dependencies is
434 // copied. This is equivalent to Cargo's OUT_DIR variable.
435 CargoOutDir() android.OptionalPath
Dan Albert06feee92021-03-19 15:06:02 -0700436
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400437 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
438 CargoPkgVersion() string
439
440 // CargoEnvCompat returns whether Cargo environment variables should be used.
441 CargoEnvCompat() bool
442
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800443 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200444 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400446 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400447
448 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400449
450 Disabled() bool
451 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400452
Ivan Lozanodd055472020-09-28 13:22:45 -0400453 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900454
455 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400456}
457
Matthew Maurerbb3add12020-06-25 09:34:12 -0700458type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700459 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400460 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700461}
462
463type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400464 linkDirs []string
465 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700466}
467
Matthew Maurerbb3add12020-06-25 09:34:12 -0700468func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
469 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
470}
471
Ivan Lozano2093af22020-08-25 12:48:19 -0400472func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
473 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
474}
475
Colin Cross0de8a1e2020-09-18 14:15:30 -0700476func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
477 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700478 LinkDirs: flagExporter.linkDirs,
479 LinkObjects: flagExporter.linkObjects,
480 })
481}
482
Matthew Maurerbb3add12020-06-25 09:34:12 -0700483var _ exportedFlagsProducer = (*flagExporter)(nil)
484
485func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700486 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700487}
488
Colin Cross0de8a1e2020-09-18 14:15:30 -0700489type FlagExporterInfo struct {
490 Flags []string
491 LinkDirs []string // TODO: this should be android.Paths
492 LinkObjects []string // TODO: this should be android.Paths
493}
494
495var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
496
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400497func (mod *Module) isCoverageVariant() bool {
498 return mod.coverage.Properties.IsCoverageVariant
499}
500
501var _ cc.Coverage = (*Module)(nil)
502
503func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
504 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
505}
506
Ivan Lozanod7586b62021-04-01 09:49:36 -0400507func (mod *Module) VndkVersion() string {
508 return mod.Properties.VndkVersion
509}
510
511func (mod *Module) PreventInstall() bool {
512 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400513}
514
515func (mod *Module) HideFromMake() {
516 mod.Properties.HideFromMake = true
517}
518
519func (mod *Module) MarkAsCoverageVariant(coverage bool) {
520 mod.coverage.Properties.IsCoverageVariant = coverage
521}
522
523func (mod *Module) EnableCoverageIfNeeded() {
524 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525}
526
527func defaultsFactory() android.Module {
528 return DefaultsFactory()
529}
530
531type Defaults struct {
532 android.ModuleBase
533 android.DefaultsModuleBase
534}
535
536func DefaultsFactory(props ...interface{}) android.Module {
537 module := &Defaults{}
538
539 module.AddProperties(props...)
540 module.AddProperties(
541 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500542 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100543 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400544 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700545 &BaseCompilerProperties{},
546 &BinaryCompilerProperties{},
547 &LibraryCompilerProperties{},
548 &ProcMacroCompilerProperties{},
549 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400550 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700551 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400552 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400553 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200554 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500555 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556 )
557
558 android.InitDefaultsModule(module)
559 return module
560}
561
562func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700563 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700564}
565
Ivan Lozano183a3212019-10-18 14:18:45 -0700566func (mod *Module) CcLibrary() bool {
567 if mod.compiler != nil {
568 if _, ok := mod.compiler.(*libraryDecorator); ok {
569 return true
570 }
571 }
572 return false
573}
574
575func (mod *Module) CcLibraryInterface() bool {
576 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400577 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
578 // VariantIs{Static,Shared} is set.
579 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700580 return true
581 }
582 }
583 return false
584}
585
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800586func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700587 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700588 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800589 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700590 }
591 }
592 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
593}
594
595func (mod *Module) SetStatic() {
596 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700597 if library, ok := mod.compiler.(libraryInterface); ok {
598 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700599 return
600 }
601 }
602 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
603}
604
605func (mod *Module) SetShared() {
606 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700607 if library, ok := mod.compiler.(libraryInterface); ok {
608 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700609 return
610 }
611 }
612 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
613}
614
Ivan Lozano183a3212019-10-18 14:18:45 -0700615func (mod *Module) BuildStaticVariant() bool {
616 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700617 if library, ok := mod.compiler.(libraryInterface); ok {
618 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700619 }
620 }
621 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
622}
623
624func (mod *Module) BuildSharedVariant() bool {
625 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700626 if library, ok := mod.compiler.(libraryInterface); ok {
627 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700628 }
629 }
630 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
631}
632
Ivan Lozano183a3212019-10-18 14:18:45 -0700633func (mod *Module) Module() android.Module {
634 return mod
635}
636
Ivan Lozano183a3212019-10-18 14:18:45 -0700637func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900638 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
639 return mod.compiler.strippedOutputFilePath()
640 }
641 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700642}
643
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400644func (mod *Module) CoverageFiles() android.Paths {
645 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800646 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400647 }
648 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
649}
650
Jiyong Park459feca2020-12-15 11:02:21 +0900651func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900652 if !mod.EverInstallable() {
653 return false
654 }
655
Jiyong Park459feca2020-12-15 11:02:21 +0900656 // The apex variant is not installable because it is included in the APEX and won't appear
657 // in the system partition as a standalone file.
658 if !apexInfo.IsForPlatform() {
659 return false
660 }
661
Jiyong Parke54f07e2021-04-07 15:08:04 +0900662 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900663}
664
Ivan Lozano183a3212019-10-18 14:18:45 -0700665var _ cc.LinkableInterface = (*Module)(nil)
666
Ivan Lozanoffee3342019-08-27 12:03:00 -0700667func (mod *Module) Init() android.Module {
668 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500669 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700670
671 if mod.compiler != nil {
672 mod.AddProperties(mod.compiler.compilerProps()...)
673 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400674 if mod.coverage != nil {
675 mod.AddProperties(mod.coverage.props()...)
676 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200677 if mod.clippy != nil {
678 mod.AddProperties(mod.clippy.props()...)
679 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400680 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700681 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400682 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500683 if mod.sanitize != nil {
684 mod.AddProperties(mod.sanitize.props()...)
685 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400686
Ivan Lozanoffee3342019-08-27 12:03:00 -0700687 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900688 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700689
690 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700691 return mod
692}
693
694func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
695 return &Module{
696 hod: hod,
697 multilib: multilib,
698 }
699}
700func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
701 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400702 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200703 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500704 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700705 return module
706}
707
708type ModuleContext interface {
709 android.ModuleContext
710 ModuleContextIntf
711}
712
713type BaseModuleContext interface {
714 android.BaseModuleContext
715 ModuleContextIntf
716}
717
718type DepsContext interface {
719 android.BottomUpMutatorContext
720 ModuleContextIntf
721}
722
723type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200724 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700725 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700726}
727
728type depsContext struct {
729 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700730}
731
732type moduleContext struct {
733 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700734}
735
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200736type baseModuleContext struct {
737 android.BaseModuleContext
738}
739
740func (ctx *moduleContext) RustModule() *Module {
741 return ctx.Module().(*Module)
742}
743
744func (ctx *moduleContext) toolchain() config.Toolchain {
745 return ctx.RustModule().toolchain(ctx)
746}
747
748func (ctx *depsContext) RustModule() *Module {
749 return ctx.Module().(*Module)
750}
751
752func (ctx *depsContext) toolchain() config.Toolchain {
753 return ctx.RustModule().toolchain(ctx)
754}
755
756func (ctx *baseModuleContext) RustModule() *Module {
757 return ctx.Module().(*Module)
758}
759
760func (ctx *baseModuleContext) toolchain() config.Toolchain {
761 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400762}
763
764func (mod *Module) nativeCoverage() bool {
765 return mod.compiler != nil && mod.compiler.nativeCoverage()
766}
767
Ivan Lozanod7586b62021-04-01 09:49:36 -0400768func (mod *Module) EverInstallable() bool {
769 return mod.compiler != nil &&
770 // Check to see whether the module is actually ever installable.
771 mod.compiler.everInstallable()
772}
773
774func (mod *Module) Installable() *bool {
775 return mod.Properties.Installable
776}
777
Ivan Lozanoffee3342019-08-27 12:03:00 -0700778func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
779 if mod.cachedToolchain == nil {
780 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
781 }
782 return mod.cachedToolchain
783}
784
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200785func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
786 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
787}
788
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
790}
791
792func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
793 ctx := &moduleContext{
794 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700795 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796
Jiyong Park99644e92020-11-17 22:21:02 +0900797 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
798 if !apexInfo.IsForPlatform() {
799 mod.hideApexVariantFromMake = true
800 }
801
Ivan Lozanoffee3342019-08-27 12:03:00 -0700802 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500803 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
804
805 // Differentiate static libraries that are vendor available
806 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500807 mod.Properties.SubName += cc.VendorSuffix
808 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
809 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500810 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700811
812 if !toolchain.Supported() {
813 // This toolchain's unsupported, there's nothing to do for this mod.
814 return
815 }
816
817 deps := mod.depsToPaths(ctx)
818 flags := Flags{
819 Toolchain: toolchain,
820 }
821
822 if mod.compiler != nil {
823 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400824 }
825 if mod.coverage != nil {
826 flags, deps = mod.coverage.flags(ctx, flags, deps)
827 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200828 if mod.clippy != nil {
829 flags, deps = mod.clippy.flags(ctx, flags, deps)
830 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500831 if mod.sanitize != nil {
832 flags, deps = mod.sanitize.flags(ctx, flags, deps)
833 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400834
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200835 // SourceProvider needs to call GenerateSource() before compiler calls
836 // compile() so it can provide the source. A SourceProvider has
837 // multiple variants (e.g. source, rlib, dylib). Only the "source"
838 // variant is responsible for effectively generating the source. The
839 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400840 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200841 if mod.compiler.(libraryInterface).source() {
842 mod.sourceProvider.GenerateSource(ctx, deps)
843 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
844 } else {
845 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
846 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700847 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200848 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400849 }
850
851 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100852 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900853 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900854 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200855 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900856
Dan Albert06feee92021-03-19 15:06:02 -0700857 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
858
Ivan Lozano1921e802021-05-20 13:39:16 -0400859 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
860 // RECOVERY_SNAPSHOT_VERSION is current.
861 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
862 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
863 lib.collectHeadersForSnapshot(ctx, deps)
864 }
865 }
866
Jiyong Park459feca2020-12-15 11:02:21 +0900867 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
868 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200869 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400870 }
Chris Wailes74be7642021-07-22 16:20:28 -0700871
872 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700873 }
874}
875
876func (mod *Module) deps(ctx DepsContext) Deps {
877 deps := Deps{}
878
879 if mod.compiler != nil {
880 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400881 }
882 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700883 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700884 }
885
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400886 if mod.coverage != nil {
887 deps = mod.coverage.deps(ctx, deps)
888 }
889
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500890 if mod.sanitize != nil {
891 deps = mod.sanitize.deps(ctx, deps)
892 }
893
Ivan Lozanoffee3342019-08-27 12:03:00 -0700894 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
895 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700896 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700897 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
898 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
899 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400900 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700901 return deps
902
903}
904
Ivan Lozanoffee3342019-08-27 12:03:00 -0700905type dependencyTag struct {
906 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800907 name string
908 library bool
909 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700910}
911
Jiyong Park65b62242020-11-25 12:44:59 +0900912// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
913// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
914func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800915 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900916}
917
918var _ android.InstallNeededDependencyTag = dependencyTag{}
919
Ivan Lozanoffee3342019-08-27 12:03:00 -0700920var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400921 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
922 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
923 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800924 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400925 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200926 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700927)
928
Jiyong Park99644e92020-11-17 22:21:02 +0900929func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
930 tag, ok := depTag.(dependencyTag)
931 return ok && tag == dylibDepTag
932}
933
Jiyong Park94e22fd2021-04-08 18:19:15 +0900934func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
935 tag, ok := depTag.(dependencyTag)
936 return ok && tag == rlibDepTag
937}
938
Matthew Maurer0f003b12020-06-29 14:34:06 -0700939type autoDep struct {
940 variation string
941 depTag dependencyTag
942}
943
944var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200945 rlibVariation = "rlib"
946 dylibVariation = "dylib"
947 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
948 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700949)
950
951type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500952 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700953}
954
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400955func (mod *Module) begin(ctx BaseModuleContext) {
956 if mod.coverage != nil {
957 mod.coverage.begin(ctx)
958 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500959 if mod.sanitize != nil {
960 mod.sanitize.begin(ctx)
961 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400962}
963
Ivan Lozanoffee3342019-08-27 12:03:00 -0700964func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
965 var depPaths PathDeps
966
967 directRlibDeps := []*Module{}
968 directDylibDeps := []*Module{}
969 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900970 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700971 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400972 directSrcProvidersDeps := []*Module{}
973 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974
975 ctx.VisitDirectDeps(func(dep android.Module) {
976 depName := ctx.OtherModuleName(dep)
977 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400978
Ivan Lozano89435d12020-07-31 11:01:18 -0400979 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700980 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400981 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700982
Ivan Lozanoffee3342019-08-27 12:03:00 -0700983 switch depTag {
984 case dylibDepTag:
985 dylib, ok := rustDep.compiler.(libraryInterface)
986 if !ok || !dylib.dylib() {
987 ctx.ModuleErrorf("mod %q not an dylib library", depName)
988 return
989 }
990 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400991 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700992 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400993
Ivan Lozanoffee3342019-08-27 12:03:00 -0700994 rlib, ok := rustDep.compiler.(libraryInterface)
995 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400996 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700997 return
998 }
999 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001000 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001001 case procMacroDepTag:
1002 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001003 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +01001004 }
1005
1006 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001007 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1008 // OS/Arch variant is used.
1009 var helper string
1010 if ctx.Host() {
1011 helper = "missing 'host_supported'?"
1012 } else {
1013 helper = "device module defined?"
1014 }
1015
1016 if dep.Target().Os != ctx.Os() {
1017 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1018 return
1019 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1020 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1021 return
1022 }
1023 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001024 }
1025
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001026 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001027 if depTag != procMacroDepTag {
1028 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1029 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1030 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1031 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001032 }
1033
Ivan Lozanoffee3342019-08-27 12:03:00 -07001034 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001035 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001036 if !linkFile.Valid() {
1037 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1038 depName, ctx.ModuleName())
1039 return
1040 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001041 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001042 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1043 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001044 }
1045 }
1046
Ivan Lozano89435d12020-07-31 11:01:18 -04001047 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001048 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001049 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001050 if _, ok := ccDep.(*Module); !ok {
1051 if ccDep.Module().Target().Os != ctx.Os() {
1052 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1053 return
1054 }
1055 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1056 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1057 return
1058 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001059 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001060 linkObject := ccDep.OutputFile()
1061 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001062
Ivan Lozano2093af22020-08-25 12:48:19 -04001063 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001064 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1065 }
1066
1067 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001068 switch {
1069 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001070 if cc.IsWholeStaticLib(depTag) {
1071 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1072 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001073 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1074 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001075 } else {
1076 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 -05001077 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001078 }
1079
1080 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1081 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001082 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001083 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1084
Colin Cross0de8a1e2020-09-18 14:15:30 -07001085 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1086 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1087 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1088 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1089 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001090 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001091 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001092 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001093 // For the shared lib dependencies, we may link to the stub variant
1094 // of the dependency depending on the context (e.g. if this
1095 // dependency crosses the APEX boundaries).
1096 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1097
1098 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1099 // object (either from stub or non-stub) to use.
1100 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1101 linkPath = linkPathFromFilePath(linkObject.Path())
1102
Ivan Lozanoffee3342019-08-27 12:03:00 -07001103 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001104 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001105 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1106 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1107 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1108 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001109 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001110
1111 // Record baseLibName for snapshots.
1112 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001113 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001114
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001115 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001116 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001117 case cc.IsHeaderDepTag(depTag):
1118 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1119 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1120 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1121 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001122 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001123 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001124 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001125 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001126 }
1127
1128 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001129 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1130 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001131 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001132 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001133 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001134
1135 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001136 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001137 // These are usually genrules which don't have per-target variants.
1138 directSrcDeps = append(directSrcDeps, srcDep)
1139 }
1140 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001141 })
1142
1143 var rlibDepFiles RustLibraries
1144 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001145 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001146 }
1147 var dylibDepFiles RustLibraries
1148 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001149 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001150 }
1151 var procMacroDepFiles RustLibraries
1152 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001153 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001154 }
1155
1156 var staticLibDepFiles android.Paths
1157 for _, dep := range directStaticLibDeps {
1158 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1159 }
1160
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001161 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001162 var sharedLibDepFiles android.Paths
1163 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001164 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1165 if dep.TableOfContents.Valid() {
1166 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001167 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001168 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001169 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001170 }
1171
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001172 var srcProviderDepFiles android.Paths
1173 for _, dep := range directSrcProvidersDeps {
1174 srcs, _ := dep.OutputFiles("")
1175 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1176 }
1177 for _, dep := range directSrcDeps {
1178 srcs := dep.Srcs()
1179 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1180 }
1181
Ivan Lozanoffee3342019-08-27 12:03:00 -07001182 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1183 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1184 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001185 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001186 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1187 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001188 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001189
1190 // Dedup exported flags from dependencies
1191 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001192 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001193 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001194 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1195 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1196 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001197
1198 return depPaths
1199}
1200
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001201func (mod *Module) InstallInData() bool {
1202 if mod.compiler == nil {
1203 return false
1204 }
1205 return mod.compiler.inData()
1206}
1207
Ivan Lozanoffee3342019-08-27 12:03:00 -07001208func linkPathFromFilePath(filepath android.Path) string {
1209 return strings.Split(filepath.String(), filepath.Base())[0]
1210}
Ivan Lozanod648c432020-02-06 12:05:10 -05001211
Ivan Lozanoffee3342019-08-27 12:03:00 -07001212func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1213 ctx := &depsContext{
1214 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001215 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001216
1217 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001218 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001219 var snapshotInfo *cc.SnapshotInfo
1220
1221 if ctx.Os() == android.Android {
1222 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1223 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001224
Ivan Lozano2b081132020-09-08 12:46:52 -04001225 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001226 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001227 stdLinkage = "rlib-std"
1228 }
1229
1230 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001231
Ivan Lozano2b081132020-09-08 12:46:52 -04001232 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1233 rlibDepVariations = append(rlibDepVariations,
1234 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1235 }
1236
Ivan Lozano1921e802021-05-20 13:39:16 -04001237 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001238 for _, lib := range deps.Rlibs {
1239 depTag := rlibDepTag
1240 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1241
1242 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1243 {Mutator: "rust_libraries", Variation: rlibVariation},
1244 }...), depTag, lib)
1245 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001246
1247 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001248 actx.AddVariationDependencies(
1249 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001250 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001251 dylibDepTag, deps.Dylibs...)
1252
Ivan Lozano1921e802021-05-20 13:39:16 -04001253 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001254 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1255 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001256 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001257 for _, lib := range deps.Rustlibs {
1258 depTag := autoDep.depTag
1259 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1260 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1261 {Mutator: "rust_libraries", Variation: autoDep.variation},
1262 }...), depTag, lib)
1263 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001264 } else {
1265 actx.AddVariationDependencies(
1266 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1267 autoDep.depTag, deps.Rustlibs...)
1268 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001269 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001270
1271 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001272 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001273 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001274 for _, lib := range deps.Stdlibs {
1275 depTag := rlibDepTag
1276 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1277
1278 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1279 depTag, lib)
1280 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001281 } else {
1282 actx.AddVariationDependencies(
1283 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1284 dylibDepTag, deps.Stdlibs...)
1285 }
1286 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001287
1288 for _, lib := range deps.SharedLibs {
1289 depTag := cc.SharedDepTag()
1290 name, version := cc.StubsLibNameAndVersion(lib)
1291
1292 variations := []blueprint.Variation{
1293 {Mutator: "link", Variation: "shared"},
1294 }
1295 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1296 }
1297
1298 for _, lib := range deps.WholeStaticLibs {
1299 depTag := cc.StaticDepTag(true)
1300 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1301
1302 actx.AddVariationDependencies([]blueprint.Variation{
1303 {Mutator: "link", Variation: "static"},
1304 }, depTag, lib)
1305 }
1306
1307 for _, lib := range deps.StaticLibs {
1308 depTag := cc.StaticDepTag(false)
1309 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1310
1311 actx.AddVariationDependencies([]blueprint.Variation{
1312 {Mutator: "link", Variation: "static"},
1313 }, depTag, lib)
1314 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001315
Zach Johnson3df4e632020-11-06 11:56:27 -08001316 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1317
Colin Cross565cafd2020-09-25 18:47:38 -07001318 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001319 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001320 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1321 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001322 }
1323 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001324 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1325 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001326 }
1327
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001328 if mod.sourceProvider != nil {
1329 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1330 bindgen.Properties.Custom_bindgen != "" {
1331 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1332 bindgen.Properties.Custom_bindgen)
1333 }
1334 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001335
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001336 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001337 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001338}
1339
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001340func BeginMutator(ctx android.BottomUpMutatorContext) {
1341 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1342 mod.beginMutator(ctx)
1343 }
1344}
1345
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001346func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1347 ctx := &baseModuleContext{
1348 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001349 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001350
1351 mod.begin(ctx)
1352}
1353
Ivan Lozanoffee3342019-08-27 12:03:00 -07001354func (mod *Module) Name() string {
1355 name := mod.ModuleBase.Name()
1356 if p, ok := mod.compiler.(interface {
1357 Name(string) string
1358 }); ok {
1359 name = p.Name(name)
1360 }
1361 return name
1362}
1363
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001364func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001365 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001366 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001367 }
1368}
1369
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001370var _ android.HostToolProvider = (*Module)(nil)
1371
1372func (mod *Module) HostToolPath() android.OptionalPath {
1373 if !mod.Host() {
1374 return android.OptionalPath{}
1375 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001376 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1377 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001378 }
1379 return android.OptionalPath{}
1380}
1381
Jiyong Park99644e92020-11-17 22:21:02 +09001382var _ android.ApexModule = (*Module)(nil)
1383
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001384func (mod *Module) minSdkVersion() string {
1385 return String(mod.Properties.Min_sdk_version)
1386}
1387
Jiyong Park45bf82e2020-12-15 22:29:02 +09001388var _ android.ApexModule = (*Module)(nil)
1389
1390// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001391func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001392 minSdkVersion := mod.minSdkVersion()
1393 if minSdkVersion == "apex_inherit" {
1394 return nil
1395 }
1396 if minSdkVersion == "" {
1397 return fmt.Errorf("min_sdk_version is not specificed")
1398 }
1399
1400 // Not using nativeApiLevelFromUser because the context here is not
1401 // necessarily a native context.
1402 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1403 if err != nil {
1404 return err
1405 }
1406
1407 if ver.GreaterThan(sdkVersion) {
1408 return fmt.Errorf("newer SDK(%v)", ver)
1409 }
Jiyong Park99644e92020-11-17 22:21:02 +09001410 return nil
1411}
1412
Jiyong Park45bf82e2020-12-15 22:29:02 +09001413// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001414func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1415 depTag := ctx.OtherModuleDependencyTag(dep)
1416
1417 if ccm, ok := dep.(*cc.Module); ok {
1418 if ccm.HasStubsVariants() {
1419 if cc.IsSharedDepTag(depTag) {
1420 // dynamic dep to a stubs lib crosses APEX boundary
1421 return false
1422 }
1423 if cc.IsRuntimeDepTag(depTag) {
1424 // runtime dep to a stubs lib also crosses APEX boundary
1425 return false
1426 }
1427
1428 if cc.IsHeaderDepTag(depTag) {
1429 return false
1430 }
1431 }
1432 if mod.Static() && cc.IsSharedDepTag(depTag) {
1433 // shared_lib dependency from a static lib is considered as crossing
1434 // the APEX boundary because the dependency doesn't actually is
1435 // linked; the dependency is used only during the compilation phase.
1436 return false
1437 }
1438 }
1439
1440 if depTag == procMacroDepTag {
1441 return false
1442 }
1443
1444 return true
1445}
1446
1447// Overrides ApexModule.IsInstallabeToApex()
1448func (mod *Module) IsInstallableToApex() bool {
1449 if mod.compiler != nil {
1450 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1451 return true
1452 }
1453 if _, ok := mod.compiler.(*binaryDecorator); ok {
1454 return true
1455 }
1456 }
1457 return false
1458}
1459
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001460// If a library file has a "lib" prefix, extract the library name without the prefix.
1461func libNameFromFilePath(filepath android.Path) (string, bool) {
1462 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1463 if strings.HasPrefix(libName, "lib") {
1464 libName = libName[3:]
1465 return libName, true
1466 }
1467 return "", false
1468}
1469
Ivan Lozanoffee3342019-08-27 12:03:00 -07001470var Bool = proptools.Bool
1471var BoolDefault = proptools.BoolDefault
1472var String = proptools.String
1473var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001474
1475var _ android.OutputFileProducer = (*Module)(nil)