# Use gmao/llvm-flang as base image.  Pinned to linux/amd64: this image
# currently has no arm64 variant, so an unpinned `docker build` on an
# arm64 host (e.g. Apple Silicon) happens to already resolve to amd64 --
# but only by luck, and `docker run` then warns about a platform mismatch
# every time.  Pinning here makes that explicit and stops depending on the
# base image staying amd64-only.
FROM --platform=linux/amd64 gmao/llvm-flang

# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive

# Version of the repository to build: leave unset (default) to build from
# this local checkout, including any uncommitted changes; set to a tag or
# commit hash (--build-arg GIT_REF=v0.9.0) to instead clone and check out
# that exact revision from GitHub, ignoring the local checkout.
ARG GIT_REF=""

# Install necessary packages
RUN apt-get update && apt-get install -y \
    wget \
    bzip2 \
    ca-certificates \
    curl \
    git \
    nano \
    vim \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Set up working directory
WORKDIR /app

# Install Anaconda
RUN wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh -O /tmp/anaconda.sh \
    && bash /tmp/anaconda.sh -b -p /opt/anaconda \
    && rm /tmp/anaconda.sh

# Add Anaconda to PATH
ENV PATH="/opt/anaconda/bin:${PATH}"

# Initialize conda for bash shell
RUN echo ". /opt/anaconda/etc/profile.d/conda.sh" >> ~/.bash_profile
RUN echo ". /opt/anaconda/etc/profile.d/conda.sh" >> ~/.bashrc

# Copy environment.yaml file
COPY environment.yaml .

# Create conda environment from the yaml file
# this installs fpm and intel compiler
RUN conda env create -f environment.yaml
RUN echo "conda activate band" >> ~/.bash_profile
RUN echo "conda activate band" >> ~/.bashrc

# Source - https://stackoverflow.com/a/39777387
# Posted by Ahmad Abdelghany, modified by community. See post 'Timeline' for change history
# Retrieved 2025-11-09, License - CC BY-SA 4.0
# this command, together with putting conda activation in .bash_profile, means we have
# enabled the conda environment for every command
SHELL ["/bin/bash", "--login", "-c"] 

# Stage the source: defaults to this local checkout; if GIT_REF is set,
# the RUN step below discards this and clones that revision instead.
COPY src /tmp/band_src/src/
COPY test /tmp/band_src/test
COPY fpm.toml /tmp/band_src/
COPY python /tmp/band_src/python/
COPY scripts /tmp/band_src/scripts/

RUN if [ -n "$GIT_REF" ]; then \
        echo "Building band_distribution @ $GIT_REF (overriding local checkout)" && \
        rm -rf /tmp/band_src && \
        git clone https://github.com/det-lab/band_distribution.git /tmp/band_src && \
        cd /tmp/band_src && git checkout "$GIT_REF"; \
    else \
        echo "Building band_distribution from the local checkout"; \
    fi

RUN mkdir -p /app/src /app/test /app/python /app/scripts \
    && cp -r /tmp/band_src/src/. /app/src/ \
    && cp -r /tmp/band_src/test/. /app/test/ \
    && cp -r /tmp/band_src/python/. /app/python/ \
    && cp -r /tmp/band_src/scripts/. /app/scripts/ \
    && cp /tmp/band_src/fpm.toml /app/ \
    && rm -rf /tmp/band_src

# version.f90.inc (PpqFort_version(); not committed, see the script) must
# exist before fpm build/test -- it is a plain `include` in src/PpqFort_m.f90
RUN python scripts/generate_version_include.py

# Link the LLVM OpenMP runtime into everything fpm builds here (including
# interactive fpm invocations inside the container).  The flang-compiled
# shared library references __kmpc_* symbols, and without -fopenmp at link
# time the Python ctypes load of libband_distribution.so fails.
ENV FPM_LDFLAGS="-fopenmp"

# Now compile the fortran code.
# The compiler is invoked as `flang`: the flang-new name was dropped in
# LLVM 20.  -fdo-concurrent-to-openmp=host parallelizes the do concurrent
# loops via the OpenMP runtime; without it they compile to serial loops.
# LD_LIBRARY_PATH is cleared for the compile: the Intel packages in the
# conda env put their own (stripped) libLLVM on the path at activation,
# which breaks the system flang with a symbol lookup error.
RUN LD_LIBRARY_PATH="" fpm test --compiler flang --profile release --flag "-O3 -fopenmp -fdo-concurrent-to-openmp=host" \
    && LD_LIBRARY_PATH="" fpm install --prefix=. --compiler flang --profile release --flag "-O3 -fopenmp -fdo-concurrent-to-openmp=host"

# Needed to find the appropriate .so files from within python
ENV LD_LIBRARY_PATH=/app/lib:$LD_LIBRARY_PATH

# Published normalization tables (python/cli/table_registry.json), fetched and
# hash-checked at build time -- HPC compute nodes often have no internet.
# python/cli/normtables.py (and so PpqPDF(ppqn_table=NAME)) finds them here.
ENV BAND_TABLES_DIR=/app/tables
RUN mkdir -p /app/tables && python /app/python/cli/normtables.py fetch --all
