blob: 41b73719a6c8f36779c2f0478cca57c77b2d9703 [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 \":\""
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +010063 genSubDir = "out/"
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -080064)
65
Ivan Lozanoffee3342019-08-27 12:03:00 -070066type BaseCompilerProperties struct {
Ivan Lozano8a23fa42020-06-16 10:26:57 -040067 // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs)
68 Srcs []string `android:"path,arch_variant"`
69
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +020070 // name of the lint set that should be used to validate this module.
71 //
72 // Possible values are "default" (for using a sensible set of lints
73 // depending on the module's location), "android" (for the strictest
74 // lint set that applies to all Android platform code), "vendor" (for
75 // a relaxed set) and "none" (for ignoring all lint warnings and
76 // errors). The default value is "default".
77 Lints *string
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -070078
Ivan Lozanoffee3342019-08-27 12:03:00 -070079 // flags to pass to rustc
80 Flags []string `android:"path,arch_variant"`
81
82 // flags to pass to the linker
83 Ld_flags []string `android:"path,arch_variant"`
84
85 // list of rust rlib crate dependencies
86 Rlibs []string `android:"arch_variant"`
87
88 // list of rust dylib crate dependencies
89 Dylibs []string `android:"arch_variant"`
90
Matthew Maurer0f003b12020-06-29 14:34:06 -070091 // list of rust automatic crate dependencies
92 Rustlibs []string `android:"arch_variant"`
93
Ivan Lozanoffee3342019-08-27 12:03:00 -070094 // list of rust proc_macro crate dependencies
95 Proc_macros []string `android:"arch_variant"`
96
97 // list of C shared library dependencies
98 Shared_libs []string `android:"arch_variant"`
99
Ivan Lozano63bb7682021-03-23 15:53:44 -0400100 // list of C static library dependencies. These dependencies do not normally propagate to dependents
101 // and may need to be redeclared. See whole_static_libs for bundling static dependencies into a library.
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102 Static_libs []string `android:"arch_variant"`
103
Ivan Lozano63bb7682021-03-23 15:53:44 -0400104 // Similar to static_libs, but will bundle the static library dependency into a library. This is helpful
105 // to avoid having to redeclare the dependency for dependents of this library, but in some cases may also
106 // result in bloat if multiple dependencies all include the same static library whole.
107 //
108 // The common use case for this is when the static library is unlikely to be a dependency of other modules to avoid
109 // having to redeclare the static library dependency for every dependent module.
110 // If you are not sure what to, for rust_library modules most static dependencies should go in static_libraries,
111 // and for rust_ffi modules most static dependencies should go into whole_static_libraries.
112 //
113 // For rust_ffi static variants, these libraries will be included in the resulting static library archive.
114 //
115 // For rust_library rlib variants, these libraries will be bundled into the resulting rlib library. This will
116 // include all of the static libraries symbols in any dylibs or binaries which use this rlib as well.
117 Whole_static_libs []string `android:"arch_variant"`
118
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400119 // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider
120 // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in
121 // source, and is required to conform to an enforced format matching library output files (if the output file is
122 // lib<someName><suffix>, the crate_name property must be <someName>).
Ivan Lozanoffee3342019-08-27 12:03:00 -0700123 Crate_name string `android:"arch_variant"`
124
125 // list of features to enable for this crate
126 Features []string `android:"arch_variant"`
127
128 // specific rust edition that should be used if the default version is not desired
129 Edition *string `android:"arch_variant"`
130
131 // sets name of the output
132 Stem *string `android:"arch_variant"`
133
134 // append to name of output
135 Suffix *string `android:"arch_variant"`
136
137 // install to a subdirectory of the default install path for the module
138 Relative_install_path *string `android:"arch_variant"`
Matthew Maurer99020b02019-10-31 10:44:40 -0700139
140 // whether to suppress inclusion of standard crates - defaults to false
141 No_stdlibs *bool
Ivan Lozanoea086132020-12-08 14:43:00 -0500142
143 // Change the rustlibs linkage to select rlib linkage by default for device targets.
144 // Also link libstd as an rlib as well on device targets.
145 // Note: This is the default behavior for host targets.
146 //
147 // This is primarily meant for rust_binary and rust_ffi modules where the default
148 // linkage of libstd might need to be overridden in some use cases. This should
149 // generally be avoided with other module types since it may cause collisions at
150 // linkage if all dependencies of the root binary module do not link against libstd\
151 // the same way.
152 Prefer_rlib *bool `android:"arch_variant"`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153}
154
155type baseCompiler struct {
Joel Galensonfa049382021-01-14 16:03:18 -0800156 Properties BaseCompilerProperties
Ivan Lozanoffee3342019-08-27 12:03:00 -0700157
158 // Install related
159 dir string
160 dir64 string
161 subDir string
162 relative string
Colin Cross70dda7e2019-10-01 22:05:35 -0700163 path android.InstallPath
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800164 location installLocation
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500165 sanitize *sanitize
Ivan Lozano8a23fa42020-06-16 10:26:57 -0400166
Joel Galensonfa049382021-01-14 16:03:18 -0800167 distFile android.OptionalPath
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200168 // Stripped output file. If Valid(), this file will be installed instead of outputFile.
169 strippedOutputFile android.OptionalPath
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100170
171 // If a crate has a source-generated dependency, a copy of the source file
172 // will be available in cargoOutDir (equivalent to Cargo OUT_DIR).
173 cargoOutDir android.ModuleOutPath
Ivan Lozanoffee3342019-08-27 12:03:00 -0700174}
175
Ivan Lozano26ecd6c2020-07-31 13:40:31 -0400176func (compiler *baseCompiler) Disabled() bool {
177 return false
178}
179
180func (compiler *baseCompiler) SetDisabled() {
181 panic("baseCompiler does not implement SetDisabled()")
182}
183
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400184func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath {
185 panic("baseCompiler does not implement coverageOutputZipPath()")
186}
187
Ivan Lozanoea086132020-12-08 14:43:00 -0500188func (compiler *baseCompiler) preferRlib() bool {
189 return Bool(compiler.Properties.Prefer_rlib)
190}
191
Ivan Lozanodd055472020-09-28 13:22:45 -0400192func (compiler *baseCompiler) stdLinkage(ctx *depsContext) RustLinkage {
Ivan Lozano2b081132020-09-08 12:46:52 -0400193 // For devices, we always link stdlibs in as dylibs by default.
Ivan Lozanoea086132020-12-08 14:43:00 -0500194 if compiler.preferRlib() {
195 return RlibLinkage
196 } else if ctx.Device() {
Ivan Lozanodd055472020-09-28 13:22:45 -0400197 return DylibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400198 } else {
Ivan Lozanodd055472020-09-28 13:22:45 -0400199 return RlibLinkage
Ivan Lozano2b081132020-09-08 12:46:52 -0400200 }
Ivan Lozano042504f2020-08-18 14:31:23 -0400201}
202
Ivan Lozanoffee3342019-08-27 12:03:00 -0700203var _ compiler = (*baseCompiler)(nil)
204
Chih-Hung Hsieh9a4a7ba2019-12-12 19:36:05 -0800205func (compiler *baseCompiler) inData() bool {
206 return compiler.location == InstallInData
207}
208
Ivan Lozanoffee3342019-08-27 12:03:00 -0700209func (compiler *baseCompiler) compilerProps() []interface{} {
210 return []interface{}{&compiler.Properties}
211}
212
213func (compiler *baseCompiler) featuresToFlags(features []string) []string {
214 flags := []string{}
215 for _, feature := range features {
216 flags = append(flags, "--cfg 'feature=\""+feature+"\"'")
217 }
218 return flags
219}
220
221func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags {
222
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200223 lintFlags, err := config.RustcLintsForDir(ctx.ModuleDir(), compiler.Properties.Lints)
224 if err != nil {
225 ctx.PropertyErrorf("lints", err.Error())
Chih-Hung Hsiehefdd7ac2019-09-26 18:59:27 -0700226 }
Thiébaud Weksteen9e8451e2020-08-13 12:55:59 +0200227 flags.RustFlags = append(flags.RustFlags, lintFlags)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700228 flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...)
229 flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...)
Thiébaud Weksteene81c9242020-08-03 10:46:28 +0200230 flags.RustFlags = append(flags.RustFlags, "--edition="+compiler.edition())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700231 flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...)
Joel Galenson724286c2019-09-30 13:01:37 -0700232 flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700233 flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags())
234 flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700235
236 if ctx.Host() && !ctx.Windows() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800237 rpathPrefix := `\$$ORIGIN/`
Ivan Lozanoffee3342019-08-27 12:03:00 -0700238 if ctx.Darwin() {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800239 rpathPrefix = "@loader_path/"
Ivan Lozanoffee3342019-08-27 12:03:00 -0700240 }
241
242 var rpath string
243 if ctx.toolchain().Is64Bit() {
244 rpath = "lib64"
245 } else {
246 rpath = "lib"
247 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800248 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+rpath)
249 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpathPrefix+"../"+rpath)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700250 }
251
Ivan Lozanof76cdf72021-02-12 09:55:06 -0500252 if ctx.RustModule().UseVndk() {
253 flags.RustFlags = append(flags.RustFlags, "--cfg 'android_vndk'")
254 }
255
Ivan Lozanoffee3342019-08-27 12:03:00 -0700256 return flags
257}
258
259func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path {
260 panic(fmt.Errorf("baseCrater doesn't know how to crate things!"))
261}
262
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100263func (compiler *baseCompiler) initialize(ctx ModuleContext) {
264 compiler.cargoOutDir = android.PathForModuleOut(ctx, genSubDir)
265}
266
267func (compiler *baseCompiler) CargoOutDir() android.OptionalPath {
268 return android.OptionalPathForPath(compiler.cargoOutDir)
269}
270
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500271func (compiler *baseCompiler) isDependencyRoot() bool {
272 return false
273}
274
Ivan Lozanoffee3342019-08-27 12:03:00 -0700275func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps {
276 deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...)
277 deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...)
Matthew Maurer0f003b12020-06-29 14:34:06 -0700278 deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700279 deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...)
280 deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...)
Ivan Lozano63bb7682021-03-23 15:53:44 -0400281 deps.WholeStaticLibs = append(deps.WholeStaticLibs, compiler.Properties.Whole_static_libs...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700282 deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...)
283
Matthew Maurer99020b02019-10-31 10:44:40 -0700284 if !Bool(compiler.Properties.No_stdlibs) {
285 for _, stdlib := range config.Stdlibs {
Jiyong Parkb5d2dd22020-08-31 17:22:01 +0900286 // If we're building for the primary arch of the build host, use the compiler's stdlibs
Ivan Lozanoc5d34ec2021-02-09 14:22:00 -0500287 if ctx.Target().Os == android.BuildOs {
Matthew Maurer99020b02019-10-31 10:44:40 -0700288 stdlib = stdlib + "_" + ctx.toolchain().RustTriple()
289 }
290
Ivan Lozano2b081132020-09-08 12:46:52 -0400291 deps.Stdlibs = append(deps.Stdlibs, stdlib)
Matthew Maurer99020b02019-10-31 10:44:40 -0700292 }
293 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700294 return deps
295}
296
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100297func bionicDeps(ctx DepsContext, deps Deps, static bool) Deps {
Ivan Lozanobf63d002020-10-02 10:03:23 -0400298 bionicLibs := []string{}
299 bionicLibs = append(bionicLibs, "liblog")
300 bionicLibs = append(bionicLibs, "libc")
301 bionicLibs = append(bionicLibs, "libm")
302 bionicLibs = append(bionicLibs, "libdl")
303
304 if static {
305 deps.StaticLibs = append(deps.StaticLibs, bionicLibs...)
306 } else {
307 deps.SharedLibs = append(deps.SharedLibs, bionicLibs...)
308 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700309
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +0100310 if libRuntimeBuiltins := config.BuiltinsRuntimeLibrary(ctx.toolchain()); libRuntimeBuiltins != "" {
311 deps.StaticLibs = append(deps.StaticLibs, libRuntimeBuiltins)
312 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700313 return deps
314}
315
Ivan Lozanoffee3342019-08-27 12:03:00 -0700316func (compiler *baseCompiler) crateName() string {
317 return compiler.Properties.Crate_name
318}
319
Colin Cross70dda7e2019-10-01 22:05:35 -0700320func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700321 dir := compiler.dir
322 if ctx.toolchain().Is64Bit() && compiler.dir64 != "" {
323 dir = compiler.dir64
324 }
Ivan Lozanod6fdca82020-04-07 12:30:33 -0400325 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
326 dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath)
327 }
328 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700329 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
330 }
331 return android.PathForModuleInstall(ctx, dir, compiler.subDir,
332 compiler.relativeInstallPath(), compiler.relative)
333}
334
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400335func (compiler *baseCompiler) nativeCoverage() bool {
336 return false
337}
338
Thiébaud Weksteenfabaff62020-08-27 13:48:36 +0200339func (compiler *baseCompiler) install(ctx ModuleContext) {
340 path := ctx.RustModule().outputFile
341 if compiler.strippedOutputFile.Valid() {
342 path = compiler.strippedOutputFile
343 }
344 compiler.path = ctx.InstallFile(compiler.installDir(ctx), path.Path().Base(), path.Path())
Ivan Lozanoffee3342019-08-27 12:03:00 -0700345}
346
347func (compiler *baseCompiler) getStem(ctx ModuleContext) string {
348 return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix)
349}
350
351func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string {
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200352 stem := ctx.ModuleName()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700353 if String(compiler.Properties.Stem) != "" {
354 stem = String(compiler.Properties.Stem)
355 }
356
357 return stem
358}
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700359
Ivan Lozanoffee3342019-08-27 12:03:00 -0700360func (compiler *baseCompiler) relativeInstallPath() string {
361 return String(compiler.Properties.Relative_install_path)
362}
363
Ivan Lozano43845682020-07-09 21:03:28 -0400364// 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 -0700365func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) {
366 // The srcs can contain strings with prefix ":".
367 // They are dependent modules of this module, with android.SourceDepTag.
368 // They are not the main source file compiled by rustc.
369 numSrcs := 0
370 srcIndex := 0
371 for i, s := range srcs {
372 if android.SrcIsModule(s) == "" {
373 numSrcs++
374 srcIndex = i
375 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700376 }
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700377 if numSrcs != 1 {
Ivan Lozano43845682020-07-09 21:03:28 -0400378 ctx.PropertyErrorf("srcs", incorrectSourcesError)
Chih-Hung Hsiehbbd25ae2020-05-15 17:36:30 -0700379 }
380 if srcIndex != 0 {
381 ctx.PropertyErrorf("srcs", "main source file must be the first in srcs")
382 }
383 paths := android.PathsForModuleSrc(ctx, srcs)
Ivan Lozano43845682020-07-09 21:03:28 -0400384 return paths[srcIndex], paths[1:]
Ivan Lozanoffee3342019-08-27 12:03:00 -0700385}