blob: 8ec2da2fa6aa9117c86f47b479b66deee7731378 [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 (
Ivan Lozano1776a2a2020-11-11 10:59:52 -050018 "path/filepath"
Ivan Lozanoffee3342019-08-27 12:03:00 -070019 "strings"
20
21 "github.com/google/blueprint"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040022 "github.com/google/blueprint/pathtools"
Ivan Lozanoffee3342019-08-27 12:03:00 -070023
24 "android/soong/android"
Ivan Lozanof3717ee2020-05-20 09:03:20 -040025 "android/soong/cc"
Thiébaud Weksteen71512f32020-11-03 15:17:51 +010026 "android/soong/rust/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070027)
28
29var (
30 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
31 rustc = pctx.AndroidStaticRule("rustc",
32 blueprint.RuleParams{
Ivan Lozano43845682020-07-09 21:03:28 -040033 Command: "$envVars $rustcCmd " +
Ivan Lozanoffee3342019-08-27 12:03:00 -070034 "-C linker=${config.RustLinker} " +
Ivan Lozanof1c84332019-09-20 11:00:37 -070035 "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
Chih-Hung Hsieh29aa9fd2020-08-13 15:46:21 -070036 "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
37 " && grep \"^$out:\" $out.d.raw > $out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070038 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080039 // 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 -070040 // 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 Lozanob2df9f82019-11-05 12:16:46 -080044 Deps: blueprint.DepsGCC,
45 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070046 },
Ivan Lozano43845682020-07-09 21:03:28 -040047 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040048
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020049 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
50 clippyDriver = pctx.AndroidStaticRule("clippy",
51 blueprint.RuleParams{
Ivan Lozanobae62be2020-07-21 13:28:27 -040052 Command: "$envVars $clippyCmd " +
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020053 // 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 Weksteen8e46efa2020-06-30 21:43:35 +020056 "$rustcFlags $clippyFlags",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020057 CommandDeps: []string{"$clippyCmd"},
58 },
Ivan Lozanobae62be2020-07-21 13:28:27 -040059 "rustcFlags", "libFlags", "clippyFlags", "envVars")
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 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 Lozano43845682020-07-09 21:03:28 -040068
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 Lozanoffee3342019-08-27 12:03:00 -070076)
77
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040078type buildOutput struct {
79 outputFile android.Path
80 coverageFile android.Path
81}
Ivan Lozanoffee3342019-08-27 12:03:00 -070082
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040083func init() {
84 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070085}
86
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020087func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070088 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080089 flags.RustFlags = append(flags.RustFlags, "-C lto")
90
Matthew Maurerbb3add12020-06-25 09:34:12 -070091 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070092}
93
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020094func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070095 outputFile android.WritablePath, linkDirs []string) buildOutput {
96 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070097}
98
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020099func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700100 outputFile android.WritablePath, linkDirs []string) buildOutput {
101 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700102}
103
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200104func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700105 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -0800106 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700107 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700108}
109
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200110func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700111 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -0800112 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700113 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700114}
115
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200116func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700117 flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
118 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700119}
120
121func 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 Weksteen1f7f70f2020-06-24 11:32:48 +0200129func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700130 outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131
132 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800133 var implicits android.Paths
Ivan Lozano43845682020-07-09 21:03:28 -0400134 var envVars []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400135 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700136 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400137 var implicitOutputs android.WritablePaths
138
139 output.outputFile = outputFile
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200140 crate_name := ctx.RustModule().CrateName()
141 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700142
Thiébaud Weksteen71512f32020-11-03 15:17:51 +0100143 // 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 Lozanoffee3342019-08-27 12:03:00 -0700150 inputs = append(inputs, main)
151
152 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700153 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700154 rustcFlags = append(rustcFlags, flags.RustFlags...)
155 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700156 if crate_name != "" {
157 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
158 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700159 if targetTriple != "" {
160 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700161 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700163
164 // Suppress an implicit sysroot
165 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
166
Ivan Lozanof1c84332019-09-20 11:00:37 -0700167 // Collect linker flags
168 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
169 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700170
171 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800172 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700173 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
174 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800175 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700176 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
177 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800178 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700179 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
180 }
181
Matthew Maurerbb3add12020-06-25 09:34:12 -0700182 for _, path := range linkDirs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700183 libFlags = append(libFlags, "-L "+path)
184 }
185
186 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800187 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 Lozano9d74a522020-12-01 09:25:22 -0500192 implicits = append(implicits, deps.srcProviderFiles...)
Ivan Lozano43845682020-07-09 21:03:28 -0400193
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800194 if deps.CrtBegin.Valid() {
195 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700196 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700197
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400198 if flags.Coverage {
199 var gcnoFile android.WritablePath
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400200 // Provide consistency with cc gcda output, see cc/builder.go init()
Ivan Lozano796fc4c2020-06-17 11:36:57 -0400201 profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400202
203 if outputFile.Ext() != "" {
Ivan Lozanod7ddf052020-11-16 15:21:43 -0500204 // rustc seems to split the output filename at the first '.' when determining the gcno filename
205 // so we need to do the same here.
206 gcnoFile = android.PathForModuleOut(ctx, strings.Split(outputFile.Base(), ".")[0]+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400207 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
208 ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400209 } else {
210 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400211 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
212 ctx, outputFile.Base()+".gcda").String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400213 }
214
215 implicitOutputs = append(implicitOutputs, gcnoFile)
216 output.coverageFile = gcnoFile
217 }
218
Ivan Lozano43845682020-07-09 21:03:28 -0400219 if len(deps.SrcDeps) > 0 {
Ivan Lozano10735d92020-07-22 09:14:47 -0400220 genSubDir := "out/"
221 moduleGenDir := android.PathForModuleOut(ctx, genSubDir)
Ivan Lozano43845682020-07-09 21:03:28 -0400222 var outputs android.WritablePaths
223
224 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400225 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400226 ctx.PropertyErrorf("srcs",
227 "multiple source providers generate the same filename output: "+genSrc.Base())
228 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400229 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400230 }
231
232 ctx.Build(pctx, android.BuildParams{
233 Rule: cp,
234 Description: "cp " + moduleGenDir.Rel(),
235 Outputs: outputs,
236 Inputs: deps.SrcDeps,
237 Args: map[string]string{
238 "outDir": moduleGenDir.String(),
239 },
240 })
241 implicits = append(implicits, outputs.Paths()...)
Ivan Lozano1776a2a2020-11-11 10:59:52 -0500242
243 // We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
244 // assumes that paths are relative to the source file.
245 var outDirPrefix string
246 if !filepath.IsAbs(moduleGenDir.String()) {
247 // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
248 outDirPrefix = "$$PWD/"
249 } else {
250 // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
251 outDirPrefix = ""
252 }
253 envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
Ivan Lozano43845682020-07-09 21:03:28 -0400254 }
255
Ivan Lozanobae62be2020-07-21 13:28:27 -0400256 if flags.Clippy {
257 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
258 ctx.Build(pctx, android.BuildParams{
259 Rule: clippyDriver,
260 Description: "clippy " + main.Rel(),
261 Output: clippyFile,
262 ImplicitOutputs: nil,
263 Inputs: inputs,
264 Implicits: implicits,
265 Args: map[string]string{
266 "rustcFlags": strings.Join(rustcFlags, " "),
267 "libFlags": strings.Join(libFlags, " "),
268 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
269 "envVars": strings.Join(envVars, " "),
270 },
271 })
272 // Declare the clippy build as an implicit dependency of the original crate.
273 implicits = append(implicits, clippyFile)
274 }
275
Ivan Lozanoffee3342019-08-27 12:03:00 -0700276 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400277 Rule: rustc,
278 Description: "rustc " + main.Rel(),
279 Output: outputFile,
280 ImplicitOutputs: implicitOutputs,
281 Inputs: inputs,
282 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700283 Args: map[string]string{
284 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700285 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700286 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800287 "crtBegin": deps.CrtBegin.String(),
288 "crtEnd": deps.CrtEnd.String(),
Ivan Lozano43845682020-07-09 21:03:28 -0400289 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700290 },
291 })
292
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400293 return output
294}
295
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200296func TransformCoverageFilesToZip(ctx ModuleContext,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400297 covFiles android.Paths, baseName string) android.OptionalPath {
298 if len(covFiles) > 0 {
299
300 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
301
302 ctx.Build(pctx, android.BuildParams{
303 Rule: zip,
304 Description: "zip " + outputFile.Base(),
305 Inputs: covFiles,
306 Output: outputFile,
307 })
308
309 return android.OptionalPathForPath(outputFile)
310 }
311 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700312}