FROM alpine:edge AS builder

# Update databases
RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
    apk update --no-cache

# Download base dependencies
RUN apk add --no-cache \
    git \
    make \
    cmake \
    build-base \
    linux-headers \
    boost-dev \
    eigen-dev \
    gawk \
    py3-pip \
    py3-setuptools \
    py3-wheel \
    py3-gpep517 \
    py3-build \
    ghc \
    stack \
    cython \
    py3-yaml \
    py3-simplejson \
    py3-intervaltree \
    libunwind-dev

# Install yosys
RUN apk add --no-cache yosys

WORKDIR /home/root/aur-install

# Install prjxray librairies
RUN git clone https://github.com/supersurviveur/prjxray.git --depth 1 && \
    cd prjxray && \
    git submodule update --init --depth 1 && \
    # include fasm2frames in python package
    mv utils/fasm2frames.py prjxray/ && \
    sed -E 's/(fasm2frames=)utils/\1prjxray/' -i setup.py && \
    cmake -B build \
        -S "." \
        -DCMAKE_BUILD_TYPE=None \
        -DCMAKE_INSTALL_PREFIX='/usr' \
        -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
        -Wno-dev && \
    cmake --build build -j$(nproc) && \
    python -m build --wheel --no-isolation --skip-dependency-check && \
    python -m installer dist/*.whl && \
    python -m build third_party/fasm --wheel --no-isolation --skip-dependency-check && \
    python -m installer third_party/fasm/dist/*.whl

# prjxray dependency not available on apk
RUN pip install textx --break-system-packages

# Install the zynq7 prjxray database
RUN git clone https://github.com/f4pga/prjxray-db.git --depth 1 && \
    cd prjxray-db && \
    install -dm 755 /usr/share/xray/database && \
    cp -r zynq7 /usr/share/xray/database/ && \
    # Delete the xc7z020 chip since we don't need it for the xc7z010 chip 
    rm /usr/share/xray/database/zynq7/xc7z020 -rf

# Install nextpnr-himbaechel
RUN git clone https://github.com/YosysHQ/nextpnr --depth 1 && \
    cd nextpnr && \
    git submodule update --remote --init --depth 1 himbaechel/uarch/xilinx/meta && \
    cmake -S "." -B build \
        -DARCH=himbaechel \
        -DHIMBAECHEL_UARCH=xilinx \
        -DHIMBAECHEL_XILINX_DEVICES="xc7z010" \
        -DHIMBAECHEL_PRJXRAY_DB="/usr/share/xray/database" \
        -DBUILD_PYTHON=OFF \
        -DBUILD_GUI=OFF \
        -DCMAKE_BUILD_TYPE=None \
        -DCMAKE_INSTALL_PREFIX=/usr \
        -DUSE_OPENMP=ON && \
    make -C build -j$(nproc)

# Install sv2v
RUN git clone https://github.com/zachjs/sv2v --depth 1 && \
    cd sv2v && \
    make -j$(nproc)

COPY copy_bin_deps.sh /usr/bin/
RUN chmod +x /usr/bin/copy_bin_deps.sh

# Copy all binaries and their dependencies in a /dependencies dir which will be copied in the final image
RUN mkdir -p /dependencies

RUN copy_bin_deps.sh /usr/bin/yosys
RUN copy_bin_deps.sh /home/root/aur-install/nextpnr/build/nextpnr-himbaechel
RUN copy_bin_deps.sh /home/root/aur-install/prjxray/build/tools/xc7frames2bit
RUN copy_bin_deps.sh /home/root/aur-install/sv2v/bin/sv2v
RUN cp /usr/bin/fasm2frames /dependencies/usr/bin/

# Final image
FROM alpine:edge

# Install dependencies available in apk
RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && \
    apk update --no-cache && \
    apk add --no-cache abc py3-yaml py3-simplejson py3-intervaltree py3-arpeggio openfpgaloader

# Get dependencies back
COPY --from=builder /dependencies /

# Some files are needed in addition to libs files
COPY --from=builder /usr/share/yosys /usr/share/yosys
# Chip databases
COPY --from=builder /home/root/aur-install/nextpnr/build/share /usr/share/nextpnr
COPY --from=builder /usr/share/xray/database/zynq7 /usr/share/xray/database/zynq7

# These 3 python dependencies were built in the previous image 
# COPY --from=builder /usr/lib/python3.14/site-packages/prjxray /usr/lib/python3.14/site-packages/prjxray
# COPY --from=builder /usr/lib/python3.14/site-packages/fasm /usr/lib/python3.14/site-packages/fasm
# COPY --from=builder /usr/lib/python3.14/site-packages/textx /usr/lib/python3.14/site-packages/textx
COPY --from=builder /usr/lib/python3.14/site-packages /usr/lib/python3.14/site-packages/

WORKDIR /root/code

CMD ["/bin/bash -c \"$@\""]
