blob: e6a7a93567d13ed5d828ef7186b41adf9526176d [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 (
Colin Cross225a37a2023-01-11 14:17:39 -080018 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "fmt"
20 "path/filepath"
Ivan Lozano45a9e312021-07-27 12:29:12 -040021 "strings"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070023 "github.com/google/blueprint/proptools"
24
Ivan Lozanoffee3342019-08-27 12:03:00 -070025 "android/soong/android"
26 "android/soong/rust/config"
27)
28
Ivan Lozanodd055472020-09-28 13:22:45 -040029type RustLinkage int
30
31const (
32 DefaultLinkage RustLinkage = iota
33 RlibLinkage
34 DylibLinkage
35)
36
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020037func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070038 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
39}
40
Matthew Maurer99020b02019-10-31 10:44:40 -070041func (compiler *baseCompiler) setNoStdlibs() {
42 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
43}
44
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020045func (compiler *baseCompiler) disableLints() {
46 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070047}
48
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080049func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070050 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070051 Properties: BaseCompilerProperties{},
52 dir: dir,
53 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080054 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070055 }
56}
57
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080058type installLocation int
59
60const (
61 InstallInSystem installLocation = 0
62 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -040063
64 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +010065 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080066)
67
Ivan Lozanoffee3342019-08-27 12:03:00 -070068type BaseCompilerProperties struct {
Ivan Lozanoe4db0032021-08-11 13:39:33 -040069 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
70 // Only a single source file can be defined. Modules which generate source can be included by prefixing
71 // the module name with ":", for example ":libfoo_bindgen"
72 //
73 // If no source file is defined, a single generated source module can be defined to be used as the main source.
Ivan Lozano8a23fa42020-06-16 10:26:57 -040074 Srcs []string `android:"path,arch_variant"`
75
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020076 // name of the lint set that should be used to validate this module.
77 //
78 // Possible values are "default" (for using a sensible set of lints
79 // depending on the module's location), "android" (for the strictest
80 // lint set that applies to all Android platform code), "vendor" (for
81 // a relaxed set) and "none" (for ignoring all lint warnings and
82 // errors). The default value is "default".
83 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070084
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +020085 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -040086 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070087
88 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -040089 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070090
91 // list of rust rlib crate dependencies
92 Rlibs []string `android:"arch_variant"`
93
Vinh Tran4eeb2a92023-08-14 13:29:30 -040094 // list of rust automatic crate dependencies.
95 // Rustlibs linkage is rlib for host targets and dylib for device targets.
Matthew Maurer0f003b12020-06-29 14:34:06 -070096 Rustlibs []string `android:"arch_variant"`
97
Ivan Lozanoffee3342019-08-27 12:03:00 -070098 // list of rust proc_macro crate dependencies
99 Proc_macros []string `android:"arch_variant"`
100
101 // list of C shared library dependencies
102 Shared_libs []string `android:"arch_variant"`
103
Ivan Lozano63bb7682021-03-23 15:53:44 -0400104 // list of C static library dependencies. These dependencies do not normally propagate to dependents
105 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106 Static_libs []string `android:"arch_variant"`
107
Ivan Lozano63bb7682021-03-23 15:53:44 -0400108 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
109 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
110 // result in bloat if multiple dependencies all include the same static library whole.
111 //
112 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
113 // having to redeclare the static library dependency for every dependent module.
114 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
115 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
116 //
117 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
118 //
119 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
120 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
121 Whole_static_libs []string `android:"arch_variant"`
122
Andrew Walbran797e4be2022-03-07 15:41:53 +0000123 // list of Rust system library dependencies.
124 //
125 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
126 // like `core` and `alloc`.
127 Stdlibs []string `android:"arch_variant"`
128
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400129 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
130 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
131 // source, and is required to conform to an enforced format matching library output files (if the output file is
132 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700133 Crate_name string `android:"arch_variant"`
134
135 // list of features to enable for this crate
136 Features []string `android:"arch_variant"`
137
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200138 // list of configuration options to enable for this crate. To enable features, use the "features" property.
139 Cfgs []string `android:"arch_variant"`
140
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141 // specific rust edition that should be used if the default version is not desired
142 Edition *string `android:"arch_variant"`
143
144 // sets name of the output
145 Stem *string `android:"arch_variant"`
146
147 // append to name of output
148 Suffix *string `android:"arch_variant"`
149
150 // install to a subdirectory of the default install path for the module
151 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700152
153 // whether to suppress inclusion of standard crates - defaults to false
154 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500155
156 // Change the rustlibs linkage to select rlib linkage by default for device targets.
157 // Also link libstd as an rlib as well on device targets.
158 // Note: This is the default behavior for host targets.
159 //
160 // This is primarily meant for rust_binary and rust_ffi modules where the default
161 // linkage of libstd might need to be overridden in some use cases. This should
162 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000163 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500164 // the same way.
165 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400166
167 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
168 // Will set CARGO_CRATE_NAME to the crate_name property's value.
169 // Will set CARGO_BIN_NAME to the output filename value without the extension.
170 Cargo_env_compat *bool
171
172 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
173 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700174}
175
176type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800177 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178
179 // Install related
180 dir string
181 dir64 string
182 subDir string
183 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700184 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800185 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500186 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400187
Joel Galensonfa049382021-01-14 16:03:18 -0800188 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400189
190 // unstripped output file.
191 unstrippedOutputFile android.Path
192
193 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200194 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100195
196 // If a crate has a source-generated dependency, a copy of the source file
197 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
198 cargoOutDir android.ModuleOutPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700199}
200
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400201func (compiler *baseCompiler) Disabled() bool {
202 return false
203}
204
205func (compiler *baseCompiler) SetDisabled() {
206 panic("baseCompiler does not implement SetDisabled()")
207}
208
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500209func (compiler *baseCompiler) noStdlibs() bool {
210 return Bool(compiler.Properties.No_stdlibs)
211}
212
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400213func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
214 panic("baseCompiler does not implement coverageOutputZipPath()")
215}
216
Ivan Lozanoea086132020-12-08 14:43:00 -0500217func (compiler *baseCompiler) preferRlib() bool {
218 return Bool(compiler.Properties.Prefer_rlib)
219}
220
Ivan Lozanodd055472020-09-28 13:22:45 -0400221func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400222 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500223 if compiler.preferRlib() {
224 return RlibLinkage
225 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400226 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400227 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400228 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400229 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400230}
231
Ivan Lozanoffee3342019-08-27 12:03:00 -0700232var _ compiler = (*baseCompiler)(nil)
233
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800234func (compiler *baseCompiler) inData() bool {
235 return compiler.location == InstallInData
236}
237
Ivan Lozanoffee3342019-08-27 12:03:00 -0700238func (compiler *baseCompiler) compilerProps() []interface{} {
239 return []interface{}{&compiler.Properties}
240}
241
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200242func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700243 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200244 for _, cfg := range compiler.Properties.Cfgs {
245 flags = append(flags, "--cfg '"+cfg+"'")
246 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400247
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200248 return flags
249}
250
251func (compiler *baseCompiler) featuresToFlags() []string {
252 flags := []string{}
253 for _, feature := range compiler.Properties.Features {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700254 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
255 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400256
257 return flags
258}
259
260func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
261 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
262 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
263
264 return flags
265}
266
267func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
268 if ctx.RustModule().UseVndk() {
269 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000270 if ctx.RustModule().InVendor() {
271 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
272 } else if ctx.RustModule().InProduct() {
273 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
274 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400275 }
276
277 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
278 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279 return flags
280}
281
282func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
283
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200284 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
285 if err != nil {
286 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700287 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400288
289 // linkage-related flags are disallowed.
290 for _, s := range compiler.Properties.Ld_flags {
291 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
292 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
293 }
294 }
295 for _, s := range compiler.Properties.Flags {
296 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
297 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
298 }
299 if strings.HasPrefix(s, "--extern") {
300 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
301 }
302 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
303 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
304 }
305 }
306
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200307 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700308 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200309 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700310 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700311 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700312 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700313 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
314 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Sasha Smundaka76acba2022-04-18 20:12:56 -0700315 flags.EmitXrefs = ctx.Config().EmitXrefRules()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700316
317 if ctx.Host() && !ctx.Windows() {
Colin Cross225a37a2023-01-11 14:17:39 -0800318 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700319 }
320
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400321 if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl && !ctx.Windows() {
322 // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
323 // builds. This is irrelevant for the Windows target as these are Posix specific.
324 flags.LinkFlags = append(flags.LinkFlags,
325 "-ldl",
326 "-lpthread",
327 "-lm",
328 )
329 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700330 return flags
331}
332
Sasha Smundaka76acba2022-04-18 20:12:56 -0700333func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700334 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
335}
336
Dan Albert06feee92021-03-19 15:06:02 -0700337func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
338 deps PathDeps) android.OptionalPath {
339
340 return android.OptionalPath{}
341}
342
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100343func (compiler *baseCompiler) initialize(ctx ModuleContext) {
344 compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
345}
346
347func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
348 return android.OptionalPathForPath(compiler.cargoOutDir)
349}
350
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400351func (compiler *baseCompiler) CargoEnvCompat() bool {
352 return Bool(compiler.Properties.Cargo_env_compat)
353}
354
355func (compiler *baseCompiler) CargoPkgVersion() string {
356 return String(compiler.Properties.Cargo_pkg_version)
357}
358
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400359func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
360 return compiler.unstrippedOutputFile
361}
362
Jiyong Parke54f07e2021-04-07 15:08:04 +0900363func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
364 return compiler.strippedOutputFile
365}
366
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
368 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700369 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700370 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
371 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400372 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000374 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375
Matthew Maurer99020b02019-10-31 10:44:40 -0700376 if !Bool(compiler.Properties.No_stdlibs) {
377 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700378 // If we're building for the build host, use the prebuilt stdlibs, unless the host
379 // is linux_bionic which doesn't have prebuilts.
380 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500381 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700382 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400383 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700384 }
385 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700386 return deps
387}
388
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100389func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400390 bionicLibs := []string{}
391 bionicLibs = append(bionicLibs, "liblog")
392 bionicLibs = append(bionicLibs, "libc")
393 bionicLibs = append(bionicLibs, "libm")
394 bionicLibs = append(bionicLibs, "libdl")
395
396 if static {
397 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
398 } else {
399 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
400 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400401 if ctx.RustModule().StaticExecutable() {
402 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
403 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100404 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
405 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
406 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700407 return deps
408}
409
Colin Crosse32f0932022-01-23 20:48:36 -0800410func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
411 muslLibs := []string{"libc_musl"}
412 if static {
413 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
414 } else {
415 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
416 }
417 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
418 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
419 }
420
421 return deps
422}
423
Ivan Lozanoffee3342019-08-27 12:03:00 -0700424func (compiler *baseCompiler) crateName() string {
425 return compiler.Properties.Crate_name
426}
427
Ivan Lozanod7586b62021-04-01 09:49:36 -0400428func (compiler *baseCompiler) everInstallable() bool {
429 // Most modules are installable, so return true by default.
430 return true
431}
432
Colin Cross70dda7e2019-10-01 22:05:35 -0700433func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700434 dir := compiler.dir
435 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
436 dir = compiler.dir64
437 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400438 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
439 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
440 }
441 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
443 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400444
445 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700446 if ctx.RustModule().InProduct() {
447 dir = filepath.Join(dir, "product")
448 } else if ctx.RustModule().InVendor() {
449 dir = filepath.Join(dir, "vendor")
450 } else {
451 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
452 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400453 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700454
Ivan Lozanoffee3342019-08-27 12:03:00 -0700455 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
456 compiler.relativeInstallPath(), compiler.relative)
457}
458
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400459func (compiler *baseCompiler) nativeCoverage() bool {
460 return false
461}
462
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200463func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900464 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200465 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700466}
467
468func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
469 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
470}
471
472func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200473 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700474 if String(compiler.Properties.Stem) != "" {
475 stem = String(compiler.Properties.Stem)
476 }
477
478 return stem
479}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700480
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481func (compiler *baseCompiler) relativeInstallPath() string {
482 return String(compiler.Properties.Relative_install_path)
483}
484
Ivan Lozano43845682020-07-09 21:03:28 -0400485// Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700486func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
Seth Moore3afac0b2021-10-13 15:32:18 -0700487 if len(srcs) == 0 {
488 ctx.PropertyErrorf("srcs", "srcs must not be empty")
489 }
490
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700491 // The srcs can contain strings with prefix ":".
492 // They are dependent modules of this module, with android.SourceDepTag.
493 // They are not the main source file compiled by rustc.
494 numSrcs := 0
495 srcIndex := 0
496 for i, s := range srcs {
497 if android.SrcIsModule(s) == "" {
498 numSrcs++
499 srcIndex = i
500 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700501 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400502 if numSrcs > 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400503 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700504 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400505
506 // If a main source file is not provided we expect only a single SourceProvider module to be defined
507 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700508 if srcIndex != 0 {
509 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400510 } else if numSrcs > 1 {
511 ctx.PropertyErrorf("srcs", "only a single generated source module can be defined without a main source file.")
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700512 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400513
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700514 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400515 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700516}