blob: e2415a4a7899584a098995bfb759a04bc8327244 [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
47 rustdoc(ctx ModuleContext, flags Flags, deps PathDeps) android.OptionalPath
48
49 // Output directory in which source-generated code from dependencies is
50 // copied. This is equivalent to Cargo's OUT_DIR variable.
Matthew Maurercd416532023-11-20 17:52:01 +000051 cargoOutDir() android.OptionalPath
Matthew Maurer689d6f62023-11-20 17:49:25 +000052
Matthew Maurercd416532023-11-20 17:52:01 +000053 // cargoPkgVersion returns the value of the Cargo_pkg_version property.
54 cargoPkgVersion() string
Matthew Maurer689d6f62023-11-20 17:49:25 +000055
Matthew Maurercd416532023-11-20 17:52:01 +000056 // cargoEnvCompat returns whether Cargo environment variables should be used.
57 cargoEnvCompat() bool
Matthew Maurer689d6f62023-11-20 17:49:25 +000058
59 inData() bool
60 install(ctx ModuleContext)
61 relativeInstallPath() string
62 everInstallable() bool
63
64 nativeCoverage() bool
65
66 Disabled() bool
67 SetDisabled()
68
69 stdLinkage(ctx *depsContext) RustLinkage
70 noStdlibs() bool
71
72 unstrippedOutputFilePath() android.Path
73 strippedOutputFilePath() android.OptionalPath
Matthew Maurerd221d312023-11-20 21:02:40 +000074
Matthew Maurera28404a2023-11-20 23:33:28 +000075 checkedCrateRootPath() (android.Path, error)
Matthew Maurer689d6f62023-11-20 17:49:25 +000076}
77
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020078func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070079 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
80}
81
Matthew Maurer99020b02019-10-31 10:44:40 -070082func (compiler *baseCompiler) setNoStdlibs() {
83 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
84}
85
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020086func (compiler *baseCompiler) disableLints() {
87 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070088}
89
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080090func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070091 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070092 Properties: BaseCompilerProperties{},
93 dir: dir,
94 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080095 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070096 }
97}
98
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080099type installLocation int
100
101const (
102 InstallInSystem installLocation = 0
103 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -0400104
105 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100106 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800107)
108
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109type BaseCompilerProperties struct {
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400110 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
111 // Only a single source file can be defined. Modules which generate source can be included by prefixing
112 // the module name with ":", for example ":libfoo_bindgen"
113 //
114 // 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 -0400115 Srcs []string `android:"path,arch_variant"`
116
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000117 // Entry point that is passed to rustc to begin the compilation. E.g. main.rs or lib.rs.
118 // When this property is set,
119 // * sandboxing is enabled for this module, and
120 // * the srcs attribute is interpreted as a list of all source files potentially
121 // used in compilation, including the entrypoint, and
122 // * compile_data can be used to add additional files used in compilation that
123 // not directly used as source files.
124 Crate_root *string `android:"path,arch_variant"`
125
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200126 // name of the lint set that should be used to validate this module.
127 //
128 // Possible values are "default" (for using a sensible set of lints
129 // depending on the module's location), "android" (for the strictest
130 // lint set that applies to all Android platform code), "vendor" (for
131 // a relaxed set) and "none" (for ignoring all lint warnings and
132 // errors). The default value is "default".
133 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700134
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200135 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -0400136 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700137
138 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -0400139 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140
141 // list of rust rlib crate dependencies
142 Rlibs []string `android:"arch_variant"`
143
Vinh Tran4eeb2a92023-08-14 13:29:30 -0400144 // list of rust automatic crate dependencies.
145 // Rustlibs linkage is rlib for host targets and dylib for device targets.
Matthew Maurer0f003b12020-06-29 14:34:06 -0700146 Rustlibs []string `android:"arch_variant"`
147
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148 // list of rust proc_macro crate dependencies
149 Proc_macros []string `android:"arch_variant"`
150
151 // list of C shared library dependencies
152 Shared_libs []string `android:"arch_variant"`
153
Ivan Lozano63bb7682021-03-23 15:53:44 -0400154 // list of C static library dependencies. These dependencies do not normally propagate to dependents
155 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 Static_libs []string `android:"arch_variant"`
157
Ivan Lozano63bb7682021-03-23 15:53:44 -0400158 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
159 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
160 // result in bloat if multiple dependencies all include the same static library whole.
161 //
162 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
163 // having to redeclare the static library dependency for every dependent module.
164 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
165 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
166 //
167 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
168 //
169 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
170 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
171 Whole_static_libs []string `android:"arch_variant"`
172
Andrew Walbran797e4be2022-03-07 15:41:53 +0000173 // list of Rust system library dependencies.
174 //
175 // This is usually only needed when `no_stdlibs` is true, in which case it can be used to depend on system crates
176 // like `core` and `alloc`.
177 Stdlibs []string `android:"arch_variant"`
178
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400179 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
180 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
181 // source, and is required to conform to an enforced format matching library output files (if the output file is
182 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700183 Crate_name string `android:"arch_variant"`
184
185 // list of features to enable for this crate
186 Features []string `android:"arch_variant"`
187
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200188 // list of configuration options to enable for this crate. To enable features, use the "features" property.
189 Cfgs []string `android:"arch_variant"`
190
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191 // specific rust edition that should be used if the default version is not desired
192 Edition *string `android:"arch_variant"`
193
194 // sets name of the output
195 Stem *string `android:"arch_variant"`
196
197 // append to name of output
198 Suffix *string `android:"arch_variant"`
199
200 // install to a subdirectory of the default install path for the module
201 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700202
203 // whether to suppress inclusion of standard crates - defaults to false
204 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500205
206 // Change the rustlibs linkage to select rlib linkage by default for device targets.
207 // Also link libstd as an rlib as well on device targets.
208 // Note: This is the default behavior for host targets.
209 //
210 // This is primarily meant for rust_binary and rust_ffi modules where the default
211 // linkage of libstd might need to be overridden in some use cases. This should
212 // generally be avoided with other module types since it may cause collisions at
Martin Geisler46329e92022-09-29 13:12:23 +0000213 // linkage if all dependencies of the root binary module do not link against libstd
Ivan Lozanoea086132020-12-08 14:43:00 -0500214 // the same way.
215 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400216
217 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
218 // Will set CARGO_CRATE_NAME to the crate_name property's value.
219 // Will set CARGO_BIN_NAME to the output filename value without the extension.
220 Cargo_env_compat *bool
221
222 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
223 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224}
225
226type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800227 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700228
229 // Install related
230 dir string
231 dir64 string
232 subDir string
233 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700234 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800235 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500236 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400237
Joel Galensonfa049382021-01-14 16:03:18 -0800238 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400239
240 // unstripped output file.
241 unstrippedOutputFile android.Path
242
243 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200244 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100245
246 // If a crate has a source-generated dependency, a copy of the source file
247 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
Matthew Maurercd416532023-11-20 17:52:01 +0000248 // This is stored internally because it may not be available during
249 // singleton-generation passes like rustdoc/rust_project.json, but should
250 // be stashed during initial generation.
251 cachedCargoOutDir android.ModuleOutPath
Matthew Maurera28404a2023-11-20 23:33:28 +0000252 // Calculated crate root cached internally because ModuleContext is not
253 // available to singleton targets like rustdoc/rust_project.json
254 cachedCrateRootPath android.Path
255 // If cachedCrateRootPath is nil after initialization, this will contain
256 // an explanation of why
257 cachedCrateRootError error
Ivan Lozanoffee3342019-08-27 12:03:00 -0700258}
259
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400260func (compiler *baseCompiler) Disabled() bool {
261 return false
262}
263
264func (compiler *baseCompiler) SetDisabled() {
265 panic("baseCompiler does not implement SetDisabled()")
266}
267
Ivan Lozano9ef9cb82023-02-14 10:56:14 -0500268func (compiler *baseCompiler) noStdlibs() bool {
269 return Bool(compiler.Properties.No_stdlibs)
270}
271
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400272func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
273 panic("baseCompiler does not implement coverageOutputZipPath()")
274}
275
Ivan Lozanoea086132020-12-08 14:43:00 -0500276func (compiler *baseCompiler) preferRlib() bool {
277 return Bool(compiler.Properties.Prefer_rlib)
278}
279
Ivan Lozanodd055472020-09-28 13:22:45 -0400280func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400281 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500282 if compiler.preferRlib() {
283 return RlibLinkage
284 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400285 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400286 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400287 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400288 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400289}
290
Ivan Lozanoffee3342019-08-27 12:03:00 -0700291var _ compiler = (*baseCompiler)(nil)
292
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800293func (compiler *baseCompiler) inData() bool {
294 return compiler.location == InstallInData
295}
296
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297func (compiler *baseCompiler) compilerProps() []interface{} {
298 return []interface{}{&compiler.Properties}
299}
300
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200301func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700302 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200303 for _, cfg := range compiler.Properties.Cfgs {
304 flags = append(flags, "--cfg '"+cfg+"'")
305 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400306
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200307 return flags
308}
309
310func (compiler *baseCompiler) featuresToFlags() []string {
311 flags := []string{}
312 for _, feature := range compiler.Properties.Features {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700313 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
314 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400315
316 return flags
317}
318
319func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
320 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
321 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
322
323 return flags
324}
325
326func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
327 if ctx.RustModule().UseVndk() {
328 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
Matthew Maurer65a54a82023-02-24 19:19:22 +0000329 if ctx.RustModule().InVendor() {
330 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vendor")
331 } else if ctx.RustModule().InProduct() {
332 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_product")
333 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400334 }
335
336 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
337 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700338 return flags
339}
340
341func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
342
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200343 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
344 if err != nil {
345 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700346 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400347
348 // linkage-related flags are disallowed.
349 for _, s := range compiler.Properties.Ld_flags {
350 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
351 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
352 }
353 }
354 for _, s := range compiler.Properties.Flags {
355 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
356 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
357 }
358 if strings.HasPrefix(s, "--extern") {
359 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
360 }
361 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
362 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
363 }
364 }
365
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200366 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700367 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200368 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700369 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700370 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700371 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700372 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
373 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Sasha Smundaka76acba2022-04-18 20:12:56 -0700374 flags.EmitXrefs = ctx.Config().EmitXrefRules()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700375
376 if ctx.Host() && !ctx.Windows() {
Colin Cross225a37a2023-01-11 14:17:39 -0800377 flags.LinkFlags = append(flags.LinkFlags, cc.RpathFlags(ctx)...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700378 }
379
Colin Crosse18bd202023-10-03 19:12:50 -0700380 if ctx.Os() == android.Linux {
381 // Add -lc, -lrt, -ldl, -lpthread, -lm and -lgcc_s to glibc builds to match
382 // the default behavior of device builds.
Vinh Tran50de8be2023-09-21 15:28:53 -0400383 flags.LinkFlags = append(flags.LinkFlags, config.LinuxHostGlobalLinkFlags...)
Colin Crosse18bd202023-10-03 19:12:50 -0700384 } else if ctx.Os() == android.Darwin {
385 // Add -lc, -ldl, -lpthread and -lm to glibc darwin builds to match the default
386 // behavior of device builds.
387 flags.LinkFlags = append(flags.LinkFlags,
388 "-lc",
389 "-ldl",
390 "-lpthread",
391 "-lm",
392 )
Ivan Lozano2fcbffa2023-07-27 10:40:52 -0400393 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700394 return flags
395}
396
Sasha Smundaka76acba2022-04-18 20:12:56 -0700397func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700398 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
399}
400
Dan Albert06feee92021-03-19 15:06:02 -0700401func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
402 deps PathDeps) android.OptionalPath {
403
404 return android.OptionalPath{}
405}
406
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100407func (compiler *baseCompiler) initialize(ctx ModuleContext) {
Matthew Maurercd416532023-11-20 17:52:01 +0000408 compiler.cachedCargoOutDir = android.PathForModuleOut(ctx, genSubDir)
Matthew Maurera28404a2023-11-20 23:33:28 +0000409 if compiler.Properties.Crate_root == nil {
410 compiler.cachedCrateRootPath, compiler.cachedCrateRootError = srcPathFromModuleSrcs(ctx, compiler.Properties.Srcs)
411 } else {
412 compiler.cachedCrateRootPath = android.PathForModuleSrc(ctx, *compiler.Properties.Crate_root)
413 compiler.cachedCrateRootError = nil
414 }
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100415}
416
Matthew Maurercd416532023-11-20 17:52:01 +0000417func (compiler *baseCompiler) cargoOutDir() android.OptionalPath {
418 return android.OptionalPathForPath(compiler.cachedCargoOutDir)
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100419}
420
Matthew Maurercd416532023-11-20 17:52:01 +0000421func (compiler *baseCompiler) cargoEnvCompat() bool {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400422 return Bool(compiler.Properties.Cargo_env_compat)
423}
424
Matthew Maurercd416532023-11-20 17:52:01 +0000425func (compiler *baseCompiler) cargoPkgVersion() string {
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400426 return String(compiler.Properties.Cargo_pkg_version)
427}
428
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400429func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
430 return compiler.unstrippedOutputFile
431}
432
Jiyong Parke54f07e2021-04-07 15:08:04 +0900433func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
434 return compiler.strippedOutputFile
435}
436
Ivan Lozanoffee3342019-08-27 12:03:00 -0700437func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
438 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700439 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700440 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
441 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400442 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700443 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
Andrew Walbran797e4be2022-03-07 15:41:53 +0000444 deps.Stdlibs = append(deps.Stdlibs, compiler.Properties.Stdlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700445
Matthew Maurer99020b02019-10-31 10:44:40 -0700446 if !Bool(compiler.Properties.No_stdlibs) {
447 for _, stdlib := range config.Stdlibs {
Colin Crossa8941ec2022-07-01 11:17:22 -0700448 // If we're building for the build host, use the prebuilt stdlibs, unless the host
449 // is linux_bionic which doesn't have prebuilts.
450 if ctx.Host() && !ctx.Target().HostCross && ctx.Target().Os != android.LinuxBionic {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500451 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700452 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400453 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700454 }
455 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700456 return deps
457}
458
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100459func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400460 bionicLibs := []string{}
461 bionicLibs = append(bionicLibs, "liblog")
462 bionicLibs = append(bionicLibs, "libc")
463 bionicLibs = append(bionicLibs, "libm")
464 bionicLibs = append(bionicLibs, "libdl")
465
466 if static {
467 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
468 } else {
469 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
470 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400471 if ctx.RustModule().StaticExecutable() {
472 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
473 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100474 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
475 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
476 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700477 return deps
478}
479
Colin Crosse32f0932022-01-23 20:48:36 -0800480func muslDeps(ctx DepsContext, deps Deps, static bool) Deps {
481 muslLibs := []string{"libc_musl"}
482 if static {
483 deps.StaticLibs = append(deps.StaticLibs, muslLibs...)
484 } else {
485 deps.SharedLibs = append(deps.SharedLibs, muslLibs...)
486 }
487 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
488 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
489 }
490
491 return deps
492}
493
Ivan Lozanoffee3342019-08-27 12:03:00 -0700494func (compiler *baseCompiler) crateName() string {
495 return compiler.Properties.Crate_name
496}
497
Ivan Lozanod7586b62021-04-01 09:49:36 -0400498func (compiler *baseCompiler) everInstallable() bool {
499 // Most modules are installable, so return true by default.
500 return true
501}
502
Colin Cross70dda7e2019-10-01 22:05:35 -0700503func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700504 dir := compiler.dir
505 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
506 dir = compiler.dir64
507 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400508 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
509 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
510 }
511 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700512 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
513 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400514
515 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700516 if ctx.RustModule().InProduct() {
517 dir = filepath.Join(dir, "product")
518 } else if ctx.RustModule().InVendor() {
519 dir = filepath.Join(dir, "vendor")
520 } else {
521 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
522 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400523 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700524
Ivan Lozanoffee3342019-08-27 12:03:00 -0700525 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
526 compiler.relativeInstallPath(), compiler.relative)
527}
528
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400529func (compiler *baseCompiler) nativeCoverage() bool {
530 return false
531}
532
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200533func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900534 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200535 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700536}
537
538func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
539 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
540}
541
542func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200543 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700544 if String(compiler.Properties.Stem) != "" {
545 stem = String(compiler.Properties.Stem)
546 }
547
548 return stem
549}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700550
Ivan Lozanoffee3342019-08-27 12:03:00 -0700551func (compiler *baseCompiler) relativeInstallPath() string {
552 return String(compiler.Properties.Relative_install_path)
553}
554
Matthew Maurera28404a2023-11-20 23:33:28 +0000555func (compiler *baseCompiler) checkedCrateRootPath() (android.Path, error) {
556 return compiler.cachedCrateRootPath, compiler.cachedCrateRootError
557}
558
559func crateRootPath(ctx ModuleContext, compiler compiler) android.Path {
560 root, err := compiler.checkedCrateRootPath()
561 if err != nil {
562 ctx.PropertyErrorf("srcs", err.Error())
Matthew Maurerd221d312023-11-20 21:02:40 +0000563 }
Matthew Maurera28404a2023-11-20 23:33:28 +0000564 return root
Matthew Maurerd221d312023-11-20 21:02:40 +0000565}
566
Ivan Lozano43845682020-07-09 21:03:28 -0400567// 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 +0000568func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, error) {
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700569 // The srcs can contain strings with prefix ":".
570 // They are dependent modules of this module, with android.SourceDepTag.
571 // They are not the main source file compiled by rustc.
572 numSrcs := 0
573 srcIndex := 0
574 for i, s := range srcs {
575 if android.SrcIsModule(s) == "" {
576 numSrcs++
577 srcIndex = i
578 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700579 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400580 if numSrcs > 1 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000581 return nil, errors.New(incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700582 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400583
584 // If a main source file is not provided we expect only a single SourceProvider module to be defined
585 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700586 if srcIndex != 0 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000587 return nil, errors.New("main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400588 } else if numSrcs > 1 {
Matthew Maurera28404a2023-11-20 23:33:28 +0000589 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 -0700590 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400591
Sam Delmerico63ca14e2023-09-25 12:13:17 +0000592 // TODO: b/297264540 - once all modules are sandboxed, we need to select the proper
593 // entry point file from Srcs rather than taking the first one
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700594 paths := android.PathsForModuleSrc(ctx, srcs)
Matthew Maurera28404a2023-11-20 23:33:28 +0000595 if len(paths) == 0 {
596 return nil, errors.New("srcs must not be empty")
597 }
598 return paths[srcIndex], nil
Ivan Lozanoffee3342019-08-27 12:03:00 -0700599}