blob: 16d73060340461ab2d5a96f7f3984cc40e523816 [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} " +
Thiébaud Weksteen8e46efa2020-06-30 21:43:35 +020049 "$rustcFlags $clippyFlags",
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +020050 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
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020072func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070073 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080074 flags.RustFlags = append(flags.RustFlags, "-C lto")
75
Matthew Maurerbb3add12020-06-25 09:34:12 -070076 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070077}
78
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020079func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070080 outputFile android.WritablePath, linkDirs []string) buildOutput {
81 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070082}
83
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020084func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070085 outputFile android.WritablePath, linkDirs []string) buildOutput {
86 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs)
Ivan Lozanoffee3342019-08-27 12:03:00 -070087}
88
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020089func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070090 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080091 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -070092 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070093}
94
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +020095func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -070096 outputFile android.WritablePath, linkDirs []string) buildOutput {
Ivan Lozano31b095d2019-11-20 10:14:33 -080097 flags.RustFlags = append(flags.RustFlags, "-C lto")
Matthew Maurerbb3add12020-06-25 09:34:12 -070098 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
Ivan Lozano52767be2019-10-18 14:49:46 -070099}
100
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200101func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700102 flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
103 return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
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
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200114func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
Matthew Maurerbb3add12020-06-25 09:34:12 -0700115 outputFile android.WritablePath, crate_type string, linkDirs []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
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200124 crate_name := ctx.RustModule().CrateName()
125 targetTriple := ctx.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 }
Matthew Maurerbb3add12020-06-25 09:34:12 -0700140
141 // Suppress an implicit sysroot
142 rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
143
Ivan Lozanof1c84332019-09-20 11:00:37 -0700144 // Collect linker flags
145 linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
146 linkFlags = append(linkFlags, flags.LinkFlags...)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700147
148 // Collect library/crate flags
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800149 for _, lib := range deps.RLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700150 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
151 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800152 for _, lib := range deps.DyLibs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700153 libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
154 }
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800155 for _, proc_macro := range deps.ProcMacros {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700156 libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
157 }
158
Matthew Maurerbb3add12020-06-25 09:34:12 -0700159 for _, path := range linkDirs {
Ivan Lozanoffee3342019-08-27 12:03:00 -0700160 libFlags = append(libFlags, "-L "+path)
161 }
162
163 // Collect dependencies
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800164 implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
165 implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
166 implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
167 implicits = append(implicits, deps.StaticLibs...)
168 implicits = append(implicits, deps.SharedLibs...)
169 if deps.CrtBegin.Valid() {
170 implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
Ivan Lozanof1c84332019-09-20 11:00:37 -0700171 }
Ivan Lozanoffee3342019-08-27 12:03:00 -0700172
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400173 if flags.Coverage {
174 var gcnoFile android.WritablePath
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400175 // Provide consistency with cc gcda output, see cc/builder.go init()
Ivan Lozano796fc4c2020-06-17 11:36:57 -0400176 profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400177
178 if outputFile.Ext() != "" {
179 gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400180 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
181 ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400182 } else {
183 gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
Ivan Lozanof3717ee2020-05-20 09:03:20 -0400184 rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
185 ctx, outputFile.Base()+".gcda").String())
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400186 }
187
188 implicitOutputs = append(implicitOutputs, gcnoFile)
189 output.coverageFile = gcnoFile
190 }
191
Thiébaud Weksteen92f703b2020-06-22 13:28:02 +0200192 if flags.Clippy {
193 clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
194 ctx.Build(pctx, android.BuildParams{
195 Rule: clippyDriver,
196 Description: "clippy " + main.Rel(),
197 Output: clippyFile,
198 ImplicitOutputs: nil,
199 Inputs: inputs,
200 Implicits: implicits,
201 Args: map[string]string{
202 "rustcFlags": strings.Join(rustcFlags, " "),
203 "libFlags": strings.Join(libFlags, " "),
204 "clippyFlags": strings.Join(flags.ClippyFlags, " "),
205 },
206 })
207 // Declare the clippy build as an implicit dependency of the original crate.
208 implicits = append(implicits, clippyFile)
209 }
210
Ivan Lozanoffee3342019-08-27 12:03:00 -0700211 ctx.Build(pctx, android.BuildParams{
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400212 Rule: rustc,
213 Description: "rustc " + main.Rel(),
214 Output: outputFile,
215 ImplicitOutputs: implicitOutputs,
216 Inputs: inputs,
217 Implicits: implicits,
Ivan Lozanoffee3342019-08-27 12:03:00 -0700218 Args: map[string]string{
219 "rustcFlags": strings.Join(rustcFlags, " "),
Ivan Lozanof1c84332019-09-20 11:00:37 -0700220 "linkFlags": strings.Join(linkFlags, " "),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700221 "libFlags": strings.Join(libFlags, " "),
Ivan Lozanob2df9f82019-11-05 12:16:46 -0800222 "crtBegin": deps.CrtBegin.String(),
223 "crtEnd": deps.CrtEnd.String(),
Ivan Lozanoffee3342019-08-27 12:03:00 -0700224 },
225 })
226
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400227 return output
228}
229
Thiébaud Weksteen1f7f70f2020-06-24 11:32:48 +0200230func TransformCoverageFilesToZip(ctx ModuleContext,
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400231 covFiles android.Paths, baseName string) android.OptionalPath {
232 if len(covFiles) > 0 {
233
234 outputFile := android.PathForModuleOut(ctx, baseName+".zip")
235
236 ctx.Build(pctx, android.BuildParams{
237 Rule: zip,
238 Description: "zip " + outputFile.Base(),
239 Inputs: covFiles,
240 Output: outputFile,
241 })
242
243 return android.OptionalPathForPath(outputFile)
244 }
245 return android.OptionalPath{}
Ivan Lozanoffee3342019-08-27 12:03:00 -0700246}