blob: 8b5a2bb36a764a2a0c34deb29e778f917ad62509 [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 Hsieh885f1c62019-09-29 22:38:31 -070034 "--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags",
Ivan Lozanoffee3342019-08-27 12:03:00 -070035 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080036 // 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 Lozanoffee3342019-08-27 12:03:00 -070039 },
Ivan Lozano43845682020-07-09 21:03:28 -040040 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040041
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020042 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
43 clippyDriver = pctx.AndroidStaticRule("clippy",
44 blueprint.RuleParams{
Ivan Lozanobae62be2020-07-21 13:28:27 -040045 Command: "$envVars $clippyCmd " +
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020046 // 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 Weksteen8e46efa2020-06-30 21:43:35 +020049 "$rustcFlags $clippyFlags",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020050 CommandDeps: []string{"$clippyCmd"},
51 },
Ivan Lozanobae62be2020-07-21 13:28:27 -040052 "rustcFlags", "libFlags", "clippyFlags", "envVars")
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020053
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040054 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 Lozano43845682020-07-09 21:03:28 -040061
62 cp = pctx.AndroidStaticRule("cp",
63 blueprint.RuleParams{
64 Command: "cp `cat $outDir.rsp` $outDir",
65 Rspfile: "${outDir}.rsp",
66 RspfileContent: "$in",
67 },
68 "outDir")
Ivan Lozanoffee3342019-08-27 12:03:00 -070069)
70
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040071type buildOutput struct {
72 outputFile android.Path
73 coverageFile android.Path
74}
Ivan Lozanoffee3342019-08-27 12:03:00 -070075
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040076func init() {
77 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070078}
79
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020080func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070081 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080082 flags.RustFlags = append(flags.RustFlags, "-C lto")
83
Matthew Maurerbb3add12020-06-25 09:34:12 -070084 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070085}
86
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020087func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070088 outputFile android.WritablePath, linkDirs []string) buildOutput {
89 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070090}
91
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020092func TransformSrctoDylib(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, "dylib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070095}
96
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020097func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070098 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080099 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700100 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700101}
102
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200103func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700104 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -0800105 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700106 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700107}
108
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200109func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700110 flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
111 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112}
113
114func rustLibsToPaths(libs RustLibraries) android.Paths {
115 var paths android.Paths
116 for _, lib := range libs {
117 paths = append(paths, lib.Path)
118 }
119 return paths
120}
121
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200122func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700123 outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124
125 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800126 var implicits android.Paths
Ivan Lozano43845682020-07-09 21:03:28 -0400127 var envVars []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400128 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700129 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400130 var implicitOutputs android.WritablePaths
131
132 output.outputFile = outputFile
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200133 crate_name := ctx.RustModule().CrateName()
134 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700135
136 inputs = append(inputs, main)
137
138 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700139 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700140 rustcFlags = append(rustcFlags, flags.RustFlags...)
141 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700142 if crate_name != "" {
143 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
144 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700145 if targetTriple != "" {
146 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700147 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700148 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700149
150 // Suppress an implicit sysroot
151 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
152
Ivan Lozanof1c84332019-09-20 11:00:37 -0700153 // Collect linker flags
154 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
155 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156
157 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800158 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
160 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800161 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
163 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800164 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700165 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
166 }
167
Matthew Maurerbb3add12020-06-25 09:34:12 -0700168 for _, path := range linkDirs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700169 libFlags = append(libFlags, "-L "+path)
170 }
171
172 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800173 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
174 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
175 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
176 implicits = append(implicits, deps.StaticLibs...)
177 implicits = append(implicits, deps.SharedLibs...)
Ivan Lozano43845682020-07-09 21:03:28 -0400178
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800179 if deps.CrtBegin.Valid() {
180 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700181 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400183 if flags.Coverage {
184 var gcnoFile android.WritablePath
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400185 // Provide consistency with cc gcda output, see cc/builder.go init()
Ivan Lozano796fc4c2020-06-17 11:36:57 -0400186 profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400187
188 if outputFile.Ext() != "" {
189 gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400190 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
191 ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400192 } else {
193 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400194 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
195 ctx, outputFile.Base()+".gcda").String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400196 }
197
198 implicitOutputs = append(implicitOutputs, gcnoFile)
199 output.coverageFile = gcnoFile
200 }
201
Ivan Lozano43845682020-07-09 21:03:28 -0400202 if len(deps.SrcDeps) > 0 {
203 moduleGenDir := android.PathForModuleOut(ctx, "out/")
204 var outputs android.WritablePaths
205
206 for _, genSrc := range deps.SrcDeps {
207 if android.SuffixInList(outputs.Strings(), "out/"+genSrc.Base()) {
208 ctx.PropertyErrorf("srcs",
209 "multiple source providers generate the same filename output: "+genSrc.Base())
210 }
211 outputs = append(outputs, android.PathForModuleOut(ctx, "out/"+genSrc.Base()))
212 }
213
214 ctx.Build(pctx, android.BuildParams{
215 Rule: cp,
216 Description: "cp " + moduleGenDir.Rel(),
217 Outputs: outputs,
218 Inputs: deps.SrcDeps,
219 Args: map[string]string{
220 "outDir": moduleGenDir.String(),
221 },
222 })
223 implicits = append(implicits, outputs.Paths()...)
224 envVars = append(envVars, "OUT_DIR=$$PWD/"+moduleGenDir.String())
225 }
226
Ivan Lozanobae62be2020-07-21 13:28:27 -0400227 if flags.Clippy {
228 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
229 ctx.Build(pctx, android.BuildParams{
230 Rule: clippyDriver,
231 Description: "clippy " + main.Rel(),
232 Output: clippyFile,
233 ImplicitOutputs: nil,
234 Inputs: inputs,
235 Implicits: implicits,
236 Args: map[string]string{
237 "rustcFlags": strings.Join(rustcFlags, " "),
238 "libFlags": strings.Join(libFlags, " "),
239 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
240 "envVars": strings.Join(envVars, " "),
241 },
242 })
243 // Declare the clippy build as an implicit dependency of the original crate.
244 implicits = append(implicits, clippyFile)
245 }
246
Ivan Lozanoffee3342019-08-27 12:03:00 -0700247 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400248 Rule: rustc,
249 Description: "rustc " + main.Rel(),
250 Output: outputFile,
251 ImplicitOutputs: implicitOutputs,
252 Inputs: inputs,
253 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700254 Args: map[string]string{
255 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700256 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700257 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800258 "crtBegin": deps.CrtBegin.String(),
259 "crtEnd": deps.CrtEnd.String(),
Ivan Lozano43845682020-07-09 21:03:28 -0400260 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 },
262 })
263
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400264 return output
265}
266
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200267func TransformCoverageFilesToZip(ctx ModuleContext,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400268 covFiles android.Paths, baseName string) android.OptionalPath {
269 if len(covFiles) > 0 {
270
271 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
272
273 ctx.Build(pctx, android.BuildParams{
274 Rule: zip,
275 Description: "zip " + outputFile.Base(),
276 Inputs: covFiles,
277 Output: outputFile,
278 })
279
280 return android.OptionalPathForPath(outputFile)
281 }
282 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283}