1 { lib, stdenv, glibc, buildPackages }:
4 # Sanitizers are not supported on Darwin.
5 # Sanitizer headers aren't available in older libc++ stdenvs due to a bug
6 sanitizersWorking = (stdenv.buildPlatform == stdenv.hostPlatform) && !stdenv.isDarwin && !stdenv.hostPlatform.isMusl && (
7 (stdenv.cc.isClang && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "5.0.0")
8 || (stdenv.cc.isGNU && stdenv.isLinux)
10 staticLibc = lib.optionalString (stdenv.hostPlatform.libc == "glibc") "-L ${glibc.static}/lib";
11 emulator = stdenv.hostPlatform.emulator buildPackages;
12 isCxx = stdenv.cc.libcxx != null;
13 libcxxStdenvSuffix = lib.optionalString isCxx "-libcxx";
14 in stdenv.mkDerivation {
15 pname = "cc-wrapper-test-${stdenv.cc.cc.pname}${libcxxStdenvSuffix}";
16 version = stdenv.cc.version;
19 echo "Testing: ${stdenv.cc.name}" >&2
20 echo "With libc: ${stdenv.cc.libc.name}" >&2
26 echo "checking whether compiler builds valid C binaries... " >&2
27 $CC -o cc-check ${./cc-main.c}
28 ${emulator} ./cc-check
30 echo "checking whether compiler builds valid C++ binaries... " >&2
31 $CXX -o cxx-check ${./cxx-main.cc}
32 ${emulator} ./cxx-check
34 # test for https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1431745905
35 # .../include/cxxabi.h:20:10: fatal error: '__cxxabi_config.h' file not found
37 echo "checking whether cxxabi.h can be included... " >&2
38 $CXX -o include-cxxabi ${./include-cxxabi.cc}
39 ${emulator} ./include-cxxabi
41 # cxx doesn't have libatomic.so
42 ${lib.optionalString (!isCxx) ''
43 # https://github.com/NixOS/nixpkgs/issues/91285
44 echo "checking whether libatomic.so can be linked... " >&2
45 $CXX -shared -o atomics.so ${./atomics.cc} -latomic ${lib.optionalString (stdenv.cc.isClang && lib.versionOlder stdenv.cc.version "6.0.0" ) "-std=c++17"}
46 $READELF -d ./atomics.so | grep libatomic.so && echo "ok" >&2 || echo "failed" >&2
49 ${lib.optionalString (stdenv.isDarwin && stdenv.cc.isClang) ''
50 echo "checking whether compiler can build with CoreFoundation.framework... " >&2
52 $CC -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c}
53 ${emulator} ./core-foundation-check
57 ${lib.optionalString (!stdenv.isDarwin) ''
58 echo "checking whether compiler builds valid static C binaries... " >&2
59 $CC ${staticLibc} -static -o cc-static ${./cc-main.c}
60 ${emulator} ./cc-static
61 ${lib.optionalString (stdenv.cc.isGNU && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "8.0.0") ''
62 echo "checking whether compiler builds valid static pie C binaries... " >&2
63 $CC ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c}
64 ${emulator} ./cc-static-pie
68 ${# See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
69 # `gcc` does not support this so we gate the test on `clang`
70 lib.optionalString stdenv.cc.isClang ''
71 echo "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2
74 # Make sure `--` is not parsed as a "non flag arg"; we should get an
75 # input file error here and *not* a linker error.
76 { ! $CC --; } |& grep -q "no input files"
78 # And that positional file args _must_ be files (this is just testing
79 # that we remembered to put the `--` back in the args to the compiler):
80 { ! $CC -c -- -o foo ${./foo.c}; } \
81 |& grep -q "no such file or directory: '-o'"
83 # Now check that we accept single and multiple positional file args:
84 $CC -c -DVALUE=42 -o positional/foo.o -- ${./foo.c}
85 $CC -o positional/main -- positional/foo.o ${./ldflags-main.c}
86 ${emulator} ./positional/main
89 echo "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
91 cp ${./foo.c} foo/include/foo.h
92 NIX_CFLAGS_COMPILE="-Ifoo/include -DVALUE=42" $CC -o cflags-check ${./cflags-main.c}
93 ${emulator} ./cflags-check
95 echo "checking whether compiler uses NIX_LDFLAGS... " >&2
98 ${lib.optionalString stdenv.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \
100 -o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \
103 NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c}
104 ${emulator} ./ldflags-check
106 echo "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2
108 cp ${./stdio.h} std-include/stdio.h
109 NIX_DEBUG=1 $CC -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c}
110 ${emulator} ./nostdinc-main
111 $CXX -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c}
112 ${emulator} ./nostdinc-main++
114 ${lib.optionalString sanitizersWorking ''
115 echo "checking whether sanitizers are fully functional... ">&2
116 $CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}
117 ASAN_OPTIONS=use_sigaltstack=0 ${emulator} ./sanitizers
123 meta.platforms = lib.platforms.all;