blob: 9afaec587d0248b830cfc5e06014cd848c6e448c [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)
Matthew Maurer689d6f62023-11-20 17:49:25 +000078}
79
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020080func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070081 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
82}
83
Matthew Maurer99020b02019-10-31 10:44:40 -070084func (compiler *baseCompiler) setNoStdlibs() {
85 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
86}
87
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020088func (compiler *baseCompiler) disableLints() {
89 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070090}
91
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080092func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070093 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070094 Properties: BaseCompilerProperties{},
95 dir: dir,
96 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080097 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070098 }
99}
100
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800101type installLocation int
102
103const (
104 InstallInSystem installLocation = 0
105 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -0400106
107 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100108 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800109)
110
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111type BaseCompilerProperties struct {
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400112 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
113 // Only a single source file can be defined. Modules which generate source can be included by prefixing
114 // the module name with ":", for example ":libfoo_bindgen"
115 //
116 // 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 -0400117 Srcs []string `android:"path,arch_variant"`
118
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000119 // Entry point that is passed to rustc to begin the compilation. E.g. main.rs or lib.rs.
120 // When this property is set,
121 // * sandboxing is enabled for this module, and
122 // * the srcs attribute is interpreted as a list of all source files potentially
123 // used in compilation, including the entrypoint, and
124 // * compile_data can be used to add additional files used in compilation that
125 // not directly used as source files.
126 Crate_root *string `android:"path,arch_variant"`
127
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200128 // name of the lint set that should be used to validate this module.
129 //
130 // Possible values are "default" (for using a sensible set of lints
131 // depending on the module's location), "android" (for the strictest
132 // lint set that applies to all Android platform code), "vendor" (for
133 // a relaxed set) and "none" (for ignoring all lint warnings and
134 // errors). The default value is "default".
135 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700136
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200137 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -0400138 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139
140 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -0400141 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142
143 // list of rust rlib crate dependencies
144 Rlibs []string `android:"arch_variant"`
145
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400146 // list of rust automatic crate dependencies.
147 // Rustlibs linkage is rlib for host targets and dylib for device targets.
Matthew Maurer0f003b12020-06-29 14:34:06 -0700148 Rustlibs []string `android:"arch_variant"`
149
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 // list of rust proc_macro crate dependencies
151 Proc_macros []string `android:"arch_variant"`
152
153 // list of C shared library dependencies
154 Shared_libs []string `android:"arch_variant"`
155
Ivan Lozano63bb7682021-03-23 15:53:44 -0400156 // list of C static library dependencies. These dependencies do not normally propagate to dependents
157 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158 Static_libs []string `android:"arch_variant"`
159
Ivan Lozano63bb7682021-03-23 15:53:44 -0400160 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
161 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
162 // result in bloat if multiple dependencies all include the same static library whole.
163 //
164 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
165 // having to redeclare the static library dependency for every dependent module.
166 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
167 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
168 //
169 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
170 //
171 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
172 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
173 Whole_static_libs []string `android:"arch_variant"`
174
Andrew Walbran797e4be2022-03-07 15:41:53 +0000175 // list of Rust system library dependencies.
176 //
177 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
178 // like `core` and `alloc`.
179 Stdlibs []string `android:"arch_variant"`
180
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400181 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
182 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
183 // source, and is required to conform to an enforced format matching library output files (if the output file is
184 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700185 Crate_name string `android:"arch_variant"`
186
187 // list of features to enable for this crate
188 Features []string `android:"arch_variant"`
189
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200190 // list of configuration options to enable for this crate. To enable features, use the "features" property.
191 Cfgs []string `android:"arch_variant"`
192
Ivan Lozanoffee3342019-08-27 12:03:00 -0700193 // specific rust edition that should be used if the default version is not desired
194 Edition *string `android:"arch_variant"`
195
196 // sets name of the output
197 Stem *string `android:"arch_variant"`
198
199 // append to name of output
200 Suffix *string `android:"arch_variant"`
201
202 // install to a subdirectory of the default install path for the module
203 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700204
205 // whether to suppress inclusion of standard crates - defaults to false
206 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500207
208 // Change the rustlibs linkage to select rlib linkage by default for device targets.
209 // Also link libstd as an rlib as well on device targets.
210 // Note: This is the default behavior for host targets.
211 //
212 // This is primarily meant for rust_binary and rust_ffi modules where the default
213 // linkage of libstd might need to be overridden in some use cases. This should
214 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000215 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500216 // the same way.
217 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400218
219 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
220 // Will set CARGO_CRATE_NAME to the crate_name property's value.
221 // Will set CARGO_BIN_NAME to the output filename value without the extension.
222 Cargo_env_compat *bool
223
224 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
225 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700226}
227
228type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800229 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700230
231 // Install related
232 dir string
233 dir64 string
234 subDir string
235 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700236 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800237 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500238 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400239
Joel Galensonfa049382021-01-14 16:03:18 -0800240 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400241
242 // unstripped output file.
243 unstrippedOutputFile android.Path
244
245 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200246 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100247
248 // If a crate has a source-generated dependency, a copy of the source file
249 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
Matthew Maurercd416532023-11-20 17:52:01 +0000250 // This is stored internally because it may not be available during
251 // singleton-generation passes like rustdoc/rust_project.json, but should
252 // be stashed during initial generation.
253 cachedCargoOutDir android.ModuleOutPath
Matthew Maurera28404a2023-11-20 23:33:28 +0000254 // Calculated crate root cached internally because ModuleContext is not
255 // available to singleton targets like rustdoc/rust_project.json
256 cachedCrateRootPath android.Path
257 // If cachedCrateRootPath is nil after initialization, this will contain
258 // an explanation of why
259 cachedCrateRootError error
Ivan Lozanoffee3342019-08-27 12:03:00 -0700260}
261
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400262func (compiler *baseCompiler) Disabled() bool {
263 return false
264}
265
266func (compiler *baseCompiler) SetDisabled() {
267 panic("baseCompiler does not implement SetDisabled()")
268}
269
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500270func (compiler *baseCompiler) noStdlibs() bool {
271 return Bool(compiler.Properties.No_stdlibs)
272}
273
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400274func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
275 panic("baseCompiler does not implement coverageOutputZipPath()")
276}
277
Ivan Lozanoea086132020-12-08 14:43:00 -0500278func (compiler *baseCompiler) preferRlib() bool {
279 return Bool(compiler.Properties.Prefer_rlib)
280}
281
Ivan Lozanodd055472020-09-28 13:22:45 -0400282func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400283 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500284 if compiler.preferRlib() {
285 return RlibLinkage
286 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400287 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400288 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400289 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400290 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400291}
292
Ivan Lozanoffee3342019-08-27 12:03:00 -0700293var _ compiler = (*baseCompiler)(nil)
294
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800295func (compiler *baseCompiler) inData() bool {
296 return compiler.location == InstallInData
297}
298
Ivan Lozanoffee3342019-08-27 12:03:00 -0700299func (compiler *baseCompiler) compilerProps() []interface{} {
300 return []interface{}{&compiler.Properties}
301}
302
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200303func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700304 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200305 for _, cfg := range compiler.Properties.Cfgs {
306 flags = append(flags, "--cfg '"+cfg+"'")
307 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400308
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200309 return flags
310}
311
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000312func (compiler *baseCompiler) features() []string {
313 return compiler.Properties.Features
314}
315
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200316func (compiler *baseCompiler) featuresToFlags() []string {
317 flags := []string{}
Matthew Maurerdb72f7e2023-11-21 00:20:02 +0000318 for _, feature := range compiler.features() {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700319 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
320 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400321
322 return flags
323}
324
325func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
326 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
327 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
328
329 return flags
330}
331
332func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
333 if ctx.RustModule().UseVndk() {
334 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000335 if ctx.RustModule().InVendor() {
336 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
337 } else if ctx.RustModule().InProduct() {
338 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
339 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400340 }
341
342 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
343 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700344 return flags
345}
346
347func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
348
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200349 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
350 if err != nil {
351 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700352 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400353
354 // linkage-related flags are disallowed.
355 for _, s := range compiler.Properties.Ld_flags {
356 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
357 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
358 }
359 }
360 for _, s := range compiler.Properties.Flags {
361 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
362 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
363 }
364 if strings.HasPrefix(s, "--extern") {
365 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
366 }
367 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
368 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
369 }
370 }
371
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200372 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200374 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700375 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700377 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700378 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
379 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Sasha Smundaka76acba2022-04-18 20:12:56 -0700380 flags.EmitXrefs = ctx.Config().EmitXrefRules()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700381
382 if ctx.Host() && !ctx.Windows() {
Colin Cross225a37a2023-01-11 14:17:39 -0800383 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700384 }
385
Colin Crosse18bd202023-10-03 19:12:50 -0700386 if ctx.Os() == android.Linux {
387 // Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
388 // the default behavior of device builds.
Vinh Tran50de8be2023-09-21 15:28:53 -0400389 flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
Colin Crosse18bd202023-10-03 19:12:50 -0700390 } else if ctx.Os() == android.Darwin {
391 // Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
392 // behavior of device builds.
393 flags.LinkFlags = append(flags.LinkFlags,
394 "-lc",
395 "-ldl",
396 "-lpthread",
397 "-lm",
398 )
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400399 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700400 return flags
401}
402
Sasha Smundaka76acba2022-04-18 20:12:56 -0700403func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700404 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
405}
406
Dan Albert06feee92021-03-19 15:06:02 -0700407func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
408 deps PathDeps) android.OptionalPath {
409
410 return android.OptionalPath{}
411}
412
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100413func (compiler *baseCompiler) initialize(ctx ModuleContext) {
Matthew Maurercd416532023-11-20 17:52:01 +0000414 compiler.cachedCargoOutDir = android.PathForModuleOut(ctx, genSubDir)
Matthew Maurera28404a2023-11-20 23:33:28 +0000415 if compiler.Properties.Crate_root == nil {
416 compiler.cachedCrateRootPath, compiler.cachedCrateRootError = srcPathFromModuleSrcs(ctx, compiler.Properties.Srcs)
417 } else {
418 compiler.cachedCrateRootPath = android.PathForModuleSrc(ctx, *compiler.Properties.Crate_root)
419 compiler.cachedCrateRootError = nil
420 }
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100421}
422
Matthew Maurercd416532023-11-20 17:52:01 +0000423func (compiler *baseCompiler) cargoOutDir() android.OptionalPath {
424 return android.OptionalPathForPath(compiler.cachedCargoOutDir)
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100425}
426
Matthew Maurercd416532023-11-20 17:52:01 +0000427func (compiler *baseCompiler) cargoEnvCompat() bool {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400428 return Bool(compiler.Properties.Cargo_env_compat)
429}
430
Matthew Maurercd416532023-11-20 17:52:01 +0000431func (compiler *baseCompiler) cargoPkgVersion() string {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400432 return String(compiler.Properties.Cargo_pkg_version)
433}
434
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400435func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
436 return compiler.unstrippedOutputFile
437}
438
Jiyong Parke54f07e2021-04-07 15:08:04 +0900439func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
440 return compiler.strippedOutputFile
441}
442
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
444 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700445 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700446 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
447 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400448 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700449 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000450 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700451
Matthew Maurer99020b02019-10-31 10:44:40 -0700452 if !Bool(compiler.Properties.No_stdlibs) {
453 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700454 // If we're building for the build host, use the prebuilt stdlibs, unless the host
455 // is linux_bionic which doesn't have prebuilts.
456 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500457 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700458 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400459 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700460 }
461 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700462 return deps
463}
464
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100465func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400466 bionicLibs := []string{}
467 bionicLibs = append(bionicLibs, "liblog")
468 bionicLibs = append(bionicLibs, "libc")
469 bionicLibs = append(bionicLibs, "libm")
470 bionicLibs = append(bionicLibs, "libdl")
471
472 if static {
473 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
474 } else {
475 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
476 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400477 if ctx.RustModule().StaticExecutable() {
478 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
479 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100480 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
481 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
482 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700483 return deps
484}
485
Colin Crosse32f0932022-01-23 20:48:36 -0800486func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
487 muslLibs := []string{"libc_musl"}
488 if static {
489 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
490 } else {
491 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
492 }
493 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
494 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
495 }
496
497 return deps
498}
499
Ivan Lozanoffee3342019-08-27 12:03:00 -0700500func (compiler *baseCompiler) crateName() string {
501 return compiler.Properties.Crate_name
502}
503
Ivan Lozanod7586b62021-04-01 09:49:36 -0400504func (compiler *baseCompiler) everInstallable() bool {
505 // Most modules are installable, so return true by default.
506 return true
507}
508
Colin Cross70dda7e2019-10-01 22:05:35 -0700509func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700510 dir := compiler.dir
511 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
512 dir = compiler.dir64
513 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400514 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
515 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
516 }
517 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700518 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
519 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400520
521 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700522 if ctx.RustModule().InProduct() {
523 dir = filepath.Join(dir, "product")
524 } else if ctx.RustModule().InVendor() {
525 dir = filepath.Join(dir, "vendor")
526 } else {
527 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
528 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400529 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700530
Ivan Lozanoffee3342019-08-27 12:03:00 -0700531 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
532 compiler.relativeInstallPath(), compiler.relative)
533}
534
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400535func (compiler *baseCompiler) nativeCoverage() bool {
536 return false
537}
538
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200539func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900540 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200541 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700542}
543
544func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
545 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
546}
547
548func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200549 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700550 if String(compiler.Properties.Stem) != "" {
551 stem = String(compiler.Properties.Stem)
552 }
553
554 return stem
555}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700556
Ivan Lozanoffee3342019-08-27 12:03:00 -0700557func (compiler *baseCompiler) relativeInstallPath() string {
558 return String(compiler.Properties.Relative_install_path)
559}
560
Matthew Maurera28404a2023-11-20 23:33:28 +0000561func (compiler *baseCompiler) checkedCrateRootPath() (android.Path, error) {
562 return compiler.cachedCrateRootPath, compiler.cachedCrateRootError
563}
564
565func crateRootPath(ctx ModuleContext, compiler compiler) android.Path {
566 root, err := compiler.checkedCrateRootPath()
567 if err != nil {
568 ctx.PropertyErrorf("srcs", err.Error())
Matthew Maurerd221d312023-11-20 21:02:40 +0000569 }
Matthew Maurera28404a2023-11-20 23:33:28 +0000570 return root
Matthew Maurerd221d312023-11-20 21:02:40 +0000571}
572
Ivan Lozano43845682020-07-09 21:03:28 -0400573// 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 +0000574func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, error) {
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700575 // The srcs can contain strings with prefix ":".
576 // They are dependent modules of this module, with android.SourceDepTag.
577 // They are not the main source file compiled by rustc.
578 numSrcs := 0
579 srcIndex := 0
580 for i, s := range srcs {
581 if android.SrcIsModule(s) == "" {
582 numSrcs++
583 srcIndex = i
584 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700585 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400586 if numSrcs > 1 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000587 return nil, errors.New(incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700588 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400589
590 // If a main source file is not provided we expect only a single SourceProvider module to be defined
591 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700592 if srcIndex != 0 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000593 return nil, errors.New("main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400594 } else if numSrcs > 1 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000595 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 -0700596 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400597
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000598 // TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
599 // entry point file from Srcs rather than taking the first one
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700600 paths := android.PathsForModuleSrc(ctx, srcs)
Matthew Maurera28404a2023-11-20 23:33:28 +0000601 if len(paths) == 0 {
602 return nil, errors.New("srcs must not be empty")
603 }
604 return paths[srcIndex], nil
Ivan Lozanoffee3342019-08-27 12:03:00 -0700605}