FROM debian:buster-slim

LABEL maintainer "Infer team"

# mkdir the man/man1 directory due to Debian bug #863199
RUN apt-get update && \
    mkdir -p /usr/share/man/man1 && \
    apt-get install --yes --no-install-recommends \
      autoconf \
      automake \
      bzip2 \
      cmake \
      curl \
      gcc \
      git \
      libc6-dev \
      libgmp-dev \
      libmpfr-dev \
      libsqlite3-dev \
      make \
      openjdk-11-jdk-headless \
      patch \
      pkg-config \
      python3.7 \
      python3-distutils \
      unzip \
      zlib1g-dev && \
    rm -rf /var/lib/apt/lists/*

# Install opam 2
RUN curl -sL https://github.com/ocaml/opam/releases/download/2.0.6/opam-2.0.6-x86_64-linux > /usr/bin/opam && \
    chmod +x /usr/bin/opam

# Disable sandboxing
# Without this opam fails to compile OCaml for some reason. We don't need sandboxing inside a Docker container anyway.
RUN opam init --reinit --bare --disable-sandboxing

# Download the latest Infer master
RUN cd / && \
    git clone https://github.com/facebook/infer/

# Build opam deps first, then infer. This way if any step fails we
# don't lose the significant amount of work done in the previous
# steps.
RUN cd /infer && \
    INFER_OPAM_SWITCH=4.08.1 ./build-infer.sh --only-setup-opam --no-opam-lock java && \
    opam clean

# Make sure clang is disabled
RUN eval $(opam env) && \
    cd /infer && \
    SKIP_SUBMODULES=true ./autogen.sh && \
    ./configure --disable-c-analyzers

# "Install" Infer. The tutorial assumes /infer-host is the working copy of infer so let's put it in the PATH too.
ENV PATH /infer-host/infer/bin:/infer/bin:$PATH

# build in non-optimized mode by default to speed up build times
ENV BUILD_MODE=dev

# prevent exiting by compulsively hitting Control-D
ENV IGNOREEOF=9

# should be moved earlier
ENV INFER_OPAM_SWITCH=4.08.1

# export `opam env`
ENV OPAM_SWITCH_PREFIX=/root/.opam/4.08.1
ENV CAML_LD_LIBRARY_PATH=/root/.opam/4.08.1/lib/stublibs:/root/.opam/4.08.1/lib/ocaml/stublibs:/root/.opam/4.08.1/lib/ocaml
ENV OCAML_TOPLEVEL_PATH=/root/.opam/4.08.1/lib/toplevel
ENV MANPATH=$MANPATH:/root/.opam/4.08.1/man
ENV PATH=/root/.opam/4.08.1/bin:$PATH