summary refs log tree commit diff
path: root/pkgs/build-support/cc-wrapper
diff options
context:
space:
mode:
Diffstat (limited to 'pkgs/build-support/cc-wrapper')
-rw-r--r--pkgs/build-support/cc-wrapper/add-flags28
-rw-r--r--pkgs/build-support/cc-wrapper/cc-wrapper.sh158
-rw-r--r--pkgs/build-support/cc-wrapper/default.nix251
-rw-r--r--pkgs/build-support/cc-wrapper/gnat-wrapper.sh103
-rw-r--r--pkgs/build-support/cc-wrapper/gnatlink-wrapper.sh33
-rw-r--r--pkgs/build-support/cc-wrapper/ld-solaris-wrapper.sh40
-rw-r--r--pkgs/build-support/cc-wrapper/ld-wrapper.sh166
-rw-r--r--pkgs/build-support/cc-wrapper/setup-hook.sh38
-rw-r--r--pkgs/build-support/cc-wrapper/utils.sh24
9 files changed, 841 insertions, 0 deletions
diff --git a/pkgs/build-support/cc-wrapper/add-flags b/pkgs/build-support/cc-wrapper/add-flags
new file mode 100644
index 0000000000000..d483615390477
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/add-flags
@@ -0,0 +1,28 @@
+# `-B@out@/bin' forces gcc to use ld-wrapper.sh when calling ld.
+export NIX_CFLAGS_COMPILE="-B@out@/bin/ $NIX_CFLAGS_COMPILE"
+
+if [ -e @out@/nix-support/libc-cflags ]; then
+    export NIX_CFLAGS_COMPILE="$(cat @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE"
+fi
+
+if [ -e @out@/nix-support/gcc-cflags ]; then
+    export NIX_CFLAGS_COMPILE="$(cat @out@/nix-support/cc-cflags) $NIX_CFLAGS_COMPILE"
+fi
+
+if [ -e @out@/nix-support/gnat-cflags ]; then
+    export NIX_GNATFLAGS_COMPILE="$(cat @out@/nix-support/gnat-cflags) $NIX_GNATFLAGS_COMPILE"
+fi
+
+if [ -e @out@/nix-support/libc-ldflags ]; then
+    export NIX_LDFLAGS+=" $(cat @out@/nix-support/libc-ldflags)"
+fi
+
+if [ -e @out@/nix-support/gcc-ldflags ]; then
+    export NIX_LDFLAGS+=" $(cat @out@/nix-support/cc-ldflags)"
+fi
+
+if [ -e @out@/nix-support/libc-ldflags-before ]; then
+    export NIX_LDFLAGS_BEFORE="$(cat @out@/nix-support/libc-ldflags-before) $NIX_LDFLAGS_BEFORE"
+fi
+
+export NIX_CC_WRAPPER_FLAGS_SET=1
diff --git a/pkgs/build-support/cc-wrapper/cc-wrapper.sh b/pkgs/build-support/cc-wrapper/cc-wrapper.sh
new file mode 100644
index 0000000000000..3a7e24dfcafb6
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/cc-wrapper.sh
@@ -0,0 +1,158 @@
+#! @shell@ -e
+
+if [ -n "$NIX_CC_WRAPPER_START_HOOK" ]; then
+    source "$NIX_CC_WRAPPER_START_HOOK"
+fi
+
+if [ -z "$NIX_CC_WRAPPER_FLAGS_SET" ]; then
+    source @out@/nix-support/add-flags.sh
+fi
+
+source @out@/nix-support/utils.sh
+
+
+# Figure out if linker flags should be passed.  GCC prints annoying
+# warnings when they are not needed.
+dontLink=0
+getVersion=0
+nonFlagArgs=0
+
+for i in "$@"; do
+    if [ "$i" = -c ]; then
+        dontLink=1
+    elif [ "$i" = -S ]; then
+        dontLink=1
+    elif [ "$i" = -E ]; then
+        dontLink=1
+    elif [ "$i" = -E ]; then
+        dontLink=1
+    elif [ "$i" = -M ]; then
+        dontLink=1
+    elif [ "$i" = -MM ]; then
+        dontLink=1
+    elif [ "$i" = -x ]; then
+        # At least for the cases c-header or c++-header we should set dontLink.
+        # I expect no one use -x other than making precompiled headers.
+        dontLink=1
+    elif [ "${i:0:1}" != - ]; then
+        nonFlagArgs=1
+    elif [ "$i" = -m32 ]; then
+        if [ -e @out@/nix-support/dynamic-linker-m32 ]; then
+            NIX_LDFLAGS="$NIX_LDFLAGS -dynamic-linker $(cat @out@/nix-support/dynamic-linker-m32)"
+        fi
+    fi
+done
+
+# If we pass a flag like -Wl, then gcc will call the linker unless it
+# can figure out that it has to do something else (e.g., because of a
+# "-c" flag).  So if no non-flag arguments are given, don't pass any
+# linker flags.  This catches cases like "gcc" (should just print
+# "gcc: no input files") and "gcc -v" (should print the version).
+if [ "$nonFlagArgs" = 0 ]; then
+    dontLink=1
+fi
+
+
+# Optionally filter out paths not refering to the store.
+params=("$@")
+if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" ]; then
+    rest=()
+    n=0
+    while [ $n -lt ${#params[*]} ]; do
+        p=${params[n]}
+        p2=${params[$((n+1))]}
+        if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
+            skip $p
+        elif [ "$p" = -L ] && badPath "$p2"; then
+            n=$((n + 1)); skip $p2
+        elif [ "${p:0:3}" = -I/ ] && badPath "${p:2}"; then
+            skip $p
+        elif [ "$p" = -I ] && badPath "$p2"; then
+            n=$((n + 1)); skip $p2
+        elif [ "$p" = -isystem ] && badPath "$p2"; then
+            n=$((n + 1)); skip $p2
+        else
+            rest=("${rest[@]}" "$p")
+        fi
+        n=$((n + 1))
+    done
+    params=("${rest[@]}")
+fi
+
+if test -n "@libcxx@"; then
+    NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem@libcxx@/include/c++/v1"
+    if [[ "@prog@" = *++ ]]; then
+        NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -stdlib=libc++"
+        if test -z "$NIX_SKIP_CXX"; then
+            NIX_CFLAGS_LINK="$NIX_CFLAGS_LINK -L@libcxx@/lib -stdlib=libc++"
+        fi
+        if test -z "$NIX_SKIP_CXXABI" && echo "$@" | grep -qvw -- -nostdlib; then
+            NIX_CFLAGS_LINK="$NIX_CFLAGS_LINK -L@libcxxabi@/lib -lc++abi"
+        fi
+    fi
+fi
+
+# Add the flags for the C compiler proper.
+extraAfter=($NIX_CFLAGS_COMPILE)
+extraBefore=()
+
+# When enforcing purity, pretend gcc can't find the current date and
+# time
+if [ "$NIX_ENFORCE_PURITY" = 1 ]; then
+    extraAfter+=('-D__DATE__="Jan 01 1970"'
+        '-D__TIME__="00:00:01"'
+        -Wno-builtin-macro-redefined)
+fi
+
+
+if [ "$dontLink" != 1 ]; then
+
+    # Add the flags that should only be passed to the compiler when
+    # linking.
+    extraAfter+=($NIX_CFLAGS_LINK)
+
+    # Add the flags that should be passed to the linker (and prevent
+    # `ld-wrapper' from adding NIX_LDFLAGS again).
+    for i in $NIX_LDFLAGS_BEFORE; do
+        extraBefore=(${extraBefore[@]} "-Wl,$i")
+    done
+    for i in $NIX_LDFLAGS; do
+        if [ "${i:0:3}" = -L/ ]; then
+            extraAfter+=("$i")
+        else
+            extraAfter+=("-Wl,$i")
+        fi
+    done
+    export NIX_LDFLAGS_SET=1
+fi
+
+# As a very special hack, if the arguments are just `-v', then don't
+# add anything.  This is to prevent `gcc -v' (which normally prints
+# out the version number and returns exit code 0) from printing out
+# `No input files specified' and returning exit code 1.
+if [ "$*" = -v ]; then
+    extraAfter=()
+    extraBefore=()
+fi
+
+# Optionally print debug info.
+if [ -n "$NIX_DEBUG" ]; then
+  echo "original flags to @prog@:" >&2
+  for i in "${params[@]}"; do
+      echo "  $i" >&2
+  done
+  echo "extraBefore flags to @prog@:" >&2
+  for i in ${extraBefore[@]}; do
+      echo "  $i" >&2
+  done
+  echo "extraAfter flags to @prog@:" >&2
+  for i in ${extraAfter[@]}; do
+      echo "  $i" >&2
+  done
+fi
+
+if [ -n "$NIX_CC_WRAPPER_EXEC_HOOK" ]; then
+    source "$NIX_CC_WRAPPER_EXEC_HOOK"
+fi
+
+exec @prog@ ${extraBefore[@]} "${params[@]}" "${extraAfter[@]}"
diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix
new file mode 100644
index 0000000000000..abbba31641b30
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/default.nix
@@ -0,0 +1,251 @@
+# The Nixpkgs CC is not directly usable, since it doesn't know where
+# the C library and standard header files are. Therefore the compiler
+# produced by that package cannot be installed directly in a user
+# environment and used from the command line. So we use a wrapper
+# script that sets up the right environment variables so that the
+# compiler and the linker just "work".
+
+{ name ? "", stdenv, nativeTools, nativeLibc, nativePrefix ? ""
+, cc ? null, libc ? null, binutils ? null, coreutils ? null, shell ? stdenv.shell
+, zlib ? null, extraPackages ? []
+, libcxx ? null, libcxxabi ? null
+}:
+
+with stdenv.lib;
+
+assert nativeTools -> nativePrefix != "";
+assert !nativeTools -> cc != null && binutils != null && coreutils != null;
+assert !nativeLibc -> libc != null;
+
+# For ghdl (the vhdl language provider to gcc) we need zlib in the wrapper.
+assert cc.langVhdl or false -> zlib != null;
+
+let
+
+  ccVersion = (builtins.parseDrvName cc.name).version;
+  ccName = (builtins.parseDrvName cc.name).name;
+
+in
+
+stdenv.mkDerivation {
+  name =
+    (if name != "" then name else ccName + "-wrapper") +
+    (if cc != null && ccVersion != "" then "-" + ccVersion else "");
+
+  preferLocalBuild = true;
+
+  inherit cc shell libcxx libcxxabi;
+  libc = if nativeLibc then null else libc;
+  binutils = if nativeTools then null else binutils;
+  # The wrapper scripts use 'cat', so we may need coreutils.
+  coreutils = if nativeTools then null else coreutils;
+
+  passthru = { inherit nativeTools nativeLibc nativePrefix; };
+
+  buildCommand =
+    ''
+      mkdir -p $out/bin $out/nix-support
+
+      wrap() {
+        local dst="$1"
+        local wrapper="$2"
+        export prog="$3"
+        substituteAll "$wrapper" "$out/bin/$dst"
+        chmod +x "$out/bin/$dst"
+      }
+    ''
+
+    + optionalString (!nativeLibc) ''
+      dynamicLinker="$libc/lib/$dynamicLinker"
+      echo $dynamicLinker > $out/nix-support/dynamic-linker
+
+      if [ -e $libc/lib/32/ld-linux.so.2 ]; then
+        echo $libc/lib/32/ld-linux.so.2 > $out/nix-support/dynamic-linker-m32
+      fi
+
+      # The "-B$libc/lib/" flag is a quick hack to force gcc to link
+      # against the crt1.o from our own glibc, rather than the one in
+      # /usr/lib.  (This is only an issue when using an `impure'
+      # compiler/linker, i.e., one that searches /usr/lib and so on.)
+      #
+      # Unfortunately, setting -B appears to override the default search
+      # path. Thus, the gcc-specific "../includes-fixed" directory is
+      # now longer searched and glibc's <limits.h> header fails to
+      # compile, because it uses "#include_next <limits.h>" to find the
+      # limits.h file in ../includes-fixed. To remedy the problem,
+      # another -idirafter is necessary to add that directory again.
+      echo "-B$libc/lib/ -idirafter $libc/include -idirafter $cc/lib/gcc/*/*/include-fixed" > $out/nix-support/libc-cflags
+
+      echo "-L$libc/lib" > $out/nix-support/libc-ldflags
+
+      # The dynamic linker is passed in `ldflagsBefore' to allow
+      # explicit overrides of the dynamic linker by callers to gcc/ld
+      # (the *last* value counts, so ours should come first).
+      echo "-dynamic-linker" $dynamicLinker > $out/nix-support/libc-ldflags-before
+
+      echo $libc > $out/nix-support/orig-libc
+    ''
+
+    + (if nativeTools then ''
+      ccPath="${nativePrefix}/bin"
+      ldPath="${nativePrefix}/bin"
+    '' else ''
+      echo $cc > $out/nix-support/orig-cc
+
+      # GCC shows $cc/lib in `gcc -print-search-dirs', but not
+      # $cc/lib64 (even though it does actually search there...)..
+      # This confuses libtool.  So add it to the compiler tool search
+      # path explicitly.
+      if [ -e "$cc/lib64" -a ! -L "$cc/lib64" ]; then
+        ccLDFlags+=" -L$cc/lib64"
+        ccCFlags+=" -B$cc/lib64"
+      fi
+      ccLDFlags+=" -L$cc/lib"
+
+      ${optionalString cc.langVhdl or false ''
+        ccLDFlags+=" -L${zlib}/lib"
+      ''}
+
+      # Find the gcc libraries path (may work only without multilib).
+      ${optionalString cc.langAda or false ''
+        basePath=`echo $cc/lib/*/*/*`
+        ccCFlags+=" -B$basePath -I$basePath/adainclude"
+        gnatCFlags="-aI$basePath/adainclude -aO$basePath/adalib"
+        echo "$gnatCFlags" > $out/nix-support/gnat-cflags
+      ''}
+
+      if [ -e $ccPath/clang ]; then
+        # Need files like crtbegin.o from gcc
+        # It's unclear if these will ever be provided by an LLVM project
+        ccCFlags="$ccCFlags -B$basePath"
+        ccCFlags="$ccCFlags -isystem$cc/lib/clang/$ccVersion/include"
+      fi
+
+      echo "$ccLDFlags" > $out/nix-support/cc-ldflags
+      echo "$ccCFlags" > $out/nix-support/cc-cflags
+
+      ccPath="$cc/bin"
+      ldPath="$binutils/bin"
+
+      # Propagate the wrapped cc so that if you install the wrapper,
+      # you get tools like gcov, the manpages, etc. as well (including
+      # for binutils and Glibc).
+      echo $cc $binutils $libc > $out/nix-support/propagated-user-env-packages
+
+      echo ${toString extraPackages} > $out/nix-support/propagated-native-build-inputs
+    ''
+
+    + optionalString (stdenv.isSunOS && nativePrefix != "") ''
+      # Solaris needs an additional ld wrapper.
+      ldPath="${nativePrefix}/bin"
+      ld="$out/bin/ld-solaris"
+      wrap ld-solaris ${./ld-solaris-wrapper.sh}
+    '')
+
+    + ''
+      # Create a symlink to as (the assembler).  This is useful when a
+      # cc-wrapper is installed in a user environment, as it ensures that
+      # the right assembler is called.
+      if [ -e $ldPath/as ]; then
+        ln -s $ldPath/as $out/bin/as
+      fi
+
+      wrap ld ${./ld-wrapper.sh} ''${ld:-$ldPath/ld}
+
+      if [ -e $binutils/bin/ld.gold ]; then
+        wrap ld.gold ${./ld-wrapper.sh} $binutils/bin/ld.gold
+      fi
+
+      if [ -e $binutils/bin/ld.bfd ]; then
+        wrap ld.bfd ${./ld-wrapper.sh} $binutils/bin/ld.bfd
+      fi
+
+      if [ -e $ccPath/gcc ]; then
+        wrap gcc ${./cc-wrapper.sh} $ccPath/gcc
+        ln -s gcc $out/bin/cc
+      elif [ -e $ccPath/clang ]; then
+        wrap clang ${./cc-wrapper.sh} $ccPath/clang
+        ln -s clang $out/bin/cc
+      fi
+
+      if [ -e $ccPath/g++ ]; then
+        wrap g++ ${./cc-wrapper.sh} $ccPath/g++
+        ln -s g++ $out/bin/c++
+      elif [ -e $ccPath/clang++ ]; then
+        wrap clang++ ${./cc-wrapper.sh} $ccPath/clang++
+        ln -s clang++ $out/bin/c++
+      fi
+
+      if [ -e $ccPath/cpp ]; then
+        wrap cpp ${./cc-wrapper.sh} $ccPath/cpp
+      fi
+    ''
+
+    + optionalString cc.langFortran or false ''
+      wrap gfortran ${./cc-wrapper.sh} $ccPath/gfortran
+      ln -sv gfortran $out/bin/g77
+      ln -sv gfortran $out/bin/f77
+    ''
+
+    + optionalString cc.langJava or false ''
+      wrap gcj ${./cc-wrapper.sh} $ccPath/gcj
+    ''
+
+    + optionalString cc.langGo or false ''
+      wrap ccgo ${./cc-wrapper.sh} $ccPath/gccgo
+    ''
+
+    + optionalString cc.langAda or false ''
+      wrap gnatgcc ${./cc-wrapper.sh} $ccPath/gnatgcc
+      wrap gnatmake ${./gnat-wrapper.sh} $ccPath/gnatmake
+      wrap gnatbind ${./gnat-wrapper.sh} $ccPath/gnatbind
+      wrap gnatlink ${./gnatlink-wrapper.sh} $ccPath/gnatlink
+    ''
+
+    + optionalString cc.langVhdl or false ''
+      ln -s $ccPath/ghdl $out/bin/ghdl
+    ''
+
+    + ''
+      substituteAll ${./setup-hook.sh} $out/nix-support/setup-hook
+      substituteAll ${./add-flags} $out/nix-support/add-flags.sh
+      cp -p ${./utils.sh} $out/nix-support/utils.sh
+    '';
+
+  # The dynamic linker has different names on different Linux platforms.
+  dynamicLinker =
+    if !nativeLibc then
+      (if stdenv.system == "i686-linux" then "ld-linux.so.2" else
+       if stdenv.system == "x86_64-linux" then "ld-linux-x86-64.so.2" else
+       # ARM with a wildcard, which can be "" or "-armhf".
+       if stdenv.isArm then "ld-linux*.so.3" else
+       if stdenv.system == "powerpc-linux" then "ld.so.1" else
+       if stdenv.system == "mips64el-linux" then "ld.so.1" else
+       abort "Don't know the name of the dynamic linker for this platform.")
+    else "";
+
+  crossAttrs = {
+    shell = shell.crossDrv + shell.crossDrv.shellPath;
+    libc = stdenv.ccCross.libc;
+    coreutils = coreutils.crossDrv;
+    binutils = binutils.crossDrv;
+    cc = cc.crossDrv;
+    #
+    # This is not the best way to do this. I think the reference should be
+    # the style in the gcc-cross-wrapper, but to keep a stable stdenv now I
+    # do this sufficient if/else.
+    dynamicLinker =
+      (if stdenv.cross.arch == "arm" then "ld-linux.so.3" else
+       if stdenv.cross.arch == "mips" then "ld.so.1" else
+       if stdenv.lib.hasSuffix "pc-gnu" stdenv.cross.config then "ld.so.1" else
+       abort "don't know the name of the dynamic linker for this platform");
+  };
+
+  meta =
+    let cc_ = if cc != null then cc else {}; in
+    (if cc_ ? meta then removeAttrs cc.meta ["priority"] else {}) //
+    { description =
+        stdenv.lib.attrByPath ["meta" "description"] "System C compiler" cc_
+        + " (wrapper script)";
+    };
+}
diff --git a/pkgs/build-support/cc-wrapper/gnat-wrapper.sh b/pkgs/build-support/cc-wrapper/gnat-wrapper.sh
new file mode 100644
index 0000000000000..3514ccd673258
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/gnat-wrapper.sh
@@ -0,0 +1,103 @@
+#! @shell@ -e
+
+if [ -n "$NIX_GNAT_WRAPPER_START_HOOK" ]; then
+    source "$NIX_GNAT_WRAPPER_START_HOOK"
+fi
+
+if [ -z "$NIX_GNAT_WRAPPER_FLAGS_SET" ]; then
+    source @out@/nix-support/add-flags.sh
+fi
+
+source @out@/nix-support/utils.sh
+
+
+# Figure out if linker flags should be passed.  GCC prints annoying
+# warnings when they are not needed.
+dontLink=0
+getVersion=0
+nonFlagArgs=0
+
+for i in "$@"; do
+    if [ "$i" = -c ]; then
+        dontLink=1
+    elif [ "$i" = -M ]; then
+        dontLink=1
+    elif [ "${i:0:1}" != - ]; then
+        nonFlagArgs=1
+    elif [ "$i" = -m32 ]; then
+        if [ -e @out@/nix-support/dynamic-linker-m32 ]; then
+            NIX_LDFLAGS="$NIX_LDFLAGS -dynamic-linker $(cat @out@/nix-support/dynamic-linker-m32)"
+        fi
+    fi
+done
+
+# If we pass a flag like -Wl, then gcc will call the linker unless it
+# can figure out that it has to do something else (e.g., because of a
+# "-c" flag).  So if no non-flag arguments are given, don't pass any
+# linker flags.  This catches cases like "gcc" (should just print
+# "gcc: no input files") and "gcc -v" (should print the version).
+if [ "$nonFlagArgs" = 0 ]; then
+    dontLink=1
+fi
+
+
+# Optionally filter out paths not refering to the store.
+params=("$@")
+if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" ]; then
+    rest=()
+    n=0
+    while [ $n -lt ${#params[*]} ]; do
+        p=${params[n]}
+        p2=${params[$((n+1))]}
+        if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
+            skip $p
+        elif [ "${p:0:3}" = -I/ ] && badPath "${p:2}"; then
+            skip $p
+        elif [ "${p:0:4}" = -aI/ ] && badPath "${p:3}"; then
+            skip $p
+        elif [ "${p:0:4}" = -aO/ ] && badPath "${p:3}"; then
+            skip $p
+        else
+            rest=("${rest[@]}" "$p")
+        fi
+        n=$((n + 1))
+    done
+    params=("${rest[@]}")
+fi
+
+
+# Add the flags for the GNAT compiler proper.
+extraAfter=($NIX_GNATFLAGS_COMPILE)
+extraBefore=()
+
+if [ "`basename $0`x" = "gnatmakex" ]; then
+  extraBefore=("--GNATBIND=@out@/bin/gnatbind --GNATLINK=@out@/bin/gnatlink ")
+fi
+
+# Add the flags that should be passed to the linker (and prevent
+# `ld-wrapper' from adding NIX_LDFLAGS again).
+#for i in $NIX_LDFLAGS_BEFORE; do
+#    extraBefore=(${extraBefore[@]} "-largs $i")
+#done
+
+# Optionally print debug info.
+if [ -n "$NIX_DEBUG" ]; then
+  echo "original flags to @prog@:" >&2
+  for i in "${params[@]}"; do
+      echo "  $i" >&2
+  done
+  echo "extraBefore flags to @prog@:" >&2
+  for i in ${extraBefore[@]}; do
+      echo "  $i" >&2
+  done
+  echo "extraAfter flags to @prog@:" >&2
+  for i in ${extraAfter[@]}; do
+      echo "  $i" >&2
+  done
+fi
+
+if [ -n "$NIX_GNAT_WRAPPER_EXEC_HOOK" ]; then
+    source "$NIX_GNAT_WRAPPER_EXEC_HOOK"
+fi
+
+exec @prog@ ${extraBefore[@]} "${params[@]}" ${extraAfter[@]}
diff --git a/pkgs/build-support/cc-wrapper/gnatlink-wrapper.sh b/pkgs/build-support/cc-wrapper/gnatlink-wrapper.sh
new file mode 100644
index 0000000000000..c9958dbbb4134
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/gnatlink-wrapper.sh
@@ -0,0 +1,33 @@
+#! @shell@ -e
+
+# Add the flags for the GNAT compiler proper.
+extraAfter="--GCC=@out@/bin/gcc"
+extraBefore=()
+
+# Add the flags that should be passed to the linker (and prevent
+# `ld-wrapper' from adding NIX_LDFLAGS again).
+#for i in $NIX_LDFLAGS_BEFORE; do
+#    extraBefore=(${extraBefore[@]} "-largs $i")
+#done
+
+# Optionally print debug info.
+if [ -n "$NIX_DEBUG" ]; then
+  echo "original flags to @prog@:" >&2
+  for i in "$@"; do
+      echo "  $i" >&2
+  done
+  echo "extraBefore flags to @prog@:" >&2
+  for i in ${extraBefore[@]}; do
+      echo "  $i" >&2
+  done
+  echo "extraAfter flags to @prog@:" >&2
+  for i in ${extraAfter[@]}; do
+      echo "  $i" >&2
+  done
+fi
+
+if [ -n "$NIX_GNAT_WRAPPER_EXEC_HOOK" ]; then
+    source "$NIX_GNAT_WRAPPER_EXEC_HOOK"
+fi
+
+exec @prog@ ${extraBefore[@]} "$@" ${extraAfter[@]}
diff --git a/pkgs/build-support/cc-wrapper/ld-solaris-wrapper.sh b/pkgs/build-support/cc-wrapper/ld-solaris-wrapper.sh
new file mode 100644
index 0000000000000..9216ea3198dd5
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/ld-solaris-wrapper.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+set -e
+set -u
+
+# I've also tried adding -z direct and -z lazyload, but it gave too many problems with C++ exceptions :'(
+# Also made sure libgcc would not be lazy-loaded, as suggested here: https://www.illumos.org/issues/2534#note-3
+#   but still no success.
+cmd="@prog@ -z ignore"
+
+args=("$@");
+
+# This loop makes sure all -L arguments are before -l arguments, or ld may complain it cannot find a library.
+# GNU binutils does not have this problem:
+#   http://stackoverflow.com/questions/5817269/does-the-order-of-l-and-l-options-in-the-gnu-linker-matter
+i=0;
+while [[ $i -lt $# ]]; do
+    case "${args[$i]}" in
+        -L)  cmd="$cmd ${args[$i]} ${args[($i+1)]}"; i=($i+1); ;;
+        -L*) cmd="$cmd ${args[$i]}" ;;
+        *)   ;;
+    esac
+    i=($i+1);
+done
+
+i=0;
+while [[ $i -lt $# ]]; do
+    case "${args[$i]}" in
+        -L)  i=($i+1); ;;
+        -L*) ;;
+        *)   cmd="$cmd ${args[$i]}" ;;
+    esac
+    i=($i+1);
+done
+
+# Trace:
+set -x
+exec $cmd
+
+exit 0
diff --git a/pkgs/build-support/cc-wrapper/ld-wrapper.sh b/pkgs/build-support/cc-wrapper/ld-wrapper.sh
new file mode 100644
index 0000000000000..30c531b76479d
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/ld-wrapper.sh
@@ -0,0 +1,166 @@
+#! @shell@ -e
+
+if [ -n "$NIX_LD_WRAPPER_START_HOOK" ]; then
+    source "$NIX_LD_WRAPPER_START_HOOK"
+fi
+
+if [ -z "$NIX_CC_WRAPPER_FLAGS_SET" ]; then
+    source @out@/nix-support/add-flags.sh
+fi
+
+source @out@/nix-support/utils.sh
+
+
+# Optionally filter out paths not refering to the store.
+params=("$@")
+if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" \
+        -a \( -z "$NIX_IGNORE_LD_THROUGH_GCC" -o -z "$NIX_LDFLAGS_SET" \) ]; then
+    rest=()
+    n=0
+    while [ $n -lt ${#params[*]} ]; do
+        p=${params[n]}
+        p2=${params[$((n+1))]}
+        if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
+            skip $p
+        elif [ "$p" = -L ] && badPath "$p2"; then
+            n=$((n + 1)); skip $p2
+        elif [ "$p" = -rpath ] && badPath "$p2"; then
+            n=$((n + 1)); skip $p2
+        elif [ "$p" = -dynamic-linker ] && badPath "$p2"; then
+            n=$((n + 1)); skip $p2
+        elif [ "${p:0:1}" = / ] && badPath "$p"; then
+            # We cannot skip this; barf.
+            echo "impure path \`$p' used in link" >&2
+            exit 1
+        elif [ "${p:0:9}" = --sysroot ]; then
+            # Our ld is not built with sysroot support (Can we fix that?)
+            :
+        else
+            rest=("${rest[@]}" "$p")
+        fi
+        n=$((n + 1))
+    done
+    params=("${rest[@]}")
+fi
+
+
+extra=()
+extraBefore=()
+
+if [ -z "$NIX_LDFLAGS_SET" ]; then
+    extra+=($NIX_LDFLAGS)
+    extraBefore+=($NIX_LDFLAGS_BEFORE)
+fi
+
+extra+=($NIX_LDFLAGS_AFTER)
+
+
+# Add all used dynamic libraries to the rpath.
+if [ "$NIX_DONT_SET_RPATH" != 1 ]; then
+
+    libPath=""
+    addToLibPath() {
+        local path="$1"
+        if [ "${path:0:1}" != / ]; then return 0; fi
+        case "$path" in
+            *..*|*./*|*/.*|*//*)
+                local path2
+                if path2=$(readlink -f "$path"); then
+                    path="$path2"
+                fi
+                ;;
+        esac
+        case $libPath in
+            *\ $path\ *) return 0 ;;
+        esac
+        libPath="$libPath $path "
+    }
+
+    addToRPath() {
+        # If the path is not in the store, don't add it to the rpath.
+        # This typically happens for libraries in /tmp that are later
+        # copied to $out/lib.  If not, we're screwed.
+        if [ "${1:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then return 0; fi
+        case $rpath in
+            *\ $1\ *) return 0 ;;
+        esac
+        rpath="$rpath $1 "
+    }
+
+    libs=""
+    addToLibs() {
+        libs="$libs $1"
+    }
+
+    rpath=""
+
+    # First, find all -L... switches.
+    allParams=("${params[@]}" ${extra[@]})
+    n=0
+    while [ $n -lt ${#allParams[*]} ]; do
+        p=${allParams[n]}
+        p2=${allParams[$((n+1))]}
+        if [ "${p:0:3}" = -L/ ]; then
+            addToLibPath ${p:2}
+        elif [ "$p" = -L ]; then
+            addToLibPath ${p2}
+            n=$((n + 1))
+        elif [ "$p" = -l ]; then
+            addToLibs ${p2}
+            n=$((n + 1))
+        elif [ "${p:0:2}" = -l ]; then
+            addToLibs ${p:2}
+        elif [ "$p" = -dynamic-linker ]; then
+            # Ignore the dynamic linker argument, or it
+            # will get into the next 'elif'. We don't want
+            # the dynamic linker path rpath to go always first.
+            n=$((n + 1))
+        elif [[ "$p" =~ ^[^-].*\.so($|\.) ]]; then
+            # This is a direct reference to a shared library, so add
+            # its directory to the rpath.
+            path="$(dirname "$p")";
+            addToRPath "${path}"
+        fi
+        n=$((n + 1))
+    done
+
+    # Second, for each directory in the library search path (-L...),
+    # see if it contains a dynamic library used by a -l... flag.  If
+    # so, add the directory to the rpath.
+    # It's important to add the rpath in the order of -L..., so
+    # the link time chosen objects will be those of runtime linking.
+
+    for i in $libPath; do
+        for j in $libs; do
+            if [ -f "$i/lib$j.so" ]; then
+                addToRPath $i
+                break
+            fi
+        done
+    done
+
+
+    # Finally, add `-rpath' switches.
+    for i in $rpath; do
+        extra=(${extra[@]} -rpath $i)
+    done
+fi
+
+
+# Optionally print debug info.
+if [ -n "$NIX_DEBUG" ]; then
+  echo "original flags to @prog@:" >&2
+  for i in "${params[@]}"; do
+      echo "  $i" >&2
+  done
+  echo "extra flags to @prog@:" >&2
+  for i in ${extra[@]}; do
+      echo "  $i" >&2
+  done
+fi
+
+if [ -n "$NIX_LD_WRAPPER_EXEC_HOOK" ]; then
+    source "$NIX_LD_WRAPPER_EXEC_HOOK"
+fi
+
+exec @prog@ ${extraBefore[@]} "${params[@]}" ${extra[@]}
diff --git a/pkgs/build-support/cc-wrapper/setup-hook.sh b/pkgs/build-support/cc-wrapper/setup-hook.sh
new file mode 100644
index 0000000000000..218899e9d8fcf
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/setup-hook.sh
@@ -0,0 +1,38 @@
+export NIX_CC=@out@
+
+addCVars () {
+    if [ -d $1/include ]; then
+        export NIX_CFLAGS_COMPILE+=" -isystem $1/include"
+    fi
+
+    if [ -d $1/lib64 -a ! -L $1/lib64 ]; then
+        export NIX_LDFLAGS+=" -L$1/lib64"
+    fi
+
+    if [ -d $1/lib ]; then
+        export NIX_LDFLAGS+=" -L$1/lib"
+    fi
+}
+
+envHooks+=(addCVars)
+
+# Note: these come *after* $out in the PATH (see setup.sh).
+
+if [ -n "@cc@" ]; then
+    addToSearchPath PATH @cc@/bin
+fi
+
+if [ -n "@binutils@" ]; then
+    addToSearchPath PATH @binutils@/bin
+fi
+
+if [ -n "@libc@" ]; then
+    addToSearchPath PATH @libc@/bin
+fi
+
+if [ -n "@coreutils@" ]; then
+    addToSearchPath PATH @coreutils@/bin
+fi
+
+export CC=cc
+export CXX=c++
diff --git a/pkgs/build-support/cc-wrapper/utils.sh b/pkgs/build-support/cc-wrapper/utils.sh
new file mode 100644
index 0000000000000..3ab512d85c4ec
--- /dev/null
+++ b/pkgs/build-support/cc-wrapper/utils.sh
@@ -0,0 +1,24 @@
+skip () {
+    if [ -n "$NIX_DEBUG" ]; then
+        echo "skipping impure path $1" >&2
+    fi
+}
+
+
+# Checks whether a path is impure.  E.g., `/lib/foo.so' is impure, but
+# `/nix/store/.../lib/foo.so' isn't.
+badPath() {
+    local p=$1
+
+    # Relative paths are okay (since they're presumably relative to
+    # the temporary build directory).
+    if [ "${p:0:1}" != / ]; then return 1; fi
+
+    # Otherwise, the path should refer to the store or some temporary
+    # directory (including the build directory).
+    test \
+        "$p" != "/dev/null" -a \
+        "${p:0:${#NIX_STORE}}" != "$NIX_STORE" -a \
+        "${p:0:4}" != "/tmp" -a \
+        "${p:0:${#NIX_BUILD_TOP}}" != "$NIX_BUILD_TOP"
+}