1{ lib, stdenv, glibc, buildPackages }:
2
3let
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)
9 );
10 staticLibc = lib.optionalString (stdenv.hostPlatform.libc == "glibc") "-L ${glibc.static}/lib";
11 emulator = stdenv.hostPlatform.emulator buildPackages;
12in stdenv.mkDerivation {
13 name = "cc-wrapper-test";
14
15 buildCommand = ''
16 set -o pipefail
17
18 NIX_DEBUG=1 $CC -v
19 NIX_DEBUG=1 $CXX -v
20
21 printf "checking whether compiler builds valid C binaries... " >&2
22 $CC -o cc-check ${./cc-main.c}
23 ${emulator} ./cc-check
24
25 printf "checking whether compiler builds valid C++ binaries... " >&2
26 $CXX -o cxx-check ${./cxx-main.cc}
27 ${emulator} ./cxx-check
28
29 ${lib.optionalString (stdenv.isDarwin && stdenv.cc.isClang) ''
30 printf "checking whether compiler can build with CoreFoundation.framework... " >&2
31 mkdir -p foo/lib
32 $CC -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c}
33 ${emulator} ./core-foundation-check
34 ''}
35
36
37 ${lib.optionalString (!stdenv.isDarwin) ''
38 printf "checking whether compiler builds valid static C binaries... " >&2
39 $CC ${staticLibc} -static -o cc-static ${./cc-main.c}
40 ${emulator} ./cc-static
41 ${lib.optionalString (stdenv.cc.isGNU && lib.versionAtLeast (lib.getVersion stdenv.cc.name) "8.0.0") ''
42 printf "checking whether compiler builds valid static pie C binaries... " >&2
43 $CC ${staticLibc} -static-pie -o cc-static-pie ${./cc-main.c}
44 ${emulator} ./cc-static-pie
45 ''}
46 ''}
47
48 ${# See: https://github.com/llvm/llvm-project/commit/ed1d07282cc9d8e4c25d585e03e5c8a1b6f63a74
49 # `gcc` does not support this so we gate the test on `clang`
50 lib.optionalString stdenv.cc.isClang ''
51 printf "checking whether cc-wrapper accepts -- followed by positional (file) args..." >&2
52 mkdir -p positional
53
54 # Make sure `--` is not parsed as a "non flag arg"; we should get an
55 # input file error here and *not* a linker error.
56 { ! $CC --; } |& grep -q "no input files"
57
58 # And that positional file args _must_ be files (this is just testing
59 # that we remembered to put the `--` back in the args to the compiler):
60 { ! $CC -c -- -o foo ${./foo.c}; } \
61 |& grep -q "no such file or directory: '-o'"
62
63 # Now check that we accept single and multiple positional file args:
64 $CC -c -DVALUE=42 -o positional/foo.o -- ${./foo.c}
65 $CC -o positional/main -- positional/foo.o ${./ldflags-main.c}
66 ${emulator} ./positional/main
67 ''}
68
69 printf "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2
70 mkdir -p foo/include
71 cp ${./foo.c} foo/include/foo.h
72 NIX_CFLAGS_COMPILE="-Ifoo/include -DVALUE=42" $CC -o cflags-check ${./cflags-main.c}
73 ${emulator} ./cflags-check
74
75 printf "checking whether compiler uses NIX_LDFLAGS... " >&2
76 mkdir -p foo/lib
77 $CC -shared \
78 ${lib.optionalString stdenv.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \
79 -DVALUE=42 \
80 -o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \
81 ${./foo.c}
82
83 NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c}
84 ${emulator} ./ldflags-check
85
86 printf "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2
87 mkdir -p std-include
88 cp ${./stdio.h} std-include/stdio.h
89 NIX_DEBUG=1 $CC -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c}
90 ${emulator} ./nostdinc-main
91 $CXX -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c}
92 ${emulator} ./nostdinc-main++
93
94 ${lib.optionalString sanitizersWorking ''
95 printf "checking whether sanitizers are fully functional... ">&2
96 $CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}
97 ASAN_OPTIONS=use_sigaltstack=0 ${emulator} ./sanitizers
98 ''}
99
100 touch $out
101 '';
102
103 meta.platforms = lib.platforms.all;
104}