Skip to content

Commit 3149cd1

Browse files
authored
Add a sample for showing how to build binary with clang (#439)
* Add a sample for showing how to build binary with clang * CCFLAGS => CFLAGS * Using tweakable variables in bash script
1 parent b32522c commit 3149cd1

6 files changed

+68
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Cargo.lock
55
.deps/
66
/examples/is13
77
*.DS_Store
8+
tests/programs/deps

tests/programs/_build_clang.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set -ex
2+
3+
DOCKER="${DOCKER:-docker}"
4+
# docker pull docker.io/cryptape/llvm-n-rust:20240630
5+
DOCKER_IMAGE="${DOCKER_IMAGE:-docker.io/cryptape/llvm-n-rust@sha256:bafaf76d4f342a69b8691c08e77a330b7740631f3d1d9c9bee4ead521b29ee55}"
6+
7+
$DOCKER run --rm -e UID=`id -u` -e GID=`id -g` $DOCKER_RUN_ARGS -v `pwd`:/code $DOCKER_IMAGE bash _build_clang_native.sh

tests/programs/_build_clang_native.sh

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
set -ex
2+
3+
ROOT_DIR=$(pwd)
4+
CLANG="${CLANG:-clang-18}"
5+
LD="${CLANG/clang/ld.lld}"
6+
CFLAGS="--target=riscv64 -march=rv64imac_zba_zbb_zbc_zbs -nostdinc -isystem $ROOT_DIR/deps/musl/release/include -c -fdata-sections -ffunction-sections"
7+
LDFLAGS="--gc-sections -nostdlib --sysroot $ROOT_DIR/deps/musl/release -L$ROOT_DIR/deps/musl/release/lib -lc -lgcc"
8+
9+
if [ ! -d deps ]; then
10+
mkdir deps
11+
fi
12+
13+
if [ ! -d deps/musl ]; then
14+
cd deps
15+
git clone https://github.com/xxuejie/musl
16+
cd musl
17+
git checkout 603d5e9
18+
cd ../..
19+
fi
20+
21+
if [ ! -d deps/musl/release ]; then
22+
cd deps/musl
23+
CLANG=$CLANG ./ckb/build.sh
24+
cd -
25+
fi
26+
27+
$CLANG $CFLAGS clang_sample.c -o clang_sample.o && $LD $LDFLAGS clang_sample.o -o clang_sample && rm clang_sample.o

tests/programs/clang_sample

8.2 KB
Binary file not shown.

tests/programs/clang_sample.c

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int main() {
2+
return 0;
3+
}

tests/test_misc.rs

+30
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rand::{thread_rng, Rng};
1313
use std::fs;
1414
use std::sync::atomic::{AtomicU8, Ordering};
1515
use std::sync::Arc;
16+
pub mod machine_build;
1617

1718
#[test]
1819
pub fn test_andi() {
@@ -431,3 +432,32 @@ pub fn test_outofcycles_in_syscall() {
431432
assert_eq!(machine.cycles(), 108);
432433
assert_eq!(machine.registers()[A0], 39);
433434
}
435+
436+
#[test]
437+
pub fn test_clang() {
438+
{
439+
let mut machine = machine_build::int_v1_imcb("tests/programs/clang_sample");
440+
let ret = machine.run();
441+
assert!(ret.is_ok());
442+
}
443+
444+
#[cfg(has_asm)]
445+
{
446+
let mut machine_asm = machine_build::asm_v1_imcb("tests/programs/clang_sample");
447+
let ret_asm = machine_asm.run();
448+
assert!(ret_asm.is_ok());
449+
}
450+
451+
{
452+
let mut machine = machine_build::int_v2_imacb("tests/programs/clang_sample");
453+
let ret = machine.run();
454+
assert!(ret.is_ok());
455+
}
456+
457+
#[cfg(has_asm)]
458+
{
459+
let mut machine_asm = machine_build::asm_v2_imacb("tests/programs/clang_sample");
460+
let ret_asm = machine_asm.run();
461+
assert!(ret_asm.is_ok());
462+
}
463+
}

0 commit comments

Comments
 (0)