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.hostPlatform.isDarwin && !stdenv.hostPlatform.isMusl && (
7 (stdenv.cc.isClang && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "5.0.0")
8 || (stdenv.cc.isGNU && stdenv.hostPlatform.isLinux)
10 staticLibc = lib.optionalString (stdenv.hostPlatform.libc == "glibc") "-L ${glibc.static}/lib";
11 emulator = lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) (stdenv.hostPlatform.emulator buildPackages);
12 isCxx = stdenv.cc.libcxx != null;
13 libcxxStdenvSuffix = lib.optionalString isCxx "-libcxx";
14 CC = "PATH= ${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}cc"}";
15 CXX = "PATH= ${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}c++"}";
16 READELF = "PATH= ${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}readelf"}";
17 in stdenv.mkDerivation {
18 pname = "cc-wrapper-test-${stdenv.cc.cc.pname}${libcxxStdenvSuffix}";
19 version = stdenv.cc.version;
22 echo "Testing: ${stdenv.cc.name}" >&2
23 echo "With libc: ${stdenv.cc.libc.name}" >&2
29 echo "checking whether compiler builds valid C binaries... " >&2
30 ${CC} -o cc-check ${./cc-main.c}
31 ${emulator} ./cc-check
33 echo "checking whether compiler builds valid C++ binaries... " >&2
34 ${CXX} -o cxx-check ${./cxx-main.cc}
35 ${emulator} ./cxx-check
37 # test for https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1431745905
38 # .../include/cxxabi.h:20:10: fatal error: '__cxxabi_config.h' file not found
40 echo "checking whether cxxabi.h can be included... " >&2
41 ${CXX} -o include-cxxabi ${./include-cxxabi.cc}
42 ${emulator} ./include-cxxabi
44 # cxx doesn't have libatomic.so
45 ${lib.optionalString (!isCxx) ''
46 # https://github.com/NixOS/nixpkgs/issues/91285
47 echo "checking whether libatomic.so can be linked... " >&2
48 ${CXX} -shared -o atomics.so ${./atomics.cc} -latomic ${lib.optionalString (stdenv.cc.isClang && lib.versionOlder stdenv.cc.version "6.0.0" ) "-std=c++17"}
49 ${READELF} -d ./atomics.so | grep libatomic.so && echo "ok" >&2 || echo "failed" >&2
52 # Test that linking libc++ works, and statically.
53 ${lib.optionalString isCxx ''
54 echo "checking whether can link with libc++... " >&2
55 NIX_DEBUG=1 ${CXX} ${./cxx-main.cc} -c -o cxx-main.o
56 NIX_DEBUG=1 ${CC} cxx-main.o -lc++ -o cxx-main
57 NIX_DEBUG=1 ${CC} cxx-main.o ${lib.getLib stdenv.cc.libcxx}/lib/libc++.a -o cxx-main-static
58 ${emulator} ./cxx-main
59 ${emulator} ./cxx-main-static
60 rm cxx-main{,-static,.o}
63 ${lib.optionalString (stdenv.hostPlatform.isDarwin && stdenv.cc.isClang) ''
64 echo "checking whether compiler can build with CoreFoundation.framework... " >&2
66 ${CC} -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c}
67 ${emulator} ./core-foundation-check
71 ${lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
72 echo "checking whether compiler builds valid static C binaries... " >&2
73 ${CC} ${staticLibc} -static -o cc-static ${./cc-main.c}
74 ${emulator} ./cc-static
75 ${lib.optionalString (stdenv.cc.isGNU && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "8.0.0") ''
76 echo "checking whether compiler builds valid static pie C binaries... " >&2
77 ${CC} ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c}
78 ${emulator} ./cc-static-pie
82 ${# See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
83 # `gcc` does not support this so we gate the test on `clang`
84 lib.optionalString stdenv.cc.isClang ''
85 echo "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2
88 # Make sure `--` is not parsed as a "non flag arg"; we should get an
89 # input file error here and *not* a linker error.
90 { ! ${CC} --; } |& grep -q "no input files"
92 # And that positional file args _must_ be files (this is just testing
93 # that we remembered to put the `--` back in the args to the compiler):
94 { ! ${CC} -c -- -o foo ${./foo.c}; } \
95 |& grep -q "no such file or directory: '-o'"
97 # Now check that we accept single and multiple positional file args:
98 ${CC} -c -DVALUE=42 -o positional/foo.o -- ${./foo.c}
99 ${CC} -o positional/main -- positional/foo.o ${./ldflags-main.c}
100 ${emulator} ./positional/main
103 echo "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
105 cp ${./foo.c} foo/include/foo.h
106 NIX_CFLAGS_COMPILE="-Ifoo/include -DVALUE=42" ${CC} -o cflags-check ${./cflags-main.c}
107 ${emulator} ./cflags-check
109 echo "checking whether compiler uses NIX_LDFLAGS... " >&2
112 ${lib.optionalString stdenv.hostPlatform.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \
114 -o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \
117 NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" ${CC} -lfoo -o ldflags-check ${./ldflags-main.c}
118 ${emulator} ./ldflags-check
120 echo "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2
122 cp ${./stdio.h} std-include/stdio.h
123 NIX_DEBUG=1 ${CC} -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c}
124 ${emulator} ./nostdinc-main
125 ${CXX} -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c}
126 ${emulator} ./nostdinc-main++
128 ${lib.optionalString sanitizersWorking ''
129 echo "checking whether sanitizers are fully functional... ">&2
130 ${CC} -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}
131 ASAN_OPTIONS=use_sigaltstack=0 ${emulator} ./sanitizers
134 echo "Check whether CC and LD with NIX_X_USE_RESPONSE_FILE hardcodes all required binaries..." >&2
135 NIX_CC_USE_RESPONSE_FILE=1 NIX_LD_USE_RESPONSE_FILE=1 ${CC} -v
140 meta.platforms = lib.platforms.all;