diff options
Diffstat (limited to 'pkgs/build-support')
-rwxr-xr-x | pkgs/build-support/gcc-wrapper/builder.sh | 62 | ||||
-rw-r--r-- | pkgs/build-support/gcc-wrapper/default.nix | 23 | ||||
-rw-r--r-- | pkgs/build-support/gcc-wrapper/gcc-wrapper.sh | 74 | ||||
-rw-r--r-- | pkgs/build-support/gcc-wrapper/ld-wrapper.sh | 28 | ||||
-rw-r--r-- | pkgs/build-support/gcc-wrapper/setup-hook.sh | 15 |
5 files changed, 202 insertions, 0 deletions
diff --git a/pkgs/build-support/gcc-wrapper/builder.sh b/pkgs/build-support/gcc-wrapper/builder.sh new file mode 100755 index 0000000000000..d3b1f433559c9 --- /dev/null +++ b/pkgs/build-support/gcc-wrapper/builder.sh @@ -0,0 +1,62 @@ +#! /bin/sh -e + +. $stdenv/setup + +if test -z "$isNative"; then + cflagsCompile="-B$out/bin -B$glibc/lib -isystem $glibc/include" + ldflags="-L$glibc/lib -L$gcc/lib " \ + "-dynamic-linker $glibc/lib/ld-linux.so.2" \ + "-rpath $glibc/lib -rpath $gcc/lib" +else + cflagsCompile="-B$out/bin" +fi + +mkdir $out +mkdir $out/bin + + +mkGccWrapper () { + local dst=$1 + local src=$2 + + if ! test -f "$src"; then + echo "$src does not exist (skipping)" + return + fi + + sed \ + -e "s^@cflagsCompile@^$cflagsCompile^g" \ + -e "s^@cflagsLink@^$cflagsLink^g" \ + -e "s^@ldflags@^$ldflags^g" \ + -e "s^@gcc@^$src^g" \ + < $gccWrapper > $dst + chmod +x $dst + +} + +mkGccWrapper $out/bin/gcc $gcc/bin/gcc +ln -s gcc $out/bin/cc + +mkGccWrapper $out/bin/g++ $gcc/bin/g++ +ln -s g++ $out/bin/c++ + +mkGccWrapper $out/bin/g77 $gcc/bin/g77 +ln -s g77 $out/bin/f77 + + +sed \ + -e "s^@ldflags@^$ldflags^g" \ + -e "s^@ld@^$gcc/bin/ld^g" \ + < $ldWrapper > $out/bin/ld +chmod +x $out/bin/ld + + +mkdir $out/nix-support +test -z "$isNative" && echo $gcc > $out/nix-support/orig-gcc +test -z "$isNative" && echo $glibc > $out/nix-support/orig-glibc + +sed \ + -e "s^@isNative@^$isNative^g" \ + -e "s^@gcc@^$gcc^g" \ + -e "s^@glibc@^$glibc^g" \ + < $setupHook > $out/nix-support/setup-hook diff --git a/pkgs/build-support/gcc-wrapper/default.nix b/pkgs/build-support/gcc-wrapper/default.nix new file mode 100644 index 0000000000000..da1896611645e --- /dev/null +++ b/pkgs/build-support/gcc-wrapper/default.nix @@ -0,0 +1,23 @@ +# The Nix `gcc' derivation 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. This +# derivation provides a wrapper that sets up the right environment +# variables so that the compiler and the linker just "work". + +{name, stdenv, isNative, gcc ? null, glibc ? null, binutils ? null}: + +assert isNative -> gcc != ""; +assert !isNative -> gcc != null && glibc != null && binutils != null; + +derivation { + system = stdenv.system; + builder = ./builder.sh; + setupHook = ./setup-hook.sh; + gccWrapper = ./gcc-wrapper.sh; + ldWrapper = ./ld-wrapper.sh; + inherit name stdenv isNative gcc glibc binutils; + langC = if isNative then true else gcc.langC; + langCC = if isNative then true else gcc.langCC; + langF77 = if isNative then false else gcc.langF77; +} diff --git a/pkgs/build-support/gcc-wrapper/gcc-wrapper.sh b/pkgs/build-support/gcc-wrapper/gcc-wrapper.sh new file mode 100644 index 0000000000000..59f38086f3250 --- /dev/null +++ b/pkgs/build-support/gcc-wrapper/gcc-wrapper.sh @@ -0,0 +1,74 @@ +#! /bin/sh + +if test -n "$NIX_GCC_WRAPPER_START_HOOK"; then + . "$NIX_GCC_WRAPPER_START_HOOK" +fi + +if test -z "$NIX_GLIBC_FLAGS_SET"; then + NIX_CFLAGS_COMPILE="@cflagsCompile@ $NIX_CFLAGS_COMPILE" + NIX_CFLAGS_LINK="@cflagsLink@ $NIX_CFLAGS_LINK" + NIX_LDFLAGS="@ldflags@ $NIX_LDFLAGS" +fi + +# Figure out if linker flags should be passed. GCC prints annoying +# warnings when they are not needed. +dontLink=0 +if test "$*" = "-v"; then + dontLink=1 +else + for i in "$@"; do + if test "$i" = "-c"; then + dontLink=1 + elif test "$i" = "-S"; then + dontLink=1 + elif test "$i" = "-E"; then + dontLink=1 + elif test "$i" = "-E"; then + dontLink=1 + elif test "$i" = "-M"; then + dontLink=1 + elif test "$i" = "-MM"; then + dontLink=1 + fi + done +fi + +# Add the flags for the C compiler proper. +extra=($NIX_CFLAGS_COMPILE) + +if test "$dontLink" != "1"; then + + # Add the flags that should only be passed to the compiler when + # linking. + extra=(${extra[@]} $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; do + extra=(${extra[@]} "-Wl,$i") + done + export NIX_LDFLAGS_SET=1 + + if test "$NIX_STRIP_DEBUG" = "1"; then + # Add executable-stripping flags. + extra=(${extra[@]} $NIX_CFLAGS_STRIP) + fi +fi + +# Optionally print debug info. +if test "$NIX_DEBUG" = "1"; then + echo "original flags to @gcc@:" >&2 + for i in "$@"; do + echo " $i" >&2 + done + echo "extra flags to @gcc@:" >&2 + for i in ${extra[@]}; do + echo " $i" >&2 + done +fi + +if test -n "$NIX_GCC_WRAPPER_EXEC_HOOK"; then + . "$NIX_GCC_WRAPPER_EXEC_HOOK" +fi + +exec @gcc@ "$@" ${extra[@]} diff --git a/pkgs/build-support/gcc-wrapper/ld-wrapper.sh b/pkgs/build-support/gcc-wrapper/ld-wrapper.sh new file mode 100644 index 0000000000000..2201bbe19d234 --- /dev/null +++ b/pkgs/build-support/gcc-wrapper/ld-wrapper.sh @@ -0,0 +1,28 @@ +#! /bin/sh + +if test -n "$NIX_LD_WRAPPER_START_HOOK"; then + . "$NIX_LD_WRAPPER_START_HOOK" +fi + +extra=() + +if test -z "$NIX_LDFLAGS_SET"; then + extra=(${extra[@]} $NIX_LDFLAGS) +fi + +if test "$NIX_DEBUG" = "1"; then + echo "original flags to @ld@:" >&2 + for i in "$@"; do + echo " $i" >&2 + done + echo "extra flags to @ld@:" >&2 + for i in ${extra[@]}; do + echo " $i" >&2 + done +fi + +if test -n "$NIX_LD_WRAPPER_EXEC_HOOK"; then + . "$NIX_LD_WRAPPER_EXEC_HOOK" +fi + +exec @ld@ "$@" ${extra[@]} diff --git a/pkgs/build-support/gcc-wrapper/setup-hook.sh b/pkgs/build-support/gcc-wrapper/setup-hook.sh new file mode 100644 index 0000000000000..a4c52ba803d7e --- /dev/null +++ b/pkgs/build-support/gcc-wrapper/setup-hook.sh @@ -0,0 +1,15 @@ +addCVars () { + if test -d $1/include; then + export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -I$1/include" + fi + + if test -d $1/lib; then + export NIX_LDFLAGS="$NIX_LDFLAGS -L$1/lib -rpath $1/lib" + fi +} + +envHooks=(${envHooks[@]} addCVars) + +if test -z "@isNative@"; then + PATH=$PATH:@gcc@/bin:@glibc@/bin +fi |