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" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 21 | "github.com/google/blueprint/pathtools" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 22 | |
| 23 | "android/soong/android" |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 24 | "android/soong/cc" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc") |
| 29 | rustc = pctx.AndroidStaticRule("rustc", |
| 30 | blueprint.RuleParams{ |
| 31 | Command: "$rustcCmd " + |
| 32 | "-C linker=${config.RustLinker} " + |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 33 | "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " + |
Chih-Hung Hsieh | 885f1c6 | 2019-09-29 22:38:31 -0700 | [diff] [blame] | 34 | "--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 35 | CommandDeps: []string{"$rustcCmd"}, |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 36 | // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633 |
| 37 | Deps: blueprint.DepsGCC, |
| 38 | Depfile: "$out.d", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 39 | }, |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 40 | "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 41 | |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 42 | _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver") |
| 43 | clippyDriver = pctx.AndroidStaticRule("clippy", |
| 44 | blueprint.RuleParams{ |
| 45 | Command: "$clippyCmd " + |
| 46 | // Because clippy-driver uses rustc as backend, we need to have some output even during the linting. |
| 47 | // Use the metadata output as it has the smallest footprint. |
| 48 | "--emit metadata -o $out $in ${libFlags} " + |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 49 | "$rustcFlags $clippyFlags", |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 50 | CommandDeps: []string{"$clippyCmd"}, |
| 51 | }, |
| 52 | "rustcFlags", "libFlags", "clippyFlags") |
| 53 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 54 | zip = pctx.AndroidStaticRule("zip", |
| 55 | blueprint.RuleParams{ |
| 56 | Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp", |
| 57 | CommandDeps: []string{"${SoongZipCmd}"}, |
| 58 | Rspfile: "$out.rsp", |
| 59 | RspfileContent: "$in", |
| 60 | }) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 61 | ) |
| 62 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 63 | type buildOutput struct { |
| 64 | outputFile android.Path |
| 65 | coverageFile android.Path |
| 66 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 67 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 68 | func init() { |
| 69 | pctx.HostBinToolVariable("SoongZipCmd", "soong_zip") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 72 | func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 73 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 74 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
| 75 | |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 76 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 79 | func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 80 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
| 81 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 84 | func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 85 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
| 86 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 89 | func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 90 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 91 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 92 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 95 | func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 96 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 97 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 98 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 101 | func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 102 | flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput { |
| 103 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | func rustLibsToPaths(libs RustLibraries) android.Paths { |
| 107 | var paths android.Paths |
| 108 | for _, lib := range libs { |
| 109 | paths = append(paths, lib.Path) |
| 110 | } |
| 111 | return paths |
| 112 | } |
| 113 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 114 | func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 115 | outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 116 | |
| 117 | var inputs android.Paths |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 118 | var implicits android.Paths |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 119 | var output buildOutput |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 120 | var libFlags, rustcFlags, linkFlags []string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 121 | var implicitOutputs android.WritablePaths |
| 122 | |
| 123 | output.outputFile = outputFile |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 124 | crate_name := ctx.RustModule().CrateName() |
| 125 | targetTriple := ctx.toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 126 | |
| 127 | inputs = append(inputs, main) |
| 128 | |
| 129 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 130 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 131 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
| 132 | rustcFlags = append(rustcFlags, "--crate-type="+crate_type) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 133 | if crate_name != "" { |
| 134 | rustcFlags = append(rustcFlags, "--crate-name="+crate_name) |
| 135 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 136 | if targetTriple != "" { |
| 137 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 138 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 139 | } |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 140 | |
| 141 | // Suppress an implicit sysroot |
| 142 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 143 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 144 | // Collect linker flags |
| 145 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 146 | linkFlags = append(linkFlags, flags.LinkFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 147 | |
| 148 | // Collect library/crate flags |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 149 | for _, lib := range deps.RLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 150 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 151 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 152 | for _, lib := range deps.DyLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 153 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 154 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 155 | for _, proc_macro := range deps.ProcMacros { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 156 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
| 157 | } |
| 158 | |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame^] | 159 | for _, path := range linkDirs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 160 | libFlags = append(libFlags, "-L "+path) |
| 161 | } |
| 162 | |
| 163 | // Collect dependencies |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 164 | implicits = append(implicits, rustLibsToPaths(deps.RLibs)...) |
| 165 | implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...) |
| 166 | implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...) |
| 167 | implicits = append(implicits, deps.StaticLibs...) |
| 168 | implicits = append(implicits, deps.SharedLibs...) |
| 169 | if deps.CrtBegin.Valid() { |
| 170 | implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path()) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 171 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 172 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 173 | if flags.Coverage { |
| 174 | var gcnoFile android.WritablePath |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 175 | // Provide consistency with cc gcda output, see cc/builder.go init() |
Ivan Lozano | 796fc4c | 2020-06-17 11:36:57 -0400 | [diff] [blame] | 176 | profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 177 | |
| 178 | if outputFile.Ext() != "" { |
| 179 | gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno")) |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 180 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 181 | ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 182 | } else { |
| 183 | gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno") |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 184 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 185 | ctx, outputFile.Base()+".gcda").String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | implicitOutputs = append(implicitOutputs, gcnoFile) |
| 189 | output.coverageFile = gcnoFile |
| 190 | } |
| 191 | |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 192 | if flags.Clippy { |
| 193 | clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy") |
| 194 | ctx.Build(pctx, android.BuildParams{ |
| 195 | Rule: clippyDriver, |
| 196 | Description: "clippy " + main.Rel(), |
| 197 | Output: clippyFile, |
| 198 | ImplicitOutputs: nil, |
| 199 | Inputs: inputs, |
| 200 | Implicits: implicits, |
| 201 | Args: map[string]string{ |
| 202 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 203 | "libFlags": strings.Join(libFlags, " "), |
| 204 | "clippyFlags": strings.Join(flags.ClippyFlags, " "), |
| 205 | }, |
| 206 | }) |
| 207 | // Declare the clippy build as an implicit dependency of the original crate. |
| 208 | implicits = append(implicits, clippyFile) |
| 209 | } |
| 210 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 211 | ctx.Build(pctx, android.BuildParams{ |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 212 | Rule: rustc, |
| 213 | Description: "rustc " + main.Rel(), |
| 214 | Output: outputFile, |
| 215 | ImplicitOutputs: implicitOutputs, |
| 216 | Inputs: inputs, |
| 217 | Implicits: implicits, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 218 | Args: map[string]string{ |
| 219 | "rustcFlags": strings.Join(rustcFlags, " "), |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 220 | "linkFlags": strings.Join(linkFlags, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 221 | "libFlags": strings.Join(libFlags, " "), |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 222 | "crtBegin": deps.CrtBegin.String(), |
| 223 | "crtEnd": deps.CrtEnd.String(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 224 | }, |
| 225 | }) |
| 226 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 227 | return output |
| 228 | } |
| 229 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 230 | func TransformCoverageFilesToZip(ctx ModuleContext, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 231 | covFiles android.Paths, baseName string) android.OptionalPath { |
| 232 | if len(covFiles) > 0 { |
| 233 | |
| 234 | outputFile := android.PathForModuleOut(ctx, baseName+".zip") |
| 235 | |
| 236 | ctx.Build(pctx, android.BuildParams{ |
| 237 | Rule: zip, |
| 238 | Description: "zip " + outputFile.Base(), |
| 239 | Inputs: covFiles, |
| 240 | Output: outputFile, |
| 241 | }) |
| 242 | |
| 243 | return android.OptionalPathForPath(outputFile) |
| 244 | } |
| 245 | return android.OptionalPath{} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 246 | } |