# ---------------------------------------------------------------------------
#
# Create HPCToolkit-powered image
#
# Build
#   apptainer build hpctk.sif hpctoolkit.def
#
# Note
#   apptainer version 1.3.1
#
# ---------------------------------------------------------------------------
Bootstrap: localimage
From: containers/basic/{{ BASE_CONTAINER }}.sif

%arguments
    OS_DISTRO=ubuntu
    OS_VERSION=24.04
    MPI_IMPLEMENTATION=openmpi
    MPI_VERSION=4.1.5
    FRAMEWORK_VERSION=2024.01.1
    FRAMEWORK_GIT_REF=default

%post -c /bin/bash
    DEBIAN_FRONTEND=noninteractive apt-get update
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        openjdk-21-jdk openjdk-21-jre \
        ccache cmake \
        g++-13 gcc-13 \
        libboost-atomic-dev \
        libboost-chrono-dev \
        libboost-date-time-dev \
        libboost-filesystem-dev \
        libboost-graph-dev \
        libboost-system-dev \
        libboost-thread-dev \
        libboost-timer-dev \
        libbz2-dev \
        libdw-dev \
        libelf-dev \
        libiberty-dev \
        liblzma-dev \
        libpapi-dev \
        libpfm4-dev \
        libtbb-dev \
        libunwind-dev \
        libxerces-c-dev \
        libxxhash-dev \
        libyaml-cpp-dev \
        libzstd-dev \
        make \
        mawk \
        ninja-build \
        pkg-config \
        python3 python3-dev python3-myst-parser python3-venv \
        sed \
        meson \
        zlib1g-dev
    export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
    export PATH=$PATH:$JAVA_HOME/bin
    curl -L  -o /opt/hpcviewer.tgz https://gitlab.com/hpctoolkit/hpcviewer/-/releases/2024.09/downloads/hpcviewer-linux.gtk.x86_64.tgz
    tar -xvf /opt/hpcviewer.tgz -C /opt/
    rm /opt/hpcviewer.tgz
    git clone https://gitlab.com/hpctoolkit/hpctoolkit /opt/hpctoolkit
    cd /opt/hpctoolkit
    git checkout {{ FRAMEWORK_VERSION }}
    # Create hpctoolkit-ci.ini configuration
    mkdir -p /usr/share/meson/native
    t=/usr/share/meson/native/hpctoolkit-ci.ini
    rm -f "$t"
    echo "[built-in options]" > "$t"
    echo "wrap_mode = 'nofallback'" >> "$t"
    echo "force_fallback_for = ['dyninst', 'libunwind', 'xed']" >> "$t"
    echo "" >> "$t"
    echo "[project options]" >> "$t"
    echo "auto_features = 'enabled'" >> "$t"
    case "$(dpkg --print-architecture)" in
      amd64) ;;
      *) echo "cuda = 'disabled'" >> "$t"; ;;
    esac
    echo "rocm = 'disabled'" >> "$t"
    echo "level0 = 'disabled'" >> "$t"
    echo "gtpin = 'disabled'" >> "$t"
    echo "" >> "$t"
    echo "[zstd:project options]" >> "$t"
    echo "lz4 = 'disabled'" >> "$t"
    meson setup build
    meson compile -C build
    meson install -C build
    rm -rf /opt/hpctoolkit/build
    rm -rf /opt/hpctoolkit/.git
    apt-get remove -y \
        ccache cmake \
        g++-13 gcc-13 \
        make ninja-build \
        pkg-config meson git curl
    apt-get autoremove -y
    apt-get remove -y \
        libboost-*-dev \
        libbz2-dev libdw-dev libelf-dev \
        libiberty-dev liblzma-dev \
        libpapi-dev libpfm4-dev \
        libtbb-dev libunwind-dev \
        libxerces-c-dev libxxhash-dev \
        libyaml-cpp-dev libzstd-dev \
        zlib1g-dev
    apt-get autoremove -y
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
    rm -rf /usr/share/doc/* /usr/share/man/* /usr/share/info/*
    echo "export JAVA_HOME=${JAVA_HOME}" > /opt/hpctoolkit/bashrc
    echo 'export PATH=${PATH}:${JAVA_HOME}/bin:/opt/hpcviewer' >> /opt/hpctoolkit/bashrc
    echo 'export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib64' >> /opt/hpctoolkit/bashrc
    jq --arg app hpctoolkit --arg commit {{ FRAMEWORK_GIT_REF }} \
        --arg branch {{ FRAMEWORK_GIT_REF }} \
        '.[$app] |= if . == null then
        {
            branch: $branch,
            commit: $commit,
            source_script: "/opt/hpctoolkit/bashrc",
            workflow: "hpcrun -> hpcstruct -> hpcprof -> htpcviewer",
            version: "{{ FRAMEWORK_VERSION }}"
        }
        else . +
        {
            branch: $branch,
            commit: $commit,
            source_script: "/opt/hpctoolkit/bashrc",
            workflow: "hpcrun -> hpcstruct -> hpcprof -> htpcviewer",
            version: "{{ FRAMEWORK_VERSION }}"
        } end' /apps.json > /tmp/apps.json
    mv /tmp/apps.json /apps.json

%runscript
    #!/bin/bash
    if [ $# -eq 0 ]; then
        exec /bin/bash
    else
        exec "$@"
    fi

%labels
    Maintainer Mohammed Elwardi Fadeli
    Description HPCToolkit
    AppsFile /apps.json
