Unify gcc-wrapper and clang-wrapper

This commit is contained in:
Eelco Dolstra 2014-10-10 15:48:34 +02:00
parent 0d67d13527
commit 79d0d7b437
9 changed files with 29 additions and 457 deletions

View File

@ -1,24 +0,0 @@
# `-B@out@/bin' forces clang to use ld-wrapper.sh when calling ld.
export NIX_CFLAGS_COMPILE="-B@out@/bin/ $NIX_CFLAGS_COMPILE"
if test -e @out@/nix-support/libc-cflags; then
export NIX_CFLAGS_COMPILE="$(cat @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE"
fi
if test -e @out@/nix-support/clang-cflags; then
export NIX_CFLAGS_COMPILE="$(cat @out@/nix-support/clang-cflags) $NIX_CFLAGS_COMPILE"
fi
if test -e @out@/nix-support/libc-ldflags; then
export NIX_LDFLAGS="$NIX_LDFLAGS $(cat @out@/nix-support/libc-ldflags)"
fi
if test -e @out@/nix-support/clang-ldflags; then
export NIX_LDFLAGS="$NIX_LDFLAGS $(cat @out@/nix-support/clang-ldflags)"
fi
if test -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_GCC_WRAPPER_FLAGS_SET=1

View File

@ -1,149 +0,0 @@
source $stdenv/setup
mkdir -p $out/bin
mkdir -p $out/nix-support
if test -z "$nativeLibc"; then
dynamicLinker="$libc/lib/$dynamicLinker"
echo $dynamicLinker > $out/nix-support/dynamic-linker
if test -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 clang 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.)
echo "-B$libc/lib/ -idirafter $libc/include" > $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 clang/ld
# (the *last* value counts, so ours should come first).
echo "-dynamic-linker $dynamicLinker" > $out/nix-support/libc-ldflags-before
fi
if test -n "$nativeTools"; then
if [ -n "$isDarwin" ]; then
clangPath="$clang/bin"
else
clangPath="$nativePrefix/bin"
fi
ldPath="$nativePrefix/bin"
else
clangLDFlags=""
if test -d "$gcc/lib"; then
basePath=`echo $gcc/lib/*/*/*`
# Need libgcc until the llvm compiler-rt library is complete
clangLDFlags="$clangLDFlags -L$basePath"
if test -e "$gcc/lib64"; then
clangLDFlags="$clangLDFlags -L$gcc/lib64"
else
clangLDFlags="$clangLDFlags -L$gcc/lib"
fi
fi
if test -d "$clang/lib"; then
clangLDFlags="$clangLDFlags -L$clang/lib"
fi
if [ -n "$clangLDFlags" ]; then
echo "$clangLDFlags" > $out/nix-support/clang-ldflags
fi
# Need files like crtbegin.o from gcc
# It's unclear if these will ever be provided by an LLVM project
clangCFlags="$clangCFlags -B$basePath"
clangCFlags="$clangCFlags -isystem$clang/lib/clang/$clangVersion/include"
echo "$clangCFlags" > $out/nix-support/clang-cflags
ldPath="$binutils/bin"
clangPath="$clang/bin"
fi
doSubstitute() {
local src=$1
local dst=$2
# Can't use substitute() here, because replace may not have been
# built yet (in the bootstrap).
sed \
-e "s^@out@^$out^g" \
-e "s^@shell@^$shell^g" \
-e "s^@clang@^$clang^g" \
-e "s^@clangProg@^$clangProg^g" \
-e "s^@binutils@^$binutils^g" \
-e "s^@coreutils@^$coreutils^g" \
-e "s^@libc@^$libc^g" \
-e "s^@ld@^$ldPath/ld^g" \
< "$src" > "$dst"
}
# Make wrapper scripts around clang and clang++. Also make symlinks
# cc and c++
mkClangWrapper() {
local dst=$1
local src=$2
if ! test -f "$src"; then
echo "$src does not exist (skipping)"
return 1
fi
clangProg="$src"
doSubstitute "$clangWrapper" "$dst"
chmod +x "$dst"
}
if mkClangWrapper $out/bin/clang $clangPath/clang
then
ln -sv clang $out/bin/cc
fi
if mkClangWrapper $out/bin/clang++ $clangPath/clang++
then
ln -sv clang++ $out/bin/c++
fi
# Create a symlink to as (the assembler). This is useful when a
# clang-wrapper is installed in a user environment, as it ensures that
# the right assembler is called.
ln -s $ldPath/as $out/bin/as
# Make a wrapper around the linker.
doSubstitute "$ldWrapper" "$out/bin/ld"
chmod +x "$out/bin/ld"
# Emit a setup hook. Also store the path to the original Clang and
# libc.
test -n "$clang" && echo $clang > $out/nix-support/orig-clang
test -n "$libc" && echo $libc > $out/nix-support/orig-libc
doSubstitute "$addFlags" "$out/nix-support/add-flags.sh"
doSubstitute "$setupHook" "$out/nix-support/setup-hook"
cat >> "$out/nix-support/setup-hook" << EOF
export CC=clang
export CXX=clang++
EOF
cp -p $utils $out/nix-support/utils.sh
# Propagate the wrapped clang so that if you install the wrapper, you get
# llvm tools, the manpages, etc. as well (including for binutils
# and Glibc).
if test -z "$nativeTools"; then
echo $clang $binutils $libc > $out/nix-support/propagated-user-env-packages
fi
echo $extraPackages > $out/nix-support/propagated-native-build-inputs

View File

@ -1,136 +0,0 @@
#! @shell@ -e
if test -n "$NIX_GCC_WRAPPER_START_HOOK"; then
source "$NIX_GCC_WRAPPER_START_HOOK"
fi
if test -z "$NIX_GCC_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. Clang prints annoying
# warnings when they are not needed. (does it really? Copied from gcc-wrapper)
dontLink=0
getVersion=0
nonFlagArgs=0
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
elif test "$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 test "${i:0:1}" != "-"; then
nonFlagArgs=1
elif test "$i" = "-m32"; then
if test -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 clang 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 "clang" (should just print
# "clang: no input files") and "clang -v" (should print the version).
if test "$nonFlagArgs" = "0"; then
dontLink=1
fi
# Optionally filter out paths not refering to the store.
params=("$@")
if test "$NIX_ENFORCE_PURITY" = "1" -a -n "$NIX_STORE"; then
rest=()
n=0
while test $n -lt ${#params[*]}; do
p=${params[n]}
p2=${params[$((n+1))]}
if test "${p:0:3}" = "-L/" && badPath "${p:2}"; then
skip $p
elif test "$p" = "-L" && badPath "$p2"; then
n=$((n + 1)); skip $p2
elif test "${p:0:3}" = "-I/" && badPath "${p:2}"; then
skip $p
elif test "$p" = "-I" && badPath "$p2"; then
n=$((n + 1)); skip $p2
elif test "$p" = "-isystem" && badPath "$p2"; then
n=$((n + 1)); skip $p2
else
rest=("${rest[@]}" "$p")
fi
n=$((n + 1))
done
params=("${rest[@]}")
NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE --sysroot=/var/empty"
fi
# Add the flags for the C compiler proper.
extraAfter=($NIX_CFLAGS_COMPILE)
extraBefore=()
if test "$dontLink" != "1"; then
# Add the flags that should only be passed to the compiler when
# linking.
extraAfter=(${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 test "${i:0:3}" = "-L/"; then
extraAfter=(${extraAfter[@]} "$i")
else
extraAfter=(${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 `clang -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 test "$*" = "-v"; then
extraAfter=()
extraBefore=()
fi
# Optionally print debug info.
if test "$NIX_DEBUG" = "1"; then
echo "original flags to @clangProg@:" >&2
for i in "${params[@]}"; do
echo " $i" >&2
done
echo "extraBefore flags to @clangProg@:" >&2
for i in ${extraBefore[@]}; do
echo " $i" >&2
done
echo "extraAfter flags to @clangProg@:" >&2
for i in ${extraAfter[@]}; do
echo " $i" >&2
done
fi
if test -n "$NIX_CLANG_WRAPPER_EXEC_HOOK"; then
source "$NIX_CLANG_WRAPPER_EXEC_HOOK"
fi
exec @clangProg@ ${extraBefore[@]} "${params[@]}" ${extraAfter[@]}

View File

@ -1,89 +0,0 @@
# The Nix `clang' stdenv.mkDerivation 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
# stdenv.mkDerivation provides a wrapper that sets up the right environment
# variables so that the compiler and the linker just "work".
{ name ? "", stdenv, nativeTools, nativeLibc, nativePrefix ? ""
, clang ? null, libc ? null, binutils ? null, coreutils ? null, shell ? ""
, zlib ? null, extraPackages ? []
}:
assert nativeTools -> nativePrefix != "";
assert !nativeTools -> clang != null && binutils != null && coreutils != null;
assert !nativeLibc -> libc != null;
let
clangVersion = (builtins.parseDrvName clang.name).version;
clangName = (builtins.parseDrvName clang.name).name;
in
stdenv.mkDerivation {
name =
(if name != "" then name else clangName + "-wrapper") +
(if clang != null && clangVersion != "" then "-" + clangVersion else "");
isDarwin = stdenv.isDarwin;
builder = ./builder.sh;
setupHook = ./setup-hook.sh;
clangWrapper = ./clang-wrapper.sh;
ldWrapper = ../gcc-wrapper/ld-wrapper.sh;
utils = ../gcc-wrapper/utils.sh;
addFlags = ./add-flags;
inherit nativeTools nativeLibc nativePrefix clang clangVersion extraPackages;
gcc = clang.gcc;
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;
langC = true;
langCC = true;
shell = if shell == "" then stdenv.shell else
if builtins.isAttrs shell then (shell + shell.shellPath)
else shell;
crossAttrs = {
shell = shell.crossDrv + shell.crossDrv.shellPath;
libc = libc.crossDrv;
coreutils = coreutils.crossDrv;
binutils = binutils.crossDrv;
clang = clang.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 clang_ = if clang != null then clang else {}; in
(if clang_ ? meta then removeAttrs clang.meta ["priority"] else {}) //
{ description =
stdenv.lib.attrByPath ["meta" "description"] "System C compiler" clang_
+ " (wrapper script)";
};
# 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
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 "";
preferLocalBuild = true;
}

View File

@ -1,38 +0,0 @@
export NIX_GCC=@out@
addCVars () {
if test -d $1/include; then
export NIX_CFLAGS_COMPILE="$NIX_CFLAGS_COMPILE -isystem $1/include"
fi
if test -d $1/lib64; then
export NIX_LDFLAGS="$NIX_LDFLAGS -L$1/lib64"
fi
if test -d $1/lib; then
export NIX_LDFLAGS="$NIX_LDFLAGS -L$1/lib"
fi
}
envHooks+=(addCVars)
# Note: these come *after* $out in the PATH (see setup.sh).
if test -n "@clang@"; then
addToSearchPath PATH @clang@/bin
fi
if test -n "@binutils@"; then
addToSearchPath PATH @binutils@/bin
fi
if test -n "@libc@"; then
addToSearchPath PATH @libc@/bin
fi
if test -n "@coreutils@"; then
addToSearchPath PATH @coreutils@/bin
fi
: ${CXX:=clang++}
export CXX

View File

@ -7,7 +7,7 @@
{ name ? "", stdenv, nativeTools, nativeLibc, nativePrefix ? ""
, gcc ? null, libc ? null, binutils ? null, coreutils ? null, shell ? stdenv.shell
, zlib ? null
, zlib ? null, extraPackages ? []
}:
with stdenv.lib;
@ -124,6 +124,8 @@ stdenv.mkDerivation {
# you get tools like gcov, the manpages, etc. as well (including
# for binutils and Glibc).
echo $gcc $binutils $libc > $out/nix-support/propagated-user-env-packages
echo ${toString extraPackages} > $out/nix-support/propagated-native-build-inputs
''
+ optionalString (stdenv.isSunOS && nativePrefix != "") ''
@ -161,6 +163,16 @@ stdenv.mkDerivation {
ln -s g++ $out/bin/c++
fi
if [ -e $gccPath/clang ]; then
wrap clang ${./gcc-wrapper.sh} $gccPath/clang
ln -s clang $out/bin/cc
fi
if [ -e $gccPath/clang++ ]; then
wrap clang++ ${./gcc-wrapper.sh} $gccPath/clang++
ln -s clang++ $out/bin/c++
fi
if [ -e $gccPath/cpp ]; then
wrap cpp ${./gcc-wrapper.sh} $gccPath/cpp
fi
@ -195,6 +207,14 @@ stdenv.mkDerivation {
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
if [ -e $out/bin/clang ]; then
echo 'export CC; : ''${CC:=clang}' >> $out/nix-support/setup-hook
fi
if [ -e $out/bin/clang++ ]; then
echo 'export CXX; : ''${CXX:=clang++}' >> $out/nix-support/setup-hook
fi
'';
# The dynamic linker has different names on different Linux platforms.

View File

@ -24,13 +24,13 @@ import ../generic rec {
system = stdenv.system;
gcc = import ../../build-support/clang-wrapper {
gcc = import ../../build-support/gcc-wrapper {
nativeTools = false;
nativeLibc = true;
inherit stdenv;
extraPackages = stdenv.lib.optional haveLibCxx pkgs.libcxx;
binutils = import ../../build-support/native-darwin-cctools-wrapper {inherit stdenv;};
clang = if useClang33 then pkgs.clang_33.clang else pkgs.clang.clang;
gcc = if useClang33 then pkgs.clang_33.gcc else pkgs.clang.gcc;
coreutils = pkgs.coreutils;
shell = pkgs.bash + "/bin/sh";
};

View File

@ -19,6 +19,6 @@ stdenv.mkDerivation rec {
description = "OpenSource IPsec-based VPN Solution";
homepage = https://www.strongswan.org;
license = stdenv.lib.licenses.gpl2Plus;
inherit (stdenv.gcc.clang.meta) platforms;
inherit (stdenv.gcc.gcc.meta) platforms;
};
}

View File

@ -2763,10 +2763,10 @@ let
ccl = builderDefsPackage ../development/compilers/ccl {};
clang = wrapClang llvmPackages.clang;
clang = wrapGCC llvmPackages.clang;
clang_34 = wrapClang llvmPackages_34.clang;
clang_33 = wrapClang (clangUnwrapped llvm_33 ../development/compilers/llvm/3.3/clang.nix);
clang_34 = wrapGCC llvmPackages_34.clang;
clang_33 = wrapGCC (clangUnwrapped llvm_33 ../development/compilers/llvm/3.3/clang.nix);
clangAnalyzer = callPackage ../development/tools/analysis/clang-analyzer {
clang = clang_34;
@ -2779,8 +2779,8 @@ let
clangSelf = clangWrapSelf llvmPackagesSelf.clang;
clangWrapSelf = build: (import ../build-support/clang-wrapper) {
clang = build;
clangWrapSelf = build: (import ../build-support/gcc-wrapper) {
gcc = build;
stdenv = clangStdenv;
libc = glibc;
binutils = binutils;
@ -3668,18 +3668,6 @@ let
inherit stdenv binutils coreutils zlib;
};
wrapClangWith = clangWrapper: glibc: baseClang: clangWrapper {
nativeTools = stdenv.gcc.nativeTools or false;
nativeLibc = stdenv.gcc.nativeLibc or false;
nativePrefix = stdenv.gcc.nativePrefix or "";
clang = baseClang;
libc = glibc;
binutils = stdenv.gcc.binutils;
inherit stdenv coreutils zlib;
};
wrapClang = wrapClangWith (makeOverridable (import ../build-support/clang-wrapper)) glibc;
wrapGCC = wrapGCCWith (makeOverridable (import ../build-support/gcc-wrapper)) glibc;
wrapGCCCross =