Allow rust modules to depend on and use generated source code provided by SourceProvider modules and genrule modules without resorting to hardcoded output paths. All generated sources are now copied to a dependent module's intermediates directory, then OUT_DIR is set to point to that path when calling rustc. This matches the common convention used in most rust crates to include generated source code from the path defined in the OUT_DIR environment variable. A couple other small notable changes are included in this CL: * prebuiltLibraries can no longer include generated source files as they should be prebuilt. * srcPathFromModuleSrcs now excludes the main source file from the second return value so its a list of only the generated sources. Bug: 159064919 Test: Local example rust_library compiles with rust_bindgen dependency. Test: Local example rust_library compiles with genrule dependency. Test: Collision detected when multiple providers produce similar output. Test: New Soong tests pass. Change-Id: I59f54a25368c680b9086420c47ec24ab8cd1de6b
348 lines
9.6 KiB
Go
348 lines
9.6 KiB
Go
// 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 (
|
|
"io/ioutil"
|
|
"os"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
"android/soong/android"
|
|
"android/soong/cc"
|
|
)
|
|
|
|
var (
|
|
buildDir string
|
|
)
|
|
|
|
func setUp() {
|
|
var err error
|
|
buildDir, err = ioutil.TempDir("", "soong_rust_test")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func tearDown() {
|
|
os.RemoveAll(buildDir)
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
run := func() int {
|
|
setUp()
|
|
defer tearDown()
|
|
|
|
return m.Run()
|
|
}
|
|
|
|
os.Exit(run())
|
|
}
|
|
|
|
func testConfig(bp string) android.Config {
|
|
bp = bp + GatherRequiredDepsForTest()
|
|
|
|
fs := map[string][]byte{
|
|
"foo.rs": nil,
|
|
"foo.c": nil,
|
|
"src/bar.rs": nil,
|
|
"src/any.h": nil,
|
|
"liby.so": nil,
|
|
"libz.so": nil,
|
|
}
|
|
|
|
cc.GatherRequiredFilesForTest(fs)
|
|
|
|
return android.TestArchConfig(buildDir, nil, bp, fs)
|
|
}
|
|
|
|
func testRust(t *testing.T, bp string) *android.TestContext {
|
|
return testRustContext(t, bp, false)
|
|
}
|
|
|
|
func testRustCov(t *testing.T, bp string) *android.TestContext {
|
|
return testRustContext(t, bp, true)
|
|
}
|
|
|
|
func testRustContext(t *testing.T, bp string, coverage bool) *android.TestContext {
|
|
// TODO (b/140435149)
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("Only the Linux toolchain is supported for Rust")
|
|
}
|
|
|
|
t.Helper()
|
|
config := testConfig(bp)
|
|
|
|
if coverage {
|
|
config.TestProductVariables.GcovCoverage = proptools.BoolPtr(true)
|
|
config.TestProductVariables.Native_coverage = proptools.BoolPtr(true)
|
|
config.TestProductVariables.NativeCoveragePaths = []string{"*"}
|
|
}
|
|
|
|
ctx := CreateTestContext()
|
|
ctx.Register(config)
|
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
|
android.FailIfErrored(t, errs)
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
android.FailIfErrored(t, errs)
|
|
|
|
return ctx
|
|
}
|
|
|
|
func testRustError(t *testing.T, pattern string, bp string) {
|
|
// TODO (b/140435149)
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("Only the Linux toolchain is supported for Rust")
|
|
}
|
|
|
|
t.Helper()
|
|
config := testConfig(bp)
|
|
|
|
ctx := CreateTestContext()
|
|
ctx.Register(config)
|
|
|
|
_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
|
|
if len(errs) > 0 {
|
|
android.FailIfNoMatchingErrors(t, pattern, errs)
|
|
return
|
|
}
|
|
|
|
_, errs = ctx.PrepareBuildActions(config)
|
|
if len(errs) > 0 {
|
|
android.FailIfNoMatchingErrors(t, pattern, errs)
|
|
return
|
|
}
|
|
|
|
t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
|
|
}
|
|
|
|
// Test that we can extract the lib name from a lib path.
|
|
func TestLibNameFromFilePath(t *testing.T) {
|
|
libBarPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so.so")
|
|
libLibPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/liblib.dylib.so")
|
|
|
|
libBarName := libNameFromFilePath(libBarPath)
|
|
libLibName := libNameFromFilePath(libLibPath)
|
|
|
|
expectedResult := "bar.so"
|
|
if libBarName != expectedResult {
|
|
t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libBarName)
|
|
}
|
|
|
|
expectedResult = "lib.dylib"
|
|
if libLibName != expectedResult {
|
|
t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libLibPath)
|
|
}
|
|
}
|
|
|
|
// Test that we can extract the link path from a lib path.
|
|
func TestLinkPathFromFilePath(t *testing.T) {
|
|
barPath := android.PathForTesting("out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/libbar.so")
|
|
libName := linkPathFromFilePath(barPath)
|
|
expectedResult := "out/soong/.intermediates/external/libbar/libbar/linux_glibc_x86_64_shared/"
|
|
|
|
if libName != expectedResult {
|
|
t.Errorf("libNameFromFilePath returned the wrong name; expected '%#v', got '%#v'", expectedResult, libName)
|
|
}
|
|
}
|
|
|
|
// Test to make sure dependencies are being picked up correctly.
|
|
func TestDepsTracking(t *testing.T) {
|
|
ctx := testRust(t, `
|
|
rust_ffi_host_static {
|
|
name: "libstatic",
|
|
srcs: ["foo.rs"],
|
|
crate_name: "static",
|
|
}
|
|
rust_ffi_host_shared {
|
|
name: "libshared",
|
|
srcs: ["foo.rs"],
|
|
crate_name: "shared",
|
|
}
|
|
rust_library_host_dylib {
|
|
name: "libdylib",
|
|
srcs: ["foo.rs"],
|
|
crate_name: "dylib",
|
|
}
|
|
rust_library_host_rlib {
|
|
name: "librlib",
|
|
srcs: ["foo.rs"],
|
|
crate_name: "rlib",
|
|
}
|
|
rust_proc_macro {
|
|
name: "libpm",
|
|
srcs: ["foo.rs"],
|
|
crate_name: "pm",
|
|
}
|
|
rust_binary_host {
|
|
name: "fizz-buzz",
|
|
dylibs: ["libdylib"],
|
|
rlibs: ["librlib"],
|
|
proc_macros: ["libpm"],
|
|
static_libs: ["libstatic"],
|
|
shared_libs: ["libshared"],
|
|
srcs: ["foo.rs"],
|
|
}
|
|
`)
|
|
module := ctx.ModuleForTests("fizz-buzz", "linux_glibc_x86_64").Module().(*Module)
|
|
|
|
// Since dependencies are added to AndroidMk* properties, we can check these to see if they've been picked up.
|
|
if !android.InList("libdylib", module.Properties.AndroidMkDylibs) {
|
|
t.Errorf("Dylib dependency not detected (dependency missing from AndroidMkDylibs)")
|
|
}
|
|
|
|
if !android.InList("librlib", module.Properties.AndroidMkRlibs) {
|
|
t.Errorf("Rlib dependency not detected (dependency missing from AndroidMkRlibs)")
|
|
}
|
|
|
|
if !android.InList("libpm", module.Properties.AndroidMkProcMacroLibs) {
|
|
t.Errorf("Proc_macro dependency not detected (dependency missing from AndroidMkProcMacroLibs)")
|
|
}
|
|
|
|
if !android.InList("libshared", module.Properties.AndroidMkSharedLibs) {
|
|
t.Errorf("Shared library dependency not detected (dependency missing from AndroidMkSharedLibs)")
|
|
}
|
|
|
|
if !android.InList("libstatic", module.Properties.AndroidMkStaticLibs) {
|
|
t.Errorf("Static library dependency not detected (dependency missing from AndroidMkStaticLibs)")
|
|
}
|
|
}
|
|
|
|
func TestSourceProviderDeps(t *testing.T) {
|
|
ctx := testRust(t, `
|
|
rust_binary {
|
|
name: "fizz-buzz-dep",
|
|
srcs: [
|
|
"foo.rs",
|
|
":my_generator",
|
|
":libbindings",
|
|
],
|
|
}
|
|
rust_proc_macro {
|
|
name: "libprocmacro",
|
|
srcs: [
|
|
"foo.rs",
|
|
":my_generator",
|
|
":libbindings",
|
|
],
|
|
crate_name: "procmacro",
|
|
}
|
|
rust_library {
|
|
name: "libfoo",
|
|
srcs: [
|
|
"foo.rs",
|
|
":my_generator",
|
|
":libbindings"],
|
|
crate_name: "foo",
|
|
}
|
|
genrule {
|
|
name: "my_generator",
|
|
tools: ["any_rust_binary"],
|
|
cmd: "$(location) -o $(out) $(in)",
|
|
srcs: ["src/any.h"],
|
|
out: ["src/any.rs"],
|
|
}
|
|
rust_bindgen {
|
|
name: "libbindings",
|
|
stem: "bindings",
|
|
host_supported: true,
|
|
wrapper_src: "src/any.h",
|
|
}
|
|
`)
|
|
|
|
libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib").Rule("rustc")
|
|
if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/bindings.rs") {
|
|
t.Errorf("rust_bindgen generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
|
|
}
|
|
if !android.SuffixInList(libfoo.Implicits.Strings(), "/out/any.rs") {
|
|
t.Errorf("genrule generated source not included as implicit input for libfoo; Implicits %#v", libfoo.Implicits.Strings())
|
|
}
|
|
|
|
fizzBuzz := ctx.ModuleForTests("fizz-buzz-dep", "android_arm64_armv8-a").Rule("rustc")
|
|
if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/bindings.rs") {
|
|
t.Errorf("rust_bindgen generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
|
|
}
|
|
if !android.SuffixInList(fizzBuzz.Implicits.Strings(), "/out/any.rs") {
|
|
t.Errorf("genrule generated source not included as implicit input for fizz-buzz-dep; Implicits %#v", libfoo.Implicits.Strings())
|
|
}
|
|
|
|
libprocmacro := ctx.ModuleForTests("libprocmacro", "linux_glibc_x86_64").Rule("rustc")
|
|
if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/bindings.rs") {
|
|
t.Errorf("rust_bindgen generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
|
|
}
|
|
if !android.SuffixInList(libprocmacro.Implicits.Strings(), "/out/any.rs") {
|
|
t.Errorf("genrule generated source not included as implicit input for libprocmacro; Implicits %#v", libfoo.Implicits.Strings())
|
|
}
|
|
}
|
|
|
|
// Test to make sure proc_macros use host variants when building device modules.
|
|
func TestProcMacroDeviceDeps(t *testing.T) {
|
|
ctx := testRust(t, `
|
|
rust_library_host_rlib {
|
|
name: "libbar",
|
|
srcs: ["foo.rs"],
|
|
crate_name: "bar",
|
|
}
|
|
rust_proc_macro {
|
|
name: "libpm",
|
|
rlibs: ["libbar"],
|
|
srcs: ["foo.rs"],
|
|
crate_name: "pm",
|
|
}
|
|
rust_binary {
|
|
name: "fizz-buzz",
|
|
proc_macros: ["libpm"],
|
|
srcs: ["foo.rs"],
|
|
}
|
|
`)
|
|
rustc := ctx.ModuleForTests("libpm", "linux_glibc_x86_64").Rule("rustc")
|
|
|
|
if !strings.Contains(rustc.Args["libFlags"], "libbar/linux_glibc_x86_64") {
|
|
t.Errorf("Proc_macro is not using host variant of dependent modules.")
|
|
}
|
|
}
|
|
|
|
// Test that no_stdlibs suppresses dependencies on rust standard libraries
|
|
func TestNoStdlibs(t *testing.T) {
|
|
ctx := testRust(t, `
|
|
rust_binary {
|
|
name: "fizz-buzz",
|
|
srcs: ["foo.rs"],
|
|
no_stdlibs: true,
|
|
}`)
|
|
module := ctx.ModuleForTests("fizz-buzz", "android_arm64_armv8-a").Module().(*Module)
|
|
|
|
if android.InList("libstd", module.Properties.AndroidMkDylibs) {
|
|
t.Errorf("no_stdlibs did not suppress dependency on libstd")
|
|
}
|
|
}
|
|
|
|
// Test that libraries provide both 32-bit and 64-bit variants.
|
|
func TestMultilib(t *testing.T) {
|
|
ctx := testRust(t, `
|
|
rust_library_rlib {
|
|
name: "libfoo",
|
|
srcs: ["foo.rs"],
|
|
crate_name: "foo",
|
|
}`)
|
|
|
|
_ = ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_rlib")
|
|
_ = ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_rlib")
|
|
}
|