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} " + |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 31 | "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " + |
Chih-Hung Hsieh | 885f1c6 | 2019-09-29 22:38:31 -0700 | [diff] [blame] | 32 | "--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | CommandDeps: []string{"$rustcCmd"}, |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 34 | // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633 |
| 35 | Deps: blueprint.DepsGCC, |
| 36 | Depfile: "$out.d", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 37 | }, |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 38 | "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | func init() { |
| 42 | |
| 43 | } |
| 44 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 45 | func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
| 46 | outputFile android.WritablePath, includeDirs []string) { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 47 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
| 48 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 49 | transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 52 | func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
| 53 | outputFile android.WritablePath, includeDirs []string) { |
| 54 | transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 57 | func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
| 58 | outputFile android.WritablePath, includeDirs []string) { |
| 59 | transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 62 | func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
| 63 | outputFile android.WritablePath, includeDirs []string) { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 64 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 65 | transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 68 | func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
| 69 | outputFile android.WritablePath, includeDirs []string) { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 70 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 71 | transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 74 | func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, |
| 75 | flags Flags, outputFile android.WritablePath, includeDirs []string) { |
| 76 | transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | func rustLibsToPaths(libs RustLibraries) android.Paths { |
| 80 | var paths android.Paths |
| 81 | for _, lib := range libs { |
| 82 | paths = append(paths, lib.Path) |
| 83 | } |
| 84 | return paths |
| 85 | } |
| 86 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 87 | func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags, |
| 88 | outputFile android.WritablePath, crate_type string, includeDirs []string) { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 89 | |
| 90 | var inputs android.Paths |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 91 | var implicits android.Paths |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 92 | var libFlags, rustcFlags, linkFlags []string |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 93 | crate_name := ctx.(ModuleContext).CrateName() |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 94 | targetTriple := ctx.(ModuleContext).toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 95 | |
| 96 | inputs = append(inputs, main) |
| 97 | |
| 98 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 99 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 100 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
| 101 | rustcFlags = append(rustcFlags, "--crate-type="+crate_type) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 102 | if crate_name != "" { |
| 103 | rustcFlags = append(rustcFlags, "--crate-name="+crate_name) |
| 104 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 105 | if targetTriple != "" { |
| 106 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 107 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 108 | } |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 109 | // TODO once we have static libraries in the host prebuilt .bp, this |
| 110 | // should be unconditionally added. |
| 111 | if !ctx.Host() { |
| 112 | // If we're on a device build, do not use an implicit sysroot |
| 113 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 114 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 115 | // Collect linker flags |
| 116 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 117 | linkFlags = append(linkFlags, flags.LinkFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 118 | |
| 119 | // Collect library/crate flags |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 120 | for _, lib := range deps.RLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 121 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 122 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 123 | for _, lib := range deps.DyLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 124 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 125 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 126 | for _, proc_macro := range deps.ProcMacros { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 127 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
| 128 | } |
| 129 | |
| 130 | for _, path := range includeDirs { |
| 131 | libFlags = append(libFlags, "-L "+path) |
| 132 | } |
| 133 | |
| 134 | // Collect dependencies |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 135 | implicits = append(implicits, rustLibsToPaths(deps.RLibs)...) |
| 136 | implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...) |
| 137 | implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...) |
| 138 | implicits = append(implicits, deps.StaticLibs...) |
| 139 | implicits = append(implicits, deps.SharedLibs...) |
| 140 | if deps.CrtBegin.Valid() { |
| 141 | implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path()) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 142 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 143 | |
| 144 | ctx.Build(pctx, android.BuildParams{ |
| 145 | Rule: rustc, |
| 146 | Description: "rustc " + main.Rel(), |
| 147 | Output: outputFile, |
| 148 | Inputs: inputs, |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 149 | Implicits: implicits, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 150 | Args: map[string]string{ |
| 151 | "rustcFlags": strings.Join(rustcFlags, " "), |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 152 | "linkFlags": strings.Join(linkFlags, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 153 | "libFlags": strings.Join(libFlags, " "), |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 154 | "crtBegin": deps.CrtBegin.String(), |
| 155 | "crtEnd": deps.CrtEnd.String(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 156 | }, |
| 157 | }) |
| 158 | |
| 159 | } |