2019-08-27 21:03:00 +02: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 (
|
2020-11-11 16:59:52 +01:00
|
|
|
"path/filepath"
|
2019-08-27 21:03:00 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/android"
|
2020-11-03 15:17:51 +01:00
|
|
|
"android/soong/rust/config"
|
2019-08-27 21:03:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
|
|
|
|
rustc = pctx.AndroidStaticRule("rustc",
|
|
|
|
blueprint.RuleParams{
|
2020-07-10 03:03:28 +02:00
|
|
|
Command: "$envVars $rustcCmd " +
|
2019-08-27 21:03:00 +02:00
|
|
|
"-C linker=${config.RustLinker} " +
|
2019-09-20 20:00:37 +02:00
|
|
|
"-C link-args=\"${crtBegin} ${config.RustLinkerArgs} ${linkFlags} ${crtEnd}\" " +
|
2020-08-14 00:46:21 +02:00
|
|
|
"--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
|
|
|
|
" && grep \"^$out:\" $out.d.raw > $out.d",
|
2019-08-27 21:03:00 +02:00
|
|
|
CommandDeps: []string{"$rustcCmd"},
|
2019-11-05 21:16:46 +01:00
|
|
|
// Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
|
2020-08-14 00:46:21 +02:00
|
|
|
// Rustc emits unneeded dependency lines for the .d and input .rs files.
|
|
|
|
// Those extra lines cause ninja warning:
|
|
|
|
// "warning: depfile has multiple output paths"
|
|
|
|
// For ninja, we keep/grep only the dependency rule for the rust $out file.
|
2019-11-05 21:16:46 +01:00
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Depfile: "$out.d",
|
2019-08-27 21:03:00 +02:00
|
|
|
},
|
2020-07-10 03:03:28 +02:00
|
|
|
"rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
|
2020-04-09 15:56:02 +02:00
|
|
|
|
2020-06-22 13:28:02 +02:00
|
|
|
_ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
|
|
|
|
clippyDriver = pctx.AndroidStaticRule("clippy",
|
|
|
|
blueprint.RuleParams{
|
2020-07-21 19:28:27 +02:00
|
|
|
Command: "$envVars $clippyCmd " +
|
2020-06-22 13:28:02 +02: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-06-30 21:43:35 +02:00
|
|
|
"$rustcFlags $clippyFlags",
|
2020-06-22 13:28:02 +02:00
|
|
|
CommandDeps: []string{"$clippyCmd"},
|
|
|
|
},
|
2020-07-21 19:28:27 +02:00
|
|
|
"rustcFlags", "libFlags", "clippyFlags", "envVars")
|
2020-06-22 13:28:02 +02:00
|
|
|
|
2020-04-09 15:56:02 +02: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 03:03:28 +02:00
|
|
|
|
|
|
|
cp = pctx.AndroidStaticRule("cp",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "cp `cat $outDir.rsp` $outDir",
|
|
|
|
Rspfile: "${outDir}.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
},
|
|
|
|
"outDir")
|
2019-08-27 21:03:00 +02:00
|
|
|
)
|
|
|
|
|
2020-04-09 15:56:02 +02:00
|
|
|
type buildOutput struct {
|
2021-01-15 01:03:18 +01:00
|
|
|
outputFile android.Path
|
2020-04-09 15:56:02 +02:00
|
|
|
}
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2020-04-09 15:56:02 +02:00
|
|
|
func init() {
|
|
|
|
pctx.HostBinToolVariable("SoongZipCmd", "soong_zip")
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:32:48 +02:00
|
|
|
func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-25 18:34:12 +02:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
2020-02-11 14:24:25 +01:00
|
|
|
flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
|
2019-11-20 19:14:33 +01:00
|
|
|
|
2020-06-25 18:34:12 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", linkDirs)
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:32:48 +02:00
|
|
|
func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-25 18:34:12 +02:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", linkDirs)
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:32:48 +02:00
|
|
|
func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-25 18:34:12 +02:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", linkDirs)
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:32:48 +02:00
|
|
|
func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-25 18:34:12 +02:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
2020-02-11 14:24:25 +01:00
|
|
|
flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
|
2020-06-25 18:34:12 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", linkDirs)
|
2019-10-18 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:32:48 +02:00
|
|
|
func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2020-06-25 18:34:12 +02:00
|
|
|
outputFile android.WritablePath, linkDirs []string) buildOutput {
|
2020-02-11 14:24:25 +01:00
|
|
|
flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto")
|
2020-06-25 18:34:12 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", linkDirs)
|
2019-10-18 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 11:32:48 +02:00
|
|
|
func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
|
2020-06-25 18:34:12 +02:00
|
|
|
flags Flags, outputFile android.WritablePath, linkDirs []string) buildOutput {
|
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", linkDirs)
|
2019-08-27 21:03:00 +02: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 11:32:48 +02:00
|
|
|
func transformSrctoCrate(ctx ModuleContext, main android.Path, deps PathDeps, flags Flags,
|
2020-06-25 18:34:12 +02:00
|
|
|
outputFile android.WritablePath, crate_type string, linkDirs []string) buildOutput {
|
2019-08-27 21:03:00 +02:00
|
|
|
|
|
|
|
var inputs android.Paths
|
2019-11-05 21:16:46 +01:00
|
|
|
var implicits android.Paths
|
2020-07-10 03:03:28 +02:00
|
|
|
var envVars []string
|
2020-04-09 15:56:02 +02:00
|
|
|
var output buildOutput
|
2019-09-20 20:00:37 +02:00
|
|
|
var libFlags, rustcFlags, linkFlags []string
|
2020-04-09 15:56:02 +02:00
|
|
|
var implicitOutputs android.WritablePaths
|
|
|
|
|
|
|
|
output.outputFile = outputFile
|
2020-12-21 18:11:10 +01:00
|
|
|
crateName := ctx.RustModule().CrateName()
|
2020-06-24 11:32:48 +02:00
|
|
|
targetTriple := ctx.toolchain().RustTriple()
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2020-11-03 15:17:51 +01:00
|
|
|
// libstd requires a specific environment variable to be set. This is
|
|
|
|
// not officially documented and may be removed in the future. See
|
|
|
|
// https://github.com/rust-lang/rust/blob/master/library/std/src/env.rs#L866.
|
2020-12-21 18:11:10 +01:00
|
|
|
if crateName == "std" {
|
2020-11-03 15:17:51 +01:00
|
|
|
envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.RustModule().Arch().ArchType])
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
inputs = append(inputs, main)
|
|
|
|
|
|
|
|
// Collect rustc flags
|
2019-09-20 20:00:37 +02:00
|
|
|
rustcFlags = append(rustcFlags, flags.GlobalRustFlags...)
|
2019-08-27 21:03:00 +02:00
|
|
|
rustcFlags = append(rustcFlags, flags.RustFlags...)
|
|
|
|
rustcFlags = append(rustcFlags, "--crate-type="+crate_type)
|
2020-12-21 18:11:10 +01:00
|
|
|
if crateName != "" {
|
|
|
|
rustcFlags = append(rustcFlags, "--crate-name="+crateName)
|
2019-11-01 03:38:29 +01:00
|
|
|
}
|
2019-08-27 21:03:00 +02:00
|
|
|
if targetTriple != "" {
|
|
|
|
rustcFlags = append(rustcFlags, "--target="+targetTriple)
|
2019-09-20 20:00:37 +02:00
|
|
|
linkFlags = append(linkFlags, "-target "+targetTriple)
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
2020-06-25 18:34:12 +02:00
|
|
|
|
|
|
|
// Suppress an implicit sysroot
|
|
|
|
rustcFlags = append(rustcFlags, "--sysroot=/dev/null")
|
|
|
|
|
2019-09-20 20:00:37 +02:00
|
|
|
// Collect linker flags
|
|
|
|
linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
|
|
|
|
linkFlags = append(linkFlags, flags.LinkFlags...)
|
2019-08-27 21:03:00 +02:00
|
|
|
|
|
|
|
// Collect library/crate flags
|
2019-11-05 21:16:46 +01:00
|
|
|
for _, lib := range deps.RLibs {
|
2019-08-27 21:03:00 +02:00
|
|
|
libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
|
|
|
|
}
|
2019-11-05 21:16:46 +01:00
|
|
|
for _, lib := range deps.DyLibs {
|
2019-08-27 21:03:00 +02:00
|
|
|
libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
|
|
|
|
}
|
2019-11-05 21:16:46 +01:00
|
|
|
for _, proc_macro := range deps.ProcMacros {
|
2019-08-27 21:03:00 +02:00
|
|
|
libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
|
|
|
|
}
|
|
|
|
|
2020-06-25 18:34:12 +02:00
|
|
|
for _, path := range linkDirs {
|
2019-08-27 21:03:00 +02:00
|
|
|
libFlags = append(libFlags, "-L "+path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect dependencies
|
2019-11-05 21:16:46 +01:00
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
|
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
|
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
|
|
|
|
implicits = append(implicits, deps.StaticLibs...)
|
2021-01-21 21:23:29 +01:00
|
|
|
implicits = append(implicits, deps.SharedLibDeps...)
|
2020-12-01 15:25:22 +01:00
|
|
|
implicits = append(implicits, deps.srcProviderFiles...)
|
2020-07-10 03:03:28 +02:00
|
|
|
|
2019-11-05 21:16:46 +01:00
|
|
|
if deps.CrtBegin.Valid() {
|
|
|
|
implicits = append(implicits, deps.CrtBegin.Path(), deps.CrtEnd.Path())
|
2019-09-20 20:00:37 +02:00
|
|
|
}
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2020-07-10 03:03:28 +02:00
|
|
|
if len(deps.SrcDeps) > 0 {
|
2020-07-22 15:14:47 +02:00
|
|
|
genSubDir := "out/"
|
|
|
|
moduleGenDir := android.PathForModuleOut(ctx, genSubDir)
|
2020-07-10 03:03:28 +02:00
|
|
|
var outputs android.WritablePaths
|
|
|
|
|
|
|
|
for _, genSrc := range deps.SrcDeps {
|
2020-07-22 15:14:47 +02:00
|
|
|
if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
|
2020-07-10 03:03:28 +02:00
|
|
|
ctx.PropertyErrorf("srcs",
|
|
|
|
"multiple source providers generate the same filename output: "+genSrc.Base())
|
|
|
|
}
|
2020-07-22 15:14:47 +02:00
|
|
|
outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
|
2020-07-10 03:03:28 +02: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()...)
|
2020-11-11 16:59:52 +01:00
|
|
|
|
|
|
|
// We must calculate an absolute path for OUT_DIR since Rust's include! macro (which normally consumes this)
|
|
|
|
// assumes that paths are relative to the source file.
|
|
|
|
var outDirPrefix string
|
|
|
|
if !filepath.IsAbs(moduleGenDir.String()) {
|
|
|
|
// If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
|
|
|
|
outDirPrefix = "$$PWD/"
|
|
|
|
} else {
|
|
|
|
// If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
|
|
|
|
outDirPrefix = ""
|
|
|
|
}
|
|
|
|
envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
|
2020-07-10 03:03:28 +02:00
|
|
|
}
|
|
|
|
|
2020-07-21 19:28:27 +02: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-27 21:03:00 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2020-04-09 15:56:02 +02:00
|
|
|
Rule: rustc,
|
|
|
|
Description: "rustc " + main.Rel(),
|
|
|
|
Output: outputFile,
|
|
|
|
ImplicitOutputs: implicitOutputs,
|
|
|
|
Inputs: inputs,
|
|
|
|
Implicits: implicits,
|
2019-08-27 21:03:00 +02:00
|
|
|
Args: map[string]string{
|
|
|
|
"rustcFlags": strings.Join(rustcFlags, " "),
|
2019-09-20 20:00:37 +02:00
|
|
|
"linkFlags": strings.Join(linkFlags, " "),
|
2019-08-27 21:03:00 +02:00
|
|
|
"libFlags": strings.Join(libFlags, " "),
|
2019-11-05 21:16:46 +01:00
|
|
|
"crtBegin": deps.CrtBegin.String(),
|
|
|
|
"crtEnd": deps.CrtEnd.String(),
|
2020-07-10 03:03:28 +02:00
|
|
|
"envVars": strings.Join(envVars, " "),
|
2019-08-27 21:03:00 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-04-09 15:56:02 +02:00
|
|
|
return output
|
|
|
|
}
|