1{
2 lib,
3 stdenv,
4 glibc,
5 buildPackages,
6}:
7
8let
9 # Sanitizers are not supported on Darwin.
10 # Sanitizer headers aren't available in older libc++ stdenvs due to a bug
11 sanitizersWorking =
12 (stdenv.buildPlatform == stdenv.hostPlatform)
13 && !stdenv.hostPlatform.isDarwin
14 && !stdenv.hostPlatform.isMusl
15 && (
16 (stdenv.cc.isClang && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "5.0.0")
17 || (stdenv.cc.isGNU && stdenv.hostPlatform.isLinux)
18 );
19 staticLibc = lib.optionalString (stdenv.hostPlatform.libc == "glibc") "-L ${glibc.static}/lib";
20 emulator = stdenv.hostPlatform.emulator buildPackages;
21 isCxx = stdenv.cc.libcxx != null;
22 libcxxStdenvSuffix = lib.optionalString isCxx "-libcxx";
23 CC = "PATH= ${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}cc"}";
24 CXX = "PATH= ${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}c++"}";
25 READELF = "PATH= ${lib.getExe' stdenv.cc "${stdenv.cc.targetPrefix}readelf"}";
26in
27stdenv.mkDerivation {
28 pname = "cc-wrapper-test-${stdenv.cc.cc.pname}${libcxxStdenvSuffix}";
29 version = stdenv.cc.version;
30
31 buildCommand = ''
32 echo "Testing: ${stdenv.cc.name}" >&2
33 echo "With libc: ${stdenv.cc.libc.name}" >&2
34 set -o pipefail
35
36 NIX_DEBUG=1 ${CC} -v
37 NIX_DEBUG=1 ${CXX} -v
38
39 echo "checking whether compiler builds valid C binaries... " >&2
40 ${CC} -o cc-check ${./cc-main.c}
41 ${emulator} ./cc-check
42
43 echo "checking whether compiler builds valid C++ binaries... " >&2
44 ${CXX} -o cxx-check ${./cxx-main.cc}
45 ${emulator} ./cxx-check
46
47 # test for https://github.com/NixOS/nixpkgs/issues/214524#issuecomment-1431745905
48 # .../include/cxxabi.h:20:10: fatal error: '__cxxabi_config.h' file not found
49 # in libcxxStdenv
50 echo "checking whether cxxabi.h can be included... " >&2
51 ${CXX} -o include-cxxabi ${./include-cxxabi.cc}
52 ${emulator} ./include-cxxabi
53
54 # cxx doesn't have libatomic.so
55 ${lib.optionalString (!isCxx) ''
56 # https://github.com/NixOS/nixpkgs/issues/91285
57 echo "checking whether libatomic.so can be linked... " >&2
58 ${CXX} -shared -o atomics.so ${./atomics.cc} -latomic ${
59 lib.optionalString (stdenv.cc.isClang && lib.versionOlder stdenv.cc.version "6.0.0") "-std=c++17"
60 }
61 ${READELF} -d ./atomics.so | grep libatomic.so && echo "ok" >&2 || echo "failed" >&2
62 ''}
63
64 # Test that linking libc++ works, and statically.
65 ${lib.optionalString isCxx ''
66 echo "checking whether can link with libc++... " >&2
67 NIX_DEBUG=1 ${CXX} ${./cxx-main.cc} -c -o cxx-main.o
68 NIX_DEBUG=1 ${CC} cxx-main.o -lc++ -o cxx-main
69 NIX_DEBUG=1 ${CC} cxx-main.o ${lib.getLib stdenv.cc.libcxx}/lib/libc++.a -o cxx-main-static
70 ${emulator} ./cxx-main
71 ${emulator} ./cxx-main-static
72 rm cxx-main{,-static,.o}
73 ''}
74
75 ${lib.optionalString (stdenv.hostPlatform.isDarwin && stdenv.cc.isClang) ''
76 echo "checking whether compiler can build with CoreFoundation.framework... " >&2
77 mkdir -p foo/lib
78 ${CC} -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c}
79 ${emulator} ./core-foundation-check
80 ''}
81
82
83 ${lib.optionalString (!stdenv.hostPlatform.isDarwin) ''
84 echo "checking whether compiler builds valid static C binaries... " >&2
85 ${CC} ${staticLibc} -static -o cc-static ${./cc-main.c}
86 ${emulator} ./cc-static
87 ${lib.optionalString (stdenv.cc.isGNU && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "8.0.0")
88 ''
89 echo "checking whether compiler builds valid static pie C binaries... " >&2
90 ${CC} ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c}
91 ${emulator} ./cc-static-pie
92 ''
93 }
94 ''}
95
96 ${
97 # See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
98 # `gcc` does not support this so we gate the test on `clang`
99 lib.optionalString stdenv.cc.isClang ''
100 echo "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2
101 mkdir -p positional
102
103 # Make sure `--` is not parsed as a "non flag arg"; we should get an
104 # input file error here and *not* a linker error.
105 { ! ${CC} --; } |& grep -q "no input files"
106
107 # And that positional file args _must_ be files (this is just testing
108 # that we remembered to put the `--` back in the args to the compiler):
109 { ! ${CC} -c -- -o foo ${./foo.c}; } \
110 |& grep -q "no such file or directory: '-o'"
111
112 # Now check that we accept single and multiple positional file args:
113 ${CC} -c -DVALUE=42 -o positional/foo.o -- ${./foo.c}
114 ${CC} -o positional/main -- positional/foo.o ${./ldflags-main.c}
115 ${emulator} ./positional/main
116 ''
117 }
118
119 echo "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
120 mkdir -p foo/include
121 cp ${./foo.c} foo/include/foo.h
122 NIX_CFLAGS_COMPILE="-Ifoo/include -DVALUE=42" ${CC} -o cflags-check ${./cflags-main.c}
123 ${emulator} ./cflags-check
124
125 echo "checking whether compiler uses NIX_LDFLAGS... " >&2
126 mkdir -p foo/lib
127 ${CC} -shared \
128 ${lib.optionalString stdenv.hostPlatform.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \
129 -DVALUE=42 \
130 -o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \
131 ${./foo.c}
132
133 NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" ${CC} -lfoo -o ldflags-check ${./ldflags-main.c}
134 ${emulator} ./ldflags-check
135
136 echo "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2
137 mkdir -p std-include
138 cp ${./stdio.h} std-include/stdio.h
139 NIX_DEBUG=1 ${CC} -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c}
140 ${emulator} ./nostdinc-main
141 ${CXX} -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c}
142 ${emulator} ./nostdinc-main++
143
144 ${lib.optionalString sanitizersWorking ''
145 echo "checking whether sanitizers are fully functional... ">&2
146 ${CC} -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}
147 ASAN_OPTIONS=use_sigaltstack=0 ${emulator} ./sanitizers
148 ''}
149
150 echo "Check whether CC and LD with NIX_X_USE_RESPONSE_FILE hardcodes all required binaries..." >&2
151 NIX_CC_USE_RESPONSE_FILE=1 NIX_LD_USE_RESPONSE_FILE=1 ${CC} -v
152
153 touch $out
154 '';
155
156 meta.platforms = lib.platforms.all;
157}