blob: e18f8cc1b5396e127c176385e1a02c0f9fc452fc [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
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800437 inData() bool
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200438 install(ctx ModuleContext)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700439 relativeInstallPath() string
Ivan Lozanod7586b62021-04-01 09:49:36 -0400440 everInstallable() bool
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400441
442 nativeCoverage() bool
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400443
444 Disabled() bool
445 SetDisabled()
Ivan Lozano042504f2020-08-18 14:31:23 -0400446
Ivan Lozanodd055472020-09-28 13:22:45 -0400447 stdLinkage(ctx *depsContext) RustLinkage
Jiyong Parke54f07e2021-04-07 15:08:04 +0900448
449 strippedOutputFilePath() android.OptionalPath
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400450}
451
Matthew Maurerbb3add12020-06-25 09:34:12 -0700452type exportedFlagsProducer interface {
Matthew Maurerbb3add12020-06-25 09:34:12 -0700453 exportLinkDirs(...string)
Ivan Lozano2093af22020-08-25 12:48:19 -0400454 exportLinkObjects(...string)
Matthew Maurerbb3add12020-06-25 09:34:12 -0700455}
456
457type flagExporter struct {
Ivan Lozano2093af22020-08-25 12:48:19 -0400458 linkDirs []string
459 linkObjects []string
Matthew Maurerbb3add12020-06-25 09:34:12 -0700460}
461
Matthew Maurerbb3add12020-06-25 09:34:12 -0700462func (flagExporter *flagExporter) exportLinkDirs(dirs ...string) {
463 flagExporter.linkDirs = android.FirstUniqueStrings(append(flagExporter.linkDirs, dirs...))
464}
465
Ivan Lozano2093af22020-08-25 12:48:19 -0400466func (flagExporter *flagExporter) exportLinkObjects(flags ...string) {
467 flagExporter.linkObjects = android.FirstUniqueStrings(append(flagExporter.linkObjects, flags...))
468}
469
Colin Cross0de8a1e2020-09-18 14:15:30 -0700470func (flagExporter *flagExporter) setProvider(ctx ModuleContext) {
471 ctx.SetProvider(FlagExporterInfoProvider, FlagExporterInfo{
Colin Cross0de8a1e2020-09-18 14:15:30 -0700472 LinkDirs: flagExporter.linkDirs,
473 LinkObjects: flagExporter.linkObjects,
474 })
475}
476
Matthew Maurerbb3add12020-06-25 09:34:12 -0700477var _ exportedFlagsProducer = (*flagExporter)(nil)
478
479func NewFlagExporter() *flagExporter {
Colin Cross0de8a1e2020-09-18 14:15:30 -0700480 return &flagExporter{}
Matthew Maurerbb3add12020-06-25 09:34:12 -0700481}
482
Colin Cross0de8a1e2020-09-18 14:15:30 -0700483type FlagExporterInfo struct {
484 Flags []string
485 LinkDirs []string // TODO: this should be android.Paths
486 LinkObjects []string // TODO: this should be android.Paths
487}
488
489var FlagExporterInfoProvider = blueprint.NewProvider(FlagExporterInfo{})
490
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400491func (mod *Module) isCoverageVariant() bool {
492 return mod.coverage.Properties.IsCoverageVariant
493}
494
495var _ cc.Coverage = (*Module)(nil)
496
497func (mod *Module) IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool {
498 return mod.coverage != nil && mod.coverage.Properties.NeedCoverageVariant
499}
500
Ivan Lozanod7586b62021-04-01 09:49:36 -0400501func (mod *Module) VndkVersion() string {
502 return mod.Properties.VndkVersion
503}
504
505func (mod *Module) PreventInstall() bool {
506 return mod.Properties.PreventInstall
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400507}
508
509func (mod *Module) HideFromMake() {
510 mod.Properties.HideFromMake = true
511}
512
513func (mod *Module) MarkAsCoverageVariant(coverage bool) {
514 mod.coverage.Properties.IsCoverageVariant = coverage
515}
516
517func (mod *Module) EnableCoverageIfNeeded() {
518 mod.coverage.Properties.CoverageEnabled = mod.coverage.Properties.NeedCoverageBuild
Ivan Lozanoffee3342019-08-27 12:03:00 -0700519}
520
521func defaultsFactory() android.Module {
522 return DefaultsFactory()
523}
524
525type Defaults struct {
526 android.ModuleBase
527 android.DefaultsModuleBase
528}
529
530func DefaultsFactory(props ...interface{}) android.Module {
531 module := &Defaults{}
532
533 module.AddProperties(props...)
534 module.AddProperties(
535 &BaseProperties{},
Ivan Lozano6a884432020-12-02 09:15:16 -0500536 &cc.VendorProperties{},
Jakub Kotur1d640d02021-01-06 12:40:43 +0100537 &BenchmarkProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400538 &BindgenProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700539 &BaseCompilerProperties{},
540 &BinaryCompilerProperties{},
541 &LibraryCompilerProperties{},
542 &ProcMacroCompilerProperties{},
543 &PrebuiltProperties{},
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400544 &SourceProviderProperties{},
Chih-Hung Hsieh41805be2019-10-31 20:56:47 -0700545 &TestProperties{},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400546 &cc.CoverageProperties{},
Ivan Lozanobc9e4212020-09-25 16:08:34 -0400547 &cc.RustBindgenClangProperties{},
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200548 &ClippyProperties{},
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500549 &SanitizeProperties{},
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550 )
551
552 android.InitDefaultsModule(module)
553 return module
554}
555
556func (mod *Module) CrateName() string {
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700557 return mod.compiler.crateName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700558}
559
Ivan Lozano183a3212019-10-18 14:18:45 -0700560func (mod *Module) CcLibrary() bool {
561 if mod.compiler != nil {
562 if _, ok := mod.compiler.(*libraryDecorator); ok {
563 return true
564 }
565 }
566 return false
567}
568
569func (mod *Module) CcLibraryInterface() bool {
570 if mod.compiler != nil {
Ivan Lozano89435d12020-07-31 11:01:18 -0400571 // use build{Static,Shared}() instead of {static,shared}() here because this might be called before
572 // VariantIs{Static,Shared} is set.
573 if lib, ok := mod.compiler.(libraryInterface); ok && (lib.buildShared() || lib.buildStatic()) {
Ivan Lozano183a3212019-10-18 14:18:45 -0700574 return true
575 }
576 }
577 return false
578}
579
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800580func (mod *Module) IncludeDirs() android.Paths {
Ivan Lozano183a3212019-10-18 14:18:45 -0700581 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700582 if library, ok := mod.compiler.(*libraryDecorator); ok {
Ivan Lozanoe0833b12019-11-06 19:15:49 -0800583 return library.includeDirs
Ivan Lozano183a3212019-10-18 14:18:45 -0700584 }
585 }
586 panic(fmt.Errorf("IncludeDirs called on non-library module: %q", mod.BaseModuleName()))
587}
588
589func (mod *Module) SetStatic() {
590 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700591 if library, ok := mod.compiler.(libraryInterface); ok {
592 library.setStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700593 return
594 }
595 }
596 panic(fmt.Errorf("SetStatic called on non-library module: %q", mod.BaseModuleName()))
597}
598
599func (mod *Module) SetShared() {
600 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700601 if library, ok := mod.compiler.(libraryInterface); ok {
602 library.setShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700603 return
604 }
605 }
606 panic(fmt.Errorf("SetShared called on non-library module: %q", mod.BaseModuleName()))
607}
608
Ivan Lozano183a3212019-10-18 14:18:45 -0700609func (mod *Module) BuildStaticVariant() bool {
610 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700611 if library, ok := mod.compiler.(libraryInterface); ok {
612 return library.buildStatic()
Ivan Lozano183a3212019-10-18 14:18:45 -0700613 }
614 }
615 panic(fmt.Errorf("BuildStaticVariant called on non-library module: %q", mod.BaseModuleName()))
616}
617
618func (mod *Module) BuildSharedVariant() bool {
619 if mod.compiler != nil {
Ivan Lozano52767be2019-10-18 14:49:46 -0700620 if library, ok := mod.compiler.(libraryInterface); ok {
621 return library.buildShared()
Ivan Lozano183a3212019-10-18 14:18:45 -0700622 }
623 }
624 panic(fmt.Errorf("BuildSharedVariant called on non-library module: %q", mod.BaseModuleName()))
625}
626
Ivan Lozano183a3212019-10-18 14:18:45 -0700627func (mod *Module) Module() android.Module {
628 return mod
629}
630
Ivan Lozano183a3212019-10-18 14:18:45 -0700631func (mod *Module) OutputFile() android.OptionalPath {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900632 if mod.compiler != nil && mod.compiler.strippedOutputFilePath().Valid() {
633 return mod.compiler.strippedOutputFilePath()
634 }
635 return mod.unstrippedOutputFile
Ivan Lozano183a3212019-10-18 14:18:45 -0700636}
637
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400638func (mod *Module) CoverageFiles() android.Paths {
639 if mod.compiler != nil {
Joel Galensonfa049382021-01-14 16:03:18 -0800640 return android.Paths{}
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400641 }
642 panic(fmt.Errorf("CoverageFiles called on non-library module: %q", mod.BaseModuleName()))
643}
644
Jiyong Park459feca2020-12-15 11:02:21 +0900645func (mod *Module) installable(apexInfo android.ApexInfo) bool {
Jiyong Parkbf8147a2021-05-17 13:19:33 +0900646 if !mod.EverInstallable() {
647 return false
648 }
649
Jiyong Park459feca2020-12-15 11:02:21 +0900650 // The apex variant is not installable because it is included in the APEX and won't appear
651 // in the system partition as a standalone file.
652 if !apexInfo.IsForPlatform() {
653 return false
654 }
655
Jiyong Parke54f07e2021-04-07 15:08:04 +0900656 return mod.OutputFile().Valid() && !mod.Properties.PreventInstall
Jiyong Park459feca2020-12-15 11:02:21 +0900657}
658
Ivan Lozano183a3212019-10-18 14:18:45 -0700659var _ cc.LinkableInterface = (*Module)(nil)
660
Ivan Lozanoffee3342019-08-27 12:03:00 -0700661func (mod *Module) Init() android.Module {
662 mod.AddProperties(&mod.Properties)
Ivan Lozano6a884432020-12-02 09:15:16 -0500663 mod.AddProperties(&mod.VendorProperties)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700664
665 if mod.compiler != nil {
666 mod.AddProperties(mod.compiler.compilerProps()...)
667 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400668 if mod.coverage != nil {
669 mod.AddProperties(mod.coverage.props()...)
670 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200671 if mod.clippy != nil {
672 mod.AddProperties(mod.clippy.props()...)
673 }
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400674 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700675 mod.AddProperties(mod.sourceProvider.SourceProviderProps()...)
Ivan Lozano4fef93c2020-07-08 08:39:44 -0400676 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500677 if mod.sanitize != nil {
678 mod.AddProperties(mod.sanitize.props()...)
679 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400680
Ivan Lozanoffee3342019-08-27 12:03:00 -0700681 android.InitAndroidArchModule(mod, mod.hod, mod.multilib)
Jiyong Park99644e92020-11-17 22:21:02 +0900682 android.InitApexModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700683
684 android.InitDefaultableModule(mod)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700685 return mod
686}
687
688func newBaseModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
689 return &Module{
690 hod: hod,
691 multilib: multilib,
692 }
693}
694func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module {
695 module := newBaseModule(hod, multilib)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400696 module.coverage = &coverage{}
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200697 module.clippy = &clippy{}
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500698 module.sanitize = &sanitize{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700699 return module
700}
701
702type ModuleContext interface {
703 android.ModuleContext
704 ModuleContextIntf
705}
706
707type BaseModuleContext interface {
708 android.BaseModuleContext
709 ModuleContextIntf
710}
711
712type DepsContext interface {
713 android.BottomUpMutatorContext
714 ModuleContextIntf
715}
716
717type ModuleContextIntf interface {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200718 RustModule() *Module
Ivan Lozanoffee3342019-08-27 12:03:00 -0700719 toolchain() config.Toolchain
Ivan Lozanoffee3342019-08-27 12:03:00 -0700720}
721
722type depsContext struct {
723 android.BottomUpMutatorContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700724}
725
726type moduleContext struct {
727 android.ModuleContext
Ivan Lozanoffee3342019-08-27 12:03:00 -0700728}
729
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200730type baseModuleContext struct {
731 android.BaseModuleContext
732}
733
734func (ctx *moduleContext) RustModule() *Module {
735 return ctx.Module().(*Module)
736}
737
738func (ctx *moduleContext) toolchain() config.Toolchain {
739 return ctx.RustModule().toolchain(ctx)
740}
741
742func (ctx *depsContext) RustModule() *Module {
743 return ctx.Module().(*Module)
744}
745
746func (ctx *depsContext) toolchain() config.Toolchain {
747 return ctx.RustModule().toolchain(ctx)
748}
749
750func (ctx *baseModuleContext) RustModule() *Module {
751 return ctx.Module().(*Module)
752}
753
754func (ctx *baseModuleContext) toolchain() config.Toolchain {
755 return ctx.RustModule().toolchain(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400756}
757
758func (mod *Module) nativeCoverage() bool {
759 return mod.compiler != nil && mod.compiler.nativeCoverage()
760}
761
Ivan Lozanod7586b62021-04-01 09:49:36 -0400762func (mod *Module) EverInstallable() bool {
763 return mod.compiler != nil &&
764 // Check to see whether the module is actually ever installable.
765 mod.compiler.everInstallable()
766}
767
768func (mod *Module) Installable() *bool {
769 return mod.Properties.Installable
770}
771
Ivan Lozanoffee3342019-08-27 12:03:00 -0700772func (mod *Module) toolchain(ctx android.BaseModuleContext) config.Toolchain {
773 if mod.cachedToolchain == nil {
774 mod.cachedToolchain = config.FindToolchain(ctx.Os(), ctx.Arch())
775 }
776 return mod.cachedToolchain
777}
778
Thiébaud Weksteen31f1bb82020-08-27 13:37:29 +0200779func (mod *Module) ccToolchain(ctx android.BaseModuleContext) cc_config.Toolchain {
780 return cc_config.FindToolchain(ctx.Os(), ctx.Arch())
781}
782
Ivan Lozanoffee3342019-08-27 12:03:00 -0700783func (d *Defaults) GenerateAndroidBuildActions(ctx android.ModuleContext) {
784}
785
786func (mod *Module) GenerateAndroidBuildActions(actx android.ModuleContext) {
787 ctx := &moduleContext{
788 ModuleContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700789 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700790
Jiyong Park99644e92020-11-17 22:21:02 +0900791 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
792 if !apexInfo.IsForPlatform() {
793 mod.hideApexVariantFromMake = true
794 }
795
Ivan Lozanoffee3342019-08-27 12:03:00 -0700796 toolchain := mod.toolchain(ctx)
Ivan Lozano6a884432020-12-02 09:15:16 -0500797 mod.makeLinkType = cc.GetMakeLinkType(actx, mod)
798
799 // Differentiate static libraries that are vendor available
800 if mod.UseVndk() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500801 mod.Properties.SubName += cc.VendorSuffix
802 } else if mod.InVendorRamdisk() && !mod.OnlyInVendorRamdisk() {
803 mod.Properties.SubName += cc.VendorRamdiskSuffix
Ivan Lozano6a884432020-12-02 09:15:16 -0500804 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700805
806 if !toolchain.Supported() {
807 // This toolchain's unsupported, there's nothing to do for this mod.
808 return
809 }
810
811 deps := mod.depsToPaths(ctx)
812 flags := Flags{
813 Toolchain: toolchain,
814 }
815
816 if mod.compiler != nil {
817 flags = mod.compiler.compilerFlags(ctx, flags)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400818 }
819 if mod.coverage != nil {
820 flags, deps = mod.coverage.flags(ctx, flags, deps)
821 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200822 if mod.clippy != nil {
823 flags, deps = mod.clippy.flags(ctx, flags, deps)
824 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500825 if mod.sanitize != nil {
826 flags, deps = mod.sanitize.flags(ctx, flags, deps)
827 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400828
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200829 // SourceProvider needs to call GenerateSource() before compiler calls
830 // compile() so it can provide the source. A SourceProvider has
831 // multiple variants (e.g. source, rlib, dylib). Only the "source"
832 // variant is responsible for effectively generating the source. The
833 // remaining variants relies on the "source" variant output.
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400834 if mod.sourceProvider != nil {
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200835 if mod.compiler.(libraryInterface).source() {
836 mod.sourceProvider.GenerateSource(ctx, deps)
837 mod.sourceProvider.setSubName(ctx.ModuleSubDir())
838 } else {
839 sourceMod := actx.GetDirectDepWithTag(mod.Name(), sourceDepTag)
840 sourceLib := sourceMod.(*Module).compiler.(*libraryDecorator)
Chih-Hung Hsiehc49649c2020-10-01 21:25:05 -0700841 mod.sourceProvider.setOutputFiles(sourceLib.sourceProvider.Srcs())
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200842 }
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400843 }
844
845 if mod.compiler != nil && !mod.compiler.Disabled() {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100846 mod.compiler.initialize(ctx)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900847 unstrippedOutputFile := mod.compiler.compile(ctx, flags, deps)
Jiyong Parke54f07e2021-04-07 15:08:04 +0900848 mod.unstrippedOutputFile = android.OptionalPathForPath(unstrippedOutputFile)
Thiébaud Weksteene4dd14b2021-04-14 11:18:47 +0200849 bloaty.MeasureSizeForPaths(ctx, mod.compiler.strippedOutputFilePath(), mod.unstrippedOutputFile)
Jiyong Park459feca2020-12-15 11:02:21 +0900850
Dan Albert06feee92021-03-19 15:06:02 -0700851 mod.docTimestampFile = mod.compiler.rustdoc(ctx, flags, deps)
852
Ivan Lozano1921e802021-05-20 13:39:16 -0400853 // glob exported headers for snapshot, if BOARD_VNDK_VERSION is current or
854 // RECOVERY_SNAPSHOT_VERSION is current.
855 if lib, ok := mod.compiler.(snapshotLibraryInterface); ok {
856 if cc.ShouldCollectHeadersForSnapshot(ctx, mod, apexInfo) {
857 lib.collectHeadersForSnapshot(ctx, deps)
858 }
859 }
860
Jiyong Park459feca2020-12-15 11:02:21 +0900861 apexInfo := actx.Provider(android.ApexInfoProvider).(android.ApexInfo)
862 if mod.installable(apexInfo) {
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200863 mod.compiler.install(ctx)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400864 }
Chris Wailes74be7642021-07-22 16:20:28 -0700865
866 ctx.Phony("rust", ctx.RustModule().OutputFile().Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700867 }
868}
869
870func (mod *Module) deps(ctx DepsContext) Deps {
871 deps := Deps{}
872
873 if mod.compiler != nil {
874 deps = mod.compiler.compilerDeps(ctx, deps)
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400875 }
876 if mod.sourceProvider != nil {
Andrei Homescuc7767922020-08-05 06:36:19 -0700877 deps = mod.sourceProvider.SourceProviderDeps(ctx, deps)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700878 }
879
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400880 if mod.coverage != nil {
881 deps = mod.coverage.deps(ctx, deps)
882 }
883
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500884 if mod.sanitize != nil {
885 deps = mod.sanitize.deps(ctx, deps)
886 }
887
Ivan Lozanoffee3342019-08-27 12:03:00 -0700888 deps.Rlibs = android.LastUniqueStrings(deps.Rlibs)
889 deps.Dylibs = android.LastUniqueStrings(deps.Dylibs)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700890 deps.Rustlibs = android.LastUniqueStrings(deps.Rustlibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700891 deps.ProcMacros = android.LastUniqueStrings(deps.ProcMacros)
892 deps.SharedLibs = android.LastUniqueStrings(deps.SharedLibs)
893 deps.StaticLibs = android.LastUniqueStrings(deps.StaticLibs)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400894 deps.WholeStaticLibs = android.LastUniqueStrings(deps.WholeStaticLibs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700895 return deps
896
897}
898
Ivan Lozanoffee3342019-08-27 12:03:00 -0700899type dependencyTag struct {
900 blueprint.BaseDependencyTag
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800901 name string
902 library bool
903 procMacro bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700904}
905
Jiyong Park65b62242020-11-25 12:44:59 +0900906// InstallDepNeeded returns true for rlibs, dylibs, and proc macros so that they or their transitive
907// dependencies (especially C/C++ shared libs) are installed as dependencies of a rust binary.
908func (d dependencyTag) InstallDepNeeded() bool {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800909 return d.library || d.procMacro
Jiyong Park65b62242020-11-25 12:44:59 +0900910}
911
912var _ android.InstallNeededDependencyTag = dependencyTag{}
913
Ivan Lozanoffee3342019-08-27 12:03:00 -0700914var (
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400915 customBindgenDepTag = dependencyTag{name: "customBindgenTag"}
916 rlibDepTag = dependencyTag{name: "rlibTag", library: true}
917 dylibDepTag = dependencyTag{name: "dylib", library: true}
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800918 procMacroDepTag = dependencyTag{name: "procMacro", procMacro: true}
Ivan Lozanoc564d2d2020-08-04 15:43:37 -0400919 testPerSrcDepTag = dependencyTag{name: "rust_unit_tests"}
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200920 sourceDepTag = dependencyTag{name: "source"}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700921)
922
Jiyong Park99644e92020-11-17 22:21:02 +0900923func IsDylibDepTag(depTag blueprint.DependencyTag) bool {
924 tag, ok := depTag.(dependencyTag)
925 return ok && tag == dylibDepTag
926}
927
Jiyong Park94e22fd2021-04-08 18:19:15 +0900928func IsRlibDepTag(depTag blueprint.DependencyTag) bool {
929 tag, ok := depTag.(dependencyTag)
930 return ok && tag == rlibDepTag
931}
932
Matthew Maurer0f003b12020-06-29 14:34:06 -0700933type autoDep struct {
934 variation string
935 depTag dependencyTag
936}
937
938var (
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +0200939 rlibVariation = "rlib"
940 dylibVariation = "dylib"
941 rlibAutoDep = autoDep{variation: rlibVariation, depTag: rlibDepTag}
942 dylibAutoDep = autoDep{variation: dylibVariation, depTag: dylibDepTag}
Matthew Maurer0f003b12020-06-29 14:34:06 -0700943)
944
945type autoDeppable interface {
Liz Kammer356f7d42021-01-26 09:18:53 -0500946 autoDep(ctx android.BottomUpMutatorContext) autoDep
Matthew Maurer0f003b12020-06-29 14:34:06 -0700947}
948
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400949func (mod *Module) begin(ctx BaseModuleContext) {
950 if mod.coverage != nil {
951 mod.coverage.begin(ctx)
952 }
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500953 if mod.sanitize != nil {
954 mod.sanitize.begin(ctx)
955 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400956}
957
Ivan Lozanoffee3342019-08-27 12:03:00 -0700958func (mod *Module) depsToPaths(ctx android.ModuleContext) PathDeps {
959 var depPaths PathDeps
960
961 directRlibDeps := []*Module{}
962 directDylibDeps := []*Module{}
963 directProcMacroDeps := []*Module{}
Jiyong Park7d55b612021-06-11 17:22:09 +0900964 directSharedLibDeps := []cc.SharedLibraryInfo{}
Ivan Lozano52767be2019-10-18 14:49:46 -0700965 directStaticLibDeps := [](cc.LinkableInterface){}
Ivan Lozano07cbaf42020-07-22 16:09:13 -0400966 directSrcProvidersDeps := []*Module{}
967 directSrcDeps := [](android.SourceFileProducer){}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700968
969 ctx.VisitDirectDeps(func(dep android.Module) {
970 depName := ctx.OtherModuleName(dep)
971 depTag := ctx.OtherModuleDependencyTag(dep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400972
Ivan Lozano89435d12020-07-31 11:01:18 -0400973 if rustDep, ok := dep.(*Module); ok && !rustDep.CcLibraryInterface() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700974 //Handle Rust Modules
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400975 makeLibName := cc.MakeLibName(ctx, mod, rustDep, depName+rustDep.Properties.RustSubName)
Ivan Lozano70e0a072019-09-13 14:23:15 -0700976
Ivan Lozanoffee3342019-08-27 12:03:00 -0700977 switch depTag {
978 case dylibDepTag:
979 dylib, ok := rustDep.compiler.(libraryInterface)
980 if !ok || !dylib.dylib() {
981 ctx.ModuleErrorf("mod %q not an dylib library", depName)
982 return
983 }
984 directDylibDeps = append(directDylibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400985 mod.Properties.AndroidMkDylibs = append(mod.Properties.AndroidMkDylibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700986 case rlibDepTag:
Ivan Lozano2b081132020-09-08 12:46:52 -0400987
Ivan Lozanoffee3342019-08-27 12:03:00 -0700988 rlib, ok := rustDep.compiler.(libraryInterface)
989 if !ok || !rlib.rlib() {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400990 ctx.ModuleErrorf("mod %q not an rlib library", makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700991 return
992 }
993 directRlibDeps = append(directRlibDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400994 mod.Properties.AndroidMkRlibs = append(mod.Properties.AndroidMkRlibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700995 case procMacroDepTag:
996 directProcMacroDeps = append(directProcMacroDeps, rustDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400997 mod.Properties.AndroidMkProcMacroLibs = append(mod.Properties.AndroidMkProcMacroLibs, makeLibName)
Paul Duffind5cf92e2021-07-09 17:38:55 +0100998 }
999
1000 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001001 // Since these deps are added in path_properties.go via AddDependencies, we need to ensure the correct
1002 // OS/Arch variant is used.
1003 var helper string
1004 if ctx.Host() {
1005 helper = "missing 'host_supported'?"
1006 } else {
1007 helper = "device module defined?"
1008 }
1009
1010 if dep.Target().Os != ctx.Os() {
1011 ctx.ModuleErrorf("OS mismatch on dependency %q (%s)", dep.Name(), helper)
1012 return
1013 } else if dep.Target().Arch.ArchType != ctx.Arch().ArchType {
1014 ctx.ModuleErrorf("Arch mismatch on dependency %q (%s)", dep.Name(), helper)
1015 return
1016 }
1017 directSrcProvidersDeps = append(directSrcProvidersDeps, rustDep)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001018 }
1019
Ivan Lozano2bbcacf2020-08-07 09:00:50 -04001020 //Append the dependencies exportedDirs, except for proc-macros which target a different arch/OS
Colin Cross0de8a1e2020-09-18 14:15:30 -07001021 if depTag != procMacroDepTag {
1022 exportedInfo := ctx.OtherModuleProvider(dep, FlagExporterInfoProvider).(FlagExporterInfo)
1023 depPaths.linkDirs = append(depPaths.linkDirs, exportedInfo.LinkDirs...)
1024 depPaths.depFlags = append(depPaths.depFlags, exportedInfo.Flags...)
1025 depPaths.linkObjects = append(depPaths.linkObjects, exportedInfo.LinkObjects...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001026 }
1027
Ivan Lozanoffee3342019-08-27 12:03:00 -07001028 if depTag == dylibDepTag || depTag == rlibDepTag || depTag == procMacroDepTag {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001029 linkFile := rustDep.unstrippedOutputFile
Ivan Lozano26ecd6c2020-07-31 13:40:31 -04001030 if !linkFile.Valid() {
1031 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q",
1032 depName, ctx.ModuleName())
1033 return
1034 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001035 linkDir := linkPathFromFilePath(linkFile.Path())
Matthew Maurerbb3add12020-06-25 09:34:12 -07001036 if lib, ok := mod.compiler.(exportedFlagsProducer); ok {
1037 lib.exportLinkDirs(linkDir)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001038 }
1039 }
1040
Ivan Lozano89435d12020-07-31 11:01:18 -04001041 } else if ccDep, ok := dep.(cc.LinkableInterface); ok {
Ivan Lozano52767be2019-10-18 14:49:46 -07001042 //Handle C dependencies
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001043 makeLibName := cc.MakeLibName(ctx, mod, ccDep, depName)
Ivan Lozano52767be2019-10-18 14:49:46 -07001044 if _, ok := ccDep.(*Module); !ok {
1045 if ccDep.Module().Target().Os != ctx.Os() {
1046 ctx.ModuleErrorf("OS mismatch between %q and %q", ctx.ModuleName(), depName)
1047 return
1048 }
1049 if ccDep.Module().Target().Arch.ArchType != ctx.Arch().ArchType {
1050 ctx.ModuleErrorf("Arch mismatch between %q and %q", ctx.ModuleName(), depName)
1051 return
1052 }
Ivan Lozano70e0a072019-09-13 14:23:15 -07001053 }
Ivan Lozano2093af22020-08-25 12:48:19 -04001054 linkObject := ccDep.OutputFile()
1055 linkPath := linkPathFromFilePath(linkObject.Path())
Ivan Lozano6aa66022020-02-06 13:22:43 -05001056
Ivan Lozano2093af22020-08-25 12:48:19 -04001057 if !linkObject.Valid() {
Ivan Lozanoffee3342019-08-27 12:03:00 -07001058 ctx.ModuleErrorf("Invalid output file when adding dep %q to %q", depName, ctx.ModuleName())
1059 }
1060
1061 exportDep := false
Colin Cross6e511a92020-07-27 21:26:48 -07001062 switch {
1063 case cc.IsStaticDepTag(depTag):
Ivan Lozano63bb7682021-03-23 15:53:44 -04001064 if cc.IsWholeStaticLib(depTag) {
1065 // rustc will bundle static libraries when they're passed with "-lstatic=<lib>". This will fail
1066 // if the library is not prefixed by "lib".
Ivan Lozanofb6f36f2021-02-05 12:27:08 -05001067 if libName, ok := libNameFromFilePath(linkObject.Path()); ok {
1068 depPaths.depFlags = append(depPaths.depFlags, "-lstatic="+libName)
Ivan Lozano63bb7682021-03-23 15:53:44 -04001069 } else {
1070 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 -05001071 }
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001072 }
1073
1074 // Add this to linkObjects to pass the library directly to the linker as well. This propagates
1075 // to dependencies to avoid having to redeclare static libraries for dependents of the dylib variant.
Ivan Lozano2093af22020-08-25 12:48:19 -04001076 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001077 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
1078
Colin Cross0de8a1e2020-09-18 14:15:30 -07001079 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1080 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1081 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1082 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1083 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001084 directStaticLibDeps = append(directStaticLibDeps, ccDep)
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001085 mod.Properties.AndroidMkStaticLibs = append(mod.Properties.AndroidMkStaticLibs, makeLibName)
Colin Cross6e511a92020-07-27 21:26:48 -07001086 case cc.IsSharedDepTag(depTag):
Jiyong Park7d55b612021-06-11 17:22:09 +09001087 // For the shared lib dependencies, we may link to the stub variant
1088 // of the dependency depending on the context (e.g. if this
1089 // dependency crosses the APEX boundaries).
1090 sharedLibraryInfo, exportedInfo := cc.ChooseStubOrImpl(ctx, dep)
1091
1092 // Re-get linkObject as ChooseStubOrImpl actually tells us which
1093 // object (either from stub or non-stub) to use.
1094 linkObject = android.OptionalPathForPath(sharedLibraryInfo.SharedLibrary)
1095 linkPath = linkPathFromFilePath(linkObject.Path())
1096
Ivan Lozanoffee3342019-08-27 12:03:00 -07001097 depPaths.linkDirs = append(depPaths.linkDirs, linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001098 depPaths.linkObjects = append(depPaths.linkObjects, linkObject.String())
Colin Cross0de8a1e2020-09-18 14:15:30 -07001099 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1100 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1101 depPaths.depClangFlags = append(depPaths.depClangFlags, exportedInfo.Flags...)
1102 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Jiyong Park7d55b612021-06-11 17:22:09 +09001103 directSharedLibDeps = append(directSharedLibDeps, sharedLibraryInfo)
Ivan Lozano1921e802021-05-20 13:39:16 -04001104
1105 // Record baseLibName for snapshots.
1106 mod.Properties.SnapshotSharedLibs = append(mod.Properties.SnapshotSharedLibs, cc.BaseLibName(depName))
Justin Yun5e035862021-06-29 20:50:37 +09001107 mod.Properties.SnapshotStaticLibs = append(mod.Properties.SnapshotStaticLibs, cc.BaseLibName(depName))
Ivan Lozano1921e802021-05-20 13:39:16 -04001108
Ivan Lozanoc08897c2021-04-02 12:41:32 -04001109 mod.Properties.AndroidMkSharedLibs = append(mod.Properties.AndroidMkSharedLibs, makeLibName)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001110 exportDep = true
Zach Johnson3df4e632020-11-06 11:56:27 -08001111 case cc.IsHeaderDepTag(depTag):
1112 exportedInfo := ctx.OtherModuleProvider(dep, cc.FlagExporterInfoProvider).(cc.FlagExporterInfo)
1113 depPaths.depIncludePaths = append(depPaths.depIncludePaths, exportedInfo.IncludeDirs...)
1114 depPaths.depSystemIncludePaths = append(depPaths.depSystemIncludePaths, exportedInfo.SystemIncludeDirs...)
1115 depPaths.depGeneratedHeaders = append(depPaths.depGeneratedHeaders, exportedInfo.GeneratedHeaders...)
Colin Cross6e511a92020-07-27 21:26:48 -07001116 case depTag == cc.CrtBeginDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001117 depPaths.CrtBegin = linkObject
Colin Cross6e511a92020-07-27 21:26:48 -07001118 case depTag == cc.CrtEndDepTag:
Ivan Lozano2093af22020-08-25 12:48:19 -04001119 depPaths.CrtEnd = linkObject
Ivan Lozanoffee3342019-08-27 12:03:00 -07001120 }
1121
1122 // Make sure these dependencies are propagated
Matthew Maurerbb3add12020-06-25 09:34:12 -07001123 if lib, ok := mod.compiler.(exportedFlagsProducer); ok && exportDep {
1124 lib.exportLinkDirs(linkPath)
Ivan Lozano2093af22020-08-25 12:48:19 -04001125 lib.exportLinkObjects(linkObject.String())
Ivan Lozanoffee3342019-08-27 12:03:00 -07001126 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001127 }
Ivan Lozano89435d12020-07-31 11:01:18 -04001128
1129 if srcDep, ok := dep.(android.SourceFileProducer); ok {
Paul Duffind5cf92e2021-07-09 17:38:55 +01001130 if android.IsSourceDepTagWithOutputTag(depTag, "") {
Ivan Lozano89435d12020-07-31 11:01:18 -04001131 // These are usually genrules which don't have per-target variants.
1132 directSrcDeps = append(directSrcDeps, srcDep)
1133 }
1134 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001135 })
1136
1137 var rlibDepFiles RustLibraries
1138 for _, dep := range directRlibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001139 rlibDepFiles = append(rlibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001140 }
1141 var dylibDepFiles RustLibraries
1142 for _, dep := range directDylibDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001143 dylibDepFiles = append(dylibDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001144 }
1145 var procMacroDepFiles RustLibraries
1146 for _, dep := range directProcMacroDeps {
Jiyong Parke54f07e2021-04-07 15:08:04 +09001147 procMacroDepFiles = append(procMacroDepFiles, RustLibrary{Path: dep.unstrippedOutputFile.Path(), CrateName: dep.CrateName()})
Ivan Lozanoffee3342019-08-27 12:03:00 -07001148 }
1149
1150 var staticLibDepFiles android.Paths
1151 for _, dep := range directStaticLibDeps {
1152 staticLibDepFiles = append(staticLibDepFiles, dep.OutputFile().Path())
1153 }
1154
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001155 var sharedLibFiles android.Paths
Ivan Lozanoffee3342019-08-27 12:03:00 -07001156 var sharedLibDepFiles android.Paths
1157 for _, dep := range directSharedLibDeps {
Jiyong Park7d55b612021-06-11 17:22:09 +09001158 sharedLibFiles = append(sharedLibFiles, dep.SharedLibrary)
1159 if dep.TableOfContents.Valid() {
1160 sharedLibDepFiles = append(sharedLibDepFiles, dep.TableOfContents.Path())
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001161 } else {
Jiyong Park7d55b612021-06-11 17:22:09 +09001162 sharedLibDepFiles = append(sharedLibDepFiles, dep.SharedLibrary)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001163 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001164 }
1165
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001166 var srcProviderDepFiles android.Paths
1167 for _, dep := range directSrcProvidersDeps {
1168 srcs, _ := dep.OutputFiles("")
1169 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1170 }
1171 for _, dep := range directSrcDeps {
1172 srcs := dep.Srcs()
1173 srcProviderDepFiles = append(srcProviderDepFiles, srcs...)
1174 }
1175
Ivan Lozanoffee3342019-08-27 12:03:00 -07001176 depPaths.RLibs = append(depPaths.RLibs, rlibDepFiles...)
1177 depPaths.DyLibs = append(depPaths.DyLibs, dylibDepFiles...)
1178 depPaths.SharedLibs = append(depPaths.SharedLibs, sharedLibDepFiles...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001179 depPaths.SharedLibDeps = append(depPaths.SharedLibDeps, sharedLibDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001180 depPaths.StaticLibs = append(depPaths.StaticLibs, staticLibDepFiles...)
1181 depPaths.ProcMacros = append(depPaths.ProcMacros, procMacroDepFiles...)
Ivan Lozano07cbaf42020-07-22 16:09:13 -04001182 depPaths.SrcDeps = append(depPaths.SrcDeps, srcProviderDepFiles...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001183
1184 // Dedup exported flags from dependencies
1185 depPaths.linkDirs = android.FirstUniqueStrings(depPaths.linkDirs)
Ivan Lozanoec6e9912021-01-21 15:23:29 -05001186 depPaths.linkObjects = android.FirstUniqueStrings(depPaths.linkObjects)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001187 depPaths.depFlags = android.FirstUniqueStrings(depPaths.depFlags)
Ivan Lozano45901ed2020-07-24 16:05:01 -04001188 depPaths.depClangFlags = android.FirstUniqueStrings(depPaths.depClangFlags)
1189 depPaths.depIncludePaths = android.FirstUniquePaths(depPaths.depIncludePaths)
1190 depPaths.depSystemIncludePaths = android.FirstUniquePaths(depPaths.depSystemIncludePaths)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001191
1192 return depPaths
1193}
1194
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -08001195func (mod *Module) InstallInData() bool {
1196 if mod.compiler == nil {
1197 return false
1198 }
1199 return mod.compiler.inData()
1200}
1201
Ivan Lozanoffee3342019-08-27 12:03:00 -07001202func linkPathFromFilePath(filepath android.Path) string {
1203 return strings.Split(filepath.String(), filepath.Base())[0]
1204}
Ivan Lozanod648c432020-02-06 12:05:10 -05001205
Ivan Lozanoffee3342019-08-27 12:03:00 -07001206func (mod *Module) DepsMutator(actx android.BottomUpMutatorContext) {
1207 ctx := &depsContext{
1208 BottomUpMutatorContext: actx,
Ivan Lozanoffee3342019-08-27 12:03:00 -07001209 }
Ivan Lozanoffee3342019-08-27 12:03:00 -07001210
1211 deps := mod.deps(ctx)
Colin Cross3146c5c2020-09-30 15:34:40 -07001212 var commonDepVariations []blueprint.Variation
Ivan Lozano1921e802021-05-20 13:39:16 -04001213 var snapshotInfo *cc.SnapshotInfo
1214
1215 if ctx.Os() == android.Android {
1216 deps.SharedLibs, _ = cc.RewriteLibs(mod, &snapshotInfo, actx, ctx.Config(), deps.SharedLibs)
1217 }
Ivan Lozanodd055472020-09-28 13:22:45 -04001218
Ivan Lozano2b081132020-09-08 12:46:52 -04001219 stdLinkage := "dylib-std"
Ivan Lozanodd055472020-09-28 13:22:45 -04001220 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -04001221 stdLinkage = "rlib-std"
1222 }
1223
1224 rlibDepVariations := commonDepVariations
Ivan Lozano1921e802021-05-20 13:39:16 -04001225
Ivan Lozano2b081132020-09-08 12:46:52 -04001226 if lib, ok := mod.compiler.(libraryInterface); !ok || !lib.sysroot() {
1227 rlibDepVariations = append(rlibDepVariations,
1228 blueprint.Variation{Mutator: "rust_stdlinkage", Variation: stdLinkage})
1229 }
1230
Ivan Lozano1921e802021-05-20 13:39:16 -04001231 // rlibs
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001232 for _, lib := range deps.Rlibs {
1233 depTag := rlibDepTag
1234 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1235
1236 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1237 {Mutator: "rust_libraries", Variation: rlibVariation},
1238 }...), depTag, lib)
1239 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001240
1241 // dylibs
Ivan Lozano52767be2019-10-18 14:49:46 -07001242 actx.AddVariationDependencies(
1243 append(commonDepVariations, []blueprint.Variation{
Thiébaud Weksteen295c72b2020-09-23 18:10:17 +02001244 {Mutator: "rust_libraries", Variation: dylibVariation}}...),
Ivan Lozano52767be2019-10-18 14:49:46 -07001245 dylibDepTag, deps.Dylibs...)
1246
Ivan Lozano1921e802021-05-20 13:39:16 -04001247 // rustlibs
Ivan Lozano042504f2020-08-18 14:31:23 -04001248 if deps.Rustlibs != nil && !mod.compiler.Disabled() {
1249 autoDep := mod.compiler.(autoDeppable).autoDep(ctx)
Ivan Lozano2b081132020-09-08 12:46:52 -04001250 if autoDep.depTag == rlibDepTag {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001251 for _, lib := range deps.Rustlibs {
1252 depTag := autoDep.depTag
1253 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1254 actx.AddVariationDependencies(append(rlibDepVariations, []blueprint.Variation{
1255 {Mutator: "rust_libraries", Variation: autoDep.variation},
1256 }...), depTag, lib)
1257 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001258 } else {
1259 actx.AddVariationDependencies(
1260 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: autoDep.variation}),
1261 autoDep.depTag, deps.Rustlibs...)
1262 }
Matthew Maurer0f003b12020-06-29 14:34:06 -07001263 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001264
1265 // stdlibs
Ivan Lozano2b081132020-09-08 12:46:52 -04001266 if deps.Stdlibs != nil {
Ivan Lozanodd055472020-09-28 13:22:45 -04001267 if mod.compiler.stdLinkage(ctx) == RlibLinkage {
Ivan Lozano3149e6e2021-06-01 15:09:53 -04001268 for _, lib := range deps.Stdlibs {
1269 depTag := rlibDepTag
1270 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).Rlibs)
1271
1272 actx.AddVariationDependencies(append(commonDepVariations, []blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}...),
1273 depTag, lib)
1274 }
Ivan Lozano2b081132020-09-08 12:46:52 -04001275 } else {
1276 actx.AddVariationDependencies(
1277 append(commonDepVariations, blueprint.Variation{Mutator: "rust_libraries", Variation: "dylib"}),
1278 dylibDepTag, deps.Stdlibs...)
1279 }
1280 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001281
1282 for _, lib := range deps.SharedLibs {
1283 depTag := cc.SharedDepTag()
1284 name, version := cc.StubsLibNameAndVersion(lib)
1285
1286 variations := []blueprint.Variation{
1287 {Mutator: "link", Variation: "shared"},
1288 }
1289 cc.AddSharedLibDependenciesWithVersions(ctx, mod, variations, depTag, name, version, false)
1290 }
1291
1292 for _, lib := range deps.WholeStaticLibs {
1293 depTag := cc.StaticDepTag(true)
1294 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1295
1296 actx.AddVariationDependencies([]blueprint.Variation{
1297 {Mutator: "link", Variation: "static"},
1298 }, depTag, lib)
1299 }
1300
1301 for _, lib := range deps.StaticLibs {
1302 depTag := cc.StaticDepTag(false)
1303 lib = cc.RewriteSnapshotLib(lib, cc.GetSnapshot(mod, &snapshotInfo, actx).StaticLibs)
1304
1305 actx.AddVariationDependencies([]blueprint.Variation{
1306 {Mutator: "link", Variation: "static"},
1307 }, depTag, lib)
1308 }
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001309
Zach Johnson3df4e632020-11-06 11:56:27 -08001310 actx.AddVariationDependencies(nil, cc.HeaderDepTag(), deps.HeaderLibs...)
1311
Colin Cross565cafd2020-09-25 18:47:38 -07001312 crtVariations := cc.GetCrtVariations(ctx, mod)
Ivan Lozanof1c84332019-09-20 11:00:37 -07001313 if deps.CrtBegin != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001314 actx.AddVariationDependencies(crtVariations, cc.CrtBeginDepTag,
1315 cc.RewriteSnapshotLib(deps.CrtBegin, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001316 }
1317 if deps.CrtEnd != "" {
Ivan Lozano1921e802021-05-20 13:39:16 -04001318 actx.AddVariationDependencies(crtVariations, cc.CrtEndDepTag,
1319 cc.RewriteSnapshotLib(deps.CrtEnd, cc.GetSnapshot(mod, &snapshotInfo, actx).Objects))
Ivan Lozanof1c84332019-09-20 11:00:37 -07001320 }
1321
Ivan Lozanoc564d2d2020-08-04 15:43:37 -04001322 if mod.sourceProvider != nil {
1323 if bindgen, ok := mod.sourceProvider.(*bindgenDecorator); ok &&
1324 bindgen.Properties.Custom_bindgen != "" {
1325 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), customBindgenDepTag,
1326 bindgen.Properties.Custom_bindgen)
1327 }
1328 }
Ivan Lozano1921e802021-05-20 13:39:16 -04001329
Ivan Lozano5ca5ef62019-09-23 10:10:40 -07001330 // proc_macros are compiler plugins, and so we need the host arch variant as a dependendcy.
Colin Cross0f7d2ef2019-10-16 11:03:10 -07001331 actx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), procMacroDepTag, deps.ProcMacros...)
Ivan Lozanoffee3342019-08-27 12:03:00 -07001332}
1333
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001334func BeginMutator(ctx android.BottomUpMutatorContext) {
1335 if mod, ok := ctx.Module().(*Module); ok && mod.Enabled() {
1336 mod.beginMutator(ctx)
1337 }
1338}
1339
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001340func (mod *Module) beginMutator(actx android.BottomUpMutatorContext) {
1341 ctx := &baseModuleContext{
1342 BaseModuleContext: actx,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001343 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001344
1345 mod.begin(ctx)
1346}
1347
Ivan Lozanoffee3342019-08-27 12:03:00 -07001348func (mod *Module) Name() string {
1349 name := mod.ModuleBase.Name()
1350 if p, ok := mod.compiler.(interface {
1351 Name(string) string
1352 }); ok {
1353 name = p.Name(name)
1354 }
1355 return name
1356}
1357
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001358func (mod *Module) disableClippy() {
Ivan Lozano32267c82020-08-04 16:27:16 -04001359 if mod.clippy != nil {
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +02001360 mod.clippy.Properties.Clippy_lints = proptools.StringPtr("none")
Ivan Lozano32267c82020-08-04 16:27:16 -04001361 }
1362}
1363
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001364var _ android.HostToolProvider = (*Module)(nil)
1365
1366func (mod *Module) HostToolPath() android.OptionalPath {
1367 if !mod.Host() {
1368 return android.OptionalPath{}
1369 }
Chih-Hung Hsieha7562702020-08-10 21:50:43 -07001370 if binary, ok := mod.compiler.(*binaryDecorator); ok {
1371 return android.OptionalPathForPath(binary.baseCompiler.path)
Chih-Hung Hsieh5c4e4892020-05-15 17:36:30 -07001372 }
1373 return android.OptionalPath{}
1374}
1375
Jiyong Park99644e92020-11-17 22:21:02 +09001376var _ android.ApexModule = (*Module)(nil)
1377
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001378func (mod *Module) minSdkVersion() string {
1379 return String(mod.Properties.Min_sdk_version)
1380}
1381
Jiyong Park45bf82e2020-12-15 22:29:02 +09001382var _ android.ApexModule = (*Module)(nil)
1383
1384// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001385func (mod *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, sdkVersion android.ApiLevel) error {
Ivan Lozano3e9f9e42020-12-04 15:05:43 -05001386 minSdkVersion := mod.minSdkVersion()
1387 if minSdkVersion == "apex_inherit" {
1388 return nil
1389 }
1390 if minSdkVersion == "" {
1391 return fmt.Errorf("min_sdk_version is not specificed")
1392 }
1393
1394 // Not using nativeApiLevelFromUser because the context here is not
1395 // necessarily a native context.
1396 ver, err := android.ApiLevelFromUser(ctx, minSdkVersion)
1397 if err != nil {
1398 return err
1399 }
1400
1401 if ver.GreaterThan(sdkVersion) {
1402 return fmt.Errorf("newer SDK(%v)", ver)
1403 }
Jiyong Park99644e92020-11-17 22:21:02 +09001404 return nil
1405}
1406
Jiyong Park45bf82e2020-12-15 22:29:02 +09001407// Implements android.ApexModule
Jiyong Park99644e92020-11-17 22:21:02 +09001408func (mod *Module) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
1409 depTag := ctx.OtherModuleDependencyTag(dep)
1410
1411 if ccm, ok := dep.(*cc.Module); ok {
1412 if ccm.HasStubsVariants() {
1413 if cc.IsSharedDepTag(depTag) {
1414 // dynamic dep to a stubs lib crosses APEX boundary
1415 return false
1416 }
1417 if cc.IsRuntimeDepTag(depTag) {
1418 // runtime dep to a stubs lib also crosses APEX boundary
1419 return false
1420 }
1421
1422 if cc.IsHeaderDepTag(depTag) {
1423 return false
1424 }
1425 }
1426 if mod.Static() && cc.IsSharedDepTag(depTag) {
1427 // shared_lib dependency from a static lib is considered as crossing
1428 // the APEX boundary because the dependency doesn't actually is
1429 // linked; the dependency is used only during the compilation phase.
1430 return false
1431 }
1432 }
1433
1434 if depTag == procMacroDepTag {
1435 return false
1436 }
1437
1438 return true
1439}
1440
1441// Overrides ApexModule.IsInstallabeToApex()
1442func (mod *Module) IsInstallableToApex() bool {
1443 if mod.compiler != nil {
1444 if lib, ok := mod.compiler.(*libraryDecorator); ok && (lib.shared() || lib.dylib()) {
1445 return true
1446 }
1447 if _, ok := mod.compiler.(*binaryDecorator); ok {
1448 return true
1449 }
1450 }
1451 return false
1452}
1453
Ivan Lozano3dfa12d2021-02-04 11:29:41 -05001454// If a library file has a "lib" prefix, extract the library name without the prefix.
1455func libNameFromFilePath(filepath android.Path) (string, bool) {
1456 libName := strings.TrimSuffix(filepath.Base(), filepath.Ext())
1457 if strings.HasPrefix(libName, "lib") {
1458 libName = libName[3:]
1459 return libName, true
1460 }
1461 return "", false
1462}
1463
Ivan Lozanoffee3342019-08-27 12:03:00 -07001464var Bool = proptools.Bool
1465var BoolDefault = proptools.BoolDefault
1466var String = proptools.String
1467var StringPtr = proptools.StringPtr
Ivan Lozano43845682020-07-09 21:03:28 -04001468
1469var _ android.OutputFileProducer = (*Module)(nil)