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, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 73 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 74 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
| 75 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 76 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs) |
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, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 80 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
| 81 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs) |
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, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 85 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
| 86 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs) |
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, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 90 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 91 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 92 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs) |
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, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 96 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 97 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 98 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs) |
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, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 102 | flags Flags, outputFile android.WritablePath, includeDirs []string) buildOutput { |
| 103 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs) |
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, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 115 | outputFile android.WritablePath, crate_type string, includeDirs []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 | } |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 140 | // TODO(b/159718669): Once we have defined static libraries in the host |
| 141 | // prebuilts Blueprint file, sysroot should be unconditionally sourced |
| 142 | // from /dev/null. Explicitly set sysroot to avoid clippy-driver to |
| 143 | // internally call rustc. |
| 144 | if ctx.Host() && ctx.TargetPrimary() { |
| 145 | rustcFlags = append(rustcFlags, "--sysroot=${config.RustPath}") |
| 146 | } else { |
| 147 | // If we're not targeting the host primary arch, do not use a sysroot. |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 148 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 149 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 150 | // Collect linker flags |
| 151 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 152 | linkFlags = append(linkFlags, flags.LinkFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 153 | |
| 154 | // Collect library/crate flags |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 155 | for _, lib := range deps.RLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 156 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 157 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 158 | for _, lib := range deps.DyLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 159 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 160 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 161 | for _, proc_macro := range deps.ProcMacros { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 162 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
| 163 | } |
| 164 | |
| 165 | for _, path := range includeDirs { |
| 166 | libFlags = append(libFlags, "-L "+path) |
| 167 | } |
| 168 | |
| 169 | // Collect dependencies |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 170 | implicits = append(implicits, rustLibsToPaths(deps.RLibs)...) |
| 171 | implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...) |
| 172 | implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...) |
| 173 | implicits = append(implicits, deps.StaticLibs...) |
| 174 | implicits = append(implicits, deps.SharedLibs...) |
| 175 | if deps.CrtBegin.Valid() { |
| 176 | implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path()) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 177 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 178 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 179 | if flags.Coverage { |
| 180 | var gcnoFile android.WritablePath |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 181 | // Provide consistency with cc gcda output, see cc/builder.go init() |
Ivan Lozano | 796fc4c | 2020-06-17 11:36:57 -0400 | [diff] [blame] | 182 | profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 183 | |
| 184 | if outputFile.Ext() != "" { |
| 185 | gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno")) |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 186 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 187 | ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 188 | } else { |
| 189 | gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno") |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 190 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 191 | ctx, outputFile.Base()+".gcda").String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | implicitOutputs = append(implicitOutputs, gcnoFile) |
| 195 | output.coverageFile = gcnoFile |
| 196 | } |
| 197 | |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 198 | if flags.Clippy { |
| 199 | clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy") |
| 200 | ctx.Build(pctx, android.BuildParams{ |
| 201 | Rule: clippyDriver, |
| 202 | Description: "clippy " + main.Rel(), |
| 203 | Output: clippyFile, |
| 204 | ImplicitOutputs: nil, |
| 205 | Inputs: inputs, |
| 206 | Implicits: implicits, |
| 207 | Args: map[string]string{ |
| 208 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 209 | "libFlags": strings.Join(libFlags, " "), |
| 210 | "clippyFlags": strings.Join(flags.ClippyFlags, " "), |
| 211 | }, |
| 212 | }) |
| 213 | // Declare the clippy build as an implicit dependency of the original crate. |
| 214 | implicits = append(implicits, clippyFile) |
| 215 | } |
| 216 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 217 | ctx.Build(pctx, android.BuildParams{ |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 218 | Rule: rustc, |
| 219 | Description: "rustc " + main.Rel(), |
| 220 | Output: outputFile, |
| 221 | ImplicitOutputs: implicitOutputs, |
| 222 | Inputs: inputs, |
| 223 | Implicits: implicits, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 224 | Args: map[string]string{ |
| 225 | "rustcFlags": strings.Join(rustcFlags, " "), |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 226 | "linkFlags": strings.Join(linkFlags, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 227 | "libFlags": strings.Join(libFlags, " "), |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 228 | "crtBegin": deps.CrtBegin.String(), |
| 229 | "crtEnd": deps.CrtEnd.String(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 230 | }, |
| 231 | }) |
| 232 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 233 | return output |
| 234 | } |
| 235 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 236 | func TransformCoverageFilesToZip(ctx ModuleContext, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 237 | covFiles android.Paths, baseName string) android.OptionalPath { |
| 238 | if len(covFiles) > 0 { |
| 239 | |
| 240 | outputFile := android.PathForModuleOut(ctx, baseName+".zip") |
| 241 | |
| 242 | ctx.Build(pctx, android.BuildParams{ |
| 243 | Rule: zip, |
| 244 | Description: "zip " + outputFile.Base(), |
| 245 | Inputs: covFiles, |
| 246 | Output: outputFile, |
| 247 | }) |
| 248 | |
| 249 | return android.OptionalPathForPath(outputFile) |
| 250 | } |
| 251 | return android.OptionalPath{} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 252 | } |