blob: 3c9f6d4a414540fb291599a33bca16a6d5208692 [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.
50 CargoOutDir() android.OptionalPath
51
52 // CargoPkgVersion returns the value of the Cargo_pkg_version property.
53 CargoPkgVersion() string
54
55 // CargoEnvCompat returns whether Cargo environment variables should be used.
56 CargoEnvCompat() bool
57
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
73}
74
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020075func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070076 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
77}
78
Matthew Maurer99020b02019-10-31 10:44:40 -070079func (compiler *baseCompiler) setNoStdlibs() {
80 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
81}
82
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020083func (compiler *baseCompiler) disableLints() {
84 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070085}
86
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080087func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070088 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070089 Properties: BaseCompilerProperties{},
90 dir: dir,
91 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080092 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070093 }
94}
95
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080096type installLocation int
97
98const (
99 InstallInSystem installLocation = 0
100 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -0400101
102 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100103 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800104)
105
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106type BaseCompilerProperties struct {
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400107 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
108 // Only a single source file can be defined. Modules which generate source can be included by prefixing
109 // the module name with ":", for example ":libfoo_bindgen"
110 //
111 // 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 -0400112 Srcs []string `android:"path,arch_variant"`
113
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000114 // Entry point that is passed to rustc to begin the compilation. E.g. main.rs or lib.rs.
115 // When this property is set,
116 // * sandboxing is enabled for this module, and
117 // * the srcs attribute is interpreted as a list of all source files potentially
118 // used in compilation, including the entrypoint, and
119 // * compile_data can be used to add additional files used in compilation that
120 // not directly used as source files.
121 Crate_root *string `android:"path,arch_variant"`
122
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200123 // name of the lint set that should be used to validate this module.
124 //
125 // Possible values are "default" (for using a sensible set of lints
126 // depending on the module's location), "android" (for the strictest
127 // lint set that applies to all Android platform code), "vendor" (for
128 // a relaxed set) and "none" (for ignoring all lint warnings and
129 // errors). The default value is "default".
130 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700131
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200132 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -0400133 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134
135 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -0400136 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137
138 // list of rust rlib crate dependencies
139 Rlibs []string `android:"arch_variant"`
140
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400141 // list of rust automatic crate dependencies.
142 // Rustlibs linkage is rlib for host targets and dylib for device targets.
Matthew Maurer0f003b12020-06-29 14:34:06 -0700143 Rustlibs []string `android:"arch_variant"`
144
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 // list of rust proc_macro crate dependencies
146 Proc_macros []string `android:"arch_variant"`
147
148 // list of C shared library dependencies
149 Shared_libs []string `android:"arch_variant"`
150
Ivan Lozano63bb7682021-03-23 15:53:44 -0400151 // list of C static library dependencies. These dependencies do not normally propagate to dependents
152 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 Static_libs []string `android:"arch_variant"`
154
Ivan Lozano63bb7682021-03-23 15:53:44 -0400155 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
156 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
157 // result in bloat if multiple dependencies all include the same static library whole.
158 //
159 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
160 // having to redeclare the static library dependency for every dependent module.
161 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
162 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
163 //
164 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
165 //
166 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
167 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
168 Whole_static_libs []string `android:"arch_variant"`
169
Andrew Walbran797e4be2022-03-07 15:41:53 +0000170 // list of Rust system library dependencies.
171 //
172 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
173 // like `core` and `alloc`.
174 Stdlibs []string `android:"arch_variant"`
175
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400176 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
177 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
178 // source, and is required to conform to an enforced format matching library output files (if the output file is
179 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180 Crate_name string `android:"arch_variant"`
181
182 // list of features to enable for this crate
183 Features []string `android:"arch_variant"`
184
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200185 // list of configuration options to enable for this crate. To enable features, use the "features" property.
186 Cfgs []string `android:"arch_variant"`
187
Ivan Lozanoffee3342019-08-27 12:03:00 -0700188 // specific rust edition that should be used if the default version is not desired
189 Edition *string `android:"arch_variant"`
190
191 // sets name of the output
192 Stem *string `android:"arch_variant"`
193
194 // append to name of output
195 Suffix *string `android:"arch_variant"`
196
197 // install to a subdirectory of the default install path for the module
198 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700199
200 // whether to suppress inclusion of standard crates - defaults to false
201 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500202
203 // Change the rustlibs linkage to select rlib linkage by default for device targets.
204 // Also link libstd as an rlib as well on device targets.
205 // Note: This is the default behavior for host targets.
206 //
207 // This is primarily meant for rust_binary and rust_ffi modules where the default
208 // linkage of libstd might need to be overridden in some use cases. This should
209 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000210 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500211 // the same way.
212 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400213
214 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
215 // Will set CARGO_CRATE_NAME to the crate_name property's value.
216 // Will set CARGO_BIN_NAME to the output filename value without the extension.
217 Cargo_env_compat *bool
218
219 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
220 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221}
222
223type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800224 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700225
226 // Install related
227 dir string
228 dir64 string
229 subDir string
230 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700231 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800232 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500233 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400234
Joel Galensonfa049382021-01-14 16:03:18 -0800235 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400236
237 // unstripped output file.
238 unstrippedOutputFile android.Path
239
240 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200241 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100242
243 // If a crate has a source-generated dependency, a copy of the source file
244 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
245 cargoOutDir android.ModuleOutPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246}
247
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400248func (compiler *baseCompiler) Disabled() bool {
249 return false
250}
251
252func (compiler *baseCompiler) SetDisabled() {
253 panic("baseCompiler does not implement SetDisabled()")
254}
255
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500256func (compiler *baseCompiler) noStdlibs() bool {
257 return Bool(compiler.Properties.No_stdlibs)
258}
259
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400260func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
261 panic("baseCompiler does not implement coverageOutputZipPath()")
262}
263
Ivan Lozanoea086132020-12-08 14:43:00 -0500264func (compiler *baseCompiler) preferRlib() bool {
265 return Bool(compiler.Properties.Prefer_rlib)
266}
267
Ivan Lozanodd055472020-09-28 13:22:45 -0400268func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400269 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500270 if compiler.preferRlib() {
271 return RlibLinkage
272 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400273 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400274 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400275 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400276 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400277}
278
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279var _ compiler = (*baseCompiler)(nil)
280
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800281func (compiler *baseCompiler) inData() bool {
282 return compiler.location == InstallInData
283}
284
Ivan Lozanoffee3342019-08-27 12:03:00 -0700285func (compiler *baseCompiler) compilerProps() []interface{} {
286 return []interface{}{&compiler.Properties}
287}
288
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200289func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200291 for _, cfg := range compiler.Properties.Cfgs {
292 flags = append(flags, "--cfg '"+cfg+"'")
293 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400294
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200295 return flags
296}
297
298func (compiler *baseCompiler) featuresToFlags() []string {
299 flags := []string{}
300 for _, feature := range compiler.Properties.Features {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700301 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
302 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400303
304 return flags
305}
306
307func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
308 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
309 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
310
311 return flags
312}
313
314func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
315 if ctx.RustModule().UseVndk() {
316 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000317 if ctx.RustModule().InVendor() {
318 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
319 } else if ctx.RustModule().InProduct() {
320 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
321 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400322 }
323
324 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
325 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700326 return flags
327}
328
329func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
330
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200331 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
332 if err != nil {
333 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700334 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400335
336 // linkage-related flags are disallowed.
337 for _, s := range compiler.Properties.Ld_flags {
338 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
339 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
340 }
341 }
342 for _, s := range compiler.Properties.Flags {
343 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
344 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
345 }
346 if strings.HasPrefix(s, "--extern") {
347 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
348 }
349 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
350 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
351 }
352 }
353
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200354 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700355 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200356 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700357 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700358 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700359 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700360 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
361 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Sasha Smundaka76acba2022-04-18 20:12:56 -0700362 flags.EmitXrefs = ctx.Config().EmitXrefRules()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700363
364 if ctx.Host() && !ctx.Windows() {
Colin Cross225a37a2023-01-11 14:17:39 -0800365 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700366 }
367
Colin Crosse18bd202023-10-03 19:12:50 -0700368 if ctx.Os() == android.Linux {
369 // Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
370 // the default behavior of device builds.
Vinh Tran50de8be2023-09-21 15:28:53 -0400371 flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
Colin Crosse18bd202023-10-03 19:12:50 -0700372 } else if ctx.Os() == android.Darwin {
373 // Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
374 // behavior of device builds.
375 flags.LinkFlags = append(flags.LinkFlags,
376 "-lc",
377 "-ldl",
378 "-lpthread",
379 "-lm",
380 )
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400381 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700382 return flags
383}
384
Sasha Smundaka76acba2022-04-18 20:12:56 -0700385func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700386 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
387}
388
Dan Albert06feee92021-03-19 15:06:02 -0700389func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
390 deps PathDeps) android.OptionalPath {
391
392 return android.OptionalPath{}
393}
394
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100395func (compiler *baseCompiler) initialize(ctx ModuleContext) {
396 compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
397}
398
399func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
400 return android.OptionalPathForPath(compiler.cargoOutDir)
401}
402
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400403func (compiler *baseCompiler) CargoEnvCompat() bool {
404 return Bool(compiler.Properties.Cargo_env_compat)
405}
406
407func (compiler *baseCompiler) CargoPkgVersion() string {
408 return String(compiler.Properties.Cargo_pkg_version)
409}
410
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400411func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
412 return compiler.unstrippedOutputFile
413}
414
Jiyong Parke54f07e2021-04-07 15:08:04 +0900415func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
416 return compiler.strippedOutputFile
417}
418
Ivan Lozanoffee3342019-08-27 12:03:00 -0700419func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
420 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700421 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700422 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
423 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400424 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700425 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000426 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700427
Matthew Maurer99020b02019-10-31 10:44:40 -0700428 if !Bool(compiler.Properties.No_stdlibs) {
429 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700430 // If we're building for the build host, use the prebuilt stdlibs, unless the host
431 // is linux_bionic which doesn't have prebuilts.
432 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500433 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700434 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400435 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700436 }
437 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700438 return deps
439}
440
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100441func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400442 bionicLibs := []string{}
443 bionicLibs = append(bionicLibs, "liblog")
444 bionicLibs = append(bionicLibs, "libc")
445 bionicLibs = append(bionicLibs, "libm")
446 bionicLibs = append(bionicLibs, "libdl")
447
448 if static {
449 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
450 } else {
451 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
452 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400453 if ctx.RustModule().StaticExecutable() {
454 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
455 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100456 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
457 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
458 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700459 return deps
460}
461
Colin Crosse32f0932022-01-23 20:48:36 -0800462func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
463 muslLibs := []string{"libc_musl"}
464 if static {
465 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
466 } else {
467 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
468 }
469 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
470 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
471 }
472
473 return deps
474}
475
Ivan Lozanoffee3342019-08-27 12:03:00 -0700476func (compiler *baseCompiler) crateName() string {
477 return compiler.Properties.Crate_name
478}
479
Ivan Lozanod7586b62021-04-01 09:49:36 -0400480func (compiler *baseCompiler) everInstallable() bool {
481 // Most modules are installable, so return true by default.
482 return true
483}
484
Colin Cross70dda7e2019-10-01 22:05:35 -0700485func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700486 dir := compiler.dir
487 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
488 dir = compiler.dir64
489 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400490 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
491 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
492 }
493 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
495 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400496
497 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700498 if ctx.RustModule().InProduct() {
499 dir = filepath.Join(dir, "product")
500 } else if ctx.RustModule().InVendor() {
501 dir = filepath.Join(dir, "vendor")
502 } else {
503 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
504 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400505 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700506
Ivan Lozanoffee3342019-08-27 12:03:00 -0700507 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
508 compiler.relativeInstallPath(), compiler.relative)
509}
510
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400511func (compiler *baseCompiler) nativeCoverage() bool {
512 return false
513}
514
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200515func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900516 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200517 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518}
519
520func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
521 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
522}
523
524func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200525 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700526 if String(compiler.Properties.Stem) != "" {
527 stem = String(compiler.Properties.Stem)
528 }
529
530 return stem
531}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700532
Ivan Lozanoffee3342019-08-27 12:03:00 -0700533func (compiler *baseCompiler) relativeInstallPath() string {
534 return String(compiler.Properties.Relative_install_path)
535}
536
Ivan Lozano43845682020-07-09 21:03:28 -0400537// 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 -0700538func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
Seth Moore3afac0b2021-10-13 15:32:18 -0700539 if len(srcs) == 0 {
540 ctx.PropertyErrorf("srcs", "srcs must not be empty")
541 }
542
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700543 // The srcs can contain strings with prefix ":".
544 // They are dependent modules of this module, with android.SourceDepTag.
545 // They are not the main source file compiled by rustc.
546 numSrcs := 0
547 srcIndex := 0
548 for i, s := range srcs {
549 if android.SrcIsModule(s) == "" {
550 numSrcs++
551 srcIndex = i
552 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700553 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400554 if numSrcs > 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400555 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700556 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400557
558 // If a main source file is not provided we expect only a single SourceProvider module to be defined
559 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700560 if srcIndex != 0 {
561 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400562 } else if numSrcs > 1 {
563 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 -0700564 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400565
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000566 // TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
567 // entry point file from Srcs rather than taking the first one
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700568 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400569 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700570}