blob: 3fa3ccd696174312bfa8c5274fa668061446ca42 [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
Sam Delmerico60375c42023-09-11 17:18:08 +000076 // Entry point that is passed to rustc to begin the compilation. E.g. main.rs or lib.rs.
77 // When this property is set,
78 // * sandboxing is enabled for this module, and
79 // * the srcs attribute is interpreted as a list of all source files potentially
80 // used in compilation, including the entrypoint, and
81 // * compile_data can be used to add additional files used in compilation that
82 // not directly used as source files.
83 Crate_root *string `android:"path,arch_variant"`
84
Sam Delmericoa588d152023-06-16 10:28:04 -040085 // Additional data files that are used during compilation only. These are not accessible at runtime.
86 Compile_data []string `android:"path,arch_variant"`
87
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020088 // name of the lint set that should be used to validate this module.
89 //
90 // Possible values are "default" (for using a sensible set of lints
91 // depending on the module's location), "android" (for the strictest
92 // lint set that applies to all Android platform code), "vendor" (for
93 // a relaxed set) and "none" (for ignoring all lint warnings and
94 // errors). The default value is "default".
95 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070096
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +020097 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -040098 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070099
100 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -0400101 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102
103 // list of rust rlib crate dependencies
104 Rlibs []string `android:"arch_variant"`
105
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400106 // list of rust automatic crate dependencies.
107 // Rustlibs linkage is rlib for host targets and dylib for device targets.
Matthew Maurer0f003b12020-06-29 14:34:06 -0700108 Rustlibs []string `android:"arch_variant"`
109
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110 // list of rust proc_macro crate dependencies
111 Proc_macros []string `android:"arch_variant"`
112
113 // list of C shared library dependencies
114 Shared_libs []string `android:"arch_variant"`
115
Ivan Lozano63bb7682021-03-23 15:53:44 -0400116 // list of C static library dependencies. These dependencies do not normally propagate to dependents
117 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118 Static_libs []string `android:"arch_variant"`
119
Ivan Lozano63bb7682021-03-23 15:53:44 -0400120 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
121 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
122 // result in bloat if multiple dependencies all include the same static library whole.
123 //
124 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
125 // having to redeclare the static library dependency for every dependent module.
126 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
127 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
128 //
129 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
130 //
131 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
132 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
133 Whole_static_libs []string `android:"arch_variant"`
134
Andrew Walbran797e4be2022-03-07 15:41:53 +0000135 // list of Rust system library dependencies.
136 //
137 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
138 // like `core` and `alloc`.
139 Stdlibs []string `android:"arch_variant"`
140
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400141 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
142 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
143 // source, and is required to conform to an enforced format matching library output files (if the output file is
144 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 Crate_name string `android:"arch_variant"`
146
147 // list of features to enable for this crate
148 Features []string `android:"arch_variant"`
149
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200150 // list of configuration options to enable for this crate. To enable features, use the "features" property.
151 Cfgs []string `android:"arch_variant"`
152
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 // specific rust edition that should be used if the default version is not desired
154 Edition *string `android:"arch_variant"`
155
156 // sets name of the output
157 Stem *string `android:"arch_variant"`
158
159 // append to name of output
160 Suffix *string `android:"arch_variant"`
161
162 // install to a subdirectory of the default install path for the module
163 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700164
165 // whether to suppress inclusion of standard crates - defaults to false
166 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500167
168 // Change the rustlibs linkage to select rlib linkage by default for device targets.
169 // Also link libstd as an rlib as well on device targets.
170 // Note: This is the default behavior for host targets.
171 //
172 // This is primarily meant for rust_binary and rust_ffi modules where the default
173 // linkage of libstd might need to be overridden in some use cases. This should
174 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000175 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500176 // the same way.
177 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400178
179 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
180 // Will set CARGO_CRATE_NAME to the crate_name property's value.
181 // Will set CARGO_BIN_NAME to the output filename value without the extension.
182 Cargo_env_compat *bool
183
184 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
185 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700186}
187
188type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800189 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700190
191 // Install related
192 dir string
193 dir64 string
194 subDir string
195 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700196 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800197 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500198 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400199
Joel Galensonfa049382021-01-14 16:03:18 -0800200 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400201
202 // unstripped output file.
203 unstrippedOutputFile android.Path
204
205 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200206 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100207
208 // If a crate has a source-generated dependency, a copy of the source file
209 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
210 cargoOutDir android.ModuleOutPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211}
212
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400213func (compiler *baseCompiler) Disabled() bool {
214 return false
215}
216
217func (compiler *baseCompiler) SetDisabled() {
218 panic("baseCompiler does not implement SetDisabled()")
219}
220
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500221func (compiler *baseCompiler) noStdlibs() bool {
222 return Bool(compiler.Properties.No_stdlibs)
223}
224
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400225func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
226 panic("baseCompiler does not implement coverageOutputZipPath()")
227}
228
Ivan Lozanoea086132020-12-08 14:43:00 -0500229func (compiler *baseCompiler) preferRlib() bool {
230 return Bool(compiler.Properties.Prefer_rlib)
231}
232
Ivan Lozanodd055472020-09-28 13:22:45 -0400233func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400234 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500235 if compiler.preferRlib() {
236 return RlibLinkage
237 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400238 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400239 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400240 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400241 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400242}
243
Ivan Lozanoffee3342019-08-27 12:03:00 -0700244var _ compiler = (*baseCompiler)(nil)
245
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800246func (compiler *baseCompiler) inData() bool {
247 return compiler.location == InstallInData
248}
249
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250func (compiler *baseCompiler) compilerProps() []interface{} {
251 return []interface{}{&compiler.Properties}
252}
253
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200254func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200256 for _, cfg := range compiler.Properties.Cfgs {
257 flags = append(flags, "--cfg '"+cfg+"'")
258 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400259
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200260 return flags
261}
262
263func (compiler *baseCompiler) featuresToFlags() []string {
264 flags := []string{}
265 for _, feature := range compiler.Properties.Features {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
267 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400268
269 return flags
270}
271
272func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
273 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
274 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
275
276 return flags
277}
278
279func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
280 if ctx.RustModule().UseVndk() {
281 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000282 if ctx.RustModule().InVendor() {
283 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
284 } else if ctx.RustModule().InProduct() {
285 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
286 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400287 }
288
289 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
290 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291 return flags
292}
293
294func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
295
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200296 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
297 if err != nil {
298 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700299 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400300
301 // linkage-related flags are disallowed.
302 for _, s := range compiler.Properties.Ld_flags {
303 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
304 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
305 }
306 }
307 for _, s := range compiler.Properties.Flags {
308 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
309 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
310 }
311 if strings.HasPrefix(s, "--extern") {
312 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
313 }
314 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
315 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
316 }
317 }
318
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200319 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700320 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200321 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700322 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700323 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700324 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700325 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
326 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Sasha Smundaka76acba2022-04-18 20:12:56 -0700327 flags.EmitXrefs = ctx.Config().EmitXrefRules()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700328
329 if ctx.Host() && !ctx.Windows() {
Colin Cross225a37a2023-01-11 14:17:39 -0800330 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700331 }
332
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400333 if !ctx.toolchain().Bionic() && ctx.Os() != android.LinuxMusl && !ctx.Windows() {
334 // Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of device
335 // builds. This is irrelevant for the Windows target as these are Posix specific.
336 flags.LinkFlags = append(flags.LinkFlags,
337 "-ldl",
338 "-lpthread",
339 "-lm",
340 )
341 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700342 return flags
343}
344
Sasha Smundaka76acba2022-04-18 20:12:56 -0700345func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700346 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
347}
348
Sam Delmericoa588d152023-06-16 10:28:04 -0400349func (compile *baseCompiler) crateRoot(ctx ModuleContext) android.Path {
350 if compile.Properties.Crate_root != nil {
351 return android.PathForModuleSrc(ctx, *compile.Properties.Crate_root)
352 }
353 return nil
354}
355
356// compilationSourcesAndData returns a list of files necessary to complete the compilation.
357// This includes the rust source files as well as any other data files that
358// are referenced during the build.
359func (compile *baseCompiler) compilationSourcesAndData(ctx ModuleContext) android.Paths {
360 return android.PathsForModuleSrc(ctx, android.Concat(
361 compile.Properties.Srcs,
362 compile.Properties.Compile_data,
363 ))
364}
365
Dan Albert06feee92021-03-19 15:06:02 -0700366func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
367 deps PathDeps) android.OptionalPath {
368
369 return android.OptionalPath{}
370}
371
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100372func (compiler *baseCompiler) initialize(ctx ModuleContext) {
373 compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
374}
375
376func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
377 return android.OptionalPathForPath(compiler.cargoOutDir)
378}
379
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400380func (compiler *baseCompiler) CargoEnvCompat() bool {
381 return Bool(compiler.Properties.Cargo_env_compat)
382}
383
384func (compiler *baseCompiler) CargoPkgVersion() string {
385 return String(compiler.Properties.Cargo_pkg_version)
386}
387
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400388func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
389 return compiler.unstrippedOutputFile
390}
391
Jiyong Parke54f07e2021-04-07 15:08:04 +0900392func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
393 return compiler.strippedOutputFile
394}
395
Ivan Lozanoffee3342019-08-27 12:03:00 -0700396func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
397 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700398 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700399 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
400 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400401 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700402 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000403 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404
Matthew Maurer99020b02019-10-31 10:44:40 -0700405 if !Bool(compiler.Properties.No_stdlibs) {
406 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700407 // If we're building for the build host, use the prebuilt stdlibs, unless the host
408 // is linux_bionic which doesn't have prebuilts.
409 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500410 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700411 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400412 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700413 }
414 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415 return deps
416}
417
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100418func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400419 bionicLibs := []string{}
420 bionicLibs = append(bionicLibs, "liblog")
421 bionicLibs = append(bionicLibs, "libc")
422 bionicLibs = append(bionicLibs, "libm")
423 bionicLibs = append(bionicLibs, "libdl")
424
425 if static {
426 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
427 } else {
428 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
429 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400430 if ctx.RustModule().StaticExecutable() {
431 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
432 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100433 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
434 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
435 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700436 return deps
437}
438
Colin Crosse32f0932022-01-23 20:48:36 -0800439func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
440 muslLibs := []string{"libc_musl"}
441 if static {
442 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
443 } else {
444 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
445 }
446 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
447 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
448 }
449
450 return deps
451}
452
Ivan Lozanoffee3342019-08-27 12:03:00 -0700453func (compiler *baseCompiler) crateName() string {
454 return compiler.Properties.Crate_name
455}
456
Ivan Lozanod7586b62021-04-01 09:49:36 -0400457func (compiler *baseCompiler) everInstallable() bool {
458 // Most modules are installable, so return true by default.
459 return true
460}
461
Colin Cross70dda7e2019-10-01 22:05:35 -0700462func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700463 dir := compiler.dir
464 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
465 dir = compiler.dir64
466 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400467 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
468 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
469 }
470 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700471 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
472 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400473
474 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700475 if ctx.RustModule().InProduct() {
476 dir = filepath.Join(dir, "product")
477 } else if ctx.RustModule().InVendor() {
478 dir = filepath.Join(dir, "vendor")
479 } else {
480 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
481 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400482 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700483
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
485 compiler.relativeInstallPath(), compiler.relative)
486}
487
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400488func (compiler *baseCompiler) nativeCoverage() bool {
489 return false
490}
491
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200492func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900493 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200494 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700495}
496
497func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
498 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
499}
500
501func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200502 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700503 if String(compiler.Properties.Stem) != "" {
504 stem = String(compiler.Properties.Stem)
505 }
506
507 return stem
508}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700509
Ivan Lozanoffee3342019-08-27 12:03:00 -0700510func (compiler *baseCompiler) relativeInstallPath() string {
511 return String(compiler.Properties.Relative_install_path)
512}
513
Ivan Lozano43845682020-07-09 21:03:28 -0400514// 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 -0700515func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
Seth Moore3afac0b2021-10-13 15:32:18 -0700516 if len(srcs) == 0 {
517 ctx.PropertyErrorf("srcs", "srcs must not be empty")
518 }
519
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700520 // The srcs can contain strings with prefix ":".
521 // They are dependent modules of this module, with android.SourceDepTag.
522 // They are not the main source file compiled by rustc.
523 numSrcs := 0
524 srcIndex := 0
525 for i, s := range srcs {
526 if android.SrcIsModule(s) == "" {
527 numSrcs++
528 srcIndex = i
529 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700530 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400531 if numSrcs > 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400532 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700533 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400534
535 // If a main source file is not provided we expect only a single SourceProvider module to be defined
536 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700537 if srcIndex != 0 {
538 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400539 } else if numSrcs > 1 {
540 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 -0700541 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400542
Sam Delmerico60375c42023-09-11 17:18:08 +0000543 // TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
544 // entry point file from Srcs rather than taking the first one
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700545 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400546 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700547}