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 | |
| 42 | zip = pctx.AndroidStaticRule("zip", |
| 43 | blueprint.RuleParams{ |
| 44 | Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp", |
| 45 | CommandDeps: []string{"${SoongZipCmd}"}, |
| 46 | Rspfile: "$out.rsp", |
| 47 | RspfileContent: "$in", |
| 48 | }) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 49 | ) |
| 50 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 51 | type buildOutput struct { |
| 52 | outputFile android.Path |
| 53 | coverageFile android.Path |
| 54 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 55 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 56 | func init() { |
| 57 | pctx.HostBinToolVariable("SoongZipCmd", "soong_zip") |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 60 | func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 61 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 62 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
| 63 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 64 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 67 | func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 68 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
| 69 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 72 | func TransformSrctoDylib(ctx android.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 { |
| 74 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 77 | func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 78 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 79 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 80 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 83 | func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 84 | outputFile android.WritablePath, includeDirs []string) buildOutput { |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 85 | flags.RustFlags = append(flags.RustFlags, "-C lto") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 86 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs) |
Ivan Lozano | 52767be | 2019-10-18 14:49:46 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 89 | func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 90 | flags Flags, outputFile android.WritablePath, includeDirs []string) buildOutput { |
| 91 | return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func rustLibsToPaths(libs RustLibraries) android.Paths { |
| 95 | var paths android.Paths |
| 96 | for _, lib := range libs { |
| 97 | paths = append(paths, lib.Path) |
| 98 | } |
| 99 | return paths |
| 100 | } |
| 101 | |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 102 | func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags, |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 103 | outputFile android.WritablePath, crate_type string, includeDirs []string) buildOutput { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | |
| 105 | var inputs android.Paths |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 106 | var implicits android.Paths |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 107 | var output buildOutput |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 108 | var libFlags, rustcFlags, linkFlags []string |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 109 | var implicitOutputs android.WritablePaths |
| 110 | |
| 111 | output.outputFile = outputFile |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 112 | crate_name := ctx.(ModuleContext).CrateName() |
Ivan Lozano | 5ca5ef6 | 2019-09-23 10:10:40 -0700 | [diff] [blame] | 113 | targetTriple := ctx.(ModuleContext).toolchain().RustTriple() |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 114 | |
| 115 | inputs = append(inputs, main) |
| 116 | |
| 117 | // Collect rustc flags |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 118 | rustcFlags = append(rustcFlags, flags.GlobalRustFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 119 | rustcFlags = append(rustcFlags, flags.RustFlags...) |
| 120 | rustcFlags = append(rustcFlags, "--crate-type="+crate_type) |
Ivan Lozano | ad8b18b | 2019-10-31 19:38:29 -0700 | [diff] [blame] | 121 | if crate_name != "" { |
| 122 | rustcFlags = append(rustcFlags, "--crate-name="+crate_name) |
| 123 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 124 | if targetTriple != "" { |
| 125 | rustcFlags = append(rustcFlags, "--target="+targetTriple) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 126 | linkFlags = append(linkFlags, "-target "+targetTriple) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 127 | } |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 128 | // TODO once we have static libraries in the host prebuilt .bp, this |
| 129 | // should be unconditionally added. |
Ivan Lozano | 9d1df10 | 2020-04-28 10:10:23 -0400 | [diff] [blame] | 130 | if !(ctx.Host() && ctx.TargetPrimary()) { |
| 131 | // If we're not targeting the host primary arch, do not use an implicit sysroot |
Matthew Maurer | 99020b0 | 2019-10-31 10:44:40 -0700 | [diff] [blame] | 132 | rustcFlags = append(rustcFlags, "--sysroot=/dev/null") |
| 133 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 134 | // Collect linker flags |
| 135 | linkFlags = append(linkFlags, flags.GlobalLinkFlags...) |
| 136 | linkFlags = append(linkFlags, flags.LinkFlags...) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 137 | |
| 138 | // Collect library/crate flags |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 139 | for _, lib := range deps.RLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 140 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 141 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 142 | for _, lib := range deps.DyLibs { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 143 | libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String()) |
| 144 | } |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 145 | for _, proc_macro := range deps.ProcMacros { |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 146 | libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String()) |
| 147 | } |
| 148 | |
| 149 | for _, path := range includeDirs { |
| 150 | libFlags = append(libFlags, "-L "+path) |
| 151 | } |
| 152 | |
| 153 | // Collect dependencies |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 154 | implicits = append(implicits, rustLibsToPaths(deps.RLibs)...) |
| 155 | implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...) |
| 156 | implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...) |
| 157 | implicits = append(implicits, deps.StaticLibs...) |
| 158 | implicits = append(implicits, deps.SharedLibs...) |
| 159 | if deps.CrtBegin.Valid() { |
| 160 | implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path()) |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 161 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 162 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 163 | if flags.Coverage { |
| 164 | var gcnoFile android.WritablePath |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 165 | // Provide consistency with cc gcda output, see cc/builder.go init() |
Ivan Lozano | 796fc4c | 2020-06-17 11:36:57 -0400 | [diff] [blame^] | 166 | profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 167 | |
| 168 | if outputFile.Ext() != "" { |
| 169 | gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno")) |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 170 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 171 | ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 172 | } else { |
| 173 | gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno") |
Ivan Lozano | f3717ee | 2020-05-20 09:03:20 -0400 | [diff] [blame] | 174 | rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut( |
| 175 | ctx, outputFile.Base()+".gcda").String()) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | implicitOutputs = append(implicitOutputs, gcnoFile) |
| 179 | output.coverageFile = gcnoFile |
| 180 | } |
| 181 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 182 | ctx.Build(pctx, android.BuildParams{ |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 183 | Rule: rustc, |
| 184 | Description: "rustc " + main.Rel(), |
| 185 | Output: outputFile, |
| 186 | ImplicitOutputs: implicitOutputs, |
| 187 | Inputs: inputs, |
| 188 | Implicits: implicits, |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 189 | Args: map[string]string{ |
| 190 | "rustcFlags": strings.Join(rustcFlags, " "), |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 191 | "linkFlags": strings.Join(linkFlags, " "), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 192 | "libFlags": strings.Join(libFlags, " "), |
Ivan Lozano | b2df9f8 | 2019-11-05 12:16:46 -0800 | [diff] [blame] | 193 | "crtBegin": deps.CrtBegin.String(), |
| 194 | "crtEnd": deps.CrtEnd.String(), |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 195 | }, |
| 196 | }) |
| 197 | |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 198 | return output |
| 199 | } |
| 200 | |
| 201 | func TransformCoverageFilesToZip(ctx android.ModuleContext, |
| 202 | covFiles android.Paths, baseName string) android.OptionalPath { |
| 203 | if len(covFiles) > 0 { |
| 204 | |
| 205 | outputFile := android.PathForModuleOut(ctx, baseName+".zip") |
| 206 | |
| 207 | ctx.Build(pctx, android.BuildParams{ |
| 208 | Rule: zip, |
| 209 | Description: "zip " + outputFile.Base(), |
| 210 | Inputs: covFiles, |
| 211 | Output: outputFile, |
| 212 | }) |
| 213 | |
| 214 | return android.OptionalPathForPath(outputFile) |
| 215 | } |
| 216 | return android.OptionalPath{} |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 217 | } |