blob: 5033fbac2f5e975f5191f3aa9f645f90a8dfbdb9 [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"
Matthew Maurera28404a2023-11-20 23:33:28 +000019 "errors"
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "fmt"
21 "path/filepath"
Ivan Lozano45a9e312021-07-27 12:29:12 -040022 "strings"
Ivan Lozanoffee3342019-08-27 12:03:00 -070023
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070024 "github.com/google/blueprint/proptools"
25
Ivan Lozanoffee3342019-08-27 12:03:00 -070026 "android/soong/android"
27 "android/soong/rust/config"
28)
29
Ivan Lozanodd055472020-09-28 13:22:45 -040030type RustLinkage int
31
32const (
33 DefaultLinkage RustLinkage = iota
34 RlibLinkage
35 DylibLinkage
36)
37
Matthew Maurer689d6f62023-11-20 17:49:25 +000038type compiler interface {
39 initialize(ctx ModuleContext)
40 compilerFlags(ctx ModuleContext, flags Flags) Flags
41 cfgFlags(ctx ModuleContext, flags Flags) Flags
42 featureFlags(ctx ModuleContext, flags Flags) Flags
43 compilerProps() []interface{}
44 compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput
45 compilerDeps(ctx DepsContext, deps Deps) Deps
46 crateName() string
Matthew Maurerdb72f7e2023-11-21 00:20:02 +000047 edition() string
48 features() []string
Matthew Maurer689d6f62023-11-20 17:49:25 +000049 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
50
51 // Output directory in which source-generated code from dependencies is
52 // copied. This is equivalent to Cargo's OUT_DIR variable.
Matthew Maurercd416532023-11-20 17:52:01 +000053 cargoOutDir() android.OptionalPath
Matthew Maurer689d6f62023-11-20 17:49:25 +000054
Matthew Maurercd416532023-11-20 17:52:01 +000055 // cargoPkgVersion returns the value of the Cargo_pkg_version property.
56 cargoPkgVersion() string
Matthew Maurer689d6f62023-11-20 17:49:25 +000057
Matthew Maurercd416532023-11-20 17:52:01 +000058 // cargoEnvCompat returns whether Cargo environment variables should be used.
59 cargoEnvCompat() bool
Matthew Maurer689d6f62023-11-20 17:49:25 +000060
61 inData() bool
62 install(ctx ModuleContext)
63 relativeInstallPath() string
64 everInstallable() bool
65
66 nativeCoverage() bool
67
68 Disabled() bool
69 SetDisabled()
70
71 stdLinkage(ctx *depsContext) RustLinkage
72 noStdlibs() bool
73
74 unstrippedOutputFilePath() android.Path
75 strippedOutputFilePath() android.OptionalPath
Matthew Maurerd221d312023-11-20 21:02:40 +000076
Matthew Maurera28404a2023-11-20 23:33:28 +000077 checkedCrateRootPath() (android.Path, error)
Andrew Walbran52533232024-03-19 11:36:04 +000078
79 Aliases() map[string]string
Matthew Maurer689d6f62023-11-20 17:49:25 +000080}
81
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020082func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070083 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
84}
85
Matthew Maurer99020b02019-10-31 10:44:40 -070086func (compiler *baseCompiler) setNoStdlibs() {
87 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
88}
89
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020090func (compiler *baseCompiler) disableLints() {
91 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070092}
93
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080094func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070095 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070096 Properties: BaseCompilerProperties{},
97 dir: dir,
98 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080099 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100 }
101}
102
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800103type installLocation int
104
105const (
106 InstallInSystem installLocation = 0
107 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -0400108
109 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100110 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800111)
112
Ivan Lozanoffee3342019-08-27 12:03:00 -0700113type BaseCompilerProperties struct {
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400114 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
115 // Only a single source file can be defined. Modules which generate source can be included by prefixing
116 // the module name with ":", for example ":libfoo_bindgen"
117 //
118 // 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 -0400119 Srcs []string `android:"path,arch_variant"`
120
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000121 // Entry point that is passed to rustc to begin the compilation. E.g. main.rs or lib.rs.
122 // When this property is set,
123 // * sandboxing is enabled for this module, and
124 // * the srcs attribute is interpreted as a list of all source files potentially
125 // used in compilation, including the entrypoint, and
126 // * compile_data can be used to add additional files used in compilation that
127 // not directly used as source files.
128 Crate_root *string `android:"path,arch_variant"`
129
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200130 // name of the lint set that should be used to validate this module.
131 //
132 // Possible values are "default" (for using a sensible set of lints
133 // depending on the module's location), "android" (for the strictest
134 // lint set that applies to all Android platform code), "vendor" (for
135 // a relaxed set) and "none" (for ignoring all lint warnings and
136 // errors). The default value is "default".
137 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700138
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200139 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -0400140 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
142 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -0400143 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144
Andrew Walbran52533232024-03-19 11:36:04 +0000145 // Rust crate dependencies to rename. Each entry should be a string of the form "dependencyname:alias".
146 //
147 // "dependencyname" here should be the name of the crate, not the Android module. This is
148 // equivalent to writing `alias = { package = "dependencyname" }` in a `Cargo.toml`.
149 Aliases []string
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 // list of rust rlib crate dependencies
152 Rlibs []string `android:"arch_variant"`
153
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400154 // list of rust automatic crate dependencies.
155 // Rustlibs linkage is rlib for host targets and dylib for device targets.
Matthew Maurer0f003b12020-06-29 14:34:06 -0700156 Rustlibs []string `android:"arch_variant"`
157
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158 // list of rust proc_macro crate dependencies
159 Proc_macros []string `android:"arch_variant"`
160
161 // list of C shared library dependencies
162 Shared_libs []string `android:"arch_variant"`
163
Ivan Lozano63bb7682021-03-23 15:53:44 -0400164 // list of C static library dependencies. These dependencies do not normally propagate to dependents
165 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700166 Static_libs []string `android:"arch_variant"`
167
Ivan Lozano63bb7682021-03-23 15:53:44 -0400168 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
169 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
170 // result in bloat if multiple dependencies all include the same static library whole.
171 //
172 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
173 // having to redeclare the static library dependency for every dependent module.
174 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
175 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
176 //
177 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
178 //
179 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
180 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
181 Whole_static_libs []string `android:"arch_variant"`
182
Andrew Walbran797e4be2022-03-07 15:41:53 +0000183 // list of Rust system library dependencies.
184 //
185 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
186 // like `core` and `alloc`.
187 Stdlibs []string `android:"arch_variant"`
188
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400189 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
190 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
191 // source, and is required to conform to an enforced format matching library output files (if the output file is
192 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700193 Crate_name string `android:"arch_variant"`
194
195 // list of features to enable for this crate
196 Features []string `android:"arch_variant"`
197
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200198 // list of configuration options to enable for this crate. To enable features, use the "features" property.
199 Cfgs []string `android:"arch_variant"`
200
Ivan Lozanoffee3342019-08-27 12:03:00 -0700201 // specific rust edition that should be used if the default version is not desired
202 Edition *string `android:"arch_variant"`
203
204 // sets name of the output
205 Stem *string `android:"arch_variant"`
206
207 // append to name of output
208 Suffix *string `android:"arch_variant"`
209
210 // install to a subdirectory of the default install path for the module
211 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700212
213 // whether to suppress inclusion of standard crates - defaults to false
Ivan Lozanoc5182022023-11-14 10:23:31 -0500214 No_stdlibs *bool `android:"arch_variant"`
Ivan Lozanoea086132020-12-08 14:43:00 -0500215
216 // Change the rustlibs linkage to select rlib linkage by default for device targets.
217 // Also link libstd as an rlib as well on device targets.
218 // Note: This is the default behavior for host targets.
219 //
220 // This is primarily meant for rust_binary and rust_ffi modules where the default
221 // linkage of libstd might need to be overridden in some use cases. This should
222 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000223 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500224 // the same way.
225 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400226
227 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
228 // Will set CARGO_CRATE_NAME to the crate_name property's value.
229 // Will set CARGO_BIN_NAME to the output filename value without the extension.
230 Cargo_env_compat *bool
231
232 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
233 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700234}
235
236type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800237 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700238
239 // Install related
240 dir string
241 dir64 string
242 subDir string
243 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700244 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800245 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500246 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400247
Joel Galensonfa049382021-01-14 16:03:18 -0800248 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400249
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800250 installDeps android.InstallPaths
251
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400252 // unstripped output file.
253 unstrippedOutputFile android.Path
254
255 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200256 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100257
258 // If a crate has a source-generated dependency, a copy of the source file
259 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
Matthew Maurercd416532023-11-20 17:52:01 +0000260 // This is stored internally because it may not be available during
261 // singleton-generation passes like rustdoc/rust_project.json, but should
262 // be stashed during initial generation.
263 cachedCargoOutDir android.ModuleOutPath
Matthew Maurera28404a2023-11-20 23:33:28 +0000264 // Calculated crate root cached internally because ModuleContext is not
265 // available to singleton targets like rustdoc/rust_project.json
266 cachedCrateRootPath android.Path
267 // If cachedCrateRootPath is nil after initialization, this will contain
268 // an explanation of why
269 cachedCrateRootError error
Ivan Lozanoffee3342019-08-27 12:03:00 -0700270}
271
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400272func (compiler *baseCompiler) Disabled() bool {
273 return false
274}
275
276func (compiler *baseCompiler) SetDisabled() {
277 panic("baseCompiler does not implement SetDisabled()")
278}
279
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500280func (compiler *baseCompiler) noStdlibs() bool {
281 return Bool(compiler.Properties.No_stdlibs)
282}
283
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400284func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
285 panic("baseCompiler does not implement coverageOutputZipPath()")
286}
287
Ivan Lozanoea086132020-12-08 14:43:00 -0500288func (compiler *baseCompiler) preferRlib() bool {
289 return Bool(compiler.Properties.Prefer_rlib)
290}
291
Andrew Walbran52533232024-03-19 11:36:04 +0000292func (compiler *baseCompiler) Aliases() map[string]string {
293 aliases := map[string]string{}
294 for _, entry := range compiler.Properties.Aliases {
295 dep, alias, found := strings.Cut(entry, ":")
296 if !found {
297 panic(fmt.Errorf("invalid aliases entry %q missing ':'", entry))
298 }
299 aliases[dep] = alias
300 }
301 return aliases
302}
303
Ivan Lozanodd055472020-09-28 13:22:45 -0400304func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400305 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500306 if compiler.preferRlib() {
307 return RlibLinkage
308 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400309 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400310 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400311 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400312 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400313}
314
Ivan Lozanoffee3342019-08-27 12:03:00 -0700315var _ compiler = (*baseCompiler)(nil)
316
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800317func (compiler *baseCompiler) inData() bool {
318 return compiler.location == InstallInData
319}
320
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321func (compiler *baseCompiler) compilerProps() []interface{} {
322 return []interface{}{&compiler.Properties}
323}
324
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400325func cfgsToFlags(cfgs []string) []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700326 flags := []string{}
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400327 for _, cfg := range cfgs {
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200328 flags = append(flags, "--cfg '"+cfg+"'")
329 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400330
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200331 return flags
332}
333
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000334func (compiler *baseCompiler) features() []string {
335 return compiler.Properties.Features
336}
337
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200338func (compiler *baseCompiler) featuresToFlags() []string {
339 flags := []string{}
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000340 for _, feature := range compiler.features() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700341 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
342 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400343
344 return flags
345}
346
347func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
348 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
349 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
350
351 return flags
352}
353
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400354func CommonDefaultCfgFlags(flags Flags, vendor bool, product bool) Flags {
355 var cfgs []string
356 if vendor || product {
357 cfgs = append(cfgs, "android_vndk")
358 if vendor {
359 cfgs = append(cfgs, "android_vendor")
360 } else if product {
361 cfgs = append(cfgs, "android_product")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000362 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400363 }
364
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400365 flags.RustFlags = append(flags.RustFlags, cfgsToFlags(cfgs)...)
366 flags.RustdocFlags = append(flags.RustdocFlags, cfgsToFlags(cfgs)...)
367 return flags
368}
369
370func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
371 flags = CommonDefaultCfgFlags(flags, ctx.RustModule().InVendor(), ctx.RustModule().InProduct())
372
373 flags.RustFlags = append(flags.RustFlags, cfgsToFlags(compiler.Properties.Cfgs)...)
374 flags.RustdocFlags = append(flags.RustdocFlags, cfgsToFlags(compiler.Properties.Cfgs)...)
375
376 return flags
377}
378
379func CommonDefaultFlags(ctx android.ModuleContext, toolchain config.Toolchain, flags Flags) Flags {
380 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
381 flags.GlobalRustFlags = append(flags.GlobalRustFlags, toolchain.ToolchainRustFlags())
382 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, toolchain.ToolchainLinkFlags())
383 flags.EmitXrefs = ctx.Config().EmitXrefRules()
384
385 if ctx.Host() && !ctx.Windows() {
386 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
387 }
388
389 if ctx.Os() == android.Linux {
390 // Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
391 // the default behavior of device builds.
392 flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
393 } else if ctx.Os() == android.Darwin {
394 // Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
395 // behavior of device builds.
396 flags.LinkFlags = append(flags.LinkFlags,
397 "-lc",
398 "-ldl",
399 "-lpthread",
400 "-lm",
401 )
402 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700403 return flags
404}
405
406func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
407
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400408 flags = CommonDefaultFlags(ctx, ctx.toolchain(), flags)
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200409 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
410 if err != nil {
411 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700412 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400413
414 // linkage-related flags are disallowed.
415 for _, s := range compiler.Properties.Ld_flags {
416 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
417 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
418 }
419 }
420 for _, s := range compiler.Properties.Flags {
421 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
422 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
423 }
424 if strings.HasPrefix(s, "--extern") {
425 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
426 }
427 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
428 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
429 }
430 }
431
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200432 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700433 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200434 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700435 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700436 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438 return flags
439}
440
Sasha Smundaka76acba2022-04-18 20:12:56 -0700441func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700442 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
443}
444
Dan Albert06feee92021-03-19 15:06:02 -0700445func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
446 deps PathDeps) android.OptionalPath {
447
448 return android.OptionalPath{}
449}
450
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100451func (compiler *baseCompiler) initialize(ctx ModuleContext) {
Matthew Maurercd416532023-11-20 17:52:01 +0000452 compiler.cachedCargoOutDir = android.PathForModuleOut(ctx, genSubDir)
Matthew Maurera28404a2023-11-20 23:33:28 +0000453 if compiler.Properties.Crate_root == nil {
454 compiler.cachedCrateRootPath, compiler.cachedCrateRootError = srcPathFromModuleSrcs(ctx, compiler.Properties.Srcs)
455 } else {
456 compiler.cachedCrateRootPath = android.PathForModuleSrc(ctx, *compiler.Properties.Crate_root)
457 compiler.cachedCrateRootError = nil
458 }
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100459}
460
Matthew Maurercd416532023-11-20 17:52:01 +0000461func (compiler *baseCompiler) cargoOutDir() android.OptionalPath {
462 return android.OptionalPathForPath(compiler.cachedCargoOutDir)
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100463}
464
Matthew Maurercd416532023-11-20 17:52:01 +0000465func (compiler *baseCompiler) cargoEnvCompat() bool {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400466 return Bool(compiler.Properties.Cargo_env_compat)
467}
468
Matthew Maurercd416532023-11-20 17:52:01 +0000469func (compiler *baseCompiler) cargoPkgVersion() string {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400470 return String(compiler.Properties.Cargo_pkg_version)
471}
472
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400473func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
474 return compiler.unstrippedOutputFile
475}
476
Jiyong Parke54f07e2021-04-07 15:08:04 +0900477func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
478 return compiler.strippedOutputFile
479}
480
Ivan Lozanoffee3342019-08-27 12:03:00 -0700481func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
482 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700483 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700484 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
485 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400486 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700487 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000488 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700489
Matthew Maurer99020b02019-10-31 10:44:40 -0700490 if !Bool(compiler.Properties.No_stdlibs) {
491 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700492 // If we're building for the build host, use the prebuilt stdlibs, unless the host
493 // is linux_bionic which doesn't have prebuilts.
494 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500495 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700496 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400497 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700498 }
499 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700500 return deps
501}
502
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100503func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400504 bionicLibs := []string{}
505 bionicLibs = append(bionicLibs, "liblog")
506 bionicLibs = append(bionicLibs, "libc")
507 bionicLibs = append(bionicLibs, "libm")
508 bionicLibs = append(bionicLibs, "libdl")
509
510 if static {
511 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
512 } else {
513 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
514 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400515 if ctx.RustModule().StaticExecutable() {
516 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
517 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100518 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
519 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
520 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700521 return deps
522}
523
Colin Crosse32f0932022-01-23 20:48:36 -0800524func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
525 muslLibs := []string{"libc_musl"}
526 if static {
527 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
528 } else {
529 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
530 }
531 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
532 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
533 }
534
535 return deps
536}
537
Ivan Lozanoffee3342019-08-27 12:03:00 -0700538func (compiler *baseCompiler) crateName() string {
539 return compiler.Properties.Crate_name
540}
541
Ivan Lozanod7586b62021-04-01 09:49:36 -0400542func (compiler *baseCompiler) everInstallable() bool {
543 // Most modules are installable, so return true by default.
544 return true
545}
546
Colin Cross70dda7e2019-10-01 22:05:35 -0700547func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700548 dir := compiler.dir
549 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
550 dir = compiler.dir64
551 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400552 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
553 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
554 }
555 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700556 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
557 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400558
Kiyoung Kimaa394802024-01-08 12:55:45 +0900559 if compiler.location == InstallInData && ctx.RustModule().InVendorOrProduct() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700560 if ctx.RustModule().InProduct() {
561 dir = filepath.Join(dir, "product")
562 } else if ctx.RustModule().InVendor() {
563 dir = filepath.Join(dir, "vendor")
564 } else {
565 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
566 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400567 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700568
Ivan Lozanoffee3342019-08-27 12:03:00 -0700569 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
570 compiler.relativeInstallPath(), compiler.relative)
571}
572
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400573func (compiler *baseCompiler) nativeCoverage() bool {
574 return false
575}
576
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200577func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900578 path := ctx.RustModule().OutputFile()
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800579 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path(), compiler.installDeps...)
580}
581
582func (compiler *baseCompiler) installTestData(ctx ModuleContext, data []android.DataPath) {
583 installedData := ctx.InstallTestData(compiler.installDir(ctx), data)
584 compiler.installDeps = append(compiler.installDeps, installedData...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700585}
586
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400587func (compiler *baseCompiler) getStem(ctx android.ModuleContext) string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700588 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
589}
590
Ivan Lozano28ed8f42024-05-06 21:46:39 -0400591func (compiler *baseCompiler) getStemWithoutSuffix(ctx android.BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200592 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700593 if String(compiler.Properties.Stem) != "" {
594 stem = String(compiler.Properties.Stem)
595 }
596
597 return stem
598}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700599
Ivan Lozanoffee3342019-08-27 12:03:00 -0700600func (compiler *baseCompiler) relativeInstallPath() string {
601 return String(compiler.Properties.Relative_install_path)
602}
603
Matthew Maurera28404a2023-11-20 23:33:28 +0000604func (compiler *baseCompiler) checkedCrateRootPath() (android.Path, error) {
605 return compiler.cachedCrateRootPath, compiler.cachedCrateRootError
606}
Seth Moore3afac0b2021-10-13 15:32:18 -0700607
Matthew Maurera28404a2023-11-20 23:33:28 +0000608func crateRootPath(ctx ModuleContext, compiler compiler) android.Path {
609 root, err := compiler.checkedCrateRootPath()
610 if err != nil {
611 ctx.PropertyErrorf("srcs", err.Error())
Matthew Maurerd221d312023-11-20 21:02:40 +0000612 }
Matthew Maurera28404a2023-11-20 23:33:28 +0000613 return root
Matthew Maurerd221d312023-11-20 21:02:40 +0000614}
615
Ivan Lozano43845682020-07-09 21:03:28 -0400616// Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs.
Matthew Maurera28404a2023-11-20 23:33:28 +0000617func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, error) {
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700618 // The srcs can contain strings with prefix ":".
619 // They are dependent modules of this module, with android.SourceDepTag.
620 // They are not the main source file compiled by rustc.
621 numSrcs := 0
622 srcIndex := 0
623 for i, s := range srcs {
624 if android.SrcIsModule(s) == "" {
625 numSrcs++
626 srcIndex = i
627 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700628 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400629 if numSrcs > 1 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000630 return nil, errors.New(incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700631 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400632
633 // If a main source file is not provided we expect only a single SourceProvider module to be defined
634 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700635 if srcIndex != 0 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000636 return nil, errors.New("main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400637 } else if numSrcs > 1 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000638 return nil, errors.New("only a single generated source module can be defined without a main source file.")
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700639 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400640
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000641 // TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
642 // entry point file from Srcs rather than taking the first one
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700643 paths := android.PathsForModuleSrc(ctx, srcs)
Matthew Maurera28404a2023-11-20 23:33:28 +0000644 if len(paths) == 0 {
645 return nil, errors.New("srcs must not be empty")
646 }
647 return paths[srcIndex], nil
Ivan Lozanoffee3342019-08-27 12:03:00 -0700648}