FROM ubuntu:23.10

ENV DEBIAN_FRONTEND=noninteractive

WORKDIR /root

# Install prerequisites
RUN apt update -qq && apt install -y --no-install-recommends \
    autoconf \
    automake \
    autotools-dev \
    curl \
    python3 \
    python3-pip \
    libmpc-dev \
    libmpfr-dev \
    libgmp-dev \
    gawk \
    build-essential \
    bison \
    flex \
    texinfo \
    gperf \
    libtool \
    patchutils \
    bc \
    zlib1g-dev \
    libexpat-dev \
    ninja-build \
    git \
    cmake \
    libglib2.0-dev \
    libslirp-dev \
    && rm -rf /var/lib/apt/lists/*

# Compile and install toolchain
RUN git clone https://github.com/riscv/riscv-gnu-toolchain

WORKDIR /root/riscv-gnu-toolchain

RUN ./configure --prefix=/opt/riscv && \
    make -j8

WORKDIR /root

RUN rm -rf /root/riscv-gnu-toolchain

# Install additional prerequisites
RUN apt update -qq && apt install -y --no-install-recommends \
    device-tree-compiler \
    && rm -rf /var/lib/apt/lists/*

# Compile and install Spike
RUN git clone https://github.com/riscv/riscv-isa-sim.git

WORKDIR /root/riscv-isa-sim

RUN mkdir build && \
    cd build && \
    ../configure --prefix=/opt/riscv && \
    make -j8 && \
    make install

WORKDIR /root

RUN rm -rf /root/riscv-isa-sim

# Pass commands into container
ENTRYPOINT ["bash", "-lc"]