blob: c39a4a181c4f081f62bcccfa3455c9ecf1e4c3d9 [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
Thiébaud Weksteene81c9242020-08-03 10:46:28 +020027func (compiler *baseCompiler) edition() string {
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070028 return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition)
29}
30
Matthew Maurer99020b02019-10-31 10:44:40 -070031func (compiler *baseCompiler) setNoStdlibs() {
32 compiler.Properties.No_stdlibs = proptools.BoolPtr(true)
33}
34
Stephen Craneda931d42020-08-04 13:02:28 -070035func (compiler *baseCompiler) setNoLint() {
36 compiler.Properties.No_lint = proptools.BoolPtr(true)
37}
38
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080039func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070040 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070041 Properties: BaseCompilerProperties{},
42 dir: dir,
43 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080044 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 }
46}
47
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080048type installLocation int
49
50const (
51 InstallInSystem installLocation = 0
52 InstallInData = iota
Ivan Lozano43845682020-07-09 21:03:28 -040053
54 incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\""
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080055)
56
Ivan Lozanoffee3342019-08-27 12:03:00 -070057type BaseCompilerProperties struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040058 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs)
59 Srcs []string `android:"path,arch_variant"`
60
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020061 // whether to suppress the standard lint flags - default to false
62 No_lint *bool
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070063
Ivan Lozanoffee3342019-08-27 12:03:00 -070064 // flags to pass to rustc
65 Flags []string `android:"path,arch_variant"`
66
67 // flags to pass to the linker
68 Ld_flags []string `android:"path,arch_variant"`
69
70 // list of rust rlib crate dependencies
71 Rlibs []string `android:"arch_variant"`
72
73 // list of rust dylib crate dependencies
74 Dylibs []string `android:"arch_variant"`
75
Matthew Maurer0f003b12020-06-29 14:34:06 -070076 // list of rust automatic crate dependencies
77 Rustlibs []string `android:"arch_variant"`
78
Ivan Lozanoffee3342019-08-27 12:03:00 -070079 // list of rust proc_macro crate dependencies
80 Proc_macros []string `android:"arch_variant"`
81
82 // list of C shared library dependencies
83 Shared_libs []string `android:"arch_variant"`
84
85 // list of C static library dependencies
86 Static_libs []string `android:"arch_variant"`
87
Ivan Lozano26ecd6c2020-07-31 13:40:31 -040088 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
89 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
90 // source, and is required to conform to an enforced format matching library output files (if the output file is
91 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -070092 Crate_name string `android:"arch_variant"`
93
94 // list of features to enable for this crate
95 Features []string `android:"arch_variant"`
96
97 // specific rust edition that should be used if the default version is not desired
98 Edition *string `android:"arch_variant"`
99
100 // sets name of the output
101 Stem *string `android:"arch_variant"`
102
103 // append to name of output
104 Suffix *string `android:"arch_variant"`
105
106 // install to a subdirectory of the default install path for the module
107 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700108
109 // whether to suppress inclusion of standard crates - defaults to false
110 No_stdlibs *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111}
112
113type baseCompiler struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400114 Properties BaseCompilerProperties
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400115 coverageFile android.Path //rustc generates a single gcno file
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116
117 // Install related
118 dir string
119 dir64 string
120 subDir string
121 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700122 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800123 location installLocation
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400124
125 coverageOutputZipFile android.OptionalPath
126 unstrippedOutputFile android.Path
127 distFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128}
129
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400130func (compiler *baseCompiler) Disabled() bool {
131 return false
132}
133
134func (compiler *baseCompiler) SetDisabled() {
135 panic("baseCompiler does not implement SetDisabled()")
136}
137
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400138func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
139 panic("baseCompiler does not implement coverageOutputZipPath()")
140}
141
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142var _ compiler = (*baseCompiler)(nil)
143
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800144func (compiler *baseCompiler) inData() bool {
145 return compiler.location == InstallInData
146}
147
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148func (compiler *baseCompiler) compilerProps() []interface{} {
149 return []interface{}{&compiler.Properties}
150}
151
152func (compiler *baseCompiler) featuresToFlags(features []string) []string {
153 flags := []string{}
154 for _, feature := range features {
155 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
156 }
157 return flags
158}
159
160func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
161
Thiébaud Weksteen4318e1c2020-08-07 13:56:35 +0200162 if Bool(compiler.Properties.No_lint) {
163 flags.RustFlags = append(flags.RustFlags, config.AllowAllLints)
164 } else {
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200165 flags.RustFlags = append(flags.RustFlags, config.RustcLintsForDir(ctx.ModuleDir()))
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700166 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700167 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
168 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200169 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700171 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700172 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
173 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700174
175 if ctx.Host() && !ctx.Windows() {
176 rpath_prefix := `\$$ORIGIN/`
177 if ctx.Darwin() {
178 rpath_prefix = "@loader_path/"
179 }
180
181 var rpath string
182 if ctx.toolchain().Is64Bit() {
183 rpath = "lib64"
184 } else {
185 rpath = "lib"
186 }
187 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
188 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+"../"+rpath)
189 }
190
191 return flags
192}
193
194func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
195 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
196}
197
198func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
199 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
200 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700201 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700202 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
203 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
204 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
205
Matthew Maurer99020b02019-10-31 10:44:40 -0700206 if !Bool(compiler.Properties.No_stdlibs) {
207 for _, stdlib := range config.Stdlibs {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400208 // If we're building for the primary host target, use the compiler's stdlibs
209 if ctx.Host() && ctx.TargetPrimary() {
Matthew Maurer99020b02019-10-31 10:44:40 -0700210 stdlib = stdlib + "_" + ctx.toolchain().RustTriple()
211 }
212
Matthew Maurerc761eec2020-06-25 00:47:46 -0700213 deps.Rustlibs = append(deps.Rustlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700214 }
215 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700216 return deps
217}
218
Ivan Lozano45901ed2020-07-24 16:05:01 -0400219func bionicDeps(deps Deps) Deps {
Ivan Lozanof1c84332019-09-20 11:00:37 -0700220 deps.SharedLibs = append(deps.SharedLibs, "liblog")
221 deps.SharedLibs = append(deps.SharedLibs, "libc")
222 deps.SharedLibs = append(deps.SharedLibs, "libm")
223 deps.SharedLibs = append(deps.SharedLibs, "libdl")
224
225 //TODO(b/141331117) libstd requires libgcc on Android
226 deps.StaticLibs = append(deps.StaticLibs, "libgcc")
227
228 return deps
229}
230
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231func (compiler *baseCompiler) crateName() string {
232 return compiler.Properties.Crate_name
233}
234
Colin Cross70dda7e2019-10-01 22:05:35 -0700235func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700236 dir := compiler.dir
237 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
238 dir = compiler.dir64
239 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400240 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
241 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
242 }
243 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700244 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
245 }
246 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
247 compiler.relativeInstallPath(), compiler.relative)
248}
249
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400250func (compiler *baseCompiler) nativeCoverage() bool {
251 return false
252}
253
Ivan Lozanoffee3342019-08-27 12:03:00 -0700254func (compiler *baseCompiler) install(ctx ModuleContext, file android.Path) {
255 compiler.path = ctx.InstallFile(compiler.installDir(ctx), file.Base(), file)
256}
257
258func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
259 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
260}
261
262func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200263 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264 if String(compiler.Properties.Stem) != "" {
265 stem = String(compiler.Properties.Stem)
266 }
267
268 return stem
269}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700270
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271func (compiler *baseCompiler) relativeInstallPath() string {
272 return String(compiler.Properties.Relative_install_path)
273}
274
Ivan Lozano43845682020-07-09 21:03:28 -0400275// 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 -0700276func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
277 // The srcs can contain strings with prefix ":".
278 // They are dependent modules of this module, with android.SourceDepTag.
279 // They are not the main source file compiled by rustc.
280 numSrcs := 0
281 srcIndex := 0
282 for i, s := range srcs {
283 if android.SrcIsModule(s) == "" {
284 numSrcs++
285 srcIndex = i
286 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700287 }
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700288 if numSrcs != 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400289 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700290 }
291 if srcIndex != 0 {
292 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
293 }
294 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400295 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700296}