FROM ubuntu:24.04 as harebuild

RUN apt-get update -qq \
  && apt-get install -qy --no-install-recommends \
    build-essential \
    git \
    scdoc \
    ca-certificates \
    wget

RUN wget https://c9x.me/compile/release/qbe-1.2.tar.xz && \
  tar -xf qbe-1.2.tar.xz && \
  cd qbe-1.2 && \
  make && \
  make install

RUN git clone https://git.sr.ht/~sircmpwn/harec && \
      cd harec && \
      cp configs/linux.mk config.mk && \
      make && \
      make install

RUN git clone https://git.sr.ht/~sircmpwn/hare && \
      cd hare && \
      cp configs/linux.mk config.mk && \
      make && \
      make install

FROM rust:1.78 as rustbuild

WORKDIR /src
COPY ./ ./
RUN cargo build --release

FROM ubuntu:24.04

RUN apt-get update -qq \
  && apt-get install -qy --no-install-recommends \
    ca-certificates \
    tzdata \
    bubblewrap \
    binutils \
    vim

COPY --from=harebuild /usr/local /usr/local
COPY --from=rustbuild /src/target/release/backend /usr/local/bin/backend

ENTRYPOINT ["/usr/local/bin/backend"]