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 | "strings" |
| 19 | |
| 20 | "github.com/google/blueprint" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc") |
| 27 | rustc = pctx.AndroidStaticRule("rustc", |
| 28 | blueprint.RuleParams{ |
| 29 | Command: "$rustcCmd " + |
| 30 | "-C linker=${config.RustLinker} " + |
| 31 | "-C link-args=\"${config.RustLinkerArgs} ${linkFlags}\" " + |
| 32 | "-o $out $in ${libFlags} $rustcFlags " + |
| 33 | "&& $rustcCmd --emit=dep-info -o $out.d $in ${libFlags} $rustcFlags", |
| 34 | CommandDeps: []string{"$rustcCmd"}, |
| 35 | Depfile: "$out.d", |
| 36 | Deps: blueprint.DepsGCC, // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633 |
| 37 | }, |
| 38 | "rustcFlags", "linkFlags", "libFlags") |
| 39 | ) |
| 40 | |
| 41 | func init() { |
| 42 | |
| 43 | } |
| 44 | |
| 45 | func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) { |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame^] | 46 | transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "bin", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) { |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame^] | 50 | transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "rlib", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) { |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame^] | 54 | transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "dylib", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, outputFile android.WritablePath, includeDirs []string) { |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame^] | 58 | transformSrctoCrate(ctx, mainSrc, deps.RLibs, deps.DyLibs, deps.ProcMacros, deps.StaticLibs, deps.SharedLibs, flags, outputFile, "proc-macro", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func rustLibsToPaths(libs RustLibraries) android.Paths { |
| 62 | var paths android.Paths |
| 63 | for _, lib := range libs { |
| 64 | paths = append(paths, lib.Path) |
| 65 | } |
| 66 | return paths |
| 67 | } |
| 68 | |
| 69 | func transformSrctoCrate(ctx android.ModuleContext, main android.Path, |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame^] | 70 | rlibs, dylibs, proc_macros RustLibraries, static_libs, shared_libs android.Paths, flags Flags, outputFile android.WritablePath, crate_type string, includeDirs []string) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 71 | |
| 72 | var inputs android.Paths |
| 73 | var deps android.Paths |
| 74 | var libFlags, rustcFlags []string |
| 75 | crate_name := ctx.(ModuleContext).CrateName() |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame^] | 76 | targetTriple := ctx.(ModuleContext).toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 77 | |
| 78 | inputs = append(inputs, main) |
| 79 | |
| 80 | // Collect rustc flags |
| 81 | rustcFlags = append(rustcFlags, flags.GlobalFlags...) |
| 82 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
| 83 | rustcFlags = append(rustcFlags, "--crate-type="+crate_type) |
| 84 | rustcFlags = append(rustcFlags, "--crate-name="+crate_name) |
| 85 | if targetTriple != "" { |
| 86 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
| 87 | } |
| 88 | |
| 89 | // Collect library/crate flags |
| 90 | for _, lib := range rlibs { |
| 91 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 92 | } |
| 93 | for _, lib := range dylibs { |
| 94 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 95 | } |
| 96 | for _, proc_macro := range proc_macros { |
| 97 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
| 98 | } |
| 99 | |
| 100 | for _, path := range includeDirs { |
| 101 | libFlags = append(libFlags, "-L "+path) |
| 102 | } |
| 103 | |
| 104 | // Collect dependencies |
| 105 | deps = append(deps, rustLibsToPaths(rlibs)...) |
| 106 | deps = append(deps, rustLibsToPaths(dylibs)...) |
| 107 | deps = append(deps, rustLibsToPaths(proc_macros)...) |
| 108 | deps = append(deps, static_libs...) |
| 109 | deps = append(deps, shared_libs...) |
| 110 | |
| 111 | ctx.Build(pctx, android.BuildParams{ |
| 112 | Rule: rustc, |
| 113 | Description: "rustc " + main.Rel(), |
| 114 | Output: outputFile, |
| 115 | Inputs: inputs, |
| 116 | Implicits: deps, |
| 117 | Args: map[string]string{ |
| 118 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 119 | "linkFlags": strings.Join(flags.LinkFlags, " "), |
| 120 | "libFlags": strings.Join(libFlags, " "), |
| 121 | }, |
| 122 | }) |
| 123 | |
| 124 | } |