#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUT_DIR="$ROOT/target/release"
SRC_DIR="$ROOT/lang/c-abi/shims"

mkdir -p "$OUT_DIR"

cc_cmd=${CC:-cc}

echo "[build] cc=$cc_cmd"
echo "[build] compiling libhako_llvmc_ffi.so ..."

YYJSON_DIR="$ROOT/plugins/nyash-json-plugin/c/yyjson"

"$cc_cmd" -fPIC -shared \
  -I"$YYJSON_DIR" \
  -o "$OUT_DIR/libhako_llvmc_ffi.so" \
  "$SRC_DIR/hako_llvmc_ffi.c" \
  "$SRC_DIR/hako_aot.c" \
  "$SRC_DIR/hako_json_v1.c" \
  "$YYJSON_DIR/yyjson.c"

echo "[build] done: $OUT_DIR/libhako_llvmc_ffi.so"
