Skip to content

Commit

Permalink
Add a sample for showing how to build binary with clang (#439)
Browse files Browse the repository at this point in the history
* Add a sample for showing how to build binary with clang

* CCFLAGS => CFLAGS

* Using tweakable variables in bash script
  • Loading branch information
mohanson authored Aug 21, 2024
1 parent b32522c commit 3149cd1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Cargo.lock
.deps/
/examples/is13
*.DS_Store
tests/programs/deps
7 changes: 7 additions & 0 deletions tests/programs/_build_clang.sh
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions tests/programs/_build_clang_native.sh
Original file line number Diff line number Diff line change
@@ -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
Binary file added tests/programs/clang_sample
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/programs/clang_sample.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
int main() {
return 0;
}
30 changes: 30 additions & 0 deletions tests/test_misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 3149cd1

Please sign in to comment.