blob: 654b1e6bf41d00ad901693c1d8cd18b79f0c1dd6 [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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
15package rust
16
17import (
18 "strings"
19
20 "github.com/google/blueprint"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040021 "github.com/google/blueprint/pathtools"
Ivan Lozanoffee3342019-08-27 12:03:00 -070022
23 "android/soong/android"
Ivan Lozanof3717ee2020-05-20 09:03:20 -040024 "android/soong/cc"
Ivan Lozanoffee3342019-08-27 12:03:00 -070025)
26
27var (
28 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
29 rustc = pctx.AndroidStaticRule("rustc",
30 blueprint.RuleParams{
Ivan Lozano43845682020-07-09 21:03:28 -040031 Command: "$envVars $rustcCmd " +
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "-C linker=${config.RustLinker} " +
Ivan Lozanof1c84332019-09-20 11:00:37 -070033 "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
Chih-Hung Hsieh29aa9fd2020-08-13 15:46:21 -070034 "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
35 " && grep \"^$out:\" $out.d.raw > $out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070036 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080037 // Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
Chih-Hung Hsieh29aa9fd2020-08-13 15:46:21 -070038 // Rustc emits unneeded dependency lines for the .d and input .rs files.
39 // Those extra lines cause ninja warning:
40 // "warning: depfile has multiple output paths"
41 // For ninja, we keep/grep only the dependency rule for the rust $out file.
Ivan Lozanob2df9f82019-11-05 12:16:46 -080042 Deps: blueprint.DepsGCC,
43 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070044 },
Ivan Lozano43845682020-07-09 21:03:28 -040045 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040046
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020047 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
48 clippyDriver = pctx.AndroidStaticRule("clippy",
49 blueprint.RuleParams{
Ivan Lozanobae62be2020-07-21 13:28:27 -040050 Command: "$envVars $clippyCmd " +
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020051 // Because clippy-driver uses rustc as backend, we need to have some output even during the linting.
52 // Use the metadata output as it has the smallest footprint.
53 "--emit metadata -o $out $in ${libFlags} " +
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020054 "$rustcFlags $clippyFlags",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020055 CommandDeps: []string{"$clippyCmd"},
56 },
Ivan Lozanobae62be2020-07-21 13:28:27 -040057 "rustcFlags", "libFlags", "clippyFlags", "envVars")
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020058
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040059 zip = pctx.AndroidStaticRule("zip",
60 blueprint.RuleParams{
61 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
62 CommandDeps: []string{"${SoongZipCmd}"},
63 Rspfile: "$out.rsp",
64 RspfileContent: "$in",
65 })
Ivan Lozano43845682020-07-09 21:03:28 -040066
67 cp = pctx.AndroidStaticRule("cp",
68 blueprint.RuleParams{
69 Command: "cp `cat $outDir.rsp` $outDir",
70 Rspfile: "${outDir}.rsp",
71 RspfileContent: "$in",
72 },
73 "outDir")
Ivan Lozanoffee3342019-08-27 12:03:00 -070074)
75
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040076type buildOutput struct {
77 outputFile android.Path
78 coverageFile android.Path
79}
Ivan Lozanoffee3342019-08-27 12:03:00 -070080
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040081func init() {
82 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070083}
84
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020085func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070086 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080087 flags.RustFlags = append(flags.RustFlags, "-C lto")
88
Matthew Maurerbb3add12020-06-25 09:34:12 -070089 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070090}
91
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020092func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070093 outputFile android.WritablePath, linkDirs []string) buildOutput {
94 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070095}
96
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020097func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070098 outputFile android.WritablePath, linkDirs []string) buildOutput {
99 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700100}
101
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200102func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700103 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -0800104 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700105 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700106}
107
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200108func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700109 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -0800110 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700111 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700112}
113
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200114func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700115 flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
116 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700117}
118
119func rustLibsToPaths(libs RustLibraries) android.Paths {
120 var paths android.Paths
121 for _, lib := range libs {
122 paths = append(paths, lib.Path)
123 }
124 return paths
125}
126
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200127func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700128 outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700129
130 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800131 var implicits android.Paths
Ivan Lozano43845682020-07-09 21:03:28 -0400132 var envVars []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400133 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700134 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400135 var implicitOutputs android.WritablePaths
136
137 output.outputFile = outputFile
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200138 crate_name := ctx.RustModule().CrateName()
139 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140
141 inputs = append(inputs, main)
142
143 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700144 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 rustcFlags = append(rustcFlags, flags.RustFlags...)
146 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700147 if crate_name != "" {
148 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
149 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 if targetTriple != "" {
151 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700152 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700154
155 // Suppress an implicit sysroot
156 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
157
Ivan Lozanof1c84332019-09-20 11:00:37 -0700158 // Collect linker flags
159 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
160 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161
162 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800163 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700164 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
165 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800166 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700167 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
168 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800169 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
171 }
172
Matthew Maurerbb3add12020-06-25 09:34:12 -0700173 for _, path := range linkDirs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700174 libFlags = append(libFlags, "-L "+path)
175 }
176
177 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800178 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
179 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
180 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
181 implicits = append(implicits, deps.StaticLibs...)
182 implicits = append(implicits, deps.SharedLibs...)
Ivan Lozano43845682020-07-09 21:03:28 -0400183
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800184 if deps.CrtBegin.Valid() {
185 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700186 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700187
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400188 if flags.Coverage {
189 var gcnoFile android.WritablePath
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400190 // Provide consistency with cc gcda output, see cc/builder.go init()
Ivan Lozano796fc4c2020-06-17 11:36:57 -0400191 profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400192
193 if outputFile.Ext() != "" {
194 gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400195 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
196 ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400197 } else {
198 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400199 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
200 ctx, outputFile.Base()+".gcda").String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400201 }
202
203 implicitOutputs = append(implicitOutputs, gcnoFile)
204 output.coverageFile = gcnoFile
205 }
206
Ivan Lozano43845682020-07-09 21:03:28 -0400207 if len(deps.SrcDeps) > 0 {
Ivan Lozano10735d92020-07-22 09:14:47 -0400208 genSubDir := "out/"
209 moduleGenDir := android.PathForModuleOut(ctx, genSubDir)
Ivan Lozano43845682020-07-09 21:03:28 -0400210 var outputs android.WritablePaths
211
212 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400213 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400214 ctx.PropertyErrorf("srcs",
215 "multiple source providers generate the same filename output: "+genSrc.Base())
216 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400217 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400218 }
219
220 ctx.Build(pctx, android.BuildParams{
221 Rule: cp,
222 Description: "cp " + moduleGenDir.Rel(),
223 Outputs: outputs,
224 Inputs: deps.SrcDeps,
225 Args: map[string]string{
226 "outDir": moduleGenDir.String(),
227 },
228 })
229 implicits = append(implicits, outputs.Paths()...)
230 envVars = append(envVars, "OUT_DIR=$$PWD/"+moduleGenDir.String())
231 }
232
Ivan Lozanobae62be2020-07-21 13:28:27 -0400233 if flags.Clippy {
234 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
235 ctx.Build(pctx, android.BuildParams{
236 Rule: clippyDriver,
237 Description: "clippy " + main.Rel(),
238 Output: clippyFile,
239 ImplicitOutputs: nil,
240 Inputs: inputs,
241 Implicits: implicits,
242 Args: map[string]string{
243 "rustcFlags": strings.Join(rustcFlags, " "),
244 "libFlags": strings.Join(libFlags, " "),
245 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
246 "envVars": strings.Join(envVars, " "),
247 },
248 })
249 // Declare the clippy build as an implicit dependency of the original crate.
250 implicits = append(implicits, clippyFile)
251 }
252
Ivan Lozanoffee3342019-08-27 12:03:00 -0700253 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400254 Rule: rustc,
255 Description: "rustc " + main.Rel(),
256 Output: outputFile,
257 ImplicitOutputs: implicitOutputs,
258 Inputs: inputs,
259 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700260 Args: map[string]string{
261 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700262 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700263 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800264 "crtBegin": deps.CrtBegin.String(),
265 "crtEnd": deps.CrtEnd.String(),
Ivan Lozano43845682020-07-09 21:03:28 -0400266 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700267 },
268 })
269
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400270 return output
271}
272
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200273func TransformCoverageFilesToZip(ctx ModuleContext,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400274 covFiles android.Paths, baseName string) android.OptionalPath {
275 if len(covFiles) > 0 {
276
277 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
278
279 ctx.Build(pctx, android.BuildParams{
280 Rule: zip,
281 Description: "zip " + outputFile.Base(),
282 Inputs: covFiles,
283 Output: outputFile,
284 })
285
286 return android.OptionalPathForPath(outputFile)
287 }
288 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700289}