blob: c20179bd09f830a955540d2122a5809b9f042d3f [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
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070027func getEdition(compiler *baseCompiler) string {
28 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
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080035func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler {
Ivan Lozanoffee3342019-08-27 12:03:00 -070036 return &baseCompiler{
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -070037 Properties: BaseCompilerProperties{},
38 dir: dir,
39 dir64: dir64,
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080040 location: location,
Ivan Lozanoffee3342019-08-27 12:03:00 -070041 }
42}
43
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080044type installLocation int
45
46const (
47 InstallInSystem installLocation = 0
48 InstallInData = iota
49)
50
Ivan Lozanoffee3342019-08-27 12:03:00 -070051type BaseCompilerProperties struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040052 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs)
53 Srcs []string `android:"path,arch_variant"`
54
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020055 // whether to suppress the standard lint flags - default to false
56 No_lint *bool
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070057
Ivan Lozanoffee3342019-08-27 12:03:00 -070058 // flags to pass to rustc
59 Flags []string `android:"path,arch_variant"`
60
61 // flags to pass to the linker
62 Ld_flags []string `android:"path,arch_variant"`
63
64 // list of rust rlib crate dependencies
65 Rlibs []string `android:"arch_variant"`
66
67 // list of rust dylib crate dependencies
68 Dylibs []string `android:"arch_variant"`
69
Matthew Maurer0f003b12020-06-29 14:34:06 -070070 // list of rust automatic crate dependencies
71 Rustlibs []string `android:"arch_variant"`
72
Ivan Lozanoffee3342019-08-27 12:03:00 -070073 // list of rust proc_macro crate dependencies
74 Proc_macros []string `android:"arch_variant"`
75
76 // list of C shared library dependencies
77 Shared_libs []string `android:"arch_variant"`
78
79 // list of C static library dependencies
80 Static_libs []string `android:"arch_variant"`
81
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070082 // crate name, required for libraries. This must be the expected extern crate name used in source
Ivan Lozanoffee3342019-08-27 12:03:00 -070083 Crate_name string `android:"arch_variant"`
84
85 // list of features to enable for this crate
86 Features []string `android:"arch_variant"`
87
88 // specific rust edition that should be used if the default version is not desired
89 Edition *string `android:"arch_variant"`
90
91 // sets name of the output
92 Stem *string `android:"arch_variant"`
93
94 // append to name of output
95 Suffix *string `android:"arch_variant"`
96
97 // install to a subdirectory of the default install path for the module
98 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -070099
100 // whether to suppress inclusion of standard crates - defaults to false
101 No_stdlibs *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102}
103
104type baseCompiler struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400105 Properties BaseCompilerProperties
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400106 coverageFile android.Path //rustc generates a single gcno file
Ivan Lozanoffee3342019-08-27 12:03:00 -0700107
108 // Install related
109 dir string
110 dir64 string
111 subDir string
112 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700113 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800114 location installLocation
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400115
116 coverageOutputZipFile android.OptionalPath
117 unstrippedOutputFile android.Path
118 distFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119}
120
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400121func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
122 panic("baseCompiler does not implement coverageOutputZipPath()")
123}
124
Ivan Lozanoffee3342019-08-27 12:03:00 -0700125var _ compiler = (*baseCompiler)(nil)
126
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800127func (compiler *baseCompiler) inData() bool {
128 return compiler.location == InstallInData
129}
130
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131func (compiler *baseCompiler) compilerProps() []interface{} {
132 return []interface{}{&compiler.Properties}
133}
134
135func (compiler *baseCompiler) featuresToFlags(features []string) []string {
136 flags := []string{}
137 for _, feature := range features {
138 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
139 }
140 return flags
141}
142
143func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
144
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +0200145 if !Bool(compiler.Properties.No_lint) {
146 flags.RustFlags = append(flags.RustFlags, config.RustcLintsForDir(ctx.ModuleDir()))
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700147 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
149 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...)
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -0700150 flags.RustFlags = append(flags.RustFlags, "--edition="+getEdition(compiler))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700152 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700153 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
154 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155
156 if ctx.Host() && !ctx.Windows() {
157 rpath_prefix := `\$$ORIGIN/`
158 if ctx.Darwin() {
159 rpath_prefix = "@loader_path/"
160 }
161
162 var rpath string
163 if ctx.toolchain().Is64Bit() {
164 rpath = "lib64"
165 } else {
166 rpath = "lib"
167 }
168 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
169 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+"../"+rpath)
170 }
171
172 return flags
173}
174
175func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
176 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
177}
178
179func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
180 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
181 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700182 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700183 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
184 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
185 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
186
Matthew Maurer99020b02019-10-31 10:44:40 -0700187 if !Bool(compiler.Properties.No_stdlibs) {
188 for _, stdlib := range config.Stdlibs {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400189 // If we're building for the primary host target, use the compiler's stdlibs
190 if ctx.Host() && ctx.TargetPrimary() {
Matthew Maurer99020b02019-10-31 10:44:40 -0700191 stdlib = stdlib + "_" + ctx.toolchain().RustTriple()
192 }
193
Matthew Maurerc761eec2020-06-25 00:47:46 -0700194 deps.Rustlibs = append(deps.Rustlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700195 }
196 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700197 return deps
198}
199
Ivan Lozanof1c84332019-09-20 11:00:37 -0700200func (compiler *baseCompiler) bionicDeps(ctx DepsContext, deps Deps) Deps {
201 deps.SharedLibs = append(deps.SharedLibs, "liblog")
202 deps.SharedLibs = append(deps.SharedLibs, "libc")
203 deps.SharedLibs = append(deps.SharedLibs, "libm")
204 deps.SharedLibs = append(deps.SharedLibs, "libdl")
205
206 //TODO(b/141331117) libstd requires libgcc on Android
207 deps.StaticLibs = append(deps.StaticLibs, "libgcc")
208
209 return deps
210}
211
Ivan Lozanoffee3342019-08-27 12:03:00 -0700212func (compiler *baseCompiler) crateName() string {
213 return compiler.Properties.Crate_name
214}
215
Colin Cross70dda7e2019-10-01 22:05:35 -0700216func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217 dir := compiler.dir
218 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
219 dir = compiler.dir64
220 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400221 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
222 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
223 }
224 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700225 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
226 }
227 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
228 compiler.relativeInstallPath(), compiler.relative)
229}
230
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400231func (compiler *baseCompiler) nativeCoverage() bool {
232 return false
233}
234
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235func (compiler *baseCompiler) install(ctx ModuleContext, file android.Path) {
236 compiler.path = ctx.InstallFile(compiler.installDir(ctx), file.Base(), file)
237}
238
239func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
240 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
241}
242
243func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200244 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700245 if String(compiler.Properties.Stem) != "" {
246 stem = String(compiler.Properties.Stem)
247 }
248
249 return stem
250}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700251
Ivan Lozanoffee3342019-08-27 12:03:00 -0700252func (compiler *baseCompiler) relativeInstallPath() string {
253 return String(compiler.Properties.Relative_install_path)
254}
255
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700256func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
257 // The srcs can contain strings with prefix ":".
258 // They are dependent modules of this module, with android.SourceDepTag.
259 // They are not the main source file compiled by rustc.
260 numSrcs := 0
261 srcIndex := 0
262 for i, s := range srcs {
263 if android.SrcIsModule(s) == "" {
264 numSrcs++
265 srcIndex = i
266 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700267 }
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700268 if numSrcs != 1 {
269 ctx.PropertyErrorf("srcs", "srcs can only contain one path for a rust file")
270 }
271 if srcIndex != 0 {
272 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
273 }
274 paths := android.PathsForModuleSrc(ctx, srcs)
275 return paths[srcIndex], paths
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276}