FROM haskell:8.8.4 AS build

RUN echo "deb http://deb.debian.org/debian buster-backports main" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y wget libncurses-dev unzip clang-11 llvm-11-tools

WORKDIR /usr/local/bin
RUN curl -o solvers.zip -sL "https://github.com/GaloisInc/what4-solvers/releases/download/snapshot-20210917/ubuntu-18.04-bin.zip"
RUN unzip solvers.zip && rm solvers.zip && chmod +x *

RUN cabal v2-update

ARG DIR=/crux-llvm
WORKDIR ${DIR}
RUN mkdir -p ${DIR}/build
ADD crucible ${DIR}/build/crucible
ADD crucible-llvm ${DIR}/build/crucible-llvm
ADD crucible-symio ${DIR}/build/crucible-symio
ADD crux ${DIR}/build/crux
ADD crux-llvm ${DIR}/build/crux-llvm
ADD dependencies ${DIR}/build/dependencies
ADD .github/cabal.project.crux-llvm ${DIR}/build/cabal.project
ADD cabal.GHC-8.8.4.config ${DIR}/build/cabal.project.freeze
# Workaround until we have an LLVM 11 build available
RUN cp $DIR/build/crux-llvm/c-src/libcxx-7.1.0.bc $DIR/build/crux-llvm/c-src/libcxx-11.0.1.bc

WORKDIR ${DIR}/build
RUN cabal v2-build --only-dependencies crux-llvm
RUN cabal v2-build crux-llvm
ENV CLANG=clang-11
ENV LLVM_LINK=llvm-link-11
RUN cabal v2-test crux-llvm
RUN cp `cabal v2-exec which crux-llvm` /usr/local/bin
RUN cp `cabal v2-exec which crux-llvm-svcomp` /usr/local/bin

FROM debian:buster-slim

USER root
RUN echo "deb http://deb.debian.org/debian buster-backports main" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y libgmp10 zlibc zlib1g clang-11 llvm-11-tools unzip locales

COPY --from=build /usr/local/bin/* /usr/local/bin/

ARG DIR=/crux-llvm
WORKDIR ${DIR}
ADD crux-llvm/c-src c-src
# Use LLVM 7 bitcode file for libcxx until LLVM version is available
RUN cp c-src/libcxx-7.1.0.bc c-src/libcxx-11.0.1.bc

# (Temporary) fix for
# https://github.com/galoisinc/crucible/issues/887: the libDir default
# is relative to the executable or failing that, the data dir as
# reported by the build process, but neither matches WORKDIR/c-src.
RUN cp -r c-src /usr/local/

RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
    locale-gen
ENV LD_LIBRARY_PATH=/usr/local/lib
ENV CLANG=clang-11
ENV LLVM_LINK=llvm-link-11
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENTRYPOINT ["/usr/local/bin/crux-llvm"]
