blob: 3040e5d9ed3ceb6966651e9e6f97703f09dfe988 [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 (
18 "fmt"
19 "path/filepath"
Ivan Lozano45a9e312021-07-27 12:29:12 -040020 "strings"
Ivan Lozanoffee3342019-08-27 12:03:00 -070021
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070022 "github.com/google/blueprint/proptools"
23
Ivan Lozanoffee3342019-08-27 12:03:00 -070024 "android/soong/android"
25 "android/soong/rust/config"
26)
27
Ivan Lozanodd055472020-09-28 13:22:45 -040028type RustLinkage int
29
30const (
31 DefaultLinkage RustLinkage = iota
32 RlibLinkage
33 DylibLinkage
34)
35
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020036func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070037 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
38}
39
Matthew Maurer99020b02019-10-31 10:44:40 -070040func (compiler *baseCompiler) setNoStdlibs() {
41 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
42}
43
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020044func (compiler *baseCompiler) disableLints() {
45 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070046}
47
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080048func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070049 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070050 Properties: BaseCompilerProperties{},
51 dir: dir,
52 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080053 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070054 }
55}
56
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080057type installLocation int
58
59const (
60 InstallInSystem installLocation = 0
61 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -040062
63 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +010064 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080065)
66
Ivan Lozanoffee3342019-08-27 12:03:00 -070067type BaseCompilerProperties struct {
Ivan Lozanoe4db0032021-08-11 13:39:33 -040068 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs).
69 // Only a single source file can be defined. Modules which generate source can be included by prefixing
70 // the module name with ":", for example ":libfoo_bindgen"
71 //
72 // 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 -040073 Srcs []string `android:"path,arch_variant"`
74
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020075 // name of the lint set that should be used to validate this module.
76 //
77 // Possible values are "default" (for using a sensible set of lints
78 // depending on the module's location), "android" (for the strictest
79 // lint set that applies to all Android platform code), "vendor" (for
80 // a relaxed set) and "none" (for ignoring all lint warnings and
81 // errors). The default value is "default".
82 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070083
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +020084 // flags to pass to rustc. To enable configuration options or features, use the "cfgs" or "features" properties.
Liz Kammereda93982021-04-20 10:15:41 -040085 Flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070086
87 // flags to pass to the linker
Liz Kammereda93982021-04-20 10:15:41 -040088 Ld_flags []string `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -070089
90 // list of rust rlib crate dependencies
91 Rlibs []string `android:"arch_variant"`
92
93 // list of rust dylib crate dependencies
94 Dylibs []string `android:"arch_variant"`
95
Matthew Maurer0f003b12020-06-29 14:34:06 -070096 // list of rust automatic crate dependencies
97 Rustlibs []string `android:"arch_variant"`
98
Ivan Lozanoffee3342019-08-27 12:03:00 -070099 // list of rust proc_macro crate dependencies
100 Proc_macros []string `android:"arch_variant"`
101
102 // list of C shared library dependencies
103 Shared_libs []string `android:"arch_variant"`
104
Ivan Lozano63bb7682021-03-23 15:53:44 -0400105 // list of C static library dependencies. These dependencies do not normally propagate to dependents
106 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107 Static_libs []string `android:"arch_variant"`
108
Ivan Lozano63bb7682021-03-23 15:53:44 -0400109 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
110 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
111 // result in bloat if multiple dependencies all include the same static library whole.
112 //
113 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
114 // having to redeclare the static library dependency for every dependent module.
115 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
116 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
117 //
118 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
119 //
120 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
121 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
122 Whole_static_libs []string `android:"arch_variant"`
123
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400124 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
125 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
126 // source, and is required to conform to an enforced format matching library output files (if the output file is
127 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128 Crate_name string `android:"arch_variant"`
129
130 // list of features to enable for this crate
131 Features []string `android:"arch_variant"`
132
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200133 // list of configuration options to enable for this crate. To enable features, use the "features" property.
134 Cfgs []string `android:"arch_variant"`
135
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136 // specific rust edition that should be used if the default version is not desired
137 Edition *string `android:"arch_variant"`
138
139 // sets name of the output
140 Stem *string `android:"arch_variant"`
141
142 // append to name of output
143 Suffix *string `android:"arch_variant"`
144
145 // install to a subdirectory of the default install path for the module
146 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700147
148 // whether to suppress inclusion of standard crates - defaults to false
149 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500150
151 // Change the rustlibs linkage to select rlib linkage by default for device targets.
152 // Also link libstd as an rlib as well on device targets.
153 // Note: This is the default behavior for host targets.
154 //
155 // This is primarily meant for rust_binary and rust_ffi modules where the default
156 // linkage of libstd might need to be overridden in some use cases. This should
157 // generally be avoided with other module types since it may cause collisions at
158 // linkage if all dependencies of the root binary module do not link against libstd\
159 // the same way.
160 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400161
162 // Enables emitting certain Cargo environment variables. Only intended to be used for compatibility purposes.
163 // Will set CARGO_CRATE_NAME to the crate_name property's value.
164 // Will set CARGO_BIN_NAME to the output filename value without the extension.
165 Cargo_env_compat *bool
166
167 // If cargo_env_compat is true, sets the CARGO_PKG_VERSION env var to this value.
168 Cargo_pkg_version *string
Ivan Lozanoffee3342019-08-27 12:03:00 -0700169}
170
171type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800172 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700173
174 // Install related
175 dir string
176 dir64 string
177 subDir string
178 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700179 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800180 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500181 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400182
Joel Galensonfa049382021-01-14 16:03:18 -0800183 distFile android.OptionalPath
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400184
185 // unstripped output file.
186 unstrippedOutputFile android.Path
187
188 // stripped output file.
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200189 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100190
191 // If a crate has a source-generated dependency, a copy of the source file
192 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
193 cargoOutDir android.ModuleOutPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700194}
195
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400196func (compiler *baseCompiler) Disabled() bool {
197 return false
198}
199
200func (compiler *baseCompiler) SetDisabled() {
201 panic("baseCompiler does not implement SetDisabled()")
202}
203
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400204func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
205 panic("baseCompiler does not implement coverageOutputZipPath()")
206}
207
Ivan Lozanoea086132020-12-08 14:43:00 -0500208func (compiler *baseCompiler) preferRlib() bool {
209 return Bool(compiler.Properties.Prefer_rlib)
210}
211
Ivan Lozanodd055472020-09-28 13:22:45 -0400212func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400213 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500214 if compiler.preferRlib() {
215 return RlibLinkage
216 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400217 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400218 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400219 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400220 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400221}
222
Ivan Lozanoffee3342019-08-27 12:03:00 -0700223var _ compiler = (*baseCompiler)(nil)
224
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800225func (compiler *baseCompiler) inData() bool {
226 return compiler.location == InstallInData
227}
228
Ivan Lozanoffee3342019-08-27 12:03:00 -0700229func (compiler *baseCompiler) compilerProps() []interface{} {
230 return []interface{}{&compiler.Properties}
231}
232
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200233func (compiler *baseCompiler) cfgsToFlags() []string {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700234 flags := []string{}
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200235 for _, cfg := range compiler.Properties.Cfgs {
236 flags = append(flags, "--cfg '"+cfg+"'")
237 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400238
Thiébaud Weksteenc44e7372021-04-07 14:53:06 +0200239 return flags
240}
241
242func (compiler *baseCompiler) featuresToFlags() []string {
243 flags := []string{}
244 for _, feature := range compiler.Properties.Features {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
246 }
Ivan Lozano67eada32021-09-23 11:50:33 -0400247
248 return flags
249}
250
251func (compiler *baseCompiler) featureFlags(ctx ModuleContext, flags Flags) Flags {
252 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags()...)
253 flags.RustdocFlags = append(flags.RustdocFlags, compiler.featuresToFlags()...)
254
255 return flags
256}
257
258func (compiler *baseCompiler) cfgFlags(ctx ModuleContext, flags Flags) Flags {
259 if ctx.RustModule().UseVndk() {
260 compiler.Properties.Cfgs = append(compiler.Properties.Cfgs, "android_vndk")
261 }
262
263 flags.RustFlags = append(flags.RustFlags, compiler.cfgsToFlags()...)
264 flags.RustdocFlags = append(flags.RustdocFlags, compiler.cfgsToFlags()...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700265 return flags
266}
267
268func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
269
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200270 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
271 if err != nil {
272 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700273 }
Ivan Lozano45a9e312021-07-27 12:29:12 -0400274
275 // linkage-related flags are disallowed.
276 for _, s := range compiler.Properties.Ld_flags {
277 if strings.HasPrefix(s, "-Wl,-l") || strings.HasPrefix(s, "-Wl,-L") {
278 ctx.PropertyErrorf("ld_flags", "'-Wl,-l' and '-Wl,-L' flags cannot be manually specified")
279 }
280 }
281 for _, s := range compiler.Properties.Flags {
282 if strings.HasPrefix(s, "-l") || strings.HasPrefix(s, "-L") {
283 ctx.PropertyErrorf("flags", "'-l' and '-L' flags cannot be manually specified")
284 }
285 if strings.HasPrefix(s, "--extern") {
286 ctx.PropertyErrorf("flags", "'--extern' flag cannot be manually specified")
287 }
288 if strings.HasPrefix(s, "-Clink-args=") || strings.HasPrefix(s, "-C link-args=") {
289 ctx.PropertyErrorf("flags", "'-C link-args' flag cannot be manually specified")
290 }
291 }
292
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200293 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200295 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Dan Albert06feee92021-03-19 15:06:02 -0700296 flags.RustdocFlags = append(flags.RustdocFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700298 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700299 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
300 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700301
302 if ctx.Host() && !ctx.Windows() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800303 rpathPrefix := `\$$ORIGIN/`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700304 if ctx.Darwin() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800305 rpathPrefix = "@loader_path/"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700306 }
307
308 var rpath string
309 if ctx.toolchain().Is64Bit() {
310 rpath = "lib64"
311 } else {
312 rpath = "lib"
313 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800314 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+rpath)
315 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+"../"+rpath)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700316 }
317
318 return flags
319}
320
321func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
322 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
323}
324
Dan Albert06feee92021-03-19 15:06:02 -0700325func (compiler *baseCompiler) rustdoc(ctx ModuleContext, flags Flags,
326 deps PathDeps) android.OptionalPath {
327
328 return android.OptionalPath{}
329}
330
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100331func (compiler *baseCompiler) initialize(ctx ModuleContext) {
332 compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
333}
334
335func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
336 return android.OptionalPathForPath(compiler.cargoOutDir)
337}
338
Ivan Lozanoa9a1fc02021-08-11 15:13:43 -0400339func (compiler *baseCompiler) CargoEnvCompat() bool {
340 return Bool(compiler.Properties.Cargo_env_compat)
341}
342
343func (compiler *baseCompiler) CargoPkgVersion() string {
344 return String(compiler.Properties.Cargo_pkg_version)
345}
346
Ivan Lozano8d10fc32021-11-05 16:36:47 -0400347func (compiler *baseCompiler) unstrippedOutputFilePath() android.Path {
348 return compiler.unstrippedOutputFile
349}
350
Jiyong Parke54f07e2021-04-07 15:08:04 +0900351func (compiler *baseCompiler) strippedOutputFilePath() android.OptionalPath {
352 return compiler.strippedOutputFile
353}
354
Ivan Lozanoffee3342019-08-27 12:03:00 -0700355func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
356 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
357 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700358 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700359 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
360 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400361 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700362 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
363
Matthew Maurer99020b02019-10-31 10:44:40 -0700364 if !Bool(compiler.Properties.No_stdlibs) {
365 for _, stdlib := range config.Stdlibs {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500366 // If we're building for the build host, use the prebuilt stdlibs
Colin Cross0c66bc62021-07-20 09:47:41 -0700367 if ctx.Target().Os == ctx.Config().BuildOS {
Ivan Lozanofba2aa22021-11-11 09:29:07 -0500368 stdlib = "prebuilt_" + stdlib
Matthew Maurer99020b02019-10-31 10:44:40 -0700369 }
Ivan Lozano2b081132020-09-08 12:46:52 -0400370 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700371 }
372 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700373 return deps
374}
375
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100376func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400377 bionicLibs := []string{}
378 bionicLibs = append(bionicLibs, "liblog")
379 bionicLibs = append(bionicLibs, "libc")
380 bionicLibs = append(bionicLibs, "libm")
381 bionicLibs = append(bionicLibs, "libdl")
382
383 if static {
384 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
385 } else {
386 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
387 }
Ivan Lozano8711c5c2021-08-13 13:14:06 -0400388 if ctx.RustModule().StaticExecutable() {
389 deps.StaticLibs = append(deps.StaticLibs, "libunwind")
390 }
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100391 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
392 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
393 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700394 return deps
395}
396
Ivan Lozanoffee3342019-08-27 12:03:00 -0700397func (compiler *baseCompiler) crateName() string {
398 return compiler.Properties.Crate_name
399}
400
Ivan Lozanod7586b62021-04-01 09:49:36 -0400401func (compiler *baseCompiler) everInstallable() bool {
402 // Most modules are installable, so return true by default.
403 return true
404}
405
Colin Cross70dda7e2019-10-01 22:05:35 -0700406func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700407 dir := compiler.dir
408 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
409 dir = compiler.dir64
410 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400411 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
412 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
413 }
414 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700415 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
416 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400417
418 if compiler.location == InstallInData && ctx.RustModule().UseVndk() {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700419 if ctx.RustModule().InProduct() {
420 dir = filepath.Join(dir, "product")
421 } else if ctx.RustModule().InVendor() {
422 dir = filepath.Join(dir, "vendor")
423 } else {
424 ctx.ModuleErrorf("Unknown data+VNDK installation kind")
425 }
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400426 }
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700427
Ivan Lozanoffee3342019-08-27 12:03:00 -0700428 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
429 compiler.relativeInstallPath(), compiler.relative)
430}
431
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400432func (compiler *baseCompiler) nativeCoverage() bool {
433 return false
434}
435
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200436func (compiler *baseCompiler) install(ctx ModuleContext) {
Jiyong Parke54f07e2021-04-07 15:08:04 +0900437 path := ctx.RustModule().OutputFile()
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200438 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700439}
440
441func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
442 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
443}
444
445func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200446 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700447 if String(compiler.Properties.Stem) != "" {
448 stem = String(compiler.Properties.Stem)
449 }
450
451 return stem
452}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700453
Ivan Lozanoffee3342019-08-27 12:03:00 -0700454func (compiler *baseCompiler) relativeInstallPath() string {
455 return String(compiler.Properties.Relative_install_path)
456}
457
Ivan Lozano43845682020-07-09 21:03:28 -0400458// 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 -0700459func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
Seth Moore3afac0b2021-10-13 15:32:18 -0700460 if len(srcs) == 0 {
461 ctx.PropertyErrorf("srcs", "srcs must not be empty")
462 }
463
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700464 // The srcs can contain strings with prefix ":".
465 // They are dependent modules of this module, with android.SourceDepTag.
466 // They are not the main source file compiled by rustc.
467 numSrcs := 0
468 srcIndex := 0
469 for i, s := range srcs {
470 if android.SrcIsModule(s) == "" {
471 numSrcs++
472 srcIndex = i
473 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700474 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400475 if numSrcs > 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400476 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700477 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400478
479 // If a main source file is not provided we expect only a single SourceProvider module to be defined
480 // within srcs, with the expectation that the first source it provides is the entry point.
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700481 if srcIndex != 0 {
482 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400483 } else if numSrcs > 1 {
484 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 -0700485 }
Ivan Lozanoe4db0032021-08-11 13:39:33 -0400486
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700487 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400488 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700489}