blob: c26f208ee109d1f979b523c4fd25b06a2539c7d5 [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"
20
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070021 "github.com/google/blueprint/proptools"
22
Ivan Lozanoffee3342019-08-27 12:03:00 -070023 "android/soong/android"
24 "android/soong/rust/config"
25)
26
Ivan Lozanodd055472020-09-28 13:22:45 -040027type RustLinkage int
28
29const (
30 DefaultLinkage RustLinkage = iota
31 RlibLinkage
32 DylibLinkage
33)
34
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020035func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070036 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
37}
38
Matthew Maurer99020b02019-10-31 10:44:40 -070039func (compiler *baseCompiler) setNoStdlibs() {
40 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
41}
42
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020043func (compiler *baseCompiler) disableLints() {
44 compiler.Properties.Lints = proptools.StringPtr("none")
Stephen Craneda931d42020-08-04 13:02:28 -070045}
46
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080047func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070048 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070049 Properties: BaseCompilerProperties{},
50 dir: dir,
51 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080052 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070053 }
54}
55
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080056type installLocation int
57
58const (
59 InstallInSystem installLocation = 0
60 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -040061
62 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080063)
64
Ivan Lozanoffee3342019-08-27 12:03:00 -070065type BaseCompilerProperties struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040066 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs)
67 Srcs []string `android:"path,arch_variant"`
68
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020069 // name of the lint set that should be used to validate this module.
70 //
71 // Possible values are "default" (for using a sensible set of lints
72 // depending on the module's location), "android" (for the strictest
73 // lint set that applies to all Android platform code), "vendor" (for
74 // a relaxed set) and "none" (for ignoring all lint warnings and
75 // errors). The default value is "default".
76 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070077
Ivan Lozanoffee3342019-08-27 12:03:00 -070078 // flags to pass to rustc
79 Flags []string `android:"path,arch_variant"`
80
81 // flags to pass to the linker
82 Ld_flags []string `android:"path,arch_variant"`
83
84 // list of rust rlib crate dependencies
85 Rlibs []string `android:"arch_variant"`
86
87 // list of rust dylib crate dependencies
88 Dylibs []string `android:"arch_variant"`
89
Matthew Maurer0f003b12020-06-29 14:34:06 -070090 // list of rust automatic crate dependencies
91 Rustlibs []string `android:"arch_variant"`
92
Ivan Lozanoffee3342019-08-27 12:03:00 -070093 // list of rust proc_macro crate dependencies
94 Proc_macros []string `android:"arch_variant"`
95
96 // list of C shared library dependencies
97 Shared_libs []string `android:"arch_variant"`
98
Ivan Lozano3dfa12d2021-02-04 11:29:41 -050099 // list of C static library dependencies. Note, static libraries prefixed by "lib" will be passed to rustc
100 // along with "-lstatic=<name>". This will bundle the static library into rlib/static libraries so dependents do
101 // not need to also declare the static library as a dependency. Static libraries which are not prefixed by "lib"
102 // cannot be passed to rustc with this flag and will not be bundled into rlib/static libraries, and thus must
103 // be redeclared in dependents.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104 Static_libs []string `android:"arch_variant"`
105
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400106 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
107 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
108 // source, and is required to conform to an enforced format matching library output files (if the output file is
109 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110 Crate_name string `android:"arch_variant"`
111
112 // list of features to enable for this crate
113 Features []string `android:"arch_variant"`
114
115 // specific rust edition that should be used if the default version is not desired
116 Edition *string `android:"arch_variant"`
117
118 // sets name of the output
119 Stem *string `android:"arch_variant"`
120
121 // append to name of output
122 Suffix *string `android:"arch_variant"`
123
124 // install to a subdirectory of the default install path for the module
125 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700126
127 // whether to suppress inclusion of standard crates - defaults to false
128 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500129
130 // Change the rustlibs linkage to select rlib linkage by default for device targets.
131 // Also link libstd as an rlib as well on device targets.
132 // Note: This is the default behavior for host targets.
133 //
134 // This is primarily meant for rust_binary and rust_ffi modules where the default
135 // linkage of libstd might need to be overridden in some use cases. This should
136 // generally be avoided with other module types since it may cause collisions at
137 // linkage if all dependencies of the root binary module do not link against libstd\
138 // the same way.
139 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140}
141
142type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800143 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700144
145 // Install related
146 dir string
147 dir64 string
148 subDir string
149 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700150 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800151 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500152 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400153
Joel Galensonfa049382021-01-14 16:03:18 -0800154 distFile android.OptionalPath
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200155 // Stripped output file. If Valid(), this file will be installed instead of outputFile.
156 strippedOutputFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157}
158
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400159func (compiler *baseCompiler) Disabled() bool {
160 return false
161}
162
163func (compiler *baseCompiler) SetDisabled() {
164 panic("baseCompiler does not implement SetDisabled()")
165}
166
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400167func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
168 panic("baseCompiler does not implement coverageOutputZipPath()")
169}
170
Ivan Lozanoea086132020-12-08 14:43:00 -0500171func (compiler *baseCompiler) preferRlib() bool {
172 return Bool(compiler.Properties.Prefer_rlib)
173}
174
Ivan Lozanodd055472020-09-28 13:22:45 -0400175func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400176 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500177 if compiler.preferRlib() {
178 return RlibLinkage
179 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400180 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400181 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400182 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400183 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400184}
185
Ivan Lozanoffee3342019-08-27 12:03:00 -0700186var _ compiler = (*baseCompiler)(nil)
187
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800188func (compiler *baseCompiler) inData() bool {
189 return compiler.location == InstallInData
190}
191
Ivan Lozanoffee3342019-08-27 12:03:00 -0700192func (compiler *baseCompiler) compilerProps() []interface{} {
193 return []interface{}{&compiler.Properties}
194}
195
196func (compiler *baseCompiler) featuresToFlags(features []string) []string {
197 flags := []string{}
198 for _, feature := range features {
199 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
200 }
201 return flags
202}
203
204func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
205
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200206 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
207 if err != nil {
208 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700209 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200210 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
212 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200213 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700214 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700215 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700216 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
217 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700218
219 if ctx.Host() && !ctx.Windows() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800220 rpathPrefix := `\$$ORIGIN/`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221 if ctx.Darwin() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800222 rpathPrefix = "@loader_path/"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700223 }
224
225 var rpath string
226 if ctx.toolchain().Is64Bit() {
227 rpath = "lib64"
228 } else {
229 rpath = "lib"
230 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800231 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+rpath)
232 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+"../"+rpath)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700233 }
234
Ivan Lozanof76cdf72021-02-12 09:55:06 -0500235 if ctx.RustModule().UseVndk() {
236 flags.RustFlags = append(flags.RustFlags, "--cfg 'android_vndk'")
237 }
238
Ivan Lozanoffee3342019-08-27 12:03:00 -0700239 return flags
240}
241
242func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
243 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
244}
245
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500246func (compiler *baseCompiler) isDependencyRoot() bool {
247 return false
248}
249
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
251 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
252 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700253 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700254 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
255 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
256 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
257
Matthew Maurer99020b02019-10-31 10:44:40 -0700258 if !Bool(compiler.Properties.No_stdlibs) {
259 for _, stdlib := range config.Stdlibs {
Jiyong Parkb5d2dd22020-08-31 17:22:01 +0900260 // If we're building for the primary arch of the build host, use the compiler's stdlibs
Ivan Lozanoc5d34ec2021-02-09 14:22:00 -0500261 if ctx.Target().Os == android.BuildOs {
Matthew Maurer99020b02019-10-31 10:44:40 -0700262 stdlib = stdlib + "_" + ctx.toolchain().RustTriple()
263 }
264
Ivan Lozano2b081132020-09-08 12:46:52 -0400265 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700266 }
267 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268 return deps
269}
270
Ivan Lozanobf63d002020-10-02 10:03:23 -0400271func bionicDeps(deps Deps, static bool) Deps {
272 bionicLibs := []string{}
273 bionicLibs = append(bionicLibs, "liblog")
274 bionicLibs = append(bionicLibs, "libc")
275 bionicLibs = append(bionicLibs, "libm")
276 bionicLibs = append(bionicLibs, "libdl")
277
278 if static {
279 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
280 } else {
281 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
282 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700283
284 //TODO(b/141331117) libstd requires libgcc on Android
285 deps.StaticLibs = append(deps.StaticLibs, "libgcc")
286
287 return deps
288}
289
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290func (compiler *baseCompiler) crateName() string {
291 return compiler.Properties.Crate_name
292}
293
Colin Cross70dda7e2019-10-01 22:05:35 -0700294func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700295 dir := compiler.dir
296 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
297 dir = compiler.dir64
298 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400299 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
300 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
301 }
302 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700303 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
304 }
305 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
306 compiler.relativeInstallPath(), compiler.relative)
307}
308
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400309func (compiler *baseCompiler) nativeCoverage() bool {
310 return false
311}
312
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200313func (compiler *baseCompiler) install(ctx ModuleContext) {
314 path := ctx.RustModule().outputFile
315 if compiler.strippedOutputFile.Valid() {
316 path = compiler.strippedOutputFile
317 }
318 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700319}
320
321func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
322 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
323}
324
325func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200326 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700327 if String(compiler.Properties.Stem) != "" {
328 stem = String(compiler.Properties.Stem)
329 }
330
331 return stem
332}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700333
Ivan Lozanoffee3342019-08-27 12:03:00 -0700334func (compiler *baseCompiler) relativeInstallPath() string {
335 return String(compiler.Properties.Relative_install_path)
336}
337
Ivan Lozano43845682020-07-09 21:03:28 -0400338// 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 -0700339func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
340 // The srcs can contain strings with prefix ":".
341 // They are dependent modules of this module, with android.SourceDepTag.
342 // They are not the main source file compiled by rustc.
343 numSrcs := 0
344 srcIndex := 0
345 for i, s := range srcs {
346 if android.SrcIsModule(s) == "" {
347 numSrcs++
348 srcIndex = i
349 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700350 }
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700351 if numSrcs != 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400352 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700353 }
354 if srcIndex != 0 {
355 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
356 }
357 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400358 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700359}