2022-12-28 21:21:41 +00:00
|
|
|
{ lib, stdenv, glibc, buildPackages }:
|
2021-02-05 17:12:51 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
let
|
|
|
|
# Sanitizers are not supported on Darwin.
|
|
|
|
# Sanitizer headers aren't available in older libc++ stdenvs due to a bug
|
2022-12-28 21:21:41 +00:00
|
|
|
sanitizersWorking = (stdenv.buildPlatform == stdenv.hostPlatform) && !stdenv.isDarwin && !stdenv.hostPlatform.isMusl && (
|
2021-02-05 17:12:51 +00:00
|
|
|
(stdenv.cc.isClang && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "5.0.0")
|
2021-01-15 22:18:51 +00:00
|
|
|
|| (stdenv.cc.isGNU && stdenv.isLinux)
|
|
|
|
);
|
2021-02-05 17:12:51 +00:00
|
|
|
staticLibc = lib.optionalString (stdenv.hostPlatform.libc == "glibc") "-L ${glibc.static}/lib";
|
2022-12-28 21:21:41 +00:00
|
|
|
emulator = stdenv.hostPlatform.emulator buildPackages;
|
2020-04-24 23:36:52 +00:00
|
|
|
in stdenv.mkDerivation {
|
|
|
|
name = "cc-wrapper-test";
|
|
|
|
|
|
|
|
buildCommand = ''
|
|
|
|
NIX_DEBUG=1 $CC -v
|
|
|
|
NIX_DEBUG=1 $CXX -v
|
|
|
|
|
|
|
|
printf "checking whether compiler builds valid C binaries... " >&2
|
|
|
|
$CC -o cc-check ${./cc-main.c}
|
2022-12-28 21:21:41 +00:00
|
|
|
${emulator} ./cc-check
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
printf "checking whether compiler builds valid C++ binaries... " >&2
|
|
|
|
$CXX -o cxx-check ${./cxx-main.cc}
|
2022-12-28 21:21:41 +00:00
|
|
|
${emulator} ./cxx-check
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (stdenv.isDarwin && stdenv.cc.isClang) ''
|
2020-04-24 23:36:52 +00:00
|
|
|
printf "checking whether compiler can build with CoreFoundation.framework... " >&2
|
|
|
|
mkdir -p foo/lib
|
|
|
|
$CC -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c}
|
2022-12-28 21:21:41 +00:00
|
|
|
${emulator} ./core-foundation-check
|
2020-04-24 23:36:52 +00:00
|
|
|
''}
|
|
|
|
|
2021-01-15 22:18:51 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString (!stdenv.isDarwin) ''
|
2021-01-15 22:18:51 +00:00
|
|
|
printf "checking whether compiler builds valid static C binaries... " >&2
|
|
|
|
$CC ${staticLibc} -static -o cc-static ${./cc-main.c}
|
2022-12-28 21:21:41 +00:00
|
|
|
${emulator} ./cc-static
|
2021-06-28 23:13:55 +00:00
|
|
|
${lib.optionalString (stdenv.cc.isGNU && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "8.0.0") ''
|
2021-01-15 22:18:51 +00:00
|
|
|
printf "checking whether compiler builds valid static pie C binaries... " >&2
|
|
|
|
$CC ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c}
|
2022-12-28 21:21:41 +00:00
|
|
|
${emulator} ./cc-static-pie
|
2021-01-15 22:18:51 +00:00
|
|
|
''}
|
|
|
|
''}
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
printf "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
|
|
|
|
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-12-28 21:21:41 +00:00
|
|
|
${emulator} ./cflags-check
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
printf "checking whether compiler uses NIX_LDFLAGS... " >&2
|
|
|
|
mkdir -p foo/lib
|
|
|
|
$CC -shared \
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString stdenv.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \
|
2020-04-24 23:36:52 +00:00
|
|
|
-DVALUE=42 \
|
|
|
|
-o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \
|
|
|
|
${./foo.c}
|
|
|
|
|
|
|
|
NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c}
|
2022-12-28 21:21:41 +00:00
|
|
|
${emulator} ./ldflags-check
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-08-20 17:08:02 +00:00
|
|
|
printf "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2
|
|
|
|
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-12-28 21:21:41 +00:00
|
|
|
${emulator} ./nostdinc-main
|
2020-08-20 17:08:02 +00:00
|
|
|
$CXX -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c}
|
2022-12-28 21:21:41 +00:00
|
|
|
${emulator} ./nostdinc-main++
|
2020-08-20 17:08:02 +00:00
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
${lib.optionalString sanitizersWorking ''
|
2020-04-24 23:36:52 +00:00
|
|
|
printf "checking whether sanitizers are fully functional... ">&2
|
|
|
|
$CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}
|
2022-12-28 21:21:41 +00:00
|
|
|
ASAN_OPTIONS=use_sigaltstack=0 ${emulator} ./sanitizers
|
2020-04-24 23:36:52 +00:00
|
|
|
''}
|
|
|
|
|
|
|
|
touch $out
|
|
|
|
'';
|
|
|
|
|
2021-02-05 17:12:51 +00:00
|
|
|
meta.platforms = lib.platforms.all;
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|