2019-08-28 03:03:00 +08:00
|
|
|
// Copyright 2019 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
2020-04-09 21:56:02 +08:00
|
|
|
"github.com/google/blueprint/pathtools"
|
2019-08-28 03:03:00 +08:00
|
|
|
|
|
|
|
"android/soong/android"
|
2020-05-20 21:03:20 +08:00
|
|
|
"android/soong/cc"
|
2019-08-28 03:03:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
|
|
|
|
rustc = pctx.AndroidStaticRule("rustc",
|
|
|
|
blueprint.RuleParams{
|
2020-07-10 09:03:28 +08:00
|
|
|
Command: "$envVars $rustcCmd " +
|
2019-08-28 03:03:00 +08:00
|
|
|
"-C linker=${config.RustLinker} " +
|
2019-09-21 02:00:37 +08:00
|
|
|
"-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
|
2019-09-30 13:38:31 +08:00
|
|
|
"--emit link -o $out --emit dep-info=$out.d $in ${libFlags} $rustcFlags",
|
2019-08-28 03:03:00 +08:00
|
|
|
CommandDeps: []string{"$rustcCmd"},
|
2019-11-06 04:16:46 +08:00
|
|
|
// Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
|
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Depfile: "$out.d",
|
2019-08-28 03:03:00 +08:00
|
|
|
},
|
2020-07-10 09:03:28 +08:00
|
|
|
"rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
|
2020-04-09 21:56:02 +08:00
|
|
|
|
2020-06-22 19:28:02 +08:00
|
|
|
_ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
|
|
|
|
clippyDriver = pctx.AndroidStaticRule("clippy",
|
|
|
|
blueprint.RuleParams{
|
2020-07-22 01:28:27 +08:00
|
|
|
Command: "$envVars $clippyCmd " +
|
2020-06-22 19:28:02 +08:00
|
|
|
// Because clippy-driver uses rustc as backend, we need to have some output even during the linting.
|
|
|
|
// Use the metadata output as it has the smallest footprint.
|
|
|
|
"--emit metadata -o $out $in ${libFlags} " +
|
2020-07-01 03:43:35 +08:00
|
|
|
"$rustcFlags $clippyFlags",
|
2020-06-22 19:28:02 +08:00
|
|
|
CommandDeps: []string{"$clippyCmd"},
|
|
|
|
},
|
2020-07-22 01:28:27 +08:00
|
|
|
"rustcFlags", "libFlags", "clippyFlags", "envVars")
|
2020-06-22 19:28:02 +08:00
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
zip = pctx.AndroidStaticRule("zip",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "cat $out.rsp | tr ' ' '\\n' | tr -d \\' | sort -u > ${out}.tmp && ${SoongZipCmd} -o ${out} -C $$OUT_DIR -l ${out}.tmp",
|
|
|
|
CommandDeps: []string{"${SoongZipCmd}"},
|
|
|
|
Rspfile: "$out.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
})
|
2020-07-10 09:03:28 +08:00
|
|
|
|
|
|
|
cp = pctx.AndroidStaticRule("cp",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "cp `cat $outDir.rsp` $outDir",
|
|
|
|
Rspfile: "${outDir}.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
},
|
|
|
|
"outDir")
|
2019-08-28 03:03:00 +08:00
|
|
|
)
|
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
type buildOutput struct {
|
|
|
|
outputFile android.Path
|
|
|
|
coverageFile android.Path
|
|
|
|
}
|
2019-08-28 03:03:00 +08:00
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
func init() {
|
|
|
|
pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-26 00:34:12 +08:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
2019-11-21 02:14:33 +08:00
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C lto")
|
|
|
|
|
2020-06-26 00:34:12 +08:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-26 00:34:12 +08:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-26 00:34:12 +08:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-26 00:34:12 +08:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
2019-11-21 02:14:33 +08:00
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C lto")
|
2020-06-26 00:34:12 +08:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs)
|
2019-10-19 05:49:46 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-26 00:34:12 +08:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
2019-11-21 02:14:33 +08:00
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C lto")
|
2020-06-26 00:34:12 +08:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
|
2019-10-19 05:49:46 +08:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
|
2020-06-26 00:34:12 +08:00
|
|
|
flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
|
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func rustLibsToPaths(libs RustLibraries) android.Paths {
|
|
|
|
var paths android.Paths
|
|
|
|
for _, lib := range libs {
|
|
|
|
paths = append(paths, lib.Path)
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
|
2020-06-26 00:34:12 +08:00
|
|
|
outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput {
|
2019-08-28 03:03:00 +08:00
|
|
|
|
|
|
|
var inputs android.Paths
|
2019-11-06 04:16:46 +08:00
|
|
|
var implicits android.Paths
|
2020-07-10 09:03:28 +08:00
|
|
|
var envVars []string
|
2020-04-09 21:56:02 +08:00
|
|
|
var output buildOutput
|
2019-09-21 02:00:37 +08:00
|
|
|
var libFlags, rustcFlags, linkFlags []string
|
2020-04-09 21:56:02 +08:00
|
|
|
var implicitOutputs android.WritablePaths
|
|
|
|
|
|
|
|
output.outputFile = outputFile
|
2020-06-24 17:32:48 +08:00
|
|
|
crate_name := ctx.RustModule().CrateName()
|
|
|
|
targetTriple := ctx.toolchain().RustTriple()
|
2019-08-28 03:03:00 +08:00
|
|
|
|
|
|
|
inputs = append(inputs, main)
|
|
|
|
|
|
|
|
// Collect rustc flags
|
2019-09-21 02:00:37 +08:00
|
|
|
rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
|
2019-08-28 03:03:00 +08:00
|
|
|
rustcFlags = append(rustcFlags, flags.RustFlags...)
|
|
|
|
rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
|
2019-11-01 10:38:29 +08:00
|
|
|
if crate_name != "" {
|
|
|
|
rustcFlags = append(rustcFlags, "--crate-name="+crate_name)
|
|
|
|
}
|
2019-08-28 03:03:00 +08:00
|
|
|
if targetTriple != "" {
|
|
|
|
rustcFlags = append(rustcFlags, "--target="+targetTriple)
|
2019-09-21 02:00:37 +08:00
|
|
|
linkFlags = append(linkFlags, "-target "+targetTriple)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
2020-06-26 00:34:12 +08:00
|
|
|
|
|
|
|
// Suppress an implicit sysroot
|
|
|
|
rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
|
|
|
|
|
2019-09-21 02:00:37 +08:00
|
|
|
// Collect linker flags
|
|
|
|
linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
|
|
|
|
linkFlags = append(linkFlags, flags.LinkFlags...)
|
2019-08-28 03:03:00 +08:00
|
|
|
|
|
|
|
// Collect library/crate flags
|
2019-11-06 04:16:46 +08:00
|
|
|
for _, lib := range deps.RLibs {
|
2019-08-28 03:03:00 +08:00
|
|
|
libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
|
|
|
|
}
|
2019-11-06 04:16:46 +08:00
|
|
|
for _, lib := range deps.DyLibs {
|
2019-08-28 03:03:00 +08:00
|
|
|
libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
|
|
|
|
}
|
2019-11-06 04:16:46 +08:00
|
|
|
for _, proc_macro := range deps.ProcMacros {
|
2019-08-28 03:03:00 +08:00
|
|
|
libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:34:12 +08:00
|
|
|
for _, path := range linkDirs {
|
2019-08-28 03:03:00 +08:00
|
|
|
libFlags = append(libFlags, "-L "+path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect dependencies
|
2019-11-06 04:16:46 +08:00
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
|
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
|
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
|
|
|
|
implicits = append(implicits, deps.StaticLibs...)
|
|
|
|
implicits = append(implicits, deps.SharedLibs...)
|
2020-07-10 09:03:28 +08:00
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
if deps.CrtBegin.Valid() {
|
|
|
|
implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
|
2019-09-21 02:00:37 +08:00
|
|
|
}
|
2019-08-28 03:03:00 +08:00
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
if flags.Coverage {
|
|
|
|
var gcnoFile android.WritablePath
|
2020-05-20 21:03:20 +08:00
|
|
|
// Provide consistency with cc gcda output, see cc/builder.go init()
|
2020-06-17 23:36:57 +08:00
|
|
|
profileEmitArg := strings.TrimPrefix(cc.PwdPrefix(), "PWD=") + "/"
|
2020-04-09 21:56:02 +08:00
|
|
|
|
|
|
|
if outputFile.Ext() != "" {
|
|
|
|
gcnoFile = android.PathForModuleOut(ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcno"))
|
2020-05-20 21:03:20 +08:00
|
|
|
rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
|
|
|
|
ctx, pathtools.ReplaceExtension(outputFile.Base(), "gcda")).String())
|
2020-04-09 21:56:02 +08:00
|
|
|
} else {
|
|
|
|
gcnoFile = android.PathForModuleOut(ctx, outputFile.Base()+".gcno")
|
2020-05-20 21:03:20 +08:00
|
|
|
rustcFlags = append(rustcFlags, "-Z profile-emit="+profileEmitArg+android.PathForModuleOut(
|
|
|
|
ctx, outputFile.Base()+".gcda").String())
|
2020-04-09 21:56:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
implicitOutputs = append(implicitOutputs, gcnoFile)
|
|
|
|
output.coverageFile = gcnoFile
|
|
|
|
}
|
|
|
|
|
2020-07-10 09:03:28 +08:00
|
|
|
if len(deps.SrcDeps) > 0 {
|
2020-07-22 21:14:47 +08:00
|
|
|
genSubDir := "out/"
|
|
|
|
moduleGenDir := android.PathForModuleOut(ctx, genSubDir)
|
2020-07-10 09:03:28 +08:00
|
|
|
var outputs android.WritablePaths
|
|
|
|
|
|
|
|
for _, genSrc := range deps.SrcDeps {
|
2020-07-22 21:14:47 +08:00
|
|
|
if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
|
2020-07-10 09:03:28 +08:00
|
|
|
ctx.PropertyErrorf("srcs",
|
|
|
|
"multiple source providers generate the same filename output: "+genSrc.Base())
|
|
|
|
}
|
2020-07-22 21:14:47 +08:00
|
|
|
outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
|
2020-07-10 09:03:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: cp,
|
|
|
|
Description: "cp " + moduleGenDir.Rel(),
|
|
|
|
Outputs: outputs,
|
|
|
|
Inputs: deps.SrcDeps,
|
|
|
|
Args: map[string]string{
|
|
|
|
"outDir": moduleGenDir.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
implicits = append(implicits, outputs.Paths()...)
|
|
|
|
envVars = append(envVars, "OUT_DIR=$$PWD/"+moduleGenDir.String())
|
|
|
|
}
|
|
|
|
|
2020-07-22 01:28:27 +08:00
|
|
|
if flags.Clippy {
|
|
|
|
clippyFile := android.PathForModuleOut(ctx, outputFile.Base()+".clippy")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: clippyDriver,
|
|
|
|
Description: "clippy " + main.Rel(),
|
|
|
|
Output: clippyFile,
|
|
|
|
ImplicitOutputs: nil,
|
|
|
|
Inputs: inputs,
|
|
|
|
Implicits: implicits,
|
|
|
|
Args: map[string]string{
|
|
|
|
"rustcFlags": strings.Join(rustcFlags, " "),
|
|
|
|
"libFlags": strings.Join(libFlags, " "),
|
|
|
|
"clippyFlags": strings.Join(flags.ClippyFlags, " "),
|
|
|
|
"envVars": strings.Join(envVars, " "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// Declare the clippy build as an implicit dependency of the original crate.
|
|
|
|
implicits = append(implicits, clippyFile)
|
|
|
|
}
|
|
|
|
|
2019-08-28 03:03:00 +08:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-04-09 21:56:02 +08:00
|
|
|
Rule: rustc,
|
|
|
|
Description: "rustc " + main.Rel(),
|
|
|
|
Output: outputFile,
|
|
|
|
ImplicitOutputs: implicitOutputs,
|
|
|
|
Inputs: inputs,
|
|
|
|
Implicits: implicits,
|
2019-08-28 03:03:00 +08:00
|
|
|
Args: map[string]string{
|
|
|
|
"rustcFlags": strings.Join(rustcFlags, " "),
|
2019-09-21 02:00:37 +08:00
|
|
|
"linkFlags": strings.Join(linkFlags, " "),
|
2019-08-28 03:03:00 +08:00
|
|
|
"libFlags": strings.Join(libFlags, " "),
|
2019-11-06 04:16:46 +08:00
|
|
|
"crtBegin": deps.CrtBegin.String(),
|
|
|
|
"crtEnd": deps.CrtEnd.String(),
|
2020-07-10 09:03:28 +08:00
|
|
|
"envVars": strings.Join(envVars, " "),
|
2019-08-28 03:03:00 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-06-24 17:32:48 +08:00
|
|
|
func TransformCoverageFilesToZip(ctx ModuleContext,
|
2020-04-09 21:56:02 +08:00
|
|
|
covFiles android.Paths, baseName string) android.OptionalPath {
|
|
|
|
if len(covFiles) > 0 {
|
|
|
|
|
|
|
|
outputFile := android.PathForModuleOut(ctx, baseName+".zip")
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: zip,
|
|
|
|
Description: "zip " + outputFile.Base(),
|
|
|
|
Inputs: covFiles,
|
|
|
|
Output: outputFile,
|
|
|
|
})
|
|
|
|
|
|
|
|
return android.OptionalPathForPath(outputFile)
|
|
|
|
}
|
|
|
|
return android.OptionalPath{}
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|