blob: b1913235ed89cc714b1244fc31c685456e640db8 [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{
31 Command: "$rustcCmd " +
32 "-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 Lozanof1c84332019-09-20 11:00:37 -070040 "rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd")
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{
45 Command: "$clippyCmd " +
46 // 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} " +
49 "$clippyFlags $rustcFlags",
50 CommandDeps: []string{"$clippyCmd"},
51 },
52 "rustcFlags", "libFlags", "clippyFlags")
53
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 Lozanoffee3342019-08-27 12:03:00 -070061)
62
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040063type buildOutput struct {
64 outputFile android.Path
65 coverageFile android.Path
66}
Ivan Lozanoffee3342019-08-27 12:03:00 -070067
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040068func init() {
69 pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
Ivan Lozanoffee3342019-08-27 12:03:00 -070070}
71
Ivan Lozanob2df9f82019-11-05 12:16:46 -080072func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040073 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080074 flags.RustFlags = append(flags.RustFlags, "-C lto")
75
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040076 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070077}
78
Ivan Lozanob2df9f82019-11-05 12:16:46 -080079func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040080 outputFile android.WritablePath, includeDirs []string) buildOutput {
81 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070082}
83
Ivan Lozanob2df9f82019-11-05 12:16:46 -080084func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040085 outputFile android.WritablePath, includeDirs []string) buildOutput {
86 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070087}
88
Ivan Lozanob2df9f82019-11-05 12:16:46 -080089func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040090 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080091 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040092 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070093}
94
Ivan Lozanob2df9f82019-11-05 12:16:46 -080095func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040096 outputFile android.WritablePath, includeDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080097 flags.RustFlags = append(flags.RustFlags, "-C lto")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040098 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070099}
100
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800101func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400102 flags Flags, outputFile android.WritablePath, includeDirs []string) buildOutput {
103 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104}
105
106func rustLibsToPaths(libs RustLibraries) android.Paths {
107 var paths android.Paths
108 for _, lib := range libs {
109 paths = append(paths, lib.Path)
110 }
111 return paths
112}
113
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800114func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400115 outputFile android.WritablePath, crate_type string, includeDirs []string) buildOutput {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700116
117 var inputs android.Paths
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800118 var implicits android.Paths
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400119 var output buildOutput
Ivan Lozanof1c84332019-09-20 11:00:37 -0700120 var libFlags, rustcFlags, linkFlags []string
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400121 var implicitOutputs android.WritablePaths
122
123 output.outputFile = outputFile
Ivan Lozanoffee3342019-08-27 12:03:00 -0700124 crate_name := ctx.(ModuleContext).CrateName()
Ivan Lozano5ca5ef62019-09-23 10:10:40 -0700125 targetTriple := ctx.(ModuleContext).toolchain().RustTriple()
Ivan Lozanoffee3342019-08-27 12:03:00 -0700126
127 inputs = append(inputs, main)
128
129 // Collect rustc flags
Ivan Lozanof1c84332019-09-20 11:00:37 -0700130 rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700131 rustcFlags = append(rustcFlags, flags.RustFlags...)
132 rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
Ivan Lozanoad8b18b2019-10-31 19:38:29 -0700133 if crate_name != "" {
134 rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
135 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700136 if targetTriple != "" {
137 rustcFlags = append(rustcFlags, "--target="+targetTriple)
Ivan Lozanof1c84332019-09-20 11:00:37 -0700138 linkFlags = append(linkFlags, "-target "+targetTriple)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700139 }
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200140 // TODO(b/159718669): Once we have defined static libraries in the host
141 // prebuilts Blueprint file, sysroot should be unconditionally sourced
142 // from /dev/null. Explicitly set sysroot to avoid clippy-driver to
143 // internally call rustc.
144 if ctx.Host() && ctx.TargetPrimary() {
145 rustcFlags = append(rustcFlags, "--sysroot=${config.RustPath}")
146 } else {
147 // If we're not targeting the host primary arch, do not use a sysroot.
Matthew Maurer99020b02019-10-31 10:44:40 -0700148 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
149 }
Ivan Lozanof1c84332019-09-20 11:00:37 -0700150 // Collect linker flags
151 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
152 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153
154 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800155 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
157 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800158 for _, lib := range deps.DyLibs {
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 _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700162 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
163 }
164
165 for _, path := range includeDirs {
166 libFlags = append(libFlags, "-L "+path)
167 }
168
169 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800170 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
171 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
172 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
173 implicits = append(implicits, deps.StaticLibs...)
174 implicits = append(implicits, deps.SharedLibs...)
175 if deps.CrtBegin.Valid() {
176 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700177 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700178
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400179 if flags.Coverage {
180 var gcnoFile android.WritablePath
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400181 // Provide consistency with cc gcda output, see cc/builder.go init()
Ivan Lozano796fc4c2020-06-17 11:36:57 -0400182 profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400183
184 if outputFile.Ext() != "" {
185 gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400186 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
187 ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400188 } else {
189 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400190 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
191 ctx, outputFile.Base()+".gcda").String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400192 }
193
194 implicitOutputs = append(implicitOutputs, gcnoFile)
195 output.coverageFile = gcnoFile
196 }
197
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200198 if flags.Clippy {
199 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
200 ctx.Build(pctx, android.BuildParams{
201 Rule: clippyDriver,
202 Description: "clippy " + main.Rel(),
203 Output: clippyFile,
204 ImplicitOutputs: nil,
205 Inputs: inputs,
206 Implicits: implicits,
207 Args: map[string]string{
208 "rustcFlags": strings.Join(rustcFlags, " "),
209 "libFlags": strings.Join(libFlags, " "),
210 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
211 },
212 })
213 // Declare the clippy build as an implicit dependency of the original crate.
214 implicits = append(implicits, clippyFile)
215 }
216
Ivan Lozanoffee3342019-08-27 12:03:00 -0700217 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400218 Rule: rustc,
219 Description: "rustc " + main.Rel(),
220 Output: outputFile,
221 ImplicitOutputs: implicitOutputs,
222 Inputs: inputs,
223 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224 Args: map[string]string{
225 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700226 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700227 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800228 "crtBegin": deps.CrtBegin.String(),
229 "crtEnd": deps.CrtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700230 },
231 })
232
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400233 return output
234}
235
236func TransformCoverageFilesToZip(ctx android.ModuleContext,
237 covFiles android.Paths, baseName string) android.OptionalPath {
238 if len(covFiles) > 0 {
239
240 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
241
242 ctx.Build(pctx, android.BuildParams{
243 Rule: zip,
244 Description: "zip " + outputFile.Base(),
245 Inputs: covFiles,
246 Output: outputFile,
247 })
248
249 return android.OptionalPathForPath(outputFile)
250 }
251 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700252}