blob: 899502a64d04c4013f3acab48265c288aca50d46 [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
Matthew Maurer689d6f62023-11-20 17:49:25 +000037type compiler interface {
38 initialize(ctx ModuleContext)
39 compilerFlags(ctx ModuleContext, flags Flags) Flags
40 cfgFlags(ctx ModuleContext, flags Flags) Flags
41 featureFlags(ctx ModuleContext, flags Flags) Flags
42 compilerProps() []interface{}
43 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
44 compilerDeps(ctx DepsContext, deps Deps) Deps
45 crateName() string
46 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
47
48 // Output directory in which source-generated code from dependencies is
49 // copied. This is equivalent to Cargo's OUT_DIR variable.
Matthew Maurercd416532023-11-20 17:52:01 +000050 cargoOutDir() android.OptionalPath
Matthew Maurer689d6f62023-11-20 17:49:25 +000051
Matthew Maurercd416532023-11-20 17:52:01 +000052 // cargoPkgVersion returns the value of the Cargo_pkg_version property.
53 cargoPkgVersion() string
Matthew Maurer689d6f62023-11-20 17:49:25 +000054
Matthew Maurercd416532023-11-20 17:52:01 +000055 // cargoEnvCompat returns whether Cargo environment variables should be used.
56 cargoEnvCompat() bool
Matthew Maurer689d6f62023-11-20 17:49:25 +000057
58 inData() bool
59 install(ctx ModuleContext)
60 relativeInstallPath() string
61 everInstallable() bool
62
63 nativeCoverage() bool
64
65 Disabled() bool
66 SetDisabled()
67
68 stdLinkage(ctx *depsContext) RustLinkage
69 noStdlibs() bool
70
71 unstrippedOutputFilePath() android.Path
72 strippedOutputFilePath() android.OptionalPath
Matthew Maurerd221d312023-11-20 21:02:40 +000073
74 crateRootPath(ctx ModuleContext) android.Path
Matthew Maurer689d6f62023-11-20 17:49:25 +000075}
76
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020077func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070078 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
79}
80
Matthew Maurer99020b02019-10-31 10:44:40 -070081func (compiler *baseCompiler) setNoStdlibs() {
82 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
83}
84
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020085func (compiler *baseCompiler) disableLints() {
86 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070087}
88
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080089func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070090 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070091 Properties: BaseCompilerProperties{},
92 dir: dir,
93 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080094 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070095 }
96}
97
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080098type installLocation int
99
100const (
101 InstallInSystem installLocation = 0
102 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -0400103
104 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100105 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800106)
107
Ivan Lozanoffee3342019-08-27 12:03:00 -0700108type BaseCompilerProperties struct {
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400109 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
110 // Only a single source file can be defined. Modules which generate source can be included by prefixing
111 // the module name with ":", for example ":libfoo_bindgen"
112 //
113 // 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 -0400114 Srcs []string `android:"path,arch_variant"`
115
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000116 // Entry point that is passed to rustc to begin the compilation. E.g. main.rs or lib.rs.
117 // When this property is set,
118 // * sandboxing is enabled for this module, and
119 // * the srcs attribute is interpreted as a list of all source files potentially
120 // used in compilation, including the entrypoint, and
121 // * compile_data can be used to add additional files used in compilation that
122 // not directly used as source files.
123 Crate_root *string `android:"path,arch_variant"`
124
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200125 // name of the lint set that should be used to validate this module.
126 //
127 // Possible values are "default" (for using a sensible set of lints
128 // depending on the module's location), "android" (for the strictest
129 // lint set that applies to all Android platform code), "vendor" (for
130 // a relaxed set) and "none" (for ignoring all lint warnings and
131 // errors). The default value is "default".
132 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700133
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200134 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -0400135 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136
137 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -0400138 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139
140 // list of rust rlib crate dependencies
141 Rlibs []string `android:"arch_variant"`
142
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400143 // list of rust automatic crate dependencies.
144 // Rustlibs linkage is rlib for host targets and dylib for device targets.
Matthew Maurer0f003b12020-06-29 14:34:06 -0700145 Rustlibs []string `android:"arch_variant"`
146
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147 // list of rust proc_macro crate dependencies
148 Proc_macros []string `android:"arch_variant"`
149
150 // list of C shared library dependencies
151 Shared_libs []string `android:"arch_variant"`
152
Ivan Lozano63bb7682021-03-23 15:53:44 -0400153 // list of C static library dependencies. These dependencies do not normally propagate to dependents
154 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 Static_libs []string `android:"arch_variant"`
156
Ivan Lozano63bb7682021-03-23 15:53:44 -0400157 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
158 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
159 // result in bloat if multiple dependencies all include the same static library whole.
160 //
161 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
162 // having to redeclare the static library dependency for every dependent module.
163 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
164 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
165 //
166 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
167 //
168 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
169 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
170 Whole_static_libs []string `android:"arch_variant"`
171
Andrew Walbran797e4be2022-03-07 15:41:53 +0000172 // list of Rust system library dependencies.
173 //
174 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
175 // like `core` and `alloc`.
176 Stdlibs []string `android:"arch_variant"`
177
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400178 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
179 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
180 // source, and is required to conform to an enforced format matching library output files (if the output file is
181 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182 Crate_name string `android:"arch_variant"`
183
184 // list of features to enable for this crate
185 Features []string `android:"arch_variant"`
186
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200187 // list of configuration options to enable for this crate. To enable features, use the "features" property.
188 Cfgs []string `android:"arch_variant"`
189
Ivan Lozanoffee3342019-08-27 12:03:00 -0700190 // specific rust edition that should be used if the default version is not desired
191 Edition *string `android:"arch_variant"`
192
193 // sets name of the output
194 Stem *string `android:"arch_variant"`
195
196 // append to name of output
197 Suffix *string `android:"arch_variant"`
198
199 // install to a subdirectory of the default install path for the module
200 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700201
202 // whether to suppress inclusion of standard crates - defaults to false
203 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500204
205 // Change the rustlibs linkage to select rlib linkage by default for device targets.
206 // Also link libstd as an rlib as well on device targets.
207 // Note: This is the default behavior for host targets.
208 //
209 // This is primarily meant for rust_binary and rust_ffi modules where the default
210 // linkage of libstd might need to be overridden in some use cases. This should
211 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000212 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500213 // the same way.
214 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400215
216 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
217 // Will set CARGO_CRATE_NAME to the crate_name property's value.
218 // Will set CARGO_BIN_NAME to the output filename value without the extension.
219 Cargo_env_compat *bool
220
221 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
222 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700223}
224
225type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800226 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700227
228 // Install related
229 dir string
230 dir64 string
231 subDir string
232 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700233 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800234 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500235 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400236
Joel Galensonfa049382021-01-14 16:03:18 -0800237 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400238
239 // unstripped output file.
240 unstrippedOutputFile android.Path
241
242 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200243 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100244
245 // If a crate has a source-generated dependency, a copy of the source file
246 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
Matthew Maurercd416532023-11-20 17:52:01 +0000247 // This is stored internally because it may not be available during
248 // singleton-generation passes like rustdoc/rust_project.json, but should
249 // be stashed during initial generation.
250 cachedCargoOutDir android.ModuleOutPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700251}
252
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400253func (compiler *baseCompiler) Disabled() bool {
254 return false
255}
256
257func (compiler *baseCompiler) SetDisabled() {
258 panic("baseCompiler does not implement SetDisabled()")
259}
260
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500261func (compiler *baseCompiler) noStdlibs() bool {
262 return Bool(compiler.Properties.No_stdlibs)
263}
264
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400265func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
266 panic("baseCompiler does not implement coverageOutputZipPath()")
267}
268
Ivan Lozanoea086132020-12-08 14:43:00 -0500269func (compiler *baseCompiler) preferRlib() bool {
270 return Bool(compiler.Properties.Prefer_rlib)
271}
272
Ivan Lozanodd055472020-09-28 13:22:45 -0400273func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400274 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500275 if compiler.preferRlib() {
276 return RlibLinkage
277 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400278 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400279 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400280 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400281 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400282}
283
Ivan Lozanoffee3342019-08-27 12:03:00 -0700284var _ compiler = (*baseCompiler)(nil)
285
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800286func (compiler *baseCompiler) inData() bool {
287 return compiler.location == InstallInData
288}
289
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290func (compiler *baseCompiler) compilerProps() []interface{} {
291 return []interface{}{&compiler.Properties}
292}
293
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200294func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700295 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200296 for _, cfg := range compiler.Properties.Cfgs {
297 flags = append(flags, "--cfg '"+cfg+"'")
298 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400299
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200300 return flags
301}
302
303func (compiler *baseCompiler) featuresToFlags() []string {
304 flags := []string{}
305 for _, feature := range compiler.Properties.Features {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700306 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
307 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400308
309 return flags
310}
311
312func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
313 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
314 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
315
316 return flags
317}
318
319func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
320 if ctx.RustModule().UseVndk() {
321 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000322 if ctx.RustModule().InVendor() {
323 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
324 } else if ctx.RustModule().InProduct() {
325 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
326 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400327 }
328
329 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
330 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700331 return flags
332}
333
334func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
335
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200336 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
337 if err != nil {
338 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700339 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400340
341 // linkage-related flags are disallowed.
342 for _, s := range compiler.Properties.Ld_flags {
343 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
344 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
345 }
346 }
347 for _, s := range compiler.Properties.Flags {
348 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
349 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
350 }
351 if strings.HasPrefix(s, "--extern") {
352 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
353 }
354 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
355 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
356 }
357 }
358
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200359 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700360 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200361 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700362 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700363 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700364 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700365 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
366 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Sasha Smundaka76acba2022-04-18 20:12:56 -0700367 flags.EmitXrefs = ctx.Config().EmitXrefRules()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700368
369 if ctx.Host() && !ctx.Windows() {
Colin Cross225a37a2023-01-11 14:17:39 -0800370 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700371 }
372
Colin Crosse18bd202023-10-03 19:12:50 -0700373 if ctx.Os() == android.Linux {
374 // Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
375 // the default behavior of device builds.
Vinh Tran50de8be2023-09-21 15:28:53 -0400376 flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
Colin Crosse18bd202023-10-03 19:12:50 -0700377 } else if ctx.Os() == android.Darwin {
378 // Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
379 // behavior of device builds.
380 flags.LinkFlags = append(flags.LinkFlags,
381 "-lc",
382 "-ldl",
383 "-lpthread",
384 "-lm",
385 )
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400386 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700387 return flags
388}
389
Sasha Smundaka76acba2022-04-18 20:12:56 -0700390func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700391 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
392}
393
Dan Albert06feee92021-03-19 15:06:02 -0700394func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
395 deps PathDeps) android.OptionalPath {
396
397 return android.OptionalPath{}
398}
399
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100400func (compiler *baseCompiler) initialize(ctx ModuleContext) {
Matthew Maurercd416532023-11-20 17:52:01 +0000401 compiler.cachedCargoOutDir = android.PathForModuleOut(ctx, genSubDir)
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100402}
403
Matthew Maurercd416532023-11-20 17:52:01 +0000404func (compiler *baseCompiler) cargoOutDir() android.OptionalPath {
405 return android.OptionalPathForPath(compiler.cachedCargoOutDir)
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100406}
407
Matthew Maurercd416532023-11-20 17:52:01 +0000408func (compiler *baseCompiler) cargoEnvCompat() bool {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400409 return Bool(compiler.Properties.Cargo_env_compat)
410}
411
Matthew Maurercd416532023-11-20 17:52:01 +0000412func (compiler *baseCompiler) cargoPkgVersion() string {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400413 return String(compiler.Properties.Cargo_pkg_version)
414}
415
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400416func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
417 return compiler.unstrippedOutputFile
418}
419
Jiyong Parke54f07e2021-04-07 15:08:04 +0900420func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
421 return compiler.strippedOutputFile
422}
423
Ivan Lozanoffee3342019-08-27 12:03:00 -0700424func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
425 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700426 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
428 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400429 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700430 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000431 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700432
Matthew Maurer99020b02019-10-31 10:44:40 -0700433 if !Bool(compiler.Properties.No_stdlibs) {
434 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700435 // If we're building for the build host, use the prebuilt stdlibs, unless the host
436 // is linux_bionic which doesn't have prebuilts.
437 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500438 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700439 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400440 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700441 }
442 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443 return deps
444}
445
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100446func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400447 bionicLibs := []string{}
448 bionicLibs = append(bionicLibs, "liblog")
449 bionicLibs = append(bionicLibs, "libc")
450 bionicLibs = append(bionicLibs, "libm")
451 bionicLibs = append(bionicLibs, "libdl")
452
453 if static {
454 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
455 } else {
456 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
457 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400458 if ctx.RustModule().StaticExecutable() {
459 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
460 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100461 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
462 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
463 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700464 return deps
465}
466
Colin Crosse32f0932022-01-23 20:48:36 -0800467func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
468 muslLibs := []string{"libc_musl"}
469 if static {
470 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
471 } else {
472 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
473 }
474 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
475 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
476 }
477
478 return deps
479}
480
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481func (compiler *baseCompiler) crateName() string {
482 return compiler.Properties.Crate_name
483}
484
Ivan Lozanod7586b62021-04-01 09:49:36 -0400485func (compiler *baseCompiler) everInstallable() bool {
486 // Most modules are installable, so return true by default.
487 return true
488}
489
Colin Cross70dda7e2019-10-01 22:05:35 -0700490func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700491 dir := compiler.dir
492 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
493 dir = compiler.dir64
494 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400495 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
496 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
497 }
498 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700499 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
500 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400501
502 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700503 if ctx.RustModule().InProduct() {
504 dir = filepath.Join(dir, "product")
505 } else if ctx.RustModule().InVendor() {
506 dir = filepath.Join(dir, "vendor")
507 } else {
508 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
509 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400510 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700511
Ivan Lozanoffee3342019-08-27 12:03:00 -0700512 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
513 compiler.relativeInstallPath(), compiler.relative)
514}
515
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400516func (compiler *baseCompiler) nativeCoverage() bool {
517 return false
518}
519
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200520func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900521 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200522 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700523}
524
525func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
526 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
527}
528
529func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200530 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531 if String(compiler.Properties.Stem) != "" {
532 stem = String(compiler.Properties.Stem)
533 }
534
535 return stem
536}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700537
Ivan Lozanoffee3342019-08-27 12:03:00 -0700538func (compiler *baseCompiler) relativeInstallPath() string {
539 return String(compiler.Properties.Relative_install_path)
540}
541
Matthew Maurerd221d312023-11-20 21:02:40 +0000542func (compiler *baseCompiler) crateRootPath(ctx ModuleContext) android.Path {
543 if compiler.Properties.Crate_root == nil {
Matthew Maurer1d8e20d2023-11-20 21:18:12 +0000544 path := srcPathFromModuleSrcs(ctx, compiler.Properties.Srcs)
Matthew Maurerd221d312023-11-20 21:02:40 +0000545 return path
546 } else {
547 return android.PathForModuleSrc(ctx, *compiler.Properties.Crate_root)
548 }
549}
550
Ivan Lozano43845682020-07-09 21:03:28 -0400551// Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs.
Matthew Maurer1d8e20d2023-11-20 21:18:12 +0000552func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) android.Path {
Seth Moore3afac0b2021-10-13 15:32:18 -0700553 if len(srcs) == 0 {
554 ctx.PropertyErrorf("srcs", "srcs must not be empty")
555 }
556
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700557 // The srcs can contain strings with prefix ":".
558 // They are dependent modules of this module, with android.SourceDepTag.
559 // They are not the main source file compiled by rustc.
560 numSrcs := 0
561 srcIndex := 0
562 for i, s := range srcs {
563 if android.SrcIsModule(s) == "" {
564 numSrcs++
565 srcIndex = i
566 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700567 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400568 if numSrcs > 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400569 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700570 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400571
572 // If a main source file is not provided we expect only a single SourceProvider module to be defined
573 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700574 if srcIndex != 0 {
575 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400576 } else if numSrcs > 1 {
577 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 -0700578 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400579
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000580 // TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
581 // entry point file from Srcs rather than taking the first one
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700582 paths := android.PathsForModuleSrc(ctx, srcs)
Matthew Maurer1d8e20d2023-11-20 21:18:12 +0000583 return paths[srcIndex]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700584}