Skip to content

Commit

Permalink
gcc-riscv-docker: use Makefiles and restructure folder
Browse files Browse the repository at this point in the history
  • Loading branch information
niccantieni committed Aug 20, 2022
1 parent 77b24ba commit ce9deee
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 30 deletions.
25 changes: 24 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,27 @@ FROM ubuntu:latest

RUN apt update -y
RUN apt upgrade -y
RUN apt install -y build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu
RUN apt install -y build-essential gdb-multiarch qemu-system-misc gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu git binutils-riscv64-unknown-elf gcc-riscv64-unknown-elf device-tree-compiler

WORKDIR /root

RUN git clone https://github.com/riscv-software-src/riscv-isa-sim.git
WORKDIR riscv-isa-sim
RUN mkdir build
WORKDIR build

RUN ../configure
RUN make
RUN make install

WORKDIR /root
run git clone https://github.com/riscv-software-src/riscv-pk.git
WORKDIR riscv-pk
RUN mkdir build
WORKDIR build
RUN ../configure --host=riscv64-linux-gnu
RUN make
RUN make install

WORKDIR /root
RUN echo "export PATH=\"/usr/local/riscv64-linux-gnu/bin:$PATH\"" >> .bashrc
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Author: Nic Cantieni
# Date: 18.08.2022


NAME = gcc-riscv-docker
VERSION = latest

BUILDER = podman

DOCKERFILE = Dockerfile

BUILD = $(BUILDER) build . --tag $(NAME):$(VERSION) --file $(DOCKERFILE)

.PHONY: build
build :
$(BUILD)

.PHONY: nocache
nocache :
$(BUILD) --no-cache

.PHONY: save
save :
$(BUILDER) image save -o $(NAME).tar $(NAME):$(VERSION)

.PHONY: clean
clean :
rm $(NAME).tar
45 changes: 36 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
# `gcc-riscv-docker`: a container image for compiling RISC-V 64

This repository contains a `Dockerfile` which builds a container image based on `ubuntu:latest` and installs the gcc-crosscompiler for RISC-V 64.
This repository contains a toolchain for building and running containers (i.e. with Docker or Podman) in order to compile and run RISC-V 64 in the container.

## Usage

The script is built with `podman` in mind, but should work just as well with `Docker`. To change this, adjust the comments in the files.
### Building the container
The whole thing is built with `make`.

If you want to use Docker instead of Podman, adjust `BUILDER` in the `Makefile`.

```bash
# make scripts executable
chmod u+x build.sh
chmod u+x compile.sh
# build the image
make build

# build the container
./build.sh
# build the image without using cached results
make nocache

# compile the file
./compile.sh main.c -o main.o
# export the image to gcc-riscv-docker.tar
make save

# delete the .tar
make clean
```

### Using the container

An exemplary usage is in `./example`, and there is a separate `Makefile`.

If you want to use Docker instead of Podman, adjust `RUNNER` in the `Makefile`.

To adjust which files are built, change `COMPILE_FILES` and `COMPILE_OUT` in the `Makefile` accordingly.

```bash
cd example

# compile main.c to main.o
make compile

# run main.o with spike and a proxy-kernel (pk)
make run

# just start a container and connect a shell (for inspection etc.)
# ATTN: The container is deleted, only the contents of the outside
# local directory will be kept
make login
```
11 changes: 0 additions & 11 deletions build.sh

This file was deleted.

9 changes: 0 additions & 9 deletions compile.sh

This file was deleted.

63 changes: 63 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Author: Nic Cantieni
# Date: 18.08.2022

# Runs a temporary (--rm) container of the image gccriscv:latest, and mounts the current working directory into /builddir in the container, sets that as the working directory and invoces riscv64-linux-gnu-gcc with COMPILER_FLAGS

NAME = gcc-riscv-docker
RUNNER = podman

CONTAINER = gcc-riscv-docker
CONTAINER_VERSION = latest
CONTAINER_IDENT = $(CONTAINER):$(CONTAINER_VERSION)

VOLUME_LOCAL = $$PWD
VOLUME_CONTAINER = /builddir

CONTAINER_FLAGS = --rm --name $(NAME) \
--volume $(VOLUME_LOCAL):$(VOLUME_CONTAINER) \
--workdir $(VOLUME_CONTAINER) \

# COMPILE #

COMPILE_FILES = main.c
COMPILE_OUT = main.o

COMPILER = riscv64-unknown-elf-gcc

COMPILE_CONTAINER_FLAGS = $(CONTAINER_FLAGS) \
--entrypoint $(COMPILER) \

COMPILER_FLAGS = -c -O3 -Wno-int-conversion -Wall \
-march=rv64ima -mabi=lp64

COMPILE = $(RUNNER) run $(COMPILE_CONTAINER_FLAGS) $(CONTAINER_IDENT) $(COMPILER_FLAGS) $(COMPILE_FILES) -o $(COMPILE_OUT)

# RUN WITH SPIKE #

SPIKE = spike
PK = /usr/local/riscv64-linux-gnu/bin/pk

RUN_CONTAINER_FLAGS = $(CONTAINER_FLAGS) \
--entrypoint $(SPIKE) \

RUNNER_FLAGS = -d $(PK)

RUN = $(RUNNER) run $(RUN_CONTAINER_FLAGS) $(CONTAINER_IDENT) $(RUNNER_FLAGS) $(COMPILE_OUT)

# LOGIN TO CONTAINER #

LOGIN_CONTAINER_FLAGS = $(CONTAINER_FLAGS) -it

LOGIN = $(RUNNER) run $(LOGIN_CONTAINER_FLAGS) $(CONTAINER_IDENT)

.PHONY: compile
compile :
$(COMPILE)

.PHONY: run
run :
$(RUN)

.PHONY: login
login :
$(LOGIN)
File renamed without changes.

0 comments on commit ce9deee

Please sign in to comment.