#!/bin/bash
#
# Copyright OpenEmbedded Contributors
#
# SPDX-License-Identifier: MIT
#
# Wrap llvm-config since the native llvm-config will remap some values correctly
# if placed in the target sysroot but for flags, it would provide the native ones.
# Provide ours from the environment instead.

NEXT_LLVM_CONFIG="$(which -a llvm-config | sed -n 2p)"
if [[ $# == 0 ]]; then
  exec "$NEXT_LLVM_CONFIG"
fi

remain=""
output=""
for arg in "$@"; do
  case "$arg" in
    --cppflags)
      output="${output} ${CPPFLAGS}"
      ;;
    --cflags)
      output="${output} ${CFLAGS}"
      ;;
    --cxxflags)
      output="${output} ${CXXFLAGS}"
      ;;
    --ldflags)
      output="${output} ${LDFLAGS}"
      ;;
    --shared-mode)
      output="${output} shared"
      ;;
    --libs)
      output="${output} -lLLVM"
      ;;
    --link-shared)
      break
      ;;
    *)
      remain="${remain} ${arg}"
      ;;
  esac
done

if [ "${remain}" != "" ]; then
      output="${output} "$("$NEXT_LLVM_CONFIG" ${remain})
fi

echo "${output}"
