set shell := ["bash", "-euo", "pipefail", "-c"]

meson_bin := env_var_or_default("MESON_BIN", "meson")
jobs := env_var_or_default("MFEM_JOBS", "4")
allow_preinstalled := env_var_or_default("MFEM_ALLOW_PREINSTALLED", "false")
dependency_prefix := env_var_or_default("MFEM_DEPENDENCY_PREFIX", "")
cuda_home := env_var_or_default("CUDA_HOME", "")
cuda_arch := env_var_or_default("MFEM_CUDA_ARCH", "native")

project_root := justfile_directory()
build_root := project_root + "/" + env_var_or_default("MFEM_BUILD_ROOT", "build")
install_root := project_root + "/" + env_var_or_default("MFEM_INSTALL_ROOT", "install")
basic_dir := build_root + "/basic"
serial_dir := build_root + "/serial"
parallel_dir := build_root + "/parallel"
debug_dir := build_root + "/debug"
cuda_dir := build_root + "/cuda"
hip_dir := build_root + "/hip"
basic_install := install_root + "/basic"
serial_install := install_root + "/serial"
parallel_install := install_root + "/parallel"
debug_install := install_root + "/debug"
cuda_install := install_root + "/cuda"
hip_install := install_root + "/hip"

cuda_cc := env_var_or_default("MFEM_CUDA_CC", env_var_or_default("CC", "cc"))
cuda_cxx := env_var_or_default("MFEM_CUDA_CXX", env_var_or_default("CXX", "c++"))

default:
    @just --list

[private]
configure dir install_dir buildtype profile allow *extra:
    #!/usr/bin/env bash
    set -euo pipefail
    setup_args=(
      "{{ dir }}"
      "--prefix={{ install_dir }}"
      "--buildtype={{ buildtype }}"
      "-Dfeature_profile={{ profile }}"
      "-Dallow_preinstalled={{ allow }}"
      "-Ddependency_prefix={{ dependency_prefix }}"
      "-Djobs={{ jobs }}"
      "-Dbuild_tests=true"
    )
    if [[ -f "{{ dir }}/meson-private/coredata.dat" ]]; then
      setup_args=(--reconfigure "${setup_args[@]}")
    fi
    "{{ meson_bin }}" setup "${setup_args[@]}" {{ extra }}

[private]
build-and-test dir:
    "{{ meson_bin }}" compile -C "{{ dir }}"
    "{{ meson_bin }}" test -C "{{ dir }}" --print-errorlogs

basic: (configure basic_dir basic_install "release" "minimal" allow_preinstalled "-Dmfem_mpi=disabled" "-Dmfem_cuda=disabled" "-Dmfem_hip=disabled") (build-and-test basic_dir)

serial: (configure serial_dir serial_install "release" "portable" allow_preinstalled "-Dmfem_mpi=disabled" "-Dmfem_metis=disabled" "-Dmfem_cuda=disabled" "-Dmfem_hip=disabled") (build-and-test serial_dir)

parallel: (configure parallel_dir parallel_install "release" "portable" allow_preinstalled "-Dmfem_mpi=enabled" "-Dmfem_sundials=enabled" "-Dmfem_cuda=disabled" "-Dmfem_hip=disabled") (build-and-test parallel_dir)

debug: (configure debug_dir debug_install "debug" "portable" allow_preinstalled) (build-and-test debug_dir)

cuda:
    #!/usr/bin/env bash
    set -euo pipefail
    cuda_root="{{ cuda_home }}"
    if [[ -z "$cuda_root" ]]; then
      if command -v nvcc >/dev/null 2>&1; then
        nvcc_path="$(command -v nvcc)"
        while [[ -L "$nvcc_path" ]]; do
          nvcc_target="$(readlink "$nvcc_path")"
          if [[ "$nvcc_target" == /* ]]; then
            nvcc_path="$nvcc_target"
          else
            nvcc_path="$(dirname "$nvcc_path")/$nvcc_target"
          fi
        done
        cuda_root="$(cd "$(dirname "$nvcc_path")/.." && pwd -P)"
      elif [[ -x /usr/local/cuda/bin/nvcc ]]; then
        cuda_root=/usr/local/cuda
      fi
    fi
    if [[ -n "$cuda_root" ]]; then
      export CUDA_HOME="$cuda_root"
      export PATH="$cuda_root/bin:$PATH"
    fi
    if ! command -v nvcc >/dev/null 2>&1; then
      echo "CUDA was requested, but nvcc was not found; set CUDA_HOME or PATH." >&2
      exit 2
    fi
    selected_arch="{{ cuda_arch }}"
    if [[ "$selected_arch" == native ]] && command -v nvidia-smi >/dev/null 2>&1; then
      if detected_arch="$(nvidia-smi --query-gpu=compute_cap --format=csv,noheader | sed -n '1{s/[[:space:].]//g;p;}')" && [[ "$detected_arch" =~ ^[0-9]+$ ]]; then
        selected_arch="$detected_arch"
      fi
    fi
    setup_args=(
      "{{ cuda_dir }}"
      "--prefix={{ cuda_install }}"
      "--buildtype=release"
      "-Dfeature_profile=portable"
      "-Dallow_preinstalled={{ allow_preinstalled }}"
      "-Ddependency_prefix={{ dependency_prefix }}"
      "-Djobs={{ jobs }}"
      "-Dbuild_tests=true"
      "-Dmfem_cuda=enabled"
      "-Dmfem_hip=disabled"
      "-Dmfem_cuda_arch=${selected_arch}"
    )
    if [[ -f "{{ cuda_dir }}/meson-private/coredata.dat" ]]; then
      setup_args=(--reconfigure "${setup_args[@]}")
    fi
    CC="{{ cuda_cc }}" CXX="{{ cuda_cxx }}" "{{ meson_bin }}" setup "${setup_args[@]}"
    "{{ meson_bin }}" compile -C "{{ cuda_dir }}"
    "{{ meson_bin }}" test -C "{{ cuda_dir }}" --print-errorlogs

hip: (configure hip_dir hip_install "release" "portable" allow_preinstalled "-Dmfem_hip=enabled" "-Dmfem_cuda=disabled") (build-and-test hip_dir)

install-basic: basic
    "{{ meson_bin }}" install -C "{{ basic_dir }}"

install-serial: serial
    "{{ meson_bin }}" install -C "{{ serial_dir }}"

install-parallel: parallel
    "{{ meson_bin }}" install -C "{{ parallel_dir }}"

fetch:
    "{{ meson_bin }}" subprojects download
