Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint/proptools" |
| 22 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 23 | "android/soong/android" |
| 24 | "android/soong/rust/config" |
| 25 | ) |
| 26 | |
Chih-Hung Hsieh | 961a30c | 2019-10-03 09:47:06 -0700 | [diff] [blame] | 27 | func getEdition(compiler *baseCompiler) string { |
| 28 | return proptools.StringDefault(compiler.Properties.Edition, config.DefaultEdition) |
| 29 | } |
| 30 | |
| 31 | func getDenyWarnings(compiler *baseCompiler) bool { |
| 32 | return BoolDefault(compiler.Properties.Deny_warnings, config.DefaultDenyWarnings) |
| 33 | } |
| 34 | |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 35 | func (compiler *baseCompiler) setNoStdlibs() { |
| 36 | compiler.Properties.No_stdlibs = proptools.BoolPtr(true) |
| 37 | } |
| 38 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 39 | func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 40 | return &baseCompiler{ |
Chih-Hung Hsieh | 961a30c | 2019-10-03 09:47:06 -0700 | [diff] [blame] | 41 | Properties: BaseCompilerProperties{}, |
| 42 | dir: dir, |
| 43 | dir64: dir64, |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 44 | location: location, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 48 | type installLocation int |
| 49 | |
| 50 | const ( |
| 51 | InstallInSystem installLocation = 0 |
| 52 | InstallInData = iota |
| 53 | ) |
| 54 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | type BaseCompilerProperties struct { |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 56 | // 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 Hsieh | efdd7ac | 2019-09-26 18:59:27 -0700 | [diff] [blame] | 59 | // whether to pass "-D warnings" to rustc. Defaults to true. |
| 60 | Deny_warnings *bool |
| 61 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 62 | // 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 Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 83 | // crate name, required for libraries. This must be the expected extern crate name used in source |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 84 | 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 Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 100 | |
| 101 | // whether to suppress inclusion of standard crates - defaults to false |
| 102 | No_stdlibs *bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | type baseCompiler struct { |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 106 | Properties BaseCompilerProperties |
| 107 | depFlags []string |
| 108 | linkDirs []string |
| 109 | coverageFile android.Path //rustc generates a single gcno file |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 110 | |
| 111 | // Install related |
| 112 | dir string |
| 113 | dir64 string |
| 114 | subDir string |
| 115 | relative string |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 116 | path android.InstallPath |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 117 | location installLocation |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 118 | |
| 119 | coverageOutputZipFile android.OptionalPath |
| 120 | unstrippedOutputFile android.Path |
| 121 | distFile android.OptionalPath |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 124 | func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath { |
| 125 | panic("baseCompiler does not implement coverageOutputZipPath()") |
| 126 | } |
| 127 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 128 | var _ compiler = (*baseCompiler)(nil) |
| 129 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 130 | func (compiler *baseCompiler) inData() bool { |
| 131 | return compiler.location == InstallInData |
| 132 | } |
| 133 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 134 | func (compiler *baseCompiler) compilerProps() []interface{} { |
| 135 | return []interface{}{&compiler.Properties} |
| 136 | } |
| 137 | |
| 138 | func (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 | |
| 146 | func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 147 | |
Chih-Hung Hsieh | 961a30c | 2019-10-03 09:47:06 -0700 | [diff] [blame] | 148 | if getDenyWarnings(compiler) { |
Chih-Hung Hsieh | efdd7ac | 2019-09-26 18:59:27 -0700 | [diff] [blame] | 149 | flags.RustFlags = append(flags.RustFlags, "-D warnings") |
| 150 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 151 | flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...) |
| 152 | flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...) |
Chih-Hung Hsieh | 961a30c | 2019-10-03 09:47:06 -0700 | [diff] [blame] | 153 | flags.RustFlags = append(flags.RustFlags, "--edition="+getEdition(compiler)) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 154 | flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...) |
Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 155 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 156 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags()) |
| 157 | flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags()) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 158 | |
| 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 | |
| 178 | func (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 | |
| 182 | func (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 Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 189 | if !Bool(compiler.Properties.No_stdlibs) { |
| 190 | for _, stdlib := range config.Stdlibs { |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 191 | // If we're building for the primary host target, use the compiler's stdlibs |
| 192 | if ctx.Host() && ctx.TargetPrimary() { |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 193 | 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 Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 200 | if (len(deps.Dylibs) > 0) || ctx.Device() { |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 201 | // Dynamically linked stdlib |
| 202 | deps.Dylibs = append(deps.Dylibs, stdlib) |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 203 | } 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 Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 209 | return deps |
| 210 | } |
| 211 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 212 | func (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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 224 | func (compiler *baseCompiler) crateName() string { |
| 225 | return compiler.Properties.Crate_name |
| 226 | } |
| 227 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 228 | func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 229 | dir := compiler.dir |
| 230 | if ctx.toolchain().Is64Bit() && compiler.dir64 != "" { |
| 231 | dir = compiler.dir64 |
| 232 | } |
Ivan Lozano | d6fdca8 | 2020-04-07 12:30:33 -0400 | [diff] [blame] | 233 | 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 Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 237 | 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 Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 243 | func (compiler *baseCompiler) nativeCoverage() bool { |
| 244 | return false |
| 245 | } |
| 246 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 247 | func (compiler *baseCompiler) install(ctx ModuleContext, file android.Path) { |
| 248 | compiler.path = ctx.InstallFile(compiler.installDir(ctx), file.Base(), file) |
| 249 | } |
| 250 | |
| 251 | func (compiler *baseCompiler) getStem(ctx ModuleContext) string { |
| 252 | return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix) |
| 253 | } |
| 254 | |
| 255 | func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string { |
ThiƩbaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame^] | 256 | stem := ctx.ModuleName() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 257 | if String(compiler.Properties.Stem) != "" { |
| 258 | stem = String(compiler.Properties.Stem) |
| 259 | } |
| 260 | |
| 261 | return stem |
| 262 | } |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 263 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 264 | func (compiler *baseCompiler) relativeInstallPath() string { |
| 265 | return String(compiler.Properties.Relative_install_path) |
| 266 | } |
| 267 | |
| 268 | func 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 | } |