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