blob: 197c7033b5bd11293dc64a92ba455ef7010ac57a [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"
22
23 "android/soong/android"
Thiébaud Weksteendf229cb2021-02-10 14:07:57 +010024 "android/soong/bloaty"
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.
Thiébaud Weksteen94c83252021-04-07 16:00:19 +020054 "--emit metadata -o $out --emit dep-info=$out.d.raw $in ${libFlags} " +
55 "$rustcFlags $clippyFlags" +
56 " && grep \"^$out:\" $out.d.raw > $out.d",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020057 CommandDeps: []string{"$clippyCmd"},
Thiébaud Weksteen94c83252021-04-07 16:00:19 +020058 Deps: blueprint.DepsGCC,
59 Depfile: "$out.d",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020060 },
Ivan Lozanobae62be2020-07-21 13:28:27 -040061 "rustcFlags", "libFlags", "clippyFlags", "envVars")
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020062
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063 zip = pctx.AndroidStaticRule("zip",
64 blueprint.RuleParams{
65 Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
66 CommandDeps: []string{"${SoongZipCmd}"},
67 Rspfile: "$out.rsp",
68 RspfileContent: "$in",
69 })
Ivan Lozano43845682020-07-09 21:03:28 -040070
71 cp = pctx.AndroidStaticRule("cp",
72 blueprint.RuleParams{
73 Command: "cp `cat $outDir.rsp` $outDir",
74 Rspfile: "${outDir}.rsp",
75 RspfileContent: "$in",
76 },
77 "outDir")
Ivan Lozanoffee3342019-08-27 12:03:00 -070078)
79
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040080type buildOutput struct {
Joel Galensonfa049382021-01-14 16:03:18 -080081 outputFile android.Path
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040082}
Ivan Lozanoffee3342019-08-27 12:03:00 -070083
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040084func init() {
85 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070086}
87
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020088func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070089 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano6cd99e62020-02-11 08:24:25 -050090 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
Ivan Lozano31b095d2019-11-20 10:14:33 -080091
Matthew Maurerbb3add12020-06-25 09:34:12 -070092 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070093}
94
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020095func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070096 outputFile android.WritablePath, linkDirs []string) buildOutput {
97 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070098}
99
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200100func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700101 outputFile android.WritablePath, linkDirs []string) buildOutput {
102 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700103}
104
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200105func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700106 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500107 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700108 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700109}
110
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200111func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700112 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano6cd99e62020-02-11 08:24:25 -0500113 flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -0700114 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -0700115}
116
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200117func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700118 flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
119 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700120}
121
122func rustLibsToPaths(libs RustLibraries) android.Paths {
123 var paths android.Paths
124 for _, lib := range libs {
125 paths = append(paths, lib.Path)
126 }
127 return paths
128}
129
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200130func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700131 outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700132
133 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800134 var implicits android.Paths
Ivan Lozano43845682020-07-09 21:03:28 -0400135 var envVars []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400136 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700137 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400138 var implicitOutputs android.WritablePaths
139
140 output.outputFile = outputFile
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800141 crateName := ctx.RustModule().CrateName()
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200142 targetTriple := ctx.toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700143
Thiébaud Weksteen71512f32020-11-03 15:17:51 +0100144 // libstd requires a specific environment variable to be set. This is
145 // not officially documented and may be removed in the future. See
146 // https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800147 if crateName == "std" {
Thiébaud Weksteen71512f32020-11-03 15:17:51 +0100148 envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType])
149 }
150
Ivan Lozanoffee3342019-08-27 12:03:00 -0700151 inputs = append(inputs, main)
152
153 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700154 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700155 rustcFlags = append(rustcFlags, flags.RustFlags...)
156 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800157 if crateName != "" {
158 rustcFlags = append(rustcFlags, "--crate-name="+crateName)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700159 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700160 if targetTriple != "" {
161 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700162 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700163 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700164
165 // Suppress an implicit sysroot
166 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
167
Ivan Lozanof1c84332019-09-20 11:00:37 -0700168 // Collect linker flags
169 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
170 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700171
172 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800173 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700174 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
175 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800176 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700177 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
178 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800179 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700180 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
181 }
182
Matthew Maurerbb3add12020-06-25 09:34:12 -0700183 for _, path := range linkDirs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700184 libFlags = append(libFlags, "-L "+path)
185 }
186
187 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800188 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
189 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
190 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
191 implicits = append(implicits, deps.StaticLibs...)
Ivan Lozanoec6e9912021-01-21 15:23:29 -0500192 implicits = append(implicits, deps.SharedLibDeps...)
Ivan Lozano9d74a522020-12-01 09:25:22 -0500193 implicits = append(implicits, deps.srcProviderFiles...)
Ivan Lozano43845682020-07-09 21:03:28 -0400194
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800195 if deps.CrtBegin.Valid() {
196 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700197 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700198
Ivan Lozano43845682020-07-09 21:03:28 -0400199 if len(deps.SrcDeps) > 0 {
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100200 moduleGenDir := ctx.RustModule().compiler.CargoOutDir()
Ivan Lozano43845682020-07-09 21:03:28 -0400201 var outputs android.WritablePaths
202
203 for _, genSrc := range deps.SrcDeps {
Ivan Lozano10735d92020-07-22 09:14:47 -0400204 if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
Ivan Lozano43845682020-07-09 21:03:28 -0400205 ctx.PropertyErrorf("srcs",
206 "multiple source providers generate the same filename output: "+genSrc.Base())
207 }
Ivan Lozano10735d92020-07-22 09:14:47 -0400208 outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
Ivan Lozano43845682020-07-09 21:03:28 -0400209 }
210
211 ctx.Build(pctx, android.BuildParams{
212 Rule: cp,
Thiébaud Weksteenee6a89b2021-02-25 16:30:57 +0100213 Description: "cp " + moduleGenDir.Path().Rel(),
Ivan Lozano43845682020-07-09 21:03:28 -0400214 Outputs: outputs,
215 Inputs: deps.SrcDeps,
216 Args: map[string]string{
217 "outDir": moduleGenDir.String(),
218 },
219 })
220 implicits = append(implicits, outputs.Paths()...)
Ivan Lozano1776a2a2020-11-11 10:59:52 -0500221
222 // We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
223 // assumes that paths are relative to the source file.
224 var outDirPrefix string
225 if !filepath.IsAbs(moduleGenDir.String()) {
226 // If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
227 outDirPrefix = "$$PWD/"
228 } else {
229 // If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
230 outDirPrefix = ""
231 }
232 envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
Ivan Lozano43845682020-07-09 21:03:28 -0400233 }
234
Thiébaud Weksteen9997ea72021-02-22 10:52:22 +0100235 envVars = append(envVars, "ANDROID_RUST_VERSION="+config.RustDefaultVersion)
236
Ivan Lozanobae62be2020-07-21 13:28:27 -0400237 if flags.Clippy {
238 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
239 ctx.Build(pctx, android.BuildParams{
240 Rule: clippyDriver,
241 Description: "clippy " + main.Rel(),
242 Output: clippyFile,
243 ImplicitOutputs: nil,
244 Inputs: inputs,
245 Implicits: implicits,
246 Args: map[string]string{
247 "rustcFlags": strings.Join(rustcFlags, " "),
248 "libFlags": strings.Join(libFlags, " "),
249 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
250 "envVars": strings.Join(envVars, " "),
251 },
252 })
253 // Declare the clippy build as an implicit dependency of the original crate.
254 implicits = append(implicits, clippyFile)
255 }
256
Thiébaud Weksteendf229cb2021-02-10 14:07:57 +0100257 bloaty.MeasureSizeForPath(ctx, outputFile)
258
Ivan Lozanoffee3342019-08-27 12:03:00 -0700259 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400260 Rule: rustc,
261 Description: "rustc " + main.Rel(),
262 Output: outputFile,
263 ImplicitOutputs: implicitOutputs,
264 Inputs: inputs,
265 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700266 Args: map[string]string{
267 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700268 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700269 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800270 "crtBegin": deps.CrtBegin.String(),
271 "crtEnd": deps.CrtEnd.String(),
Ivan Lozano43845682020-07-09 21:03:28 -0400272 "envVars": strings.Join(envVars, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700273 },
274 })
275
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400276 return output
277}