Skip to content

Commit

Permalink
Update CLI build script and Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Jun 14, 2024
1 parent 4c2040d commit 2160806
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
19 changes: 10 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@ FROM ubuntu:latest

RUN apt update && apt install -y \
cmake \
clang-14 lld-14 \
g++-12-riscv64-linux-gnu
clang-18 \
tcc libtcc-dev \
g++-13-riscv64-linux-gnu

ENV CXX=clang++-14
ENV CXX=clang++-18

COPY lib /app/lib
COPY emulator/build.sh /app/emulator/build.sh
COPY emulator/CMakeLists.txt /app/emulator/CMakeLists.txt
COPY emulator/src /app/emulator/src
COPY binaries/measure_mips/fib.c /app/emulator/fib.c

# Emulator program
# Fast emulation (with TCC JIT compilation)
WORKDIR /app/emulator
RUN ./build.sh && cp /app/emulator/.build/rvlinux /app
RUN ./build.sh --tcc && cp .build/rvlinux /app/rvlinux

# Faster emulator program (no C-extension)
WORKDIR /app/emulator/.build
RUN cmake .. -DRISCV_EXT_C=OFF -DRISCV_BINARY_TRANSLATION=ON && make -j6 && cp rvlinux /app/rvlinux-fast
# Fastest emulator (with binary translation)
WORKDIR /app/emulator
RUN ./build.sh --bintr && cp .build/rvlinux /app/rvlinux-fast

# Clean up
RUN rm -rf /app/emulator/.build

# Example program
WORKDIR /app
RUN riscv64-linux-gnu-gcc-12 -march=rv32g -mabi=ilp32d -static -O2 -nostdlib -ffreestanding emulator/fib.c -o fib
RUN riscv64-linux-gnu-gcc-13 -march=rv32g -mabi=ilp32d -static -O2 -nostdlib -ffreestanding emulator/fib.c -o fib

# Provdide a path to your cli apps executable
WORKDIR /app
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ A fib(256000000) program for testing is built automatically. You can test-run it
docker run -v $PWD/binaries:/app/binaries --rm -i -t libriscv fib
```

If you want to use `rvlinux` from terminal, or you want to compile RISC-V programs, you can enter the docker container instead of using it from the outside. A 64-bit RISC-V compiler is installed in the container, and it can be used to build RISC-V programs. You can enter the container like so:
You can enter the docker container instead of using it from the outside. A 64-bit RISC-V compiler is installed in the container, and it can be used to build RISC-V programs.

```sh
docker run -v $PWD/binaries:/app/binaries --entrypoint='' -i -t libriscv /bin/bash
```

Inside the container you have access to the emulator `rvlinux`, and the compilers `riscv64-linux-gnu-gcc-12` and `riscv64-linux-gnu-g++-12`. There is also `rvlinux-fast` which cannot run RISC-V programs with compressed instructions, but is a lot faster.
Inside the container you have access to `rvlinux`, and the compilers `riscv64-linux-gnu-gcc-13` and `riscv64-linux-gnu-g++-13`. There is also `rvlinux-fast` which uses binary translation to make emulation a lot faster, but needs time to compile beforehand.


## Installing a RISC-V GCC compiler
Expand All @@ -88,7 +89,7 @@ On Ubuntu and Linux distributions like it, you can install a 64-bit RISC-V GCC c
sudo apt install gcc-12-riscv64-linux-gnu g++-12-riscv64-linux-gnu
```

Depending on your distro you may have access to GCC versions 10 to 13. Now you have a full Linux C/C++ compiler for 64-bit RISC-V.
Depending on your distro you may have access to GCC versions 10 to 14. Now you have a full Linux C/C++ compiler for 64-bit RISC-V.

To build smaller and leaner programs you will want a (limited) Linux userspace environment. Check out the guide on how to [build a Newlib compiler](/docs/NEWLIB.md).

Expand Down
31 changes: 30 additions & 1 deletion emulator/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
#!/usr/bin/env bash
set -e

OPTS=""

function usage()
{
progname=$(basename $0)
cat << HEREDOC
Usage: $progname [--num NUM] [--time TIME_STR] [--verbose] [--dry-run]
optional arguments:
-h, --help show this help message and exit
-b, --bintr enable binary translation using system compiler
-t, --tcc jit-compile using tcc
-v, --verbose increase the verbosity of the bash script
HEREDOC
}

while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help ) usage; exit; ;;
-b|--bintr) OPTS="$OPTS -DRISCV_BINARY_TRANSLATION=ON -DRISCV_LIBTCC=OFF" ;;
-t|--tcc ) OPTS="$OPTS -DRISCV_BINARY_TRANSLATION=ON -DRISCV_LIBTCC=ON" ;;
-v|--verbose ) set -x ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done

mkdir -p .build
pushd .build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake .. -DCMAKE_BUILD_TYPE=Release $OPTS
make -j6
popd

Expand Down

0 comments on commit 2160806

Please sign in to comment.