blob: 102f9dc8d8f4ae7925b9a90de2235e8d00c6ff84 [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
99 // list of C static library dependencies
100 Static_libs []string `android:"arch_variant"`
101
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400102 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
103 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
104 // source, and is required to conform to an enforced format matching library output files (if the output file is
105 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700106 Crate_name string `android:"arch_variant"`
107
108 // list of features to enable for this crate
109 Features []string `android:"arch_variant"`
110
111 // specific rust edition that should be used if the default version is not desired
112 Edition *string `android:"arch_variant"`
113
114 // sets name of the output
115 Stem *string `android:"arch_variant"`
116
117 // append to name of output
118 Suffix *string `android:"arch_variant"`
119
120 // install to a subdirectory of the default install path for the module
121 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700122
123 // whether to suppress inclusion of standard crates - defaults to false
124 No_stdlibs *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700125}
126
127type baseCompiler struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400128 Properties BaseCompilerProperties
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400129 coverageFile android.Path //rustc generates a single gcno file
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130
131 // Install related
132 dir string
133 dir64 string
134 subDir string
135 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700136 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800137 location installLocation
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400138
139 coverageOutputZipFile android.OptionalPath
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400140 distFile android.OptionalPath
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200141 // Stripped output file. If Valid(), this file will be installed instead of outputFile.
142 strippedOutputFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143}
144
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400145func (compiler *baseCompiler) Disabled() bool {
146 return false
147}
148
149func (compiler *baseCompiler) SetDisabled() {
150 panic("baseCompiler does not implement SetDisabled()")
151}
152
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400153func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
154 panic("baseCompiler does not implement coverageOutputZipPath()")
155}
156
Ivan Lozanodd055472020-09-28 13:22:45 -0400157func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400158 // For devices, we always link stdlibs in as dylibs by default.
159 if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400160 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400161 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400162 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400163 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400164}
165
Ivan Lozanoffee3342019-08-27 12:03:00 -0700166var _ compiler = (*baseCompiler)(nil)
167
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800168func (compiler *baseCompiler) inData() bool {
169 return compiler.location == InstallInData
170}
171
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172func (compiler *baseCompiler) compilerProps() []interface{} {
173 return []interface{}{&compiler.Properties}
174}
175
176func (compiler *baseCompiler) featuresToFlags(features []string) []string {
177 flags := []string{}
178 for _, feature := range features {
179 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
180 }
181 return flags
182}
183
184func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
185
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200186 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
187 if err != nil {
188 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700189 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200190 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700191 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
192 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200193 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700194 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700195 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700196 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
197 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700198
199 if ctx.Host() && !ctx.Windows() {
200 rpath_prefix := `\$$ORIGIN/`
201 if ctx.Darwin() {
202 rpath_prefix = "@loader_path/"
203 }
204
205 var rpath string
206 if ctx.toolchain().Is64Bit() {
207 rpath = "lib64"
208 } else {
209 rpath = "lib"
210 }
211 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
212 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+"../"+rpath)
213 }
214
215 return flags
216}
217
218func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
219 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
220}
221
222func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
223 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
224 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700225 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700226 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
227 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
228 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
229
Matthew Maurer99020b02019-10-31 10:44:40 -0700230 if !Bool(compiler.Properties.No_stdlibs) {
231 for _, stdlib := range config.Stdlibs {
Jiyong Parkb5d2dd22020-08-31 17:22:01 +0900232 // If we're building for the primary arch of the build host, use the compiler's stdlibs
233 if ctx.Target().Os == android.BuildOs && ctx.TargetPrimary() {
Matthew Maurer99020b02019-10-31 10:44:40 -0700234 stdlib = stdlib + "_" + ctx.toolchain().RustTriple()
235 }
236
Ivan Lozano2b081132020-09-08 12:46:52 -0400237 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700238 }
239 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240 return deps
241}
242
Ivan Lozano45901ed2020-07-24 16:05:01 -0400243func bionicDeps(deps Deps) Deps {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700244 deps.SharedLibs = append(deps.SharedLibs, "liblog")
245 deps.SharedLibs = append(deps.SharedLibs, "libc")
246 deps.SharedLibs = append(deps.SharedLibs, "libm")
247 deps.SharedLibs = append(deps.SharedLibs, "libdl")
248
249 //TODO(b/141331117) libstd requires libgcc on Android
250 deps.StaticLibs = append(deps.StaticLibs, "libgcc")
251
252 return deps
253}
254
Ivan Lozanoffee3342019-08-27 12:03:00 -0700255func (compiler *baseCompiler) crateName() string {
256 return compiler.Properties.Crate_name
257}
258
Colin Cross70dda7e2019-10-01 22:05:35 -0700259func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700260 dir := compiler.dir
261 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
262 dir = compiler.dir64
263 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400264 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
265 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
266 }
267 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
269 }
270 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
271 compiler.relativeInstallPath(), compiler.relative)
272}
273
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400274func (compiler *baseCompiler) nativeCoverage() bool {
275 return false
276}
277
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200278func (compiler *baseCompiler) install(ctx ModuleContext) {
279 path := ctx.RustModule().outputFile
280 if compiler.strippedOutputFile.Valid() {
281 path = compiler.strippedOutputFile
282 }
283 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700284}
285
286func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
287 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
288}
289
290func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200291 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700292 if String(compiler.Properties.Stem) != "" {
293 stem = String(compiler.Properties.Stem)
294 }
295
296 return stem
297}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700298
Ivan Lozanoffee3342019-08-27 12:03:00 -0700299func (compiler *baseCompiler) relativeInstallPath() string {
300 return String(compiler.Properties.Relative_install_path)
301}
302
Ivan Lozano43845682020-07-09 21:03:28 -0400303// 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 -0700304func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
305 // The srcs can contain strings with prefix ":".
306 // They are dependent modules of this module, with android.SourceDepTag.
307 // They are not the main source file compiled by rustc.
308 numSrcs := 0
309 srcIndex := 0
310 for i, s := range srcs {
311 if android.SrcIsModule(s) == "" {
312 numSrcs++
313 srcIndex = i
314 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700315 }
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700316 if numSrcs != 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400317 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700318 }
319 if srcIndex != 0 {
320 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
321 }
322 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400323 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700324}