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 | |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 31 | func (compiler *baseCompiler) setNoStdlibs() { |
| 32 | compiler.Properties.No_stdlibs = proptools.BoolPtr(true) |
| 33 | } |
| 34 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 35 | func NewBaseCompiler(dir, dir64 string, location installLocation) *baseCompiler { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 36 | return &baseCompiler{ |
Chih-Hung Hsieh | 961a30c | 2019-10-03 09:47:06 -0700 | [diff] [blame] | 37 | Properties: BaseCompilerProperties{}, |
| 38 | dir: dir, |
| 39 | dir64: dir64, |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 40 | location: location, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 44 | type installLocation int |
| 45 | |
| 46 | const ( |
| 47 | InstallInSystem installLocation = 0 |
| 48 | InstallInData = iota |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 49 | |
| 50 | incorrectSourcesError = "srcs can only contain one path for a rust file and source providers prefixed by \":\"" |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 51 | ) |
| 52 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 53 | type BaseCompilerProperties struct { |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 54 | // path to the source file that is the main entry point of the program (e.g. main.rs or lib.rs) |
| 55 | Srcs []string `android:"path,arch_variant"` |
| 56 | |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 57 | // whether to suppress the standard lint flags - default to false |
| 58 | No_lint *bool |
Chih-Hung Hsieh | efdd7ac | 2019-09-26 18:59:27 -0700 | [diff] [blame] | 59 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 60 | // flags to pass to rustc |
| 61 | Flags []string `android:"path,arch_variant"` |
| 62 | |
| 63 | // flags to pass to the linker |
| 64 | Ld_flags []string `android:"path,arch_variant"` |
| 65 | |
| 66 | // list of rust rlib crate dependencies |
| 67 | Rlibs []string `android:"arch_variant"` |
| 68 | |
| 69 | // list of rust dylib crate dependencies |
| 70 | Dylibs []string `android:"arch_variant"` |
| 71 | |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 72 | // list of rust automatic crate dependencies |
| 73 | Rustlibs []string `android:"arch_variant"` |
| 74 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 75 | // list of rust proc_macro crate dependencies |
| 76 | Proc_macros []string `android:"arch_variant"` |
| 77 | |
| 78 | // list of C shared library dependencies |
| 79 | Shared_libs []string `android:"arch_variant"` |
| 80 | |
| 81 | // list of C static library dependencies |
| 82 | Static_libs []string `android:"arch_variant"` |
| 83 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame^] | 84 | // crate name, required for modules which produce Rust libraries: rust_library, rust_ffi and SourceProvider |
| 85 | // modules which create library variants (rust_bindgen). This must be the expected extern crate name used in |
| 86 | // source, and is required to conform to an enforced format matching library output files (if the output file is |
| 87 | // lib<someName><suffix>, the crate_name property must be <someName>). |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 88 | Crate_name string `android:"arch_variant"` |
| 89 | |
| 90 | // list of features to enable for this crate |
| 91 | Features []string `android:"arch_variant"` |
| 92 | |
| 93 | // specific rust edition that should be used if the default version is not desired |
| 94 | Edition *string `android:"arch_variant"` |
| 95 | |
| 96 | // sets name of the output |
| 97 | Stem *string `android:"arch_variant"` |
| 98 | |
| 99 | // append to name of output |
| 100 | Suffix *string `android:"arch_variant"` |
| 101 | |
| 102 | // install to a subdirectory of the default install path for the module |
| 103 | Relative_install_path *string `android:"arch_variant"` |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 104 | |
| 105 | // whether to suppress inclusion of standard crates - defaults to false |
| 106 | No_stdlibs *bool |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | type baseCompiler struct { |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 110 | Properties BaseCompilerProperties |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 111 | coverageFile android.Path //rustc generates a single gcno file |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 112 | |
| 113 | // Install related |
| 114 | dir string |
| 115 | dir64 string |
| 116 | subDir string |
| 117 | relative string |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 118 | path android.InstallPath |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 119 | location installLocation |
Ivan Lozano | 8a23fa4 | 2020-06-16 10:26:57 -0400 | [diff] [blame] | 120 | |
| 121 | coverageOutputZipFile android.OptionalPath |
| 122 | unstrippedOutputFile android.Path |
| 123 | distFile android.OptionalPath |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Ivan Lozano | 26ecd6c | 2020-07-31 13:40:31 -0400 | [diff] [blame^] | 126 | func (compiler *baseCompiler) Disabled() bool { |
| 127 | return false |
| 128 | } |
| 129 | |
| 130 | func (compiler *baseCompiler) SetDisabled() { |
| 131 | panic("baseCompiler does not implement SetDisabled()") |
| 132 | } |
| 133 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 134 | func (compiler *baseCompiler) coverageOutputZipPath() android.OptionalPath { |
| 135 | panic("baseCompiler does not implement coverageOutputZipPath()") |
| 136 | } |
| 137 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 138 | var _ compiler = (*baseCompiler)(nil) |
| 139 | |
Chih-Hung Hsieh | 9a4a7ba | 2019-12-12 19:36:05 -0800 | [diff] [blame] | 140 | func (compiler *baseCompiler) inData() bool { |
| 141 | return compiler.location == InstallInData |
| 142 | } |
| 143 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 144 | func (compiler *baseCompiler) compilerProps() []interface{} { |
| 145 | return []interface{}{&compiler.Properties} |
| 146 | } |
| 147 | |
| 148 | func (compiler *baseCompiler) featuresToFlags(features []string) []string { |
| 149 | flags := []string{} |
| 150 | for _, feature := range features { |
| 151 | flags = append(flags, "--cfg 'feature=\""+feature+"\"'") |
| 152 | } |
| 153 | return flags |
| 154 | } |
| 155 | |
| 156 | func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags) Flags { |
| 157 | |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 158 | if !Bool(compiler.Properties.No_lint) { |
| 159 | flags.RustFlags = append(flags.RustFlags, config.RustcLintsForDir(ctx.ModuleDir())) |
Chih-Hung Hsieh | efdd7ac | 2019-09-26 18:59:27 -0700 | [diff] [blame] | 160 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 161 | flags.RustFlags = append(flags.RustFlags, compiler.Properties.Flags...) |
| 162 | flags.RustFlags = append(flags.RustFlags, compiler.featuresToFlags(compiler.Properties.Features)...) |
Chih-Hung Hsieh | 961a30c | 2019-10-03 09:47:06 -0700 | [diff] [blame] | 163 | flags.RustFlags = append(flags.RustFlags, "--edition="+getEdition(compiler)) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 164 | flags.LinkFlags = append(flags.LinkFlags, compiler.Properties.Ld_flags...) |
Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 165 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, config.GlobalRustFlags...) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 166 | flags.GlobalRustFlags = append(flags.GlobalRustFlags, ctx.toolchain().ToolchainRustFlags()) |
| 167 | flags.GlobalLinkFlags = append(flags.GlobalLinkFlags, ctx.toolchain().ToolchainLinkFlags()) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 168 | |
| 169 | if ctx.Host() && !ctx.Windows() { |
| 170 | rpath_prefix := `\$$ORIGIN/` |
| 171 | if ctx.Darwin() { |
| 172 | rpath_prefix = "@loader_path/" |
| 173 | } |
| 174 | |
| 175 | var rpath string |
| 176 | if ctx.toolchain().Is64Bit() { |
| 177 | rpath = "lib64" |
| 178 | } else { |
| 179 | rpath = "lib" |
| 180 | } |
| 181 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+rpath) |
| 182 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-rpath,"+rpath_prefix+"../"+rpath) |
| 183 | } |
| 184 | |
| 185 | return flags |
| 186 | } |
| 187 | |
| 188 | func (compiler *baseCompiler) compile(ctx ModuleContext, flags Flags, deps PathDeps) android.Path { |
| 189 | panic(fmt.Errorf("baseCrater doesn't know how to crate things!")) |
| 190 | } |
| 191 | |
| 192 | func (compiler *baseCompiler) compilerDeps(ctx DepsContext, deps Deps) Deps { |
| 193 | deps.Rlibs = append(deps.Rlibs, compiler.Properties.Rlibs...) |
| 194 | deps.Dylibs = append(deps.Dylibs, compiler.Properties.Dylibs...) |
Matthew Maurer | 0f003b1 | 2020-06-29 14:34:06 -0700 | [diff] [blame] | 195 | deps.Rustlibs = append(deps.Rustlibs, compiler.Properties.Rustlibs...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 196 | deps.ProcMacros = append(deps.ProcMacros, compiler.Properties.Proc_macros...) |
| 197 | deps.StaticLibs = append(deps.StaticLibs, compiler.Properties.Static_libs...) |
| 198 | deps.SharedLibs = append(deps.SharedLibs, compiler.Properties.Shared_libs...) |
| 199 | |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 200 | if !Bool(compiler.Properties.No_stdlibs) { |
| 201 | for _, stdlib := range config.Stdlibs { |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 202 | // If we're building for the primary host target, use the compiler's stdlibs |
| 203 | if ctx.Host() && ctx.TargetPrimary() { |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 204 | stdlib = stdlib + "_" + ctx.toolchain().RustTriple() |
| 205 | } |
| 206 | |
Matthew Maurer | c761eec | 2020-06-25 00:47:46 -0700 | [diff] [blame] | 207 | deps.Rustlibs = append(deps.Rustlibs, stdlib) |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 210 | return deps |
| 211 | } |
| 212 | |
Ivan Lozano | 45901ed | 2020-07-24 16:05:01 -0400 | [diff] [blame] | 213 | func bionicDeps(deps Deps) Deps { |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 214 | deps.SharedLibs = append(deps.SharedLibs, "liblog") |
| 215 | deps.SharedLibs = append(deps.SharedLibs, "libc") |
| 216 | deps.SharedLibs = append(deps.SharedLibs, "libm") |
| 217 | deps.SharedLibs = append(deps.SharedLibs, "libdl") |
| 218 | |
| 219 | //TODO(b/141331117) libstd requires libgcc on Android |
| 220 | deps.StaticLibs = append(deps.StaticLibs, "libgcc") |
| 221 | |
| 222 | return deps |
| 223 | } |
| 224 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 225 | func (compiler *baseCompiler) crateName() string { |
| 226 | return compiler.Properties.Crate_name |
| 227 | } |
| 228 | |
Colin Cross | 70dda7e | 2019-10-01 22:05:35 -0700 | [diff] [blame] | 229 | func (compiler *baseCompiler) installDir(ctx ModuleContext) android.InstallPath { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 230 | dir := compiler.dir |
| 231 | if ctx.toolchain().Is64Bit() && compiler.dir64 != "" { |
| 232 | dir = compiler.dir64 |
| 233 | } |
Ivan Lozano | d6fdca8 | 2020-04-07 12:30:33 -0400 | [diff] [blame] | 234 | if ctx.Target().NativeBridge == android.NativeBridgeEnabled { |
| 235 | dir = filepath.Join(dir, ctx.Target().NativeBridgeRelativePath) |
| 236 | } |
| 237 | if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 238 | dir = filepath.Join(dir, ctx.Arch().ArchType.String()) |
| 239 | } |
| 240 | return android.PathForModuleInstall(ctx, dir, compiler.subDir, |
| 241 | compiler.relativeInstallPath(), compiler.relative) |
| 242 | } |
| 243 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 244 | func (compiler *baseCompiler) nativeCoverage() bool { |
| 245 | return false |
| 246 | } |
| 247 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 248 | func (compiler *baseCompiler) install(ctx ModuleContext, file android.Path) { |
| 249 | compiler.path = ctx.InstallFile(compiler.installDir(ctx), file.Base(), file) |
| 250 | } |
| 251 | |
| 252 | func (compiler *baseCompiler) getStem(ctx ModuleContext) string { |
| 253 | return compiler.getStemWithoutSuffix(ctx) + String(compiler.Properties.Suffix) |
| 254 | } |
| 255 | |
| 256 | func (compiler *baseCompiler) getStemWithoutSuffix(ctx BaseModuleContext) string { |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 257 | stem := ctx.ModuleName() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 258 | if String(compiler.Properties.Stem) != "" { |
| 259 | stem = String(compiler.Properties.Stem) |
| 260 | } |
| 261 | |
| 262 | return stem |
| 263 | } |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 264 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 265 | func (compiler *baseCompiler) relativeInstallPath() string { |
| 266 | return String(compiler.Properties.Relative_install_path) |
| 267 | } |
| 268 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 269 | // Returns the Path for the main source file along with Paths for generated source files from modules listed in srcs. |
Chih-Hung Hsieh | bbd25ae | 2020-05-15 17:36:30 -0700 | [diff] [blame] | 270 | func srcPathFromModuleSrcs(ctx ModuleContext, srcs []string) (android.Path, android.Paths) { |
| 271 | // The srcs can contain strings with prefix ":". |
| 272 | // They are dependent modules of this module, with android.SourceDepTag. |
| 273 | // They are not the main source file compiled by rustc. |
| 274 | numSrcs := 0 |
| 275 | srcIndex := 0 |
| 276 | for i, s := range srcs { |
| 277 | if android.SrcIsModule(s) == "" { |
| 278 | numSrcs++ |
| 279 | srcIndex = i |
| 280 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 281 | } |
Chih-Hung Hsieh | bbd25ae | 2020-05-15 17:36:30 -0700 | [diff] [blame] | 282 | if numSrcs != 1 { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 283 | ctx.PropertyErrorf("srcs", incorrectSourcesError) |
Chih-Hung Hsieh | bbd25ae | 2020-05-15 17:36:30 -0700 | [diff] [blame] | 284 | } |
| 285 | if srcIndex != 0 { |
| 286 | ctx.PropertyErrorf("srcs", "main source file must be the first in srcs") |
| 287 | } |
| 288 | paths := android.PathsForModuleSrc(ctx, srcs) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 289 | return paths[srcIndex], paths[1:] |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 290 | } |