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"
|
2024-05-14 03:03:34 +02:00
|
|
|
"android/soong/cc"
|
2020-11-03 15:17:51 +01:00
|
|
|
"android/soong/rust/config"
|
2019-08-27 21:03:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-09-22 05:58:59 +02:00
|
|
|
_ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
|
|
|
|
rustc = pctx.AndroidStaticRule("rustc",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "$envVars $rustcCmd " +
|
2023-10-02 20:39:17 +02:00
|
|
|
"-C linker=${config.RustLinker} " +
|
|
|
|
"-C link-args=\"${crtBegin} ${earlyLinkFlags} ${linkFlags} ${crtEnd}\" " +
|
2023-09-22 05:58:59 +02:00
|
|
|
"--emit link -o $out --emit dep-info=$out.d.raw $in ${libFlags} $rustcFlags" +
|
2024-03-28 20:27:24 +01:00
|
|
|
" && grep ^$out: $out.d.raw > $out.d",
|
2023-10-02 20:39:17 +02:00
|
|
|
CommandDeps: []string{"$rustcCmd"},
|
2023-09-22 05:58:59 +02:00
|
|
|
// Rustc deps-info writes out make compatible dep files: https://github.com/rust-lang/rust/issues/7633
|
|
|
|
// 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.
|
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Depfile: "$out.d",
|
|
|
|
},
|
2023-10-02 20:39:17 +02:00
|
|
|
"rustcFlags", "earlyLinkFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
|
2023-09-22 05:58:59 +02:00
|
|
|
|
|
|
|
_ = pctx.SourcePathVariable("rustdocCmd", "${config.RustBin}/rustdoc")
|
|
|
|
rustdoc = pctx.AndroidStaticRule("rustdoc",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "$envVars $rustdocCmd $rustdocFlags $in -o $outDir && " +
|
|
|
|
"touch $out",
|
|
|
|
CommandDeps: []string{"$rustdocCmd"},
|
|
|
|
},
|
|
|
|
"rustdocFlags", "outDir", "envVars")
|
|
|
|
|
|
|
|
_ = pctx.SourcePathVariable("clippyCmd", "${config.RustBin}/clippy-driver")
|
|
|
|
clippyDriver = pctx.AndroidStaticRule("clippy",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "$envVars $clippyCmd " +
|
|
|
|
// 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 --emit dep-info=$out.d.raw $in ${libFlags} " +
|
|
|
|
"$rustcFlags $clippyFlags" +
|
2024-04-11 17:30:27 +02:00
|
|
|
" && grep ^$out: $out.d.raw > $out.d",
|
2023-09-22 05:58:59 +02:00
|
|
|
CommandDeps: []string{"$clippyCmd"},
|
|
|
|
Deps: blueprint.DepsGCC,
|
|
|
|
Depfile: "$out.d",
|
|
|
|
},
|
|
|
|
"rustcFlags", "libFlags", "clippyFlags", "envVars")
|
|
|
|
|
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
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
cp = pctx.AndroidStaticRule("cp",
|
2020-07-10 03:03:28 +02:00
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "cp `cat $outDir.rsp` $outDir",
|
|
|
|
Rspfile: "${outDir}.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
},
|
|
|
|
"outDir")
|
2022-04-19 05:12:56 +02:00
|
|
|
|
|
|
|
// Cross-referencing:
|
|
|
|
_ = pctx.SourcePathVariable("rustExtractor",
|
|
|
|
"prebuilts/build-tools/${config.HostPrebuiltTag}/bin/rust_extractor")
|
|
|
|
_ = pctx.VariableFunc("kytheCorpus",
|
|
|
|
func(ctx android.PackageVarContext) string { return ctx.Config().XrefCorpusName() })
|
|
|
|
_ = pctx.VariableFunc("kytheCuEncoding",
|
|
|
|
func(ctx android.PackageVarContext) string { return ctx.Config().XrefCuEncoding() })
|
2023-09-22 05:58:59 +02:00
|
|
|
_ = pctx.SourcePathVariable("kytheVnames", "build/soong/vnames.json")
|
|
|
|
kytheExtract = pctx.AndroidStaticRule("kythe",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: `KYTHE_CORPUS=${kytheCorpus} ` +
|
|
|
|
`KYTHE_OUTPUT_FILE=$out ` +
|
|
|
|
`KYTHE_VNAMES=$kytheVnames ` +
|
|
|
|
`KYTHE_KZIP_ENCODING=${kytheCuEncoding} ` +
|
|
|
|
`KYTHE_CANONICALIZE_VNAME_PATHS=prefer-relative ` +
|
|
|
|
`$rustExtractor $envVars ` +
|
|
|
|
`$rustcCmd ` +
|
2023-10-02 20:39:17 +02:00
|
|
|
`-C linker=${config.RustLinker} ` +
|
|
|
|
`-C link-args="${crtBegin} ${linkFlags} ${crtEnd}" ` +
|
2023-09-22 05:58:59 +02:00
|
|
|
`$in ${libFlags} $rustcFlags`,
|
|
|
|
CommandDeps: []string{"$rustExtractor", "$kytheVnames"},
|
|
|
|
Rspfile: "${out}.rsp",
|
|
|
|
RspfileContent: "$in",
|
|
|
|
},
|
2023-10-02 20:39:17 +02:00
|
|
|
"rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd", "envVars")
|
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
|
2022-04-19 05:12:56 +02:00
|
|
|
kytheFile 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")
|
2024-05-14 03:03:34 +02:00
|
|
|
cc.TransformRlibstoStaticlib = TransformRlibstoStaticlib
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
type transformProperties struct {
|
|
|
|
crateName string
|
|
|
|
targetTriple string
|
|
|
|
is64Bit bool
|
|
|
|
bootstrap bool
|
|
|
|
inRecovery bool
|
|
|
|
inRamdisk bool
|
|
|
|
inVendorRamdisk bool
|
|
|
|
cargoOutDir android.OptionalPath
|
|
|
|
synthetic bool
|
|
|
|
crateType string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populates a standard transformProperties struct for Rust modules
|
|
|
|
func getTransformProperties(ctx ModuleContext, crateType string) transformProperties {
|
|
|
|
module := ctx.RustModule()
|
|
|
|
return transformProperties{
|
|
|
|
crateName: module.CrateName(),
|
|
|
|
is64Bit: ctx.toolchain().Is64Bit(),
|
|
|
|
targetTriple: ctx.toolchain().RustTriple(),
|
|
|
|
bootstrap: module.Bootstrap(),
|
|
|
|
inRecovery: module.InRecovery(),
|
|
|
|
inRamdisk: module.InRamdisk(),
|
|
|
|
inVendorRamdisk: module.InVendorRamdisk(),
|
|
|
|
cargoOutDir: module.compiler.cargoOutDir(),
|
|
|
|
|
|
|
|
// crateType indicates what type of crate to build
|
|
|
|
crateType: crateType,
|
|
|
|
|
|
|
|
// synthetic indicates whether this is an actual Rust module or not
|
|
|
|
synthetic: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func TransformSrcToBinary(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2021-03-19 23:06:02 +01:00
|
|
|
outputFile android.WritablePath) buildOutput {
|
2024-05-15 16:59:47 +02:00
|
|
|
if ctx.RustModule().compiler.Thinlto() {
|
|
|
|
flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
|
|
|
|
}
|
2024-02-20 22:39:40 +01:00
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, getTransformProperties(ctx, "bin"))
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func TransformSrctoRlib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2021-03-19 23:06:02 +01:00
|
|
|
outputFile android.WritablePath) buildOutput {
|
2024-05-07 03:46:39 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, getTransformProperties(ctx, "rlib"))
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2024-05-14 03:03:34 +02:00
|
|
|
func TransformRlibstoStaticlib(ctx android.ModuleContext, mainSrc android.Path, deps []cc.RustRlibDep,
|
|
|
|
outputFile android.WritablePath) android.Path {
|
|
|
|
|
|
|
|
var rustPathDeps PathDeps
|
|
|
|
var rustFlags Flags
|
|
|
|
|
|
|
|
for _, rlibDep := range deps {
|
|
|
|
rustPathDeps.RLibs = append(rustPathDeps.RLibs, RustLibrary{Path: rlibDep.LibPath, CrateName: rlibDep.CrateName})
|
|
|
|
rustPathDeps.linkDirs = append(rustPathDeps.linkDirs, rlibDep.LinkDirs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
ccModule := ctx.(cc.ModuleContext).Module().(*cc.Module)
|
|
|
|
toolchain := config.FindToolchain(ctx.Os(), ctx.Arch())
|
|
|
|
t := transformProperties{
|
|
|
|
// Crate name can be a predefined value as this is a staticlib and
|
|
|
|
// it does not need to be unique. The crate name is used for name
|
|
|
|
// mangling, but it is mixed with the metadata for that purpose, which we
|
|
|
|
// already set to the module name.
|
|
|
|
crateName: "generated_rust_staticlib",
|
|
|
|
is64Bit: toolchain.Is64Bit(),
|
|
|
|
targetTriple: toolchain.RustTriple(),
|
|
|
|
bootstrap: ccModule.Bootstrap(),
|
|
|
|
inRecovery: ccModule.InRecovery(),
|
|
|
|
inRamdisk: ccModule.InRamdisk(),
|
|
|
|
inVendorRamdisk: ccModule.InVendorRamdisk(),
|
|
|
|
|
|
|
|
// crateType indicates what type of crate to build
|
|
|
|
crateType: "staticlib",
|
|
|
|
|
|
|
|
// synthetic indicates whether this is an actual Rust module or not
|
|
|
|
synthetic: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
rustFlags = CommonDefaultFlags(ctx, toolchain, rustFlags)
|
|
|
|
rustFlags = CommonLibraryCompilerFlags(ctx, rustFlags)
|
|
|
|
rustFlags.GlobalRustFlags = append(rustFlags.GlobalRustFlags, "-C lto=thin")
|
|
|
|
|
|
|
|
rustFlags.EmitXrefs = false
|
|
|
|
|
|
|
|
return transformSrctoCrate(ctx, mainSrc, rustPathDeps, rustFlags, outputFile, t).outputFile
|
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func TransformSrctoDylib(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2021-03-19 23:06:02 +01:00
|
|
|
outputFile android.WritablePath) buildOutput {
|
2024-05-15 16:59:47 +02:00
|
|
|
if ctx.RustModule().compiler.Thinlto() {
|
|
|
|
flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
|
|
|
|
}
|
2024-02-20 22:39:40 +01:00
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, getTransformProperties(ctx, "dylib"))
|
2019-08-27 21:03:00 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func TransformSrctoStatic(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2021-03-19 23:06:02 +01:00
|
|
|
outputFile android.WritablePath) buildOutput {
|
2024-05-15 16:59:47 +02:00
|
|
|
if ctx.RustModule().compiler.Thinlto() {
|
|
|
|
flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
|
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, getTransformProperties(ctx, "staticlib"))
|
2019-10-18 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func TransformSrctoShared(ctx ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
2021-03-19 23:06:02 +01:00
|
|
|
outputFile android.WritablePath) buildOutput {
|
2024-05-15 16:59:47 +02:00
|
|
|
if ctx.RustModule().compiler.Thinlto() {
|
|
|
|
flags.GlobalRustFlags = append(flags.GlobalRustFlags, "-C lto=thin")
|
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, getTransformProperties(ctx, "cdylib"))
|
2019-10-18 23:49:46 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func TransformSrctoProcMacro(ctx ModuleContext, mainSrc android.Path, deps PathDeps,
|
2021-03-19 23:06:02 +01:00
|
|
|
flags Flags, outputFile android.WritablePath) buildOutput {
|
2024-05-07 03:46:39 +02:00
|
|
|
return transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, getTransformProperties(ctx, "proc-macro"))
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func makeLibFlags(deps PathDeps) []string {
|
2021-03-19 23:06:02 +01:00
|
|
|
var libFlags []string
|
|
|
|
|
|
|
|
// Collect library/crate flags
|
2023-09-22 05:58:59 +02:00
|
|
|
for _, lib := range deps.RLibs {
|
|
|
|
libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
|
2021-03-19 23:06:02 +01:00
|
|
|
}
|
2023-09-22 05:58:59 +02:00
|
|
|
for _, lib := range deps.DyLibs {
|
|
|
|
libFlags = append(libFlags, "--extern "+lib.CrateName+"="+lib.Path.String())
|
2021-03-19 23:06:02 +01:00
|
|
|
}
|
2023-09-22 05:58:59 +02:00
|
|
|
for _, proc_macro := range deps.ProcMacros {
|
|
|
|
libFlags = append(libFlags, "--extern "+proc_macro.CrateName+"="+proc_macro.Path.String())
|
2021-03-19 23:06:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range deps.linkDirs {
|
2023-09-22 05:58:59 +02:00
|
|
|
libFlags = append(libFlags, "-L "+path)
|
2021-03-19 23:06:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return libFlags
|
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
func rustEnvVars(ctx android.ModuleContext, deps PathDeps, crateName string, cargoOutDir android.OptionalPath) []string {
|
2021-03-19 23:06:02 +01:00
|
|
|
var envVars []string
|
|
|
|
|
|
|
|
// 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.
|
2024-05-07 03:46:39 +02:00
|
|
|
if crateName == "std" {
|
|
|
|
envVars = append(envVars, "STD_ENV_ARCH="+config.StdEnvArch[ctx.Arch().ArchType])
|
2021-03-19 23:06:02 +01:00
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
if len(deps.SrcDeps) > 0 && cargoOutDir.Valid() {
|
|
|
|
moduleGenDir := cargoOutDir
|
2021-03-19 23:06:02 +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.
|
2023-09-22 05:58:59 +02:00
|
|
|
var outDirPrefix string
|
|
|
|
if !filepath.IsAbs(moduleGenDir.String()) {
|
2021-03-19 23:06:02 +01:00
|
|
|
// If OUT_DIR is not absolute, we use $$PWD to generate an absolute path (os.Getwd() returns '/')
|
2023-09-22 05:58:59 +02:00
|
|
|
outDirPrefix = "$$PWD/"
|
2021-03-19 23:06:02 +01:00
|
|
|
} else {
|
2023-09-22 05:58:59 +02:00
|
|
|
// If OUT_DIR is absolute, then moduleGenDir will be an absolute path, so we don't need to set this to anything.
|
|
|
|
outDirPrefix = ""
|
2021-03-19 23:06:02 +01:00
|
|
|
}
|
2023-09-22 05:58:59 +02:00
|
|
|
envVars = append(envVars, "OUT_DIR="+filepath.Join(outDirPrefix, moduleGenDir.String()))
|
2023-04-01 08:05:16 +02:00
|
|
|
} else {
|
|
|
|
// TODO(pcc): Change this to "OUT_DIR=" after fixing crates to not rely on this value.
|
|
|
|
envVars = append(envVars, "OUT_DIR=out")
|
2021-03-19 23:06:02 +01:00
|
|
|
}
|
|
|
|
|
2023-06-26 23:10:13 +02:00
|
|
|
envVars = append(envVars, "ANDROID_RUST_VERSION="+config.GetRustVersion(ctx))
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
if rustMod, ok := ctx.Module().(*Module); ok && rustMod.compiler.cargoEnvCompat() {
|
|
|
|
// We only emulate cargo environment variables for 3p code, which is only ever built
|
|
|
|
// by defining a Rust module, so we only need to set these for true Rust modules.
|
|
|
|
if bin, ok := rustMod.compiler.(*binaryDecorator); ok {
|
2023-06-26 23:10:13 +02:00
|
|
|
envVars = append(envVars, "CARGO_BIN_NAME="+bin.getStem(ctx))
|
|
|
|
}
|
2024-05-07 03:46:39 +02:00
|
|
|
envVars = append(envVars, "CARGO_CRATE_NAME="+crateName)
|
|
|
|
envVars = append(envVars, "CARGO_PKG_NAME="+crateName)
|
|
|
|
pkgVersion := rustMod.compiler.cargoPkgVersion()
|
2023-06-26 23:10:13 +02:00
|
|
|
if pkgVersion != "" {
|
|
|
|
envVars = append(envVars, "CARGO_PKG_VERSION="+pkgVersion)
|
2023-07-28 18:42:20 +02:00
|
|
|
|
|
|
|
// Ensure the version is in the form of "x.y.z" (approximately semver compliant).
|
|
|
|
//
|
|
|
|
// For our purposes, we don't care to enforce that these are integers since they may
|
|
|
|
// include other characters at times (e.g. sometimes the patch version is more than an integer).
|
|
|
|
if strings.Count(pkgVersion, ".") == 2 {
|
|
|
|
var semver_parts = strings.Split(pkgVersion, ".")
|
|
|
|
envVars = append(envVars, "CARGO_PKG_VERSION_MAJOR="+semver_parts[0])
|
|
|
|
envVars = append(envVars, "CARGO_PKG_VERSION_MINOR="+semver_parts[1])
|
|
|
|
envVars = append(envVars, "CARGO_PKG_VERSION_PATCH="+semver_parts[2])
|
|
|
|
}
|
2023-06-26 23:10:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 23:40:13 +02:00
|
|
|
if ctx.Darwin() {
|
|
|
|
envVars = append(envVars, "ANDROID_RUST_DARWIN=true")
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:06:02 +01:00
|
|
|
return envVars
|
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags,
|
|
|
|
outputFile android.WritablePath, t transformProperties) buildOutput {
|
2019-08-27 21:03:00 +02:00
|
|
|
|
|
|
|
var inputs android.Paths
|
2023-10-02 20:39:17 +02:00
|
|
|
var implicits android.Paths
|
|
|
|
var orderOnly android.Paths
|
2020-04-09 15:56:02 +02:00
|
|
|
var output buildOutput
|
2021-03-19 23:06:02 +01:00
|
|
|
var rustcFlags, linkFlags []string
|
2023-09-22 05:58:59 +02:00
|
|
|
var earlyLinkFlags string
|
2020-04-09 15:56:02 +02:00
|
|
|
|
|
|
|
output.outputFile = outputFile
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
envVars := rustEnvVars(ctx, deps, t.crateName, t.cargoOutDir)
|
2023-09-22 05:58:59 +02:00
|
|
|
|
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...)
|
2024-05-07 03:46:39 +02:00
|
|
|
rustcFlags = append(rustcFlags, "--crate-type="+t.crateType)
|
|
|
|
if t.crateName != "" {
|
|
|
|
rustcFlags = append(rustcFlags, "--crate-name="+t.crateName)
|
2019-11-01 03:38:29 +01:00
|
|
|
}
|
2024-05-07 03:46:39 +02:00
|
|
|
if t.targetTriple != "" {
|
|
|
|
rustcFlags = append(rustcFlags, "--target="+t.targetTriple)
|
|
|
|
linkFlags = append(linkFlags, "-target "+t.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")
|
|
|
|
|
2024-02-20 22:39:40 +01:00
|
|
|
// Enable incremental compilation if requested by user
|
|
|
|
if ctx.Config().IsEnvTrue("SOONG_RUSTC_INCREMENTAL") {
|
|
|
|
incrementalPath := android.PathForOutput(ctx, "rustc").String()
|
|
|
|
|
|
|
|
rustcFlags = append(rustcFlags, "-C incremental="+incrementalPath)
|
|
|
|
} else {
|
|
|
|
rustcFlags = append(rustcFlags, "-C codegen-units=1")
|
|
|
|
}
|
|
|
|
|
2023-02-25 01:58:18 +01:00
|
|
|
// Disallow experimental features
|
2023-10-26 23:01:51 +02:00
|
|
|
modulePath := ctx.ModuleDir()
|
2023-02-25 01:58:18 +01:00
|
|
|
if !(android.IsThirdPartyPath(modulePath) || strings.HasPrefix(modulePath, "prebuilts")) {
|
2023-05-31 20:53:44 +02:00
|
|
|
rustcFlags = append(rustcFlags, "-Zallow-features=\"\"")
|
2021-12-04 02:17:28 +01:00
|
|
|
}
|
|
|
|
|
2019-09-20 20:00:37 +02:00
|
|
|
// Collect linker flags
|
2023-07-14 04:03:39 +02:00
|
|
|
if !ctx.Darwin() {
|
2023-09-22 05:58:59 +02:00
|
|
|
earlyLinkFlags = "-Wl,--as-needed"
|
|
|
|
}
|
|
|
|
|
|
|
|
linkFlags = append(linkFlags, flags.GlobalLinkFlags...)
|
|
|
|
linkFlags = append(linkFlags, flags.LinkFlags...)
|
|
|
|
|
|
|
|
// Check if this module needs to use the bootstrap linker
|
2024-05-07 03:46:39 +02:00
|
|
|
if t.bootstrap && !t.inRecovery && !t.inRamdisk && !t.inVendorRamdisk {
|
2023-09-22 05:58:59 +02:00
|
|
|
dynamicLinker := "-Wl,-dynamic-linker,/system/bin/bootstrap/linker"
|
2024-05-07 03:46:39 +02:00
|
|
|
if t.is64Bit {
|
2023-09-22 05:58:59 +02:00
|
|
|
dynamicLinker += "64"
|
|
|
|
}
|
|
|
|
linkFlags = append(linkFlags, dynamicLinker)
|
2021-07-22 16:52:06 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
libFlags := makeLibFlags(deps)
|
|
|
|
|
2019-08-27 21:03:00 +02:00
|
|
|
// Collect dependencies
|
2023-09-22 05:58:59 +02:00
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.RLibs)...)
|
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.DyLibs)...)
|
|
|
|
implicits = append(implicits, rustLibsToPaths(deps.ProcMacros)...)
|
2023-10-02 20:39:17 +02:00
|
|
|
implicits = append(implicits, deps.StaticLibs...)
|
|
|
|
implicits = append(implicits, deps.SharedLibDeps...)
|
2023-09-22 05:58:59 +02:00
|
|
|
implicits = append(implicits, deps.srcProviderFiles...)
|
2023-10-02 20:39:17 +02:00
|
|
|
implicits = append(implicits, deps.AfdoProfiles...)
|
2023-09-22 05:58:59 +02:00
|
|
|
|
2023-10-02 20:39:17 +02:00
|
|
|
implicits = append(implicits, deps.CrtBegin...)
|
|
|
|
implicits = append(implicits, deps.CrtEnd...)
|
2023-09-22 05:58:59 +02:00
|
|
|
|
2023-10-02 20:39:17 +02:00
|
|
|
orderOnly = append(orderOnly, deps.SharedLibs...)
|
2023-03-28 22:54:00 +02:00
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
if !t.synthetic {
|
|
|
|
// Only worry about OUT_DIR for actual Rust modules.
|
|
|
|
// Libraries built from cc use generated source, and do not utilize OUT_DIR.
|
|
|
|
if len(deps.SrcDeps) > 0 {
|
|
|
|
var outputs android.WritablePaths
|
|
|
|
|
|
|
|
for _, genSrc := range deps.SrcDeps {
|
|
|
|
if android.SuffixInList(outputs.Strings(), genSubDir+genSrc.Base()) {
|
|
|
|
ctx.PropertyErrorf("srcs",
|
|
|
|
"multiple source providers generate the same filename output: "+genSrc.Base())
|
|
|
|
}
|
|
|
|
outputs = append(outputs, android.PathForModuleOut(ctx, genSubDir+genSrc.Base()))
|
2020-07-10 03:03:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: cp,
|
|
|
|
Description: "cp " + t.cargoOutDir.Path().Rel(),
|
|
|
|
Outputs: outputs,
|
|
|
|
Inputs: deps.SrcDeps,
|
|
|
|
Args: map[string]string{
|
|
|
|
"outDir": t.cargoOutDir.String(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
implicits = append(implicits, outputs.Paths()...)
|
|
|
|
}
|
2020-07-10 03:03:28 +02:00
|
|
|
}
|
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
if !t.synthetic {
|
|
|
|
// Only worry about clippy for actual Rust modules.
|
|
|
|
// Libraries built from cc use generated source, and don't need to run clippy.
|
|
|
|
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,
|
|
|
|
OrderOnly: orderOnly,
|
|
|
|
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)
|
|
|
|
}
|
2020-07-21 19:28:27 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2023-10-02 20:39:17 +02:00
|
|
|
Rule: rustc,
|
|
|
|
Description: "rustc " + main.Rel(),
|
|
|
|
Output: outputFile,
|
|
|
|
Inputs: inputs,
|
|
|
|
Implicits: implicits,
|
|
|
|
OrderOnly: orderOnly,
|
2023-09-22 05:58:59 +02:00
|
|
|
Args: map[string]string{
|
2023-10-02 20:39:17 +02:00
|
|
|
"rustcFlags": strings.Join(rustcFlags, " "),
|
|
|
|
"earlyLinkFlags": earlyLinkFlags,
|
|
|
|
"linkFlags": strings.Join(linkFlags, " "),
|
|
|
|
"libFlags": strings.Join(libFlags, " "),
|
|
|
|
"crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
|
|
|
|
"crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
|
|
|
|
"envVars": strings.Join(envVars, " "),
|
2023-09-22 05:58:59 +02:00
|
|
|
},
|
|
|
|
})
|
2019-08-27 21:03:00 +02:00
|
|
|
|
2024-05-07 03:46:39 +02:00
|
|
|
if !t.synthetic {
|
|
|
|
// Only emit xrefs for true Rust modules.
|
|
|
|
if flags.EmitXrefs {
|
|
|
|
kytheFile := android.PathForModuleOut(ctx, outputFile.Base()+".kzip")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: kytheExtract,
|
|
|
|
Description: "Xref Rust extractor " + main.Rel(),
|
|
|
|
Output: kytheFile,
|
|
|
|
Inputs: inputs,
|
|
|
|
Implicits: implicits,
|
|
|
|
OrderOnly: orderOnly,
|
|
|
|
Args: map[string]string{
|
|
|
|
"rustcFlags": strings.Join(rustcFlags, " "),
|
|
|
|
"linkFlags": strings.Join(linkFlags, " "),
|
|
|
|
"libFlags": strings.Join(libFlags, " "),
|
|
|
|
"crtBegin": strings.Join(deps.CrtBegin.Strings(), " "),
|
|
|
|
"crtEnd": strings.Join(deps.CrtEnd.Strings(), " "),
|
|
|
|
"envVars": strings.Join(envVars, " "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
output.kytheFile = kytheFile
|
|
|
|
}
|
2022-04-19 05:12:56 +02:00
|
|
|
}
|
2020-04-09 15:56:02 +02:00
|
|
|
return output
|
|
|
|
}
|
2021-03-19 23:06:02 +01:00
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
func Rustdoc(ctx ModuleContext, main android.Path, deps PathDeps,
|
|
|
|
flags Flags) android.ModuleOutPath {
|
2021-03-19 23:06:02 +01:00
|
|
|
|
|
|
|
rustdocFlags := append([]string{}, flags.RustdocFlags...)
|
|
|
|
rustdocFlags = append(rustdocFlags, "--sysroot=/dev/null")
|
|
|
|
|
2021-04-28 02:12:02 +02:00
|
|
|
// Build an index for all our crates. -Z unstable options is required to use
|
|
|
|
// this flag.
|
|
|
|
rustdocFlags = append(rustdocFlags, "-Z", "unstable-options", "--enable-index-page")
|
|
|
|
|
2021-03-19 23:06:02 +01:00
|
|
|
targetTriple := ctx.toolchain().RustTriple()
|
|
|
|
|
|
|
|
// Collect rustc flags
|
|
|
|
if targetTriple != "" {
|
|
|
|
rustdocFlags = append(rustdocFlags, "--target="+targetTriple)
|
|
|
|
}
|
|
|
|
|
|
|
|
crateName := ctx.RustModule().CrateName()
|
2021-04-28 02:12:02 +02:00
|
|
|
rustdocFlags = append(rustdocFlags, "--crate-name "+crateName)
|
2021-03-19 23:06:02 +01:00
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
rustdocFlags = append(rustdocFlags, makeLibFlags(deps)...)
|
2021-03-19 23:06:02 +01:00
|
|
|
docTimestampFile := android.PathForModuleOut(ctx, "rustdoc.timestamp")
|
2021-04-28 02:12:02 +02:00
|
|
|
|
2021-07-30 22:25:42 +02:00
|
|
|
// Silence warnings about renamed lints for third-party crates
|
2023-10-26 23:01:51 +02:00
|
|
|
modulePath := ctx.ModuleDir()
|
2021-07-30 22:25:42 +02:00
|
|
|
if android.IsThirdPartyPath(modulePath) {
|
2023-02-15 01:09:49 +01:00
|
|
|
rustdocFlags = append(rustdocFlags, " -A warnings")
|
2021-07-30 22:25:42 +02:00
|
|
|
}
|
2021-07-28 21:07:16 +02:00
|
|
|
|
2021-04-28 02:12:02 +02:00
|
|
|
// Yes, the same out directory is used simultaneously by all rustdoc builds.
|
|
|
|
// This is what cargo does. The docs for individual crates get generated to
|
|
|
|
// a subdirectory named for the crate, and rustdoc synchronizes writes to
|
|
|
|
// shared pieces like the index and search data itself.
|
|
|
|
// https://github.com/rust-lang/rust/blob/master/src/librustdoc/html/render/write_shared.rs#L144-L146
|
|
|
|
docDir := android.PathForOutput(ctx, "rustdoc")
|
2021-03-19 23:06:02 +01:00
|
|
|
|
2023-09-22 05:58:59 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: rustdoc,
|
|
|
|
Description: "rustdoc " + main.Rel(),
|
|
|
|
Output: docTimestampFile,
|
|
|
|
Input: main,
|
|
|
|
Implicit: ctx.RustModule().UnstrippedOutputFile(),
|
|
|
|
Args: map[string]string{
|
|
|
|
"rustdocFlags": strings.Join(rustdocFlags, " "),
|
|
|
|
"outDir": docDir.String(),
|
2024-05-07 03:46:39 +02:00
|
|
|
"envVars": strings.Join(rustEnvVars(ctx, deps, crateName, ctx.RustModule().compiler.cargoOutDir()), " "),
|
2023-09-22 05:58:59 +02:00
|
|
|
},
|
|
|
|
})
|
2021-03-19 23:06:02 +01:00
|
|
|
|
|
|
|
return docTimestampFile
|
|
|
|
}
|