#!/bin/bash
#
# Wrapper script for llvm-config. Supplies the right environment variables
# for the target and delegates to the native llvm-config for anything else. This
# is needed because arguments like --ldflags, --cxxflags, etc. are set by the
# native compile rather than the target compile.
#
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
NEXT_LLVM_CONFIG="$(which -a llvm-config | sed -n 2p)"
export YOCTO_ALTERNATE_EXE_PATH="${YOCTO_ALTERNATE_EXE_PATH:="$(readlink -f "$SCRIPT_DIR/../llvm-config")"}"
export YOCTO_ALTERNATE_LIBDIR="${YOCTO_ALTERNATE_LIBDIR:="/lib"}"
if [[ $# == 0 ]]; then
  exec "$NEXT_LLVM_CONFIG"
fi

if [[ $1 == "--libs" ]]; then
  exec "$NEXT_LLVM_CONFIG" $@
fi

if [[ $1 == "--bindir" ]]; then
  unset YOCTO_ALTERNATE_EXE_PATH
  exec "$NEXT_LLVM_CONFIG" $@
fi

if [[ $1 == "--libfiles" ]]; then
  exec "$NEXT_LLVM_CONFIG" $@
fi


for arg in "$@"; do
  case "$arg" in
    --cppflags)
      echo $TARGET_CPPFLAGS
      ;;
    --cflags)
      echo $TARGET_CFLAGS
      ;;
    --cxxflags)
      echo $TARGET_CXXFLAGS
      ;;
    --ldflags)
      echo $TARGET_LDFLAGS
      ;;
    *)
      echo "$("$NEXT_LLVM_CONFIG" "$arg")"
      ;;
  esac
done
