#!/usr/bin/env bash

function ld_fn()
{
  if [[ ${CC} =~ .*conda.* ]]; then
    # Using conda compilers. Good.
    $(dirname "${BASH_SOURCE[0]}")/ld.bin "$@"
  else
    local _GCC=gcc
    if [[ -n ${CC} ]]; then
      _GCC="${CC}"
    fi
    # Is it just GCC being too new? Not really. It is that GCC is
    # compressing its debugging sections and our linker knows nothing
    # about that. Can we detect whether GCC does that or not instead?
    local GCC_SYSV=$("${_GCC}" --version)
    GCC_SYSV=${GCC_SYSV##* }
    if [[ ${GCC_SYSV} =~ 1[.].* ]] ||
       [[ ${GCC_SYSV} =~ 2[.].* ]] ||
       [[ ${GCC_SYSV} =~ 3[.].* ]] ||
       [[ ${GCC_SYSV} =~ 4[.].* ]] ||
       [[ ${GCC_SYSV} =~ 5[.].* ]] ||
       [[ ${GCC_SYSV} =~ 6[.].* ]] ||
       [[ ${GCC_SYSV} =~ 7[.].* ]]; then
      $(dirname "${BASH_SOURCE[0]}")/ld.bin "$@"
    else
      "${_GCC}" "$@"
    fi
  fi
}

_COMPILER_COMPAT_LD_ARGS=( "$@" )
ld_fn "${_COMPILER_COMPAT_LD_ARGS[@]}"

