Skip to content

Commit

Permalink
Add rust test for /usr/bin/ld symlink
Browse files Browse the repository at this point in the history
This test will fail if the symlink from `/usr/bin/ld` to the llvm
toolchain directory is not made.
  • Loading branch information
dozzman committed Sep 26, 2021
1 parent 9027b93 commit 91c8ca4
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 0 deletions.
15 changes: 15 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,18 @@ http_archive(
"https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz",
],
)

# Rust rules (for testing)

http_archive(
name = "rules_rust",
sha256 = "82b30cbb46c61a9014de0a8d0443a45e6eb6bd9add35ab421cfb1943dc3271f5",
strip_prefix = "rules_rust-e589105b4e8181dd1d0d8ccaa0cf3267efb06e86",
urls = [
"https://github.com/bazelbuild/rules_rust/archive/e589105b4e8181dd1d0d8ccaa0cf3267efb06e86.tar.gz",
],
)

load("@rules_rust//rust:repositories.bzl", "rust_repositories")

rust_repositories()
1 change: 1 addition & 0 deletions tests/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
13 changes: 13 additions & 0 deletions tests/rust/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@rules_rust//rust:rust.bzl", "rust_binary", "rust_test")
load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")

cargo_build_script(
name = "build_script",
srcs = ["build.rs"],
)

rust_test(
name = "rust_test",
srcs = ["src/rust_test.rs"],
deps = [":build_script"],
)
181 changes: 181 additions & 0 deletions tests/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*This example project is used to test Rust's compatibility (on Mac) with a custom llvm_toolchain.*

Rust's `rustc` expects to find the `ld` executable in the same directory as the compiler executable, which is not (currently) the case for Mac compilation with `llvm_toolchain` as LLVM's `ld.lld` linker is still experimental on MacOS and therefore the default linker at `/usr/bin/ld` is used. When compiling Rust code we encounter an `executable "ld" doesn't exist` error. The current fix is to manually symlink `/usr/bin/ld` into the llvm toolchain's `bin` directory to ensure it is found. This is necessary to make compilation work for both normal rust crates and crates which use build scripts (`build.rs`).

This project contains a simple build script and rust executable to test that both work well with `/usr/bin/ld` into the llvm toolchain's `bin` directory. If the build succeeds it means `rustc` found `ld` both in the build script and rust binary invocations.
17 changes: 17 additions & 0 deletions tests/rust/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::env;
use std::fs;
use std::path::Path;

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let out = Path::new(&out_dir).join("hello.rs");

let _ = fs::write(
&out,
"pub fn hello() -> &'static str {
\"Hello, world!\"
}
");

println!("cargo:rerun-if-changed=build.rs");
}
11 changes: 11 additions & 0 deletions tests/rust/src/rust_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include!(concat!(env!("OUT_DIR"), "/hello.rs"));

#[cfg(test)]
mod test {
use crate::hello;

#[test]
fn test_hello() {
assert_eq!("Hello, world!", hello());
}
}
1 change: 1 addition & 0 deletions tests/scripts/run_external_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ fi
"${bazel}" --bazelrc=/dev/null test "${test_args[@]}" \
//tests/foreign:pcre \
@openssl//:libssl \
//tests/rust:rust_test \
$("${bazel}" query 'attr(timeout, short, tests(@com_google_absl//absl/...))') \
$("${bazel}" query 'tests(@io_bazel_rules_go//tests/core/cgo:all) except set(@io_bazel_rules_go//tests/core/cgo:cc_libs_test @io_bazel_rules_go//tests/core/cgo:opts_test)')

0 comments on commit 91c8ca4

Please sign in to comment.