FROM centos:centos7 AS base-dependencies
LABEL author="James Cherry"
LABEL maintainer="James Cherry <cherry@parallaxsw.com>"

# Install dev and runtime dependencies (use vault repos since mirror repos are discontinued)
RUN sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo \
    && sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo \
    && sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo \
    && yum update -y \
    && yum install -y centos-release-scl epel-release
RUN sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo \
    && sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo \
    && sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo \ 
    && yum install -y devtoolset-11 wget cmake3 make eigen3-devel tcl swig3 flex zlib-devel valgrind \
    && yum clean -y all

# Download Bison
RUN wget https://ftp.gnu.org/gnu/bison/bison-3.8.2.tar.gz && \
    tar -xvf bison-3.8.2.tar.gz && \
    rm bison-3.8.2.tar.gz

# Build Bison
RUN source /opt/rh/devtoolset-11/enable && \
    cd bison-3.8.2 && \
    ./configure && \
    make -j`nproc` && \
    make install

# Download CUDD
RUN wget https://raw.githubusercontent.com/davidkebo/cudd/main/cudd_versions/cudd-3.0.0.tar.gz && \
    tar -xvf cudd-3.0.0.tar.gz && \
    rm cudd-3.0.0.tar.gz

# Build CUDD
RUN source /opt/rh/devtoolset-11/enable && \
    cd cudd-3.0.0 && \
    mkdir ../cudd && \
    ./configure && \
    make -j`nproc` && \
    make install

# Download TCL
RUN wget http://prdownloads.sourceforge.net/tcl/tcl8.6.16-src.tar.gz && \
    tar -xvf tcl8.6.16-src.tar.gz && \
    rm tcl8.6.16-src.tar.gz

# Build TCL
RUN source /opt/rh/devtoolset-11/enable && \
    cd tcl8.6.16 && \
    ./unix/configure && \
    make -j`nproc` && \
    make install

FROM base-dependencies AS builder

COPY . /OpenSTA
WORKDIR /OpenSTA

# Build
RUN rm -rf build && mkdir build
RUN source /opt/rh/devtoolset-11/enable && \
    cd build && \
    cmake3 .. && \
    make -j`nproc`

# Run sta on entry
ENTRYPOINT ["/OpenSTA/app/sta"]
