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

meson_bin := env_var_or_default("MESON_BIN", "meson")
python_bin := env_var_or_default("PYTHON", "python3")
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", "")
web_port := env_var_or_default("MFEM_WEB_PORT", "8000")
macos_target := env_var_or_default("MACOSX_DEPLOYMENT_TARGET", "11.0")

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"
wasm_dir := build_root + "/emscripten"
wasm_cache := build_root + "/emscripten-cache"
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"

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_examples=true"
      "-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") (build-and-test parallel_dir)

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

cuda: (configure cuda_dir cuda_install "release" "portable" allow_preinstalled "-Dmfem_cuda=enabled" "-Dmfem_hip=disabled") (build-and-test cuda_dir)

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

emscripten:
    #!/usr/bin/env bash
    set -euo pipefail
    mkdir -p "{{ wasm_cache }}"
    setup_args=(
      "{{ wasm_dir }}"
      "--cross-file={{ project_root }}/cross/wasm32.ini"
      "--prefix={{ install_root }}/emscripten"
      "--buildtype=release"
      "-Dfeature_profile=portable"
      "-Dallow_preinstalled=false"
      "-Djobs={{ jobs }}"
      "-Dbuild_python=false"
      "-Dbuild_examples=true"
      "-Dbuild_tests=true"
    )
    if [[ -f "{{ wasm_dir }}/meson-private/coredata.dat" ]]; then
      setup_args=(--reconfigure "${setup_args[@]}")
    fi
    EM_CACHE="{{ wasm_cache }}" "{{ meson_bin }}" setup "${setup_args[@]}"
    EM_CACHE="{{ wasm_cache }}" "{{ meson_bin }}" compile -C "{{ wasm_dir }}"
    EM_CACHE="{{ wasm_cache }}" "{{ meson_bin }}" test -C "{{ wasm_dir }}" --print-errorlogs

serve-emscripten port=web_port: emscripten
    @echo "MFEM browser demo: http://0.0.0.0:{{ port }}"
    "{{ python_bin }}" -m http.server "{{ port }}" --bind 0.0.0.0 --directory "{{ wasm_dir }}/web"

python:
    mkdir -p "{{ project_root }}/dist"
    MACOSX_DEPLOYMENT_TARGET="{{ macos_target }}" "{{ python_bin }}" -m build --wheel --no-isolation --outdir "{{ project_root }}/dist"

run-serial: serial
    "{{ serial_dir }}/examples/mfem-serial-poisson"

run-parallel ranks="4": parallel
    "{{ python_bin }}" "{{ project_root }}/tools/run_mpi_test.py" --launcher "{{ parallel_dir }}/build-config/mfem/mfem-prefix/bin/mpiexec" --processes "{{ ranks }}" "{{ parallel_dir }}/examples/mfem-parallel-hypre"

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
