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"
|
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ = pctx.SourcePathVariable("rustcCmd", "${config.RustBin}/rustc")
|
|
|
|
rustc = pctx.AndroidStaticRule("rustc",
|
|
|
|
blueprint.RuleParams{
|
|
|
|
Command: "$rustcCmd " +
|
|
|
|
"-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
|
|
|
},
|
2019-09-21 02:00:37 +08:00
|
|
|
"rustcFlags", "linkFlags", "libFlags", "crtBegin", "crtEnd")
|
2019-08-28 03:03:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
func TransformSrcToBinary(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
|
|
|
outputFile android.WritablePath, includeDirs []string) {
|
2019-11-21 02:14:33 +08:00
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C lto")
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "bin", includeDirs)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
func TransformSrctoRlib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
|
|
|
outputFile android.WritablePath, includeDirs []string) {
|
|
|
|
transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "rlib", includeDirs)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
func TransformSrctoDylib(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
|
|
|
outputFile android.WritablePath, includeDirs []string) {
|
|
|
|
transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "dylib", includeDirs)
|
2019-08-28 03:03:00 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
func TransformSrctoStatic(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
|
|
|
outputFile android.WritablePath, includeDirs []string) {
|
2019-11-21 02:14:33 +08:00
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C lto")
|
2019-11-06 04:16:46 +08:00
|
|
|
transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "staticlib", includeDirs)
|
2019-10-19 05:49:46 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
func TransformSrctoShared(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps, flags Flags,
|
|
|
|
outputFile android.WritablePath, includeDirs []string) {
|
2019-11-21 02:14:33 +08:00
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C lto")
|
2019-11-06 04:16:46 +08:00
|
|
|
transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "cdylib", includeDirs)
|
2019-10-19 05:49:46 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
func TransformSrctoProcMacro(ctx android.ModuleContext, mainSrc android.Path, deps PathDeps,
|
|
|
|
flags Flags, outputFile android.WritablePath, includeDirs []string) {
|
|
|
|
transformSrctoCrate(ctx, mainSrc, deps, flags, outputFile, "proc-macro", includeDirs)
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-06 04:16:46 +08:00
|
|
|
func transformSrctoCrate(ctx android.ModuleContext, main android.Path, deps PathDeps, flags Flags,
|
|
|
|
outputFile android.WritablePath, crate_type string, includeDirs []string) {
|
2019-08-28 03:03:00 +08:00
|
|
|
|
|
|
|
var inputs android.Paths
|
2019-11-06 04:16:46 +08:00
|
|
|
var implicits android.Paths
|
2019-09-21 02:00:37 +08:00
|
|
|
var libFlags, rustcFlags, linkFlags []string
|
2019-08-28 03:03:00 +08:00
|
|
|
crate_name := ctx.(ModuleContext).CrateName()
|
2019-09-24 01:10:40 +08:00
|
|
|
targetTriple := ctx.(ModuleContext).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
|
|
|
}
|
2019-11-01 01:44:40 +08:00
|
|
|
// TODO once we have static libraries in the host prebuilt .bp, this
|
|
|
|
// should be unconditionally added.
|
|
|
|
if !ctx.Host() {
|
|
|
|
// If we're on a device build, do not use 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())
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range includeDirs {
|
|
|
|
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...)
|
|
|
|
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
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: rustc,
|
|
|
|
Description: "rustc " + main.Rel(),
|
|
|
|
Output: outputFile,
|
|
|
|
Inputs: inputs,
|
2019-11-06 04:16:46 +08:00
|
|
|
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(),
|
2019-08-28 03:03:00 +08:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|