#!/bin/sh

# If you want to use a custom linker with Cargo, Cargo requires that you
# specify it in Cargo.toml or via the matching environment variable.
# Passing extra options to the linker is possible with Cargo via
# RUSTFLAGS='-C link-args', but testing showed that doing this reliably
# was difficult.
#
# Our solution to these problems is to use this wrapper script.  We pass
# in the LD and the LDFLAGS to use via environment variables.  Note that
# we do *not* quote either MOZ_CARGO_WRAP variable:
#
# * MOZ_CARGO_WRAP_LD is equivalent to CC on Unix-y platforms, and CC
#   frequently has additional arguments in addition to the compiler
#   itself.
# * MOZ_CARGO_WRAP_LDFLAGS contains space-separated arguments to pass,
#   and not quoting it ensures that each of those arguments is passed
#   as a separate argument to the actual LD.
#
# * In rare cases, we also need MOZ_CARGO_WRAP_LD_CXX, which is the
#   equivalent of CXX, when linking C++ code. Usually, this should
#   simply work by the use of CC and -lstdc++ (added by cc-rs).
#   However, in the case of sanitizer runtimes, there is a separate
#   runtime for C and C++ and linking C++ code with the C runtime can
#   fail if the requested feature is in the C++ runtime only (bug 1747298).
#
# $@ is doubly quoted for the eval. See bug 1418598.

WRAP_LD=${MOZ_CARGO_WRAP_LD}
for opt; do
  case "$opt" in
  -lc++|-lstdc++)
    WRAP_LD=${MOZ_CARGO_WRAP_LD_CXX};
    break;
    ;;
  esac
done
eval ${WRAP_LD} ${MOZ_CARGO_WRAP_LDFLAGS} '"$@"'
