diff --git a/.gitignore b/.gitignore index 978349dd..7a23bd9e 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ Cargo.lock .deps/ /examples/is13 *.DS_Store +tests/programs/deps diff --git a/tests/programs/_build_clang.sh b/tests/programs/_build_clang.sh new file mode 100644 index 00000000..5d64e671 --- /dev/null +++ b/tests/programs/_build_clang.sh @@ -0,0 +1,7 @@ +set -ex + +DOCKER="${DOCKER:-docker}" +# docker pull docker.io/cryptape/llvm-n-rust:20240630 +DOCKER_IMAGE="${DOCKER_IMAGE:-docker.io/cryptape/llvm-n-rust@sha256:bafaf76d4f342a69b8691c08e77a330b7740631f3d1d9c9bee4ead521b29ee55}" + +$DOCKER run --rm -e UID=`id -u` -e GID=`id -g` $DOCKER_RUN_ARGS -v `pwd`:/code $DOCKER_IMAGE bash _build_clang_native.sh diff --git a/tests/programs/_build_clang_native.sh b/tests/programs/_build_clang_native.sh new file mode 100644 index 00000000..b9d3dba0 --- /dev/null +++ b/tests/programs/_build_clang_native.sh @@ -0,0 +1,27 @@ +set -ex + +ROOT_DIR=$(pwd) +CLANG="${CLANG:-clang-18}" +LD="${CLANG/clang/ld.lld}" +CFLAGS="--target=riscv64 -march=rv64imac_zba_zbb_zbc_zbs -nostdinc -isystem $ROOT_DIR/deps/musl/release/include -c -fdata-sections -ffunction-sections" +LDFLAGS="--gc-sections -nostdlib --sysroot $ROOT_DIR/deps/musl/release -L$ROOT_DIR/deps/musl/release/lib -lc -lgcc" + +if [ ! -d deps ]; then + mkdir deps +fi + +if [ ! -d deps/musl ]; then + cd deps + git clone https://github.com/xxuejie/musl + cd musl + git checkout 603d5e9 + cd ../.. +fi + +if [ ! -d deps/musl/release ]; then + cd deps/musl + CLANG=$CLANG ./ckb/build.sh + cd - +fi + +$CLANG $CFLAGS clang_sample.c -o clang_sample.o && $LD $LDFLAGS clang_sample.o -o clang_sample && rm clang_sample.o diff --git a/tests/programs/clang_sample b/tests/programs/clang_sample new file mode 100755 index 00000000..9d523ae4 Binary files /dev/null and b/tests/programs/clang_sample differ diff --git a/tests/programs/clang_sample.c b/tests/programs/clang_sample.c new file mode 100644 index 00000000..33c14ce1 --- /dev/null +++ b/tests/programs/clang_sample.c @@ -0,0 +1,3 @@ +int main() { + return 0; +} diff --git a/tests/test_misc.rs b/tests/test_misc.rs index 8719d0bc..b4c9874d 100644 --- a/tests/test_misc.rs +++ b/tests/test_misc.rs @@ -13,6 +13,7 @@ use rand::{thread_rng, Rng}; use std::fs; use std::sync::atomic::{AtomicU8, Ordering}; use std::sync::Arc; +pub mod machine_build; #[test] pub fn test_andi() { @@ -431,3 +432,32 @@ pub fn test_outofcycles_in_syscall() { assert_eq!(machine.cycles(), 108); assert_eq!(machine.registers()[A0], 39); } + +#[test] +pub fn test_clang() { + { + let mut machine = machine_build::int_v1_imcb("tests/programs/clang_sample"); + let ret = machine.run(); + assert!(ret.is_ok()); + } + + #[cfg(has_asm)] + { + let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/clang_sample"); + let ret_asm = machine_asm.run(); + assert!(ret_asm.is_ok()); + } + + { + let mut machine = machine_build::int_v2_imacb("tests/programs/clang_sample"); + let ret = machine.run(); + assert!(ret.is_ok()); + } + + #[cfg(has_asm)] + { + let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/clang_sample"); + let ret_asm = machine_asm.run(); + assert!(ret_asm.is_ok()); + } +}