Bootstrap: docker
From: ubuntu:22.04
Stage: build

%files
    CMakeLists.txt /opt/tyr/CMakeLists.txt
    LICENSE /opt/tyr/LICENSE
    pyproject.toml /opt/tyr/pyproject.toml
    README.md /opt/tyr/README.md
    Config.cmake.in /opt/tyr/Config.cmake.in

    cmake /opt/tyr/cmake
    exe /opt/tyr/exe
    include /opt/tyr/include
    python /opt/tyr/python
    src /opt/tyr/src
    tests /opt/tyr/tests

%post
    # Set noninteractive mode for apt
    export DEBIAN_FRONTEND=noninteractive

    # Update and install dependencies
    # At least Python 3.9 for nanobind.
    apt-get update
    apt-get install -y \
        build-essential \
        cmake \
        git \
        wget \
        ca-certificates \
        python3.10 \
        python3.10-dev \
        python3-pip \
        ccache \
        && apt-get clean \
        && rm -rf /var/lib/apt/lists/*

    python3.10 -m pip install --no-cache-dir --upgrade pip
    PYYGGDRASIL_JOBS=2 PYPDDL_JOBS=2 python3.10 -m pip install --no-cache-dir typing_extensions pyyggdrasil>=0.0.9 pypddl>=1.0.6

    # Set working directory
    cd /opt/tyr

    NATIVE_PREFIX_PATH="$(python3.10 -c 'import pypddl, pyyggdrasil; print(f"{pypddl.native_prefix()};{pyyggdrasil.native_prefix()}")')"

    # Build Tyr
    cmake -S . -B build \
        -DCMAKE_PREFIX_PATH="${NATIVE_PREFIX_PATH}" \
        -DCMAKE_BUILD_TYPE=Release \
        -DPython_EXECUTABLE=/usr/bin/python3.10 \
        -DPython3_EXECUTABLE=/usr/bin/python3.10 \
        -DTYR_BUILD_TESTS=OFF \
        -DTYR_BUILD_PROFILING=OFF \
        -DTYR_BUILD_PYTYR=OFF \
        -DTYR_BUILD_EXECUTABLES=ON \
        -DTYR_HEADER_INSTANTIATION=OFF \
        -DTYR_STATE_STORAGE_POLICY=Tree
    cmake --build build -j2

    # Install Tyr to a staging directory
    cmake --install build --prefix=/opt/tyr/install

    mkdir -p /opt/tyr/runtime-lib
    cp -a "$(python3.10 -c 'import pypddl; print(pypddl.native_prefix() / "lib")')"/* /opt/tyr/runtime-lib/
    cp -a "$(python3.10 -c 'import pyyggdrasil; print(pyyggdrasil.native_prefix() / "lib")')"/* /opt/tyr/runtime-lib/

# Runtime stage
Bootstrap: docker
From: ubuntu:22.04
Stage: runtime

%files from build
    /opt/tyr/install /usr/local
    /opt/tyr/build/exe/gbfs_lazy /usr/local/bin/gbfs_lazy
    /opt/tyr/runtime-lib /usr/local/lib

%post
    # Install only runtime dependencies
    export DEBIAN_FRONTEND=noninteractive
    apt-get update
    apt-get install -y \
        libstdc++6 \
        libgomp1 \
        && apt-get clean \
        && rm -rf /var/lib/apt/lists/*

    # Update library cache
    ldconfig

%environment
    export LC_ALL=C
    export PATH=/usr/local/bin:$PATH
    export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

%runscript
    exec /usr/local/bin/gbfs_lazy -D "$1" -P "$2" -O "$3" -N 1 -H rpg_ff -S 

%labels
    Author Dominik Drexler
    Description Tyr Lifted FF Planner

%help
    This is an example Apptainer definition file for building and running the Tyr planner.

    Usage:
        apptainer run tyr.sif <domain.pddl> <problem.pddl> <plan.out>

    Or start an interactive shell:
        apptainer shell tyr.sif

    The gbfs_lazy executable is available at /usr/local/bin/gbfs_lazy
