2022-02-09 10:45:57 +00:00
|
|
|
{ lib, stdenv, glibc, buildPackages }:
|
2021-01-24 00:40:18 +00:00
|
|
|
|
2018-05-31 01:48:42 +00:00
|
|
|
let
|
2018-06-06 07:59:59 +00:00
|
|
|
# Sanitizers are not supported on Darwin.
|
2018-05-31 01:48:42 +00:00
|
|
|
# Sanitizer headers aren't available in older libc++ stdenvs due to a bug
|
2022-12-02 06:22:33 +00:00
|
|
|
sanitizersWorking = (stdenv.buildPlatform == stdenv.hostPlatform) && !stdenv.isDarwin && !stdenv.hostPlatform.isMusl && (
|
2021-01-24 00:40:18 +00:00
|
|
|
(stdenv.cc.isClang && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "5.0.0")
|
2020-12-25 20:52:42 +00:00
|
|
|
|| (stdenv.cc.isGNU && stdenv.isLinux)
|
|
|
|
);
|
2021-01-24 00:40:18 +00:00
|
|
|
staticLibc = lib.optionalString (stdenv.hostPlatform.libc == "glibc") "-L ${glibc.static}/lib";
|
2022-02-09 10:45:57 +00:00
|
|
|
emulator = stdenv.hostPlatform.emulator buildPackages;
|
2023-09-07 01:31:26 +00:00
|
|
|
libcxxStdenvSuffix = lib.optionalString (stdenv.cc.libcxx != null) "-libcxx";
|
2018-05-31 01:48:42 +00:00
|
|
|
in stdenv.mkDerivation {
|
2023-09-07 01:31:26 +00:00
|
|
|
pname = "cc-wrapper-test-${stdenv.cc.cc.pname}${libcxxStdenvSuffix}";
|
|
|
|
version = stdenv.cc.version;
|
2017-09-02 12:00:47 +00:00
|
|
|
|
|
|
|
buildCommand = ''
|
2023-09-07 01:31:26 +00:00
|
|
|
echo "Testing: ${stdenv.cc.name}" >&2
|
|
|
|
echo "With libc: ${stdenv.cc.libc.name}" >&2
|
2023-05-03 21:45:21 +00:00
|
|
|
set -o pipefail
|
|
|
|
|
2017-09-02 12:00:47 +00:00
|
|
|
NIX_DEBUG=1 $CC -v
|
|
|
|
NIX_DEBUG=1 $CXX -v
|
|
|
|
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether compiler builds valid C binaries... " >&2
|
2017-09-02 12:00:47 +00:00
|
|
|
$CC -o cc-check ${./cc-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./cc-check
|
2017-09-02 12:00:47 +00:00
|
|
|
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether compiler builds valid C++ binaries... " >&2
|
2017-09-02 12:00:47 +00:00
|
|
|
$CXX -o cxx-check ${./cxx-main.cc}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./cxx-check
|
2017-09-02 12:00:47 +00:00
|
|
|
|
2021-01-24 00:40:18 +00:00
|
|
|
${lib.optionalString (stdenv.isDarwin && stdenv.cc.isClang) ''
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether compiler can build with CoreFoundation.framework... " >&2
|
2017-09-02 12:00:47 +00:00
|
|
|
mkdir -p foo/lib
|
|
|
|
$CC -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./core-foundation-check
|
2017-09-02 12:00:47 +00:00
|
|
|
''}
|
|
|
|
|
2020-12-25 20:52:42 +00:00
|
|
|
|
2021-01-24 00:40:18 +00:00
|
|
|
${lib.optionalString (!stdenv.isDarwin) ''
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether compiler builds valid static C binaries... " >&2
|
2020-12-25 20:52:42 +00:00
|
|
|
$CC ${staticLibc} -static -o cc-static ${./cc-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./cc-static
|
2021-05-22 11:55:47 +00:00
|
|
|
${lib.optionalString (stdenv.cc.isGNU && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "8.0.0") ''
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether compiler builds valid static pie C binaries... " >&2
|
2020-12-25 20:52:42 +00:00
|
|
|
$CC ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./cc-static-pie
|
2020-12-25 20:52:42 +00:00
|
|
|
''}
|
|
|
|
''}
|
|
|
|
|
2023-05-03 21:45:21 +00:00
|
|
|
${# See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
|
|
|
|
# `gcc` does not support this so we gate the test on `clang`
|
|
|
|
lib.optionalString stdenv.cc.isClang ''
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2
|
2023-05-03 21:45:21 +00:00
|
|
|
mkdir -p positional
|
|
|
|
|
|
|
|
# Make sure `--` is not parsed as a "non flag arg"; we should get an
|
|
|
|
# input file error here and *not* a linker error.
|
|
|
|
{ ! $CC --; } |& grep -q "no input files"
|
|
|
|
|
|
|
|
# And that positional file args _must_ be files (this is just testing
|
|
|
|
# that we remembered to put the `--` back in the args to the compiler):
|
|
|
|
{ ! $CC -c -- -o foo ${./foo.c}; } \
|
|
|
|
|& grep -q "no such file or directory: '-o'"
|
|
|
|
|
|
|
|
# Now check that we accept single and multiple positional file args:
|
|
|
|
$CC -c -DVALUE=42 -o positional/foo.o -- ${./foo.c}
|
|
|
|
$CC -o positional/main -- positional/foo.o ${./ldflags-main.c}
|
|
|
|
${emulator} ./positional/main
|
|
|
|
''}
|
|
|
|
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
|
2017-09-02 12:00:47 +00:00
|
|
|
mkdir -p foo/include
|
|
|
|
cp ${./foo.c} foo/include/foo.h
|
|
|
|
NIX_CFLAGS_COMPILE="-Ifoo/include -DVALUE=42" $CC -o cflags-check ${./cflags-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./cflags-check
|
2017-09-02 12:00:47 +00:00
|
|
|
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether compiler uses NIX_LDFLAGS... " >&2
|
2017-09-02 12:00:47 +00:00
|
|
|
mkdir -p foo/lib
|
2017-09-12 19:36:41 +00:00
|
|
|
$CC -shared \
|
2021-01-24 00:40:18 +00:00
|
|
|
${lib.optionalString stdenv.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \
|
2017-09-12 19:36:41 +00:00
|
|
|
-DVALUE=42 \
|
|
|
|
-o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \
|
|
|
|
${./foo.c}
|
|
|
|
|
2017-09-02 12:00:47 +00:00
|
|
|
NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./ldflags-check
|
2018-05-07 20:22:50 +00:00
|
|
|
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2
|
2020-07-01 20:55:06 +00:00
|
|
|
mkdir -p std-include
|
|
|
|
cp ${./stdio.h} std-include/stdio.h
|
|
|
|
NIX_DEBUG=1 $CC -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./nostdinc-main
|
2020-07-01 20:55:06 +00:00
|
|
|
$CXX -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c}
|
2022-02-09 10:45:57 +00:00
|
|
|
${emulator} ./nostdinc-main++
|
2020-07-01 20:55:06 +00:00
|
|
|
|
2021-01-24 00:40:18 +00:00
|
|
|
${lib.optionalString sanitizersWorking ''
|
2023-09-07 01:51:52 +00:00
|
|
|
echo "checking whether sanitizers are fully functional... ">&2
|
2018-05-31 01:48:42 +00:00
|
|
|
$CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}
|
2022-12-02 06:57:27 +00:00
|
|
|
ASAN_OPTIONS=use_sigaltstack=0 ${emulator} ./sanitizers
|
2018-05-31 01:48:42 +00:00
|
|
|
''}
|
2017-09-02 12:00:47 +00:00
|
|
|
|
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
|
2021-01-24 00:40:18 +00:00
|
|
|
meta.platforms = lib.platforms.all;
|
2017-09-02 12:00:47 +00:00
|
|
|
}
|