Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sample for showing how to build binary with clang #439

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
CC="clang-18"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the spirit that someone might copy this script, I recommend using tweakable variables, see CLANG and CLANGXX in this file for an example.

LD="ld.lld-18"
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=$CC ./ckb/build.sh
cd -
fi

$CC $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());
}
}
Loading