blob: 050a2593d72db1a42061f919ed25222ba8c94f8c [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
31func getDenyWarnings(compiler *baseCompiler) bool {
32 return BoolDefault(compiler.Properties.Deny_warnings, config.DefaultDenyWarnings)
33}
34
Matthew Maurer99020b02019-10-31 10:44:40 -070035func (compiler *baseCompiler) setNoStdlibs() {
36 compiler.Properties.No_stdlibs = 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
53)
54
Ivan Lozanoffee3342019-08-27 12:03:00 -070055type BaseCompilerProperties struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040056 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs)
57 Srcs []string `android:"path,arch_variant"`
58
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070059 // whether to pass "-D warnings" to rustc. Defaults to true.
60 Deny_warnings *bool
61
Ivan Lozanoffee3342019-08-27 12:03:00 -070062 // flags to pass to rustc
63 Flags []string `android:"path,arch_variant"`
64
65 // flags to pass to the linker
66 Ld_flags []string `android:"path,arch_variant"`
67
68 // list of rust rlib crate dependencies
69 Rlibs []string `android:"arch_variant"`
70
71 // list of rust dylib crate dependencies
72 Dylibs []string `android:"arch_variant"`
73
74 // list of rust proc_macro crate dependencies
75 Proc_macros []string `android:"arch_variant"`
76
77 // list of C shared library dependencies
78 Shared_libs []string `android:"arch_variant"`
79
80 // list of C static library dependencies
81 Static_libs []string `android:"arch_variant"`
82
Ivan Lozanoad8b18b2019-10-31 19:38:29 -070083 // crate name, required for libraries. This must be the expected extern crate name used in source
Ivan Lozanoffee3342019-08-27 12:03:00 -070084 Crate_name string `android:"arch_variant"`
85
86 // list of features to enable for this crate
87 Features []string `android:"arch_variant"`
88
89 // specific rust edition that should be used if the default version is not desired
90 Edition *string `android:"arch_variant"`
91
92 // sets name of the output
93 Stem *string `android:"arch_variant"`
94
95 // append to name of output
96 Suffix *string `android:"arch_variant"`
97
98 // install to a subdirectory of the default install path for the module
99 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700100
101 // whether to suppress inclusion of standard crates - defaults to false
102 No_stdlibs *bool
Ivan Lozanoffee3342019-08-27 12:03:00 -0700103}
104
105type baseCompiler struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400106 Properties BaseCompilerProperties
107 depFlags []string
108 linkDirs []string
109 coverageFile android.Path //rustc generates a single gcno file
Ivan Lozanoffee3342019-08-27 12:03:00 -0700110
111 // Install related
112 dir string
113 dir64 string
114 subDir string
115 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700116 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800117 location installLocation
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400118
119 coverageOutputZipFile android.OptionalPath
120 unstrippedOutputFile android.Path
121 distFile android.OptionalPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700122}
123
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400124func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
125 panic("baseCompiler does not implement coverageOutputZipPath()")
126}
127
Ivan Lozanoffee3342019-08-27 12:03:00 -0700128var _ compiler = (*baseCompiler)(nil)
129
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800130func (compiler *baseCompiler) inData() bool {
131 return compiler.location == InstallInData
132}
133
Ivan Lozanoffee3342019-08-27 12:03:00 -0700134func (compiler *baseCompiler) compilerProps() []interface{} {
135 return []interface{}{&compiler.Properties}
136}
137
138func (compiler *baseCompiler) featuresToFlags(features []string) []string {
139 flags := []string{}
140 for _, feature := range features {
141 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
142 }
143 return flags
144}
145
146func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
147
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -0700148 if getDenyWarnings(compiler) {
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700149 flags.RustFlags = append(flags.RustFlags, "-D warnings")
150 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
152 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...)
Chih-Hung Hsieh961a30c2019-10-03 09:47:06 -0700153 flags.RustFlags = append(flags.RustFlags, "--edition="+getEdition(compiler))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700155 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700156 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
157 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158
159 if ctx.Host() && !ctx.Windows() {
160 rpath_prefix := `\$$ORIGIN/`
161 if ctx.Darwin() {
162 rpath_prefix = "@loader_path/"
163 }
164
165 var rpath string
166 if ctx.toolchain().Is64Bit() {
167 rpath = "lib64"
168 } else {
169 rpath = "lib"
170 }
171 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+rpath)
172 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+"../"+rpath)
173 }
174
175 return flags
176}
177
178func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
179 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
180}
181
182func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
183 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
184 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
185 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
186 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
187 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
188
Matthew Maurer99020b02019-10-31 10:44:40 -0700189 if !Bool(compiler.Properties.No_stdlibs) {
190 for _, stdlib := range config.Stdlibs {
Ivan Lozano9d1df102020-04-28 10:10:23 -0400191 // If we're building for the primary host target, use the compiler's stdlibs
192 if ctx.Host() && ctx.TargetPrimary() {
Matthew Maurer99020b02019-10-31 10:44:40 -0700193 stdlib = stdlib + "_" + ctx.toolchain().RustTriple()
194 }
195
196 // This check is technically insufficient - on the host, where
197 // static linking is the default, if one of our static
198 // dependencies uses a dynamic library, we need to dynamically
199 // link the stdlib as well.
Ivan Lozano9d1df102020-04-28 10:10:23 -0400200 if (len(deps.Dylibs) > 0) || ctx.Device() {
Matthew Maurer99020b02019-10-31 10:44:40 -0700201 // Dynamically linked stdlib
202 deps.Dylibs = append(deps.Dylibs, stdlib)
Ivan Lozano9d1df102020-04-28 10:10:23 -0400203 } else if ctx.Host() && !ctx.TargetPrimary() {
204 // Otherwise use the static in-tree stdlib for host secondary arch
205 deps.Rlibs = append(deps.Rlibs, stdlib+".static")
Matthew Maurer99020b02019-10-31 10:44:40 -0700206 }
207 }
208 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700209 return deps
210}
211
Ivan Lozanof1c84332019-09-20 11:00:37 -0700212func (compiler *baseCompiler) bionicDeps(ctx DepsContext, deps Deps) Deps {
213 deps.SharedLibs = append(deps.SharedLibs, "liblog")
214 deps.SharedLibs = append(deps.SharedLibs, "libc")
215 deps.SharedLibs = append(deps.SharedLibs, "libm")
216 deps.SharedLibs = append(deps.SharedLibs, "libdl")
217
218 //TODO(b/141331117) libstd requires libgcc on Android
219 deps.StaticLibs = append(deps.StaticLibs, "libgcc")
220
221 return deps
222}
223
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224func (compiler *baseCompiler) crateName() string {
225 return compiler.Properties.Crate_name
226}
227
Colin Cross70dda7e2019-10-01 22:05:35 -0700228func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700229 dir := compiler.dir
230 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
231 dir = compiler.dir64
232 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400233 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
234 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
235 }
236 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700237 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
238 }
239 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
240 compiler.relativeInstallPath(), compiler.relative)
241}
242
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400243func (compiler *baseCompiler) nativeCoverage() bool {
244 return false
245}
246
Ivan Lozanoffee3342019-08-27 12:03:00 -0700247func (compiler *baseCompiler) install(ctx ModuleContext, file android.Path) {
248 compiler.path = ctx.InstallFile(compiler.installDir(ctx), file.Base(), file)
249}
250
251func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
252 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
253}
254
255func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +0200256 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700257 if String(compiler.Properties.Stem) != "" {
258 stem = String(compiler.Properties.Stem)
259 }
260
261 return stem
262}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700263
Ivan Lozanoffee3342019-08-27 12:03:00 -0700264func (compiler *baseCompiler) relativeInstallPath() string {
265 return String(compiler.Properties.Relative_install_path)
266}
267
268func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) android.Path {
269 srcPaths := android.PathsForModuleSrc(ctx, srcs)
270 if len(srcPaths) != 1 {
271 ctx.PropertyErrorf("srcs", "srcs can only contain one path for rust modules")
272 }
273 return srcPaths[0]
274}