# ch-test-scope: standard

# I know Spack is used more on Red Hat systems but I don’t want to debug both
# RPM and Spack. 😭
FROM debian:trixie

# Note: Spack is a bit of an odd duck testing wise. Because it’s a package
# manager, the key tests we want are to install stuff (this includes the Spack
# test suite), and those don’t make sense at run time. Thus, most of what we
# care about is here in the Dockerfile, and test.bats just has a few
# trivialities.

RUN apt-get update && apt-get -y upgrade

# Packages to build Spack and install Charliecloud. Generated mostly with
# trial and error but with some reference to the Spack Ubuntu recipe [1].
#
# [1]: https://github.com/spack/spack/blob/e2c57453/share/spack/templates/container/ubuntu_2404.dockerfile
RUN apt-get install -y autoconf \
                       automake \
                       bats \
                       g++ \
                       gcc \
                       git \
                       fuse3 \
                       lbzip2 \
                       libcjson-dev \
                       libgc-dev \
                       libfuse3-dev \
                       liblzma-dev \
                       liblz4-dev \
                       liblzo2-dev \
                       libzstd-dev \
                       libssl-dev \
                       libtool \
                       make \
                       pkgconf \
                       python-is-python3 \
                       python3-apt \
                       python3-pip \
                       python3-requests \
                       python3-sphinx \
                       python3-sphinx-rtd-theme \
                       python3-yaml \
                       rsync \
                       unzip \
                       zlib1g-dev

# Certain Spack packages (e.g., tar) puke if they detect themselves being
# configured as UID 0. This is the override. See issue #540.
ARG FORCE_UNSAFE_CONFIGURE=1

# Install Spack. This follows the documented procedure to run it out of the
# source directory. There apparently is no “make install” type operation to
# place it at a standard path (“spack clone” simply clones another working
# directory to a new path).
#
# Depending on what’s commented below, we get either Spack’s “develop” branch
# or the latest released version. Using develop catches problems earlier, but
# that branch has a LOT more churn and some of the problems might not occur in
# a released version. I expect the right choice will change over time.
RUN git clone --depth 1 --branch releases/latest https://github.com/spack/spack
RUN cd spack && git status && git rev-parse --short HEAD

# We want to use a fair number of Spack packages as “external” to improve
# build time. Spack. Spack has a “spack external find” command, but it’s
# unreliable. First, packages have to add support manually [1]. Second, some
# packages that seem like they should be detected are not. This Python script
# uses Debian package information to build a defensible packages.yaml for the
# packages that “external find” doesn’t find.
#
# [1]: https://spack.readthedocs.io/en/latest/packaging_guide_advanced.html#make-package-findable
COPY packages-fix.py /spack
RUN mkdir -p /etc/spack \
 && python /spack/packages-fix.py bats:bats \
                                  libcjson-dev:cjson \
                                  libfuse3-dev:libfuse \
                                  libgc-dev:bdw-gc \
                                  liblzma-dev:xz \
                                  liblz4-dev:lz4 \
                                  liblzo2-dev:lzo \
                                  libzstd-dev:zstd \
                                  python3-requests:py-requests \
                                  python3-sphinx:py-sphinx \
                                  python3-sphinx-rtd-theme:py-sphinx-rtd-theme \
                                  rsync:rsync \
    > /etc/spack/packages.yaml \
 && cat /etc/spack/packages.yaml

# Set up Spack.
#
# The instructions say to source a ... large ... shell script [1]. AFAICT,
# what we actually need from that is a couple of environment variables.
#
# [1]: https://spack.readthedocs.io/en/v1.2.0/getting_started.html
ENV SPACK_ROOT=/spack
ENV PATH=/spack/bin:$PATH
RUN command -v spack
RUN spack --version
RUN spack repo update
RUN spack compiler find --scope system \
 && spack external find --scope system --not-buildable

# Install Charliecloud in a Spack environment.
#
# NOTE: The output of “concretize” is useful for externalizing dependencies.
RUN spack env create ch \
 && eval $(spack env activate --sh ch) \
 && spack add charliecloud@main +docs \
 && spack concretize

RUN eval $(spack env activate --sh ch) \
 && spack install --fail-fast

RUN eval $(spack env activate --sh ch) \
 && eval $(spack load --sh charliecloud) \
 && command -v ch-run \
 && ldd $(command -v ch-run) \
 && ch-run --version

# Clean up.
RUN /spack/bin/spack clean --all
