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 ( |
Ivan Lozano | 1776a2a | 2020-11-11 10:59:52 -0500 | [diff] [blame^] | 18 | "path/filepath" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 22 | "github.com/google/blueprint/pathtools" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 23 | |
| 24 | "android/soong/android" |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 25 | "android/soong/cc" |
Thiébaud Weksteen | 71512f3 | 2020-11-03 15:17:51 +0100 | [diff] [blame] | 26 | "android/soong/rust/config" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var ( |
| 30 | _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc") |
| 31 | rustc = pctx.AndroidStaticRule("rustc", |
| 32 | blueprint.RuleParams{ |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 33 | Command: "$envVars $rustcCmd " + |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 34 | "-C linker=${config.RustLinker} " + |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 35 | "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " + |
Chih-Hung Hsieh | 29aa9fd | 2020-08-13 15:46:21 -0700 | [diff] [blame] | 36 | "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" + |
| 37 | " && grep \"^$out:\" $out.d.raw > $out.d", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 38 | CommandDeps: []string{"$rustcCmd"}, |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 39 | // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633 |
Chih-Hung Hsieh | 29aa9fd | 2020-08-13 15:46:21 -0700 | [diff] [blame] | 40 | // Rustc emits unneeded dependency lines for the .d and input .rs files. |
| 41 | // Those extra lines cause ninja warning: |
| 42 | // "warning: depfile has multiple output paths" |
| 43 | // For ninja, we keep/grep only the dependency rule for the rust $out file. |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 44 | Deps: blueprint.DepsGCC, |
| 45 | Depfile: "$out.d", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 46 | }, |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 47 | "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 48 | |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 49 | _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver") |
| 50 | clippyDriver = pctx.AndroidStaticRule("clippy", |
| 51 | blueprint.RuleParams{ |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 52 | Command: "$envVars $clippyCmd " + |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 53 | // Because clippy-driver uses rustc as backend, we need to have some output even during the linting. |
| 54 | // Use the metadata output as it has the smallest footprint. |
| 55 | "--emit metadata -o $out $in ${libFlags} " + |
Thiébaud Weksteen | 8e46efa | 2020-06-30 21:43:35 +0200 | [diff] [blame] | 56 | "$rustcFlags $clippyFlags", |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 57 | CommandDeps: []string{"$clippyCmd"}, |
| 58 | }, |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 59 | "rustcFlags", "libFlags", "clippyFlags", "envVars") |
Thiébaud Weksteen | 92f703b | 2020-06-22 13:28:02 +0200 | [diff] [blame] | 60 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 61 | zip = pctx.AndroidStaticRule("zip", |
| 62 | blueprint.RuleParams{ |
| 63 | Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp", |
| 64 | CommandDeps: []string{"${SoongZipCmd}"}, |
| 65 | Rspfile: "$out.rsp", |
| 66 | RspfileContent: "$in", |
| 67 | }) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 68 | |
| 69 | cp = pctx.AndroidStaticRule("cp", |
| 70 | blueprint.RuleParams{ |
| 71 | Command: "cp `cat $outDir.rsp` $outDir", |
| 72 | Rspfile: "${outDir}.rsp", |
| 73 | RspfileContent: "$in", |
| 74 | }, |
| 75 | "outDir") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 76 | ) |
| 77 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 78 | type buildOutput struct { |
| 79 | outputFile android.Path |
| 80 | coverageFile android.Path |
| 81 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 82 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 83 | func init() { |
| 84 | pctx.HostBinToolVariable("SoongZipCmd", "soong_zip") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 87 | func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 88 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 89 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
| 90 | |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 91 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 94 | func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 95 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
| 96 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 99 | func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 100 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
| 101 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 104 | func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 105 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 106 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 107 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 110 | func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 111 | outputFile android.WritablePath, linkDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 112 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 113 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 116 | func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 117 | flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput { |
| 118 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | func rustLibsToPaths(libs RustLibraries) android.Paths { |
| 122 | var paths android.Paths |
| 123 | for _, lib := range libs { |
| 124 | paths = append(paths, lib.Path) |
| 125 | } |
| 126 | return paths |
| 127 | } |
| 128 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 129 | func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags, |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 130 | outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 131 | |
| 132 | var inputs android.Paths |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 133 | var implicits android.Paths |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 134 | var envVars []string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 135 | var output buildOutput |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 136 | var libFlags, rustcFlags, linkFlags []string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 137 | var implicitOutputs android.WritablePaths |
| 138 | |
| 139 | output.outputFile = outputFile |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 140 | crate_name := ctx.RustModule().CrateName() |
| 141 | targetTriple := ctx.toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 142 | |
Thiébaud Weksteen | 71512f3 | 2020-11-03 15:17:51 +0100 | [diff] [blame] | 143 | // libstd requires a specific environment variable to be set. This is |
| 144 | // not officially documented and may be removed in the future. See |
| 145 | // https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866. |
| 146 | if crate_name == "std" { |
| 147 | envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType]) |
| 148 | } |
| 149 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 150 | inputs = append(inputs, main) |
| 151 | |
| 152 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 153 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 154 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
| 155 | rustcFlags = append(rustcFlags, "--crate-type="+crate_type) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 156 | if crate_name != "" { |
| 157 | rustcFlags = append(rustcFlags, "--crate-name="+crate_name) |
| 158 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 159 | if targetTriple != "" { |
| 160 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 161 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 162 | } |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 163 | |
| 164 | // Suppress an implicit sysroot |
| 165 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 166 | |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 167 | // Collect linker flags |
| 168 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 169 | linkFlags = append(linkFlags, flags.LinkFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 170 | |
| 171 | // Collect library/crate flags |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 172 | for _, lib := range deps.RLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 173 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 174 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 175 | for _, lib := range deps.DyLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 176 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 177 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 178 | for _, proc_macro := range deps.ProcMacros { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 179 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
| 180 | } |
| 181 | |
Matthew Maurer | bb3add1 | 2020-06-25 09:34:12 -0700 | [diff] [blame] | 182 | for _, path := range linkDirs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 183 | libFlags = append(libFlags, "-L "+path) |
| 184 | } |
| 185 | |
| 186 | // Collect dependencies |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 187 | implicits = append(implicits, rustLibsToPaths(deps.RLibs)...) |
| 188 | implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...) |
| 189 | implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...) |
| 190 | implicits = append(implicits, deps.StaticLibs...) |
| 191 | implicits = append(implicits, deps.SharedLibs...) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 192 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 193 | if deps.CrtBegin.Valid() { |
| 194 | implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path()) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 195 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 196 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 197 | if flags.Coverage { |
| 198 | var gcnoFile android.WritablePath |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 199 | // Provide consistency with cc gcda output, see cc/builder.go init() |
Ivan Lozano | 796fc4c | 2020-06-17 11:36:57 -0400 | [diff] [blame] | 200 | profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 201 | |
| 202 | if outputFile.Ext() != "" { |
| 203 | gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno")) |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 204 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 205 | ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 206 | } else { |
| 207 | gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno") |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 208 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 209 | ctx, outputFile.Base()+".gcda").String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | implicitOutputs = append(implicitOutputs, gcnoFile) |
| 213 | output.coverageFile = gcnoFile |
| 214 | } |
| 215 | |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 216 | if len(deps.SrcDeps) > 0 { |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 217 | genSubDir := "out/" |
| 218 | moduleGenDir := android.PathForModuleOut(ctx, genSubDir) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 219 | var outputs android.WritablePaths |
| 220 | |
| 221 | for _, genSrc := range deps.SrcDeps { |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 222 | if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) { |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 223 | ctx.PropertyErrorf("srcs", |
| 224 | "multiple source providers generate the same filename output: "+genSrc.Base()) |
| 225 | } |
Ivan Lozano | 10735d9 | 2020-07-22 09:14:47 -0400 | [diff] [blame] | 226 | outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base())) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | ctx.Build(pctx, android.BuildParams{ |
| 230 | Rule: cp, |
| 231 | Description: "cp " + moduleGenDir.Rel(), |
| 232 | Outputs: outputs, |
| 233 | Inputs: deps.SrcDeps, |
| 234 | Args: map[string]string{ |
| 235 | "outDir": moduleGenDir.String(), |
| 236 | }, |
| 237 | }) |
| 238 | implicits = append(implicits, outputs.Paths()...) |
Ivan Lozano | 1776a2a | 2020-11-11 10:59:52 -0500 | [diff] [blame^] | 239 | |
| 240 | // We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this) |
| 241 | // assumes that paths are relative to the source file. |
| 242 | var outDirPrefix string |
| 243 | if !filepath.IsAbs(moduleGenDir.String()) { |
| 244 | // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/') |
| 245 | outDirPrefix = "$$PWD/" |
| 246 | } else { |
| 247 | // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything. |
| 248 | outDirPrefix = "" |
| 249 | } |
| 250 | envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String())) |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Ivan Lozano | bae62be | 2020-07-21 13:28:27 -0400 | [diff] [blame] | 253 | if flags.Clippy { |
| 254 | clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy") |
| 255 | ctx.Build(pctx, android.BuildParams{ |
| 256 | Rule: clippyDriver, |
| 257 | Description: "clippy " + main.Rel(), |
| 258 | Output: clippyFile, |
| 259 | ImplicitOutputs: nil, |
| 260 | Inputs: inputs, |
| 261 | Implicits: implicits, |
| 262 | Args: map[string]string{ |
| 263 | "rustcFlags": strings.Join(rustcFlags, " "), |
| 264 | "libFlags": strings.Join(libFlags, " "), |
| 265 | "clippyFlags": strings.Join(flags.ClippyFlags, " "), |
| 266 | "envVars": strings.Join(envVars, " "), |
| 267 | }, |
| 268 | }) |
| 269 | // Declare the clippy build as an implicit dependency of the original crate. |
| 270 | implicits = append(implicits, clippyFile) |
| 271 | } |
| 272 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 273 | ctx.Build(pctx, android.BuildParams{ |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 274 | Rule: rustc, |
| 275 | Description: "rustc " + main.Rel(), |
| 276 | Output: outputFile, |
| 277 | ImplicitOutputs: implicitOutputs, |
| 278 | Inputs: inputs, |
| 279 | Implicits: implicits, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 280 | Args: map[string]string{ |
| 281 | "rustcFlags": strings.Join(rustcFlags, " "), |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 282 | "linkFlags": strings.Join(linkFlags, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 283 | "libFlags": strings.Join(libFlags, " "), |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 284 | "crtBegin": deps.CrtBegin.String(), |
| 285 | "crtEnd": deps.CrtEnd.String(), |
Ivan Lozano | 4384568 | 2020-07-09 21:03:28 -0400 | [diff] [blame] | 286 | "envVars": strings.Join(envVars, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 287 | }, |
| 288 | }) |
| 289 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 290 | return output |
| 291 | } |
| 292 | |
Thiébaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 293 | func TransformCoverageFilesToZip(ctx ModuleContext, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 294 | covFiles android.Paths, baseName string) android.OptionalPath { |
| 295 | if len(covFiles) > 0 { |
| 296 | |
| 297 | outputFile := android.PathForModuleOut(ctx, baseName+".zip") |
| 298 | |
| 299 | ctx.Build(pctx, android.BuildParams{ |
| 300 | Rule: zip, |
| 301 | Description: "zip " + outputFile.Base(), |
| 302 | Inputs: covFiles, |
| 303 | Output: outputFile, |
| 304 | }) |
| 305 | |
| 306 | return android.OptionalPathForPath(outputFile) |
| 307 | } |
| 308 | return android.OptionalPath{} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 309 | } |