lol
at 23.11-beta 124 lines 5.3 kB view raw
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; 12 isCxx = stdenv.cc.libcxx != null; 13 libcxxStdenvSuffix = lib.optionalString isCxx "-libcxx"; 14in stdenv.mkDerivation { 15 pname = "cc-wrapper-test-${stdenv.cc.cc.pname}${libcxxStdenvSuffix}"; 16 version = stdenv.cc.version; 17 18 buildCommand = '' 19 echo "Testing: ${stdenv.cc.name}" >&2 20 echo "With libc: ${stdenv.cc.libc.name}" >&2 21 set -o pipefail 22 23 NIX_DEBUG=1 $CC -v 24 NIX_DEBUG=1 $CXX -v 25 26 echo "checking whether compiler builds valid C binaries... " >&2 27 $CC -o cc-check ${./cc-main.c} 28 ${emulator} ./cc-check 29 30 echo "checking whether compiler builds valid C++ binaries... " >&2 31 $CXX -o cxx-check ${./cxx-main.cc} 32 ${emulator} ./cxx-check 33 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 36 # in libcxxStdenv 37 echo "checking whether cxxabi.h can be included... " >&2 38 $CXX -o include-cxxabi ${./include-cxxabi.cc} 39 ${emulator} ./include-cxxabi 40 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 47 ''} 48 49 ${lib.optionalString (stdenv.isDarwin && stdenv.cc.isClang) '' 50 echo "checking whether compiler can build with CoreFoundation.framework... " >&2 51 mkdir -p foo/lib 52 $CC -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c} 53 ${emulator} ./core-foundation-check 54 ''} 55 56 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 65 ''} 66 ''} 67 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 72 mkdir -p positional 73 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" 77 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'" 82 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 87 ''} 88 89 echo "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2 90 mkdir -p foo/include 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 94 95 echo "checking whether compiler uses NIX_LDFLAGS... " >&2 96 mkdir -p foo/lib 97 $CC -shared \ 98 ${lib.optionalString stdenv.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \ 99 -DVALUE=42 \ 100 -o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \ 101 ${./foo.c} 102 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 105 106 echo "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2 107 mkdir -p std-include 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++ 113 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 118 ''} 119 120 touch $out 121 ''; 122 123 meta.platforms = lib.platforms.all; 124}