From 96092dc93640b8ad6520a8bae6f78d62eaba0ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Wed, 1 Jul 2020 21:55:06 +0100 Subject: [PATCH] stdenv: make -nostdinc work as intended Right now we add glibc to search path also -nostdinc was provided, which breaks projects providing their own gcc. --- .../networking/browsers/firefox/common.nix | 3 ++- .../mailreaders/thunderbird/default.nix | 3 ++- pkgs/build-support/cc-wrapper/add-flags.sh | 6 +++++- pkgs/build-support/cc-wrapper/cc-wrapper.sh | 21 ++++++++++--------- pkgs/build-support/cc-wrapper/default.nix | 4 ++-- pkgs/development/compilers/gcc/builder.sh | 2 +- .../development/libraries/gcc/libstdc++/5.nix | 2 +- pkgs/test/cc-wrapper/default.nix | 8 +++++++ pkgs/test/cc-wrapper/nostdinc-main.c | 8 +++++++ pkgs/test/cc-wrapper/stdio.h | 1 + 10 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 pkgs/test/cc-wrapper/nostdinc-main.c create mode 100644 pkgs/test/cc-wrapper/stdio.h diff --git a/pkgs/applications/networking/browsers/firefox/common.nix b/pkgs/applications/networking/browsers/firefox/common.nix index 40e402c0bdc..a68a97b6bb1 100644 --- a/pkgs/applications/networking/browsers/firefox/common.nix +++ b/pkgs/applications/networking/browsers/firefox/common.nix @@ -177,7 +177,8 @@ stdenv.mkDerivation ({ # included we need to look in a few places. # TODO: generalize this process for other use-cases. - BINDGEN_CFLAGS="$(< ${stdenv.cc}/nix-support/libc-cflags) \ + BINDGEN_CFLAGS="$(< ${stdenv.cc}/nix-support/libc-crt1-cflags) \ + $(< ${stdenv.cc}/nix-support/libc-cflags) \ $(< ${stdenv.cc}/nix-support/cc-cflags) \ $(< ${stdenv.cc}/nix-support/libcxx-cxxflags) \ ${lib.optionalString stdenv.cc.isClang "-idirafter ${stdenv.cc.cc}/lib/clang/${lib.getVersion stdenv.cc.cc}/include"} \ diff --git a/pkgs/applications/networking/mailreaders/thunderbird/default.nix b/pkgs/applications/networking/mailreaders/thunderbird/default.nix index 6cb27055ddb..3de59ba8f11 100644 --- a/pkgs/applications/networking/mailreaders/thunderbird/default.nix +++ b/pkgs/applications/networking/mailreaders/thunderbird/default.nix @@ -175,7 +175,8 @@ stdenv.mkDerivation rec { # included we need to look in a few places. # TODO: generalize this process for other use-cases. - BINDGEN_CFLAGS="$(< ${stdenv.cc}/nix-support/libc-cflags) \ + BINDGEN_CFLAGS="$(< ${stdenv.cc}/nix-support/libc-crt1-cflags) \ + $(< ${stdenv.cc}/nix-support/libc-cflags) \ $(< ${stdenv.cc}/nix-support/cc-cflags) \ $(< ${stdenv.cc}/nix-support/libcxx-cxxflags) \ ${ diff --git a/pkgs/build-support/cc-wrapper/add-flags.sh b/pkgs/build-support/cc-wrapper/add-flags.sh index 04be3f408ee..94589131b70 100644 --- a/pkgs/build-support/cc-wrapper/add-flags.sh +++ b/pkgs/build-support/cc-wrapper/add-flags.sh @@ -33,10 +33,14 @@ NIX_CFLAGS_COMPILE_@suffixSalt@="-B@out@/bin/ $NIX_CFLAGS_COMPILE_@suffixSalt@" # Export and assign separately in order that a failing $(..) will fail # the script. -if [ -e @out@/nix-support/libc-cflags ]; then +if [[ "$cInclude" = 1 ]] && [ -e @out@/nix-support/libc-cflags ]; then NIX_CFLAGS_COMPILE_@suffixSalt@="$(< @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE_@suffixSalt@" fi +if [ -e @out@/nix-support/libc-crt1-cflags ]; then + NIX_CFLAGS_COMPILE_@suffixSalt@="$(< @out@/nix-support/libc-crt1-cflags) $NIX_CFLAGS_COMPILE_@suffixSalt@" +fi + if [ -e @out@/nix-support/libcxx-cxxflags ]; then NIX_CXXSTDLIB_COMPILE_@suffixSalt@+=" $(< @out@/nix-support/libcxx-cxxflags)" fi diff --git a/pkgs/build-support/cc-wrapper/cc-wrapper.sh b/pkgs/build-support/cc-wrapper/cc-wrapper.sh index 3f9f099f3bc..7e734f57773 100644 --- a/pkgs/build-support/cc-wrapper/cc-wrapper.sh +++ b/pkgs/build-support/cc-wrapper/cc-wrapper.sh @@ -17,16 +17,6 @@ fi source @out@/nix-support/utils.bash -# Flirting with a layer violation here. -if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then - source @bintools@/nix-support/add-flags.sh -fi - -# Put this one second so libc ldflags take priority. -if [ -z "${NIX_CC_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then - source @out@/nix-support/add-flags.sh -fi - # Parse command line options and set several variables. # For instance, figure out if linker flags should be passed. @@ -37,6 +27,7 @@ cc1=0 # shellcheck disable=SC2193 [[ "@prog@" = *++ ]] && isCpp=1 || isCpp=0 cppInclude=1 +cInclude=1 expandResponseParams "$@" declare -i n=0 @@ -63,6 +54,7 @@ while (( "$n" < "$nParams" )); do elif [ "$p" = -nostdlib ]; then isCpp=-1 elif [ "$p" = -nostdinc ]; then + cInclude=0 cppInclude=0 elif [ "$p" = -nostdinc++ ]; then cppInclude=0 @@ -111,6 +103,15 @@ if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then params=(${rest+"${rest[@]}"}) fi +# Flirting with a layer violation here. +if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then + source @bintools@/nix-support/add-flags.sh +fi + +# Put this one second so libc ldflags take priority. +if [ -z "${NIX_CC_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then + source @out@/nix-support/add-flags.sh +fi # Clear march/mtune=native -- they bring impurity. if [ "$NIX_ENFORCE_NO_NATIVE_@suffixSalt@" = 1 ]; then diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index 1fef3c45908..c2a06219f1d 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -287,7 +287,7 @@ stdenv.mkDerivation { + optionalString (libc != null) ('' touch "$out/nix-support/libc-cflags" touch "$out/nix-support/libc-ldflags" - echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-cflags + echo "-B${libc_lib}${libc.libdir or "/lib/"}" >> $out/nix-support/libc-crt1-cflags '' + optionalString (!(cc.langD or false)) '' echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags '' + optionalString (isGNU && (!(cc.langD or false))) '' @@ -429,7 +429,7 @@ stdenv.mkDerivation { + optionalString (libc != null && targetPlatform.isAvr) '' for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do - echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-cflags + echo "-B${getLib libc}/avr/lib/$isa" >> $out/nix-support/libc-crt1-cflags done '' diff --git a/pkgs/development/compilers/gcc/builder.sh b/pkgs/development/compilers/gcc/builder.sh index 4b14cdb94cd..45587020844 100644 --- a/pkgs/development/compilers/gcc/builder.sh +++ b/pkgs/development/compilers/gcc/builder.sh @@ -61,7 +61,7 @@ if test "$noSysDirs" = "1"; then if [[ -e "${!curCC}/nix-support/orig-libc" ]]; then # Figure out what extra compiling flags to pass to the gcc compilers # being generated to make sure that they use our libc. - extraFlags=($(< "${!curCC}/nix-support/libc-cflags")) + extraFlags=($(< "${!curCC}/nix-support/libc-crt1-cflags") $(< "${!curCC}/nix-support/libc-cflags")) # The path to the Libc headers libc_devdir="$(< "${!curCC}/nix-support/orig-libc-dev")" diff --git a/pkgs/development/libraries/gcc/libstdc++/5.nix b/pkgs/development/libraries/gcc/libstdc++/5.nix index 2a11d835a6b..486a24defd1 100644 --- a/pkgs/development/libraries/gcc/libstdc++/5.nix +++ b/pkgs/development/libraries/gcc/libstdc++/5.nix @@ -62,7 +62,7 @@ stdenv.mkDerivation rec { # Figure out what extra flags to pass to the gcc compilers # being generated to make sure that they use our glibc. - EXTRA_FLAGS="-I$NIX_FIXINC_DUMMY $(cat $NIX_CC/nix-support/libc-cflags) -O2" + EXTRA_FLAGS="-I$NIX_FIXINC_DUMMY $(cat $NIX_CC/nix-support/libc-crt1-cflags) $(cat $NIX_CC/nix-support/libc-cflags) -O2" extraLDFlags="-L$glibc_libdir -rpath $glibc_libdir $(cat $NIX_BINTOOLS/nix-support/libc-ldflags) $(cat $NIX_BINTOOLS/nix-support/libc-ldflags-before)" for i in $extraLDFlags; do diff --git a/pkgs/test/cc-wrapper/default.nix b/pkgs/test/cc-wrapper/default.nix index 7bd82b4ab2a..c0c89d63fff 100644 --- a/pkgs/test/cc-wrapper/default.nix +++ b/pkgs/test/cc-wrapper/default.nix @@ -45,6 +45,14 @@ in stdenv.mkDerivation { NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c} ./ldflags-check + printf "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2 + mkdir -p std-include + cp ${./stdio.h} std-include/stdio.h + NIX_DEBUG=1 $CC -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c} + ./nostdinc-main + $CXX -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c} + ./nostdinc-main++ + ${optionalString sanitizersWorking '' printf "checking whether sanitizers are fully functional... ">&2 $CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c} diff --git a/pkgs/test/cc-wrapper/nostdinc-main.c b/pkgs/test/cc-wrapper/nostdinc-main.c new file mode 100644 index 00000000000..f71d155b1b2 --- /dev/null +++ b/pkgs/test/cc-wrapper/nostdinc-main.c @@ -0,0 +1,8 @@ +// This one should not come from libc because of -nostdinc +#include + +int main(int argc, char *argv[]) { + // provided by our own stdio.h + foo(); + return 0; +} diff --git a/pkgs/test/cc-wrapper/stdio.h b/pkgs/test/cc-wrapper/stdio.h new file mode 100644 index 00000000000..4bddf1d9d48 --- /dev/null +++ b/pkgs/test/cc-wrapper/stdio.h @@ -0,0 +1 @@ +static void foo(void) {}