blob: 06ae12f79296ada6668373ca5e53de53ee1a69f4 [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
94 // list of rust dylib crate dependencies
95 Dylibs []string `android:"arch_variant"`
96
Matthew Maurer0f003b12020-06-29 14:34:06 -070097 // list of rust automatic crate dependencies
98 Rustlibs []string `android:"arch_variant"`
99
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100 // list of rust proc_macro crate dependencies
101 Proc_macros []string `android:"arch_variant"`
102
103 // list of C shared library dependencies
104 Shared_libs []string `android:"arch_variant"`
105
Ivan Lozano63bb7682021-03-23 15:53:44 -0400106 // list of C static library dependencies. These dependencies do not normally propagate to dependents
107 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700108 Static_libs []string `android:"arch_variant"`
109
Ivan Lozano63bb7682021-03-23 15:53:44 -0400110 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
111 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
112 // result in bloat if multiple dependencies all include the same static library whole.
113 //
114 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
115 // having to redeclare the static library dependency for every dependent module.
116 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
117 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
118 //
119 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
120 //
121 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
122 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
123 Whole_static_libs []string `android:"arch_variant"`
124
Andrew Walbran797e4be2022-03-07 15:41:53 +0000125 // list of Rust system library dependencies.
126 //
127 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
128 // like `core` and `alloc`.
129 Stdlibs []string `android:"arch_variant"`
130
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400131 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
132 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
133 // source, and is required to conform to an enforced format matching library output files (if the output file is
134 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135 Crate_name string `android:"arch_variant"`
136
137 // list of features to enable for this crate
138 Features []string `android:"arch_variant"`
139
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200140 // list of configuration options to enable for this crate. To enable features, use the "features" property.
141 Cfgs []string `android:"arch_variant"`
142
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143 // specific rust edition that should be used if the default version is not desired
144 Edition *string `android:"arch_variant"`
145
146 // sets name of the output
147 Stem *string `android:"arch_variant"`
148
149 // append to name of output
150 Suffix *string `android:"arch_variant"`
151
152 // install to a subdirectory of the default install path for the module
153 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700154
155 // whether to suppress inclusion of standard crates - defaults to false
156 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500157
158 // Change the rustlibs linkage to select rlib linkage by default for device targets.
159 // Also link libstd as an rlib as well on device targets.
160 // Note: This is the default behavior for host targets.
161 //
162 // This is primarily meant for rust_binary and rust_ffi modules where the default
163 // linkage of libstd might need to be overridden in some use cases. This should
164 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000165 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500166 // the same way.
167 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400168
169 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
170 // Will set CARGO_CRATE_NAME to the crate_name property's value.
171 // Will set CARGO_BIN_NAME to the output filename value without the extension.
172 Cargo_env_compat *bool
173
174 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
175 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700176}
177
178type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800179 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180
181 // Install related
182 dir string
183 dir64 string
184 subDir string
185 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700186 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800187 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500188 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400189
Joel Galensonfa049382021-01-14 16:03:18 -0800190 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400191
192 // unstripped output file.
193 unstrippedOutputFile android.Path
194
195 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200196 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100197
198 // If a crate has a source-generated dependency, a copy of the source file
199 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
200 cargoOutDir android.ModuleOutPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700201}
202
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400203func (compiler *baseCompiler) Disabled() bool {
204 return false
205}
206
207func (compiler *baseCompiler) SetDisabled() {
208 panic("baseCompiler does not implement SetDisabled()")
209}
210
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500211func (compiler *baseCompiler) noStdlibs() bool {
212 return Bool(compiler.Properties.No_stdlibs)
213}
214
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400215func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
216 panic("baseCompiler does not implement coverageOutputZipPath()")
217}
218
Ivan Lozanoea086132020-12-08 14:43:00 -0500219func (compiler *baseCompiler) preferRlib() bool {
220 return Bool(compiler.Properties.Prefer_rlib)
221}
222
Ivan Lozanodd055472020-09-28 13:22:45 -0400223func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400224 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500225 if compiler.preferRlib() {
226 return RlibLinkage
227 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400228 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400229 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400230 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400231 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400232}
233
Ivan Lozanoffee3342019-08-27 12:03:00 -0700234var _ compiler = (*baseCompiler)(nil)
235
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800236func (compiler *baseCompiler) inData() bool {
237 return compiler.location == InstallInData
238}
239
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240func (compiler *baseCompiler) compilerProps() []interface{} {
241 return []interface{}{&compiler.Properties}
242}
243
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200244func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200246 for _, cfg := range compiler.Properties.Cfgs {
247 flags = append(flags, "--cfg '"+cfg+"'")
248 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400249
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200250 return flags
251}
252
253func (compiler *baseCompiler) featuresToFlags() []string {
254 flags := []string{}
255 for _, feature := range compiler.Properties.Features {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
257 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400258
259 return flags
260}
261
262func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
263 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
264 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
265
266 return flags
267}
268
269func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
270 if ctx.RustModule().UseVndk() {
271 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000272 if ctx.RustModule().InVendor() {
273 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
274 } else if ctx.RustModule().InProduct() {
275 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
276 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400277 }
278
279 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
280 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700281 return flags
282}
283
284func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
285
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200286 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
287 if err != nil {
288 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700289 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400290
291 // linkage-related flags are disallowed.
292 for _, s := range compiler.Properties.Ld_flags {
293 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
294 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
295 }
296 }
297 for _, s := range compiler.Properties.Flags {
298 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
299 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
300 }
301 if strings.HasPrefix(s, "--extern") {
302 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
303 }
304 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
305 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
306 }
307 }
308
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200309 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700310 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200311 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700312 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700313 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700314 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700315 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
316 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Sasha Smundaka76acba2022-04-18 20:12:56 -0700317 flags.EmitXrefs = ctx.Config().EmitXrefRules()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700318
319 if ctx.Host() && !ctx.Windows() {
Colin Cross225a37a2023-01-11 14:17:39 -0800320 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321 }
322
323 return flags
324}
325
Sasha Smundaka76acba2022-04-18 20:12:56 -0700326func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700327 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
328}
329
Dan Albert06feee92021-03-19 15:06:02 -0700330func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
331 deps PathDeps) android.OptionalPath {
332
333 return android.OptionalPath{}
334}
335
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100336func (compiler *baseCompiler) initialize(ctx ModuleContext) {
337 compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
338}
339
340func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
341 return android.OptionalPathForPath(compiler.cargoOutDir)
342}
343
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400344func (compiler *baseCompiler) CargoEnvCompat() bool {
345 return Bool(compiler.Properties.Cargo_env_compat)
346}
347
348func (compiler *baseCompiler) CargoPkgVersion() string {
349 return String(compiler.Properties.Cargo_pkg_version)
350}
351
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400352func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
353 return compiler.unstrippedOutputFile
354}
355
Jiyong Parke54f07e2021-04-07 15:08:04 +0900356func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
357 return compiler.strippedOutputFile
358}
359
Ivan Lozanoffee3342019-08-27 12:03:00 -0700360func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
361 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
362 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700363 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700364 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
365 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400366 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000368 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700369
Matthew Maurer99020b02019-10-31 10:44:40 -0700370 if !Bool(compiler.Properties.No_stdlibs) {
371 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700372 // If we're building for the build host, use the prebuilt stdlibs, unless the host
373 // is linux_bionic which doesn't have prebuilts.
374 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500375 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700376 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400377 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700378 }
379 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700380 return deps
381}
382
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100383func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400384 bionicLibs := []string{}
385 bionicLibs = append(bionicLibs, "liblog")
386 bionicLibs = append(bionicLibs, "libc")
387 bionicLibs = append(bionicLibs, "libm")
388 bionicLibs = append(bionicLibs, "libdl")
389
390 if static {
391 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
392 } else {
393 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
394 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400395 if ctx.RustModule().StaticExecutable() {
396 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
397 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100398 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
399 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
400 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700401 return deps
402}
403
Colin Crosse32f0932022-01-23 20:48:36 -0800404func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
405 muslLibs := []string{"libc_musl"}
406 if static {
407 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
408 } else {
409 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
410 }
411 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
412 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
413 }
414
415 return deps
416}
417
Ivan Lozanoffee3342019-08-27 12:03:00 -0700418func (compiler *baseCompiler) crateName() string {
419 return compiler.Properties.Crate_name
420}
421
Ivan Lozanod7586b62021-04-01 09:49:36 -0400422func (compiler *baseCompiler) everInstallable() bool {
423 // Most modules are installable, so return true by default.
424 return true
425}
426
Colin Cross70dda7e2019-10-01 22:05:35 -0700427func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428 dir := compiler.dir
429 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
430 dir = compiler.dir64
431 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400432 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
433 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
434 }
435 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700436 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
437 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400438
439 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700440 if ctx.RustModule().InProduct() {
441 dir = filepath.Join(dir, "product")
442 } else if ctx.RustModule().InVendor() {
443 dir = filepath.Join(dir, "vendor")
444 } else {
445 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
446 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400447 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700448
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
450 compiler.relativeInstallPath(), compiler.relative)
451}
452
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400453func (compiler *baseCompiler) nativeCoverage() bool {
454 return false
455}
456
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200457func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900458 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200459 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700460}
461
462func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
463 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
464}
465
466func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200467 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700468 if String(compiler.Properties.Stem) != "" {
469 stem = String(compiler.Properties.Stem)
470 }
471
472 return stem
473}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700474
Ivan Lozanoffee3342019-08-27 12:03:00 -0700475func (compiler *baseCompiler) relativeInstallPath() string {
476 return String(compiler.Properties.Relative_install_path)
477}
478
Ivan Lozano43845682020-07-09 21:03:28 -0400479// 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 -0700480func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
Seth Moore3afac0b2021-10-13 15:32:18 -0700481 if len(srcs) == 0 {
482 ctx.PropertyErrorf("srcs", "srcs must not be empty")
483 }
484
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700485 // The srcs can contain strings with prefix ":".
486 // They are dependent modules of this module, with android.SourceDepTag.
487 // They are not the main source file compiled by rustc.
488 numSrcs := 0
489 srcIndex := 0
490 for i, s := range srcs {
491 if android.SrcIsModule(s) == "" {
492 numSrcs++
493 srcIndex = i
494 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400496 if numSrcs > 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400497 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700498 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400499
500 // If a main source file is not provided we expect only a single SourceProvider module to be defined
501 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700502 if srcIndex != 0 {
503 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400504 } else if numSrcs > 1 {
505 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 -0700506 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400507
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700508 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400509 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700510}