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 # Test that linking libc++ works, and statically.
50 ${lib.optionalString isCxx ''
51 echo "checking whether can link with libc++... " >&2
52 NIX_DEBUG=1 $CXX ${./cxx-main.cc} -c -o cxx-main.o
53 NIX_DEBUG=1 $CC cxx-main.o -lc++ -o cxx-main
54 NIX_DEBUG=1 $CC cxx-main.o ${lib.getLib stdenv.cc.libcxx}/lib/libc++.a -o cxx-main-static
55 ${emulator} ./cxx-main
56 ${emulator} ./cxx-main-static
57 rm cxx-main{,-static,.o}
60 ${lib.optionalString (stdenv.isDarwin && stdenv.cc.isClang) ''
61 echo "checking whether compiler can build with CoreFoundation.framework... " >&2
63 $CC -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c}
64 ${emulator} ./core-foundation-check
68 ${lib.optionalString (!stdenv.isDarwin) ''
69 echo "checking whether compiler builds valid static C binaries... " >&2
70 $CC ${staticLibc} -static -o cc-static ${./cc-main.c}
71 ${emulator} ./cc-static
72 ${lib.optionalString (stdenv.cc.isGNU && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "8.0.0") ''
73 echo "checking whether compiler builds valid static pie C binaries... " >&2
74 $CC ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c}
75 ${emulator} ./cc-static-pie
79 ${# See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
80 # `gcc` does not support this so we gate the test on `clang`
81 lib.optionalString stdenv.cc.isClang ''
82 echo "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2
85 # Make sure `--` is not parsed as a "non flag arg"; we should get an
86 # input file error here and *not* a linker error.
87 { ! $CC --; } |& grep -q "no input files"
89 # And that positional file args _must_ be files (this is just testing
90 # that we remembered to put the `--` back in the args to the compiler):
91 { ! $CC -c -- -o foo ${./foo.c}; } \
92 |& grep -q "no such file or directory: '-o'"
94 # Now check that we accept single and multiple positional file args:
95 $CC -c -DVALUE=42 -o positional/foo.o -- ${./foo.c}
96 $CC -o positional/main -- positional/foo.o ${./ldflags-main.c}
97 ${emulator} ./positional/main
100 echo "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
102 cp ${./foo.c} foo/include/foo.h
103 NIX_CFLAGS_COMPILE="-Ifoo/include -DVALUE=42" $CC -o cflags-check ${./cflags-main.c}
104 ${emulator} ./cflags-check
106 echo "checking whether compiler uses NIX_LDFLAGS... " >&2
109 ${lib.optionalString stdenv.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \
111 -o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \
114 NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c}
115 ${emulator} ./ldflags-check
117 echo "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2
119 cp ${./stdio.h} std-include/stdio.h
120 NIX_DEBUG=1 $CC -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c}
121 ${emulator} ./nostdinc-main
122 $CXX -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c}
123 ${emulator} ./nostdinc-main++
125 ${lib.optionalString sanitizersWorking ''
126 echo "checking whether sanitizers are fully functional... ">&2
127 $CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}
128 ASAN_OPTIONS=use_sigaltstack=0 ${emulator} ./sanitizers
134 meta.platforms = lib.platforms.all;