blob: 3e082dc2f06077ee4199aaac465b3893f6a8194e [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"
Thiébaud Weksteen71512f32020-11-03 15:17:51 +010025 "android/soong/rust/config"
Ivan Lozanoffee3342019-08-27 12:03:00 -070026)
27
28var (
29 _ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
30 rustc = pctx.AndroidStaticRule("rustc",
31 blueprint.RuleParams{
Ivan Lozano43845682020-07-09 21:03:28 -040032 Command: "$envVars $rustcCmd " +
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 "-C linker=${config.RustLinker} " +
Ivan Lozanof1c84332019-09-20 11:00:37 -070034 "-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
Chih-Hung Hsieh29aa9fd2020-08-13 15:46:21 -070035 "--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
36 " && grep \"^$out:\" $out.d.raw > $out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070037 CommandDeps: []string{"$rustcCmd"},
Ivan Lozanob2df9f82019-11-05 12:16:46 -080038 // 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 -070039 // Rustc emits unneeded dependency lines for the .d and input .rs files.
40 // Those extra lines cause ninja warning:
41 // "warning: depfile has multiple output paths"
42 // For ninja, we keep/grep only the dependency rule for the rust $out file.
Ivan Lozanob2df9f82019-11-05 12:16:46 -080043 Deps: blueprint.DepsGCC,
44 Depfile: "$out.d",
Ivan Lozanoffee3342019-08-27 12:03:00 -070045 },
Ivan Lozano43845682020-07-09 21:03:28 -040046 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040047
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020048 _ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
49 clippyDriver = pctx.AndroidStaticRule("clippy",
50 blueprint.RuleParams{
Ivan Lozanobae62be2020-07-21 13:28:27 -040051 Command: "$envVars $clippyCmd " +
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020052 // Because clippy-driver uses rustc as backend, we need to have some output even during the linting.
53 // Use the metadata output as it has the smallest footprint.
54 "--emit metadata -o $out $in ${libFlags} " +
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020055 "$rustcFlags $clippyFlags",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020056 CommandDeps: []string{"$clippyCmd"},
57 },
Ivan Lozanobae62be2020-07-21 13:28:27 -040058 "rustcFlags", "libFlags", "clippyFlags", "envVars")
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020059
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040060 zip = pctx.AndroidStaticRule("zip",
61 blueprint.RuleParams{
62 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
63 CommandDeps: []string{"${SoongZipCmd}"},
64 Rspfile: "$out.rsp",
65 RspfileContent: "$in",
66 })
Ivan Lozano43845682020-07-09 21:03:28 -040067
68 cp = pctx.AndroidStaticRule("cp",
69 blueprint.RuleParams{
70 Command: "cp `cat $outDir.rsp` $outDir",
71 Rspfile: "${outDir}.rsp",
72 RspfileContent: "$in",
73 },
74 "outDir")
Ivan Lozanoffee3342019-08-27 12:03:00 -070075)
76
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040077type buildOutput struct {
78 outputFile android.Path
79 coverageFile android.Path
80}
Ivan Lozanoffee3342019-08-27 12:03:00 -070081
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040082func init() {
83 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070084}
85
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020086func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070087 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080088 flags.RustFlags = append(flags.RustFlags, "-C lto")
89
Matthew Maurerbb3add12020-06-25 09:34:12 -070090 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070091}
92
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020093func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070094 outputFile android.WritablePath, linkDirs []string) buildOutput {
95 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070096}
97
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020098func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070099 outputFile android.WritablePath, linkDirs []string) buildOutput {
100 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101}
102
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200103func TransformSrctoStatic(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, "staticlib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700107}
108
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200109func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700110 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -0800111 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700112 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700113}
114
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200115func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700116 flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
117 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700118}
119
120func rustLibsToPaths(libs RustLibraries) android.Paths {
121 var paths android.Paths
122 for _, lib := range libs {
123 paths = append(paths, lib.Path)
124 }
125 return paths
126}
127
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200128func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700129 outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700130
131 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800132 var implicits android.Paths
Ivan Lozano43845682020-07-09 21:03:28 -0400133 var envVars []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400134 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700135 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400136 var implicitOutputs android.WritablePaths
137
138 output.outputFile = outputFile
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200139 crate_name := ctx.RustModule().CrateName()
140 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141
Thiébaud Weksteen71512f32020-11-03 15:17:51 +0100142 // libstd requires a specific environment variable to be set. This is
143 // not officially documented and may be removed in the future. See
144 // https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866.
145 if crate_name == "std" {
146 envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType])
147 }
148
Ivan Lozanoffee3342019-08-27 12:03:00 -0700149 inputs = append(inputs, main)
150
151 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700152 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 rustcFlags = append(rustcFlags, flags.RustFlags...)
154 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700155 if crate_name != "" {
156 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
157 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700158 if targetTriple != "" {
159 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700160 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700161 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700162
163 // Suppress an implicit sysroot
164 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
165
Ivan Lozanof1c84332019-09-20 11:00:37 -0700166 // Collect linker flags
167 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
168 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700169
170 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800171 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
173 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800174 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700175 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
176 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800177 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
179 }
180
Matthew Maurerbb3add12020-06-25 09:34:12 -0700181 for _, path := range linkDirs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700182 libFlags = append(libFlags, "-L "+path)
183 }
184
185 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800186 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
187 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
188 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
189 implicits = append(implicits, deps.StaticLibs...)
190 implicits = append(implicits, deps.SharedLibs...)
Ivan Lozano43845682020-07-09 21:03:28 -0400191
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800192 if deps.CrtBegin.Valid() {
193 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700194 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700195
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400196 if flags.Coverage {
197 var gcnoFile android.WritablePath
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400198 // Provide consistency with cc gcda output, see cc/builder.go init()
Ivan Lozano796fc4c2020-06-17 11:36:57 -0400199 profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400200
201 if outputFile.Ext() != "" {
202 gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400203 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
204 ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400205 } else {
206 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400207 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
208 ctx, outputFile.Base()+".gcda").String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400209 }
210
211 implicitOutputs = append(implicitOutputs, gcnoFile)
212 output.coverageFile = gcnoFile
213 }
214
Ivan Lozano43845682020-07-09 21:03:28 -0400215 if len(deps.SrcDeps) > 0 {
Ivan Lozano10735d92020-07-22 09:14:47 -0400216 genSubDir := "out/"
217 moduleGenDir := android.PathForModuleOut(ctx, genSubDir)
Ivan Lozano43845682020-07-09 21:03:28 -0400218 var outputs android.WritablePaths
219
220 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400221 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400222 ctx.PropertyErrorf("srcs",
223 "multiple source providers generate the same filename output: "+genSrc.Base())
224 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400225 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400226 }
227
228 ctx.Build(pctx, android.BuildParams{
229 Rule: cp,
230 Description: "cp " + moduleGenDir.Rel(),
231 Outputs: outputs,
232 Inputs: deps.SrcDeps,
233 Args: map[string]string{
234 "outDir": moduleGenDir.String(),
235 },
236 })
237 implicits = append(implicits, outputs.Paths()...)
238 envVars = append(envVars, "OUT_DIR=$$PWD/"+moduleGenDir.String())
239 }
240
Ivan Lozanobae62be2020-07-21 13:28:27 -0400241 if flags.Clippy {
242 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
243 ctx.Build(pctx, android.BuildParams{
244 Rule: clippyDriver,
245 Description: "clippy " + main.Rel(),
246 Output: clippyFile,
247 ImplicitOutputs: nil,
248 Inputs: inputs,
249 Implicits: implicits,
250 Args: map[string]string{
251 "rustcFlags": strings.Join(rustcFlags, " "),
252 "libFlags": strings.Join(libFlags, " "),
253 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
254 "envVars": strings.Join(envVars, " "),
255 },
256 })
257 // Declare the clippy build as an implicit dependency of the original crate.
258 implicits = append(implicits, clippyFile)
259 }
260
Ivan Lozanoffee3342019-08-27 12:03:00 -0700261 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400262 Rule: rustc,
263 Description: "rustc " + main.Rel(),
264 Output: outputFile,
265 ImplicitOutputs: implicitOutputs,
266 Inputs: inputs,
267 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700268 Args: map[string]string{
269 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700270 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700271 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800272 "crtBegin": deps.CrtBegin.String(),
273 "crtEnd": deps.CrtEnd.String(),
Ivan Lozano43845682020-07-09 21:03:28 -0400274 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700275 },
276 })
277
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400278 return output
279}
280
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200281func TransformCoverageFilesToZip(ctx ModuleContext,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400282 covFiles android.Paths, baseName string) android.OptionalPath {
283 if len(covFiles) > 0 {
284
285 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
286
287 ctx.Build(pctx, android.BuildParams{
288 Rule: zip,
289 Description: "zip " + outputFile.Base(),
290 Inputs: covFiles,
291 Output: outputFile,
292 })
293
294 return android.OptionalPathForPath(outputFile)
295 }
296 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700297}