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.
This commit is contained in:
Jörg Thalheim 2020-07-01 21:55:06 +01:00
parent 434a0111f6
commit 96092dc936
No known key found for this signature in database
GPG Key ID: 003F2096411B5F92
10 changed files with 41 additions and 17 deletions

View File

@ -177,7 +177,8 @@ stdenv.mkDerivation ({
# included we need to look in a few places. # included we need to look in a few places.
# TODO: generalize this process for other use-cases. # 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/cc-cflags) \
$(< ${stdenv.cc}/nix-support/libcxx-cxxflags) \ $(< ${stdenv.cc}/nix-support/libcxx-cxxflags) \
${lib.optionalString stdenv.cc.isClang "-idirafter ${stdenv.cc.cc}/lib/clang/${lib.getVersion stdenv.cc.cc}/include"} \ ${lib.optionalString stdenv.cc.isClang "-idirafter ${stdenv.cc.cc}/lib/clang/${lib.getVersion stdenv.cc.cc}/include"} \

View File

@ -175,7 +175,8 @@ stdenv.mkDerivation rec {
# included we need to look in a few places. # included we need to look in a few places.
# TODO: generalize this process for other use-cases. # 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/cc-cflags) \
$(< ${stdenv.cc}/nix-support/libcxx-cxxflags) \ $(< ${stdenv.cc}/nix-support/libcxx-cxxflags) \
${ ${

View File

@ -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 # Export and assign separately in order that a failing $(..) will fail
# the script. # 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@" NIX_CFLAGS_COMPILE_@suffixSalt@="$(< @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE_@suffixSalt@"
fi 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 if [ -e @out@/nix-support/libcxx-cxxflags ]; then
NIX_CXXSTDLIB_COMPILE_@suffixSalt@+=" $(< @out@/nix-support/libcxx-cxxflags)" NIX_CXXSTDLIB_COMPILE_@suffixSalt@+=" $(< @out@/nix-support/libcxx-cxxflags)"
fi fi

View File

@ -17,16 +17,6 @@ fi
source @out@/nix-support/utils.bash 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. # Parse command line options and set several variables.
# For instance, figure out if linker flags should be passed. # For instance, figure out if linker flags should be passed.
@ -37,6 +27,7 @@ cc1=0
# shellcheck disable=SC2193 # shellcheck disable=SC2193
[[ "@prog@" = *++ ]] && isCpp=1 || isCpp=0 [[ "@prog@" = *++ ]] && isCpp=1 || isCpp=0
cppInclude=1 cppInclude=1
cInclude=1
expandResponseParams "$@" expandResponseParams "$@"
declare -i n=0 declare -i n=0
@ -63,6 +54,7 @@ while (( "$n" < "$nParams" )); do
elif [ "$p" = -nostdlib ]; then elif [ "$p" = -nostdlib ]; then
isCpp=-1 isCpp=-1
elif [ "$p" = -nostdinc ]; then elif [ "$p" = -nostdinc ]; then
cInclude=0
cppInclude=0 cppInclude=0
elif [ "$p" = -nostdinc++ ]; then elif [ "$p" = -nostdinc++ ]; then
cppInclude=0 cppInclude=0
@ -111,6 +103,15 @@ if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then
params=(${rest+"${rest[@]}"}) params=(${rest+"${rest[@]}"})
fi 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. # Clear march/mtune=native -- they bring impurity.
if [ "$NIX_ENFORCE_NO_NATIVE_@suffixSalt@" = 1 ]; then if [ "$NIX_ENFORCE_NO_NATIVE_@suffixSalt@" = 1 ]; then

View File

@ -287,7 +287,7 @@ stdenv.mkDerivation {
+ optionalString (libc != null) ('' + optionalString (libc != null) (''
touch "$out/nix-support/libc-cflags" touch "$out/nix-support/libc-cflags"
touch "$out/nix-support/libc-ldflags" 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)) '' '' + optionalString (!(cc.langD or false)) ''
echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags echo "-idirafter ${libc_dev}${libc.incdir or "/include"}" >> $out/nix-support/libc-cflags
'' + optionalString (isGNU && (!(cc.langD or false))) '' '' + optionalString (isGNU && (!(cc.langD or false))) ''
@ -429,7 +429,7 @@ stdenv.mkDerivation {
+ optionalString (libc != null && targetPlatform.isAvr) '' + optionalString (libc != null && targetPlatform.isAvr) ''
for isa in avr5 avr3 avr4 avr6 avr25 avr31 avr35 avr51 avrxmega2 avrxmega4 avrxmega5 avrxmega6 avrxmega7 tiny-stack; do 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 done
'' ''

View File

@ -61,7 +61,7 @@ if test "$noSysDirs" = "1"; then
if [[ -e "${!curCC}/nix-support/orig-libc" ]]; then if [[ -e "${!curCC}/nix-support/orig-libc" ]]; then
# Figure out what extra compiling flags to pass to the gcc compilers # Figure out what extra compiling flags to pass to the gcc compilers
# being generated to make sure that they use our libc. # 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 # The path to the Libc headers
libc_devdir="$(< "${!curCC}/nix-support/orig-libc-dev")" libc_devdir="$(< "${!curCC}/nix-support/orig-libc-dev")"

View File

@ -62,7 +62,7 @@ stdenv.mkDerivation rec {
# Figure out what extra flags to pass to the gcc compilers # Figure out what extra flags to pass to the gcc compilers
# being generated to make sure that they use our glibc. # 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)" 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 for i in $extraLDFlags; do

View File

@ -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} NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c}
./ldflags-check ./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 '' ${optionalString sanitizersWorking ''
printf "checking whether sanitizers are fully functional... ">&2 printf "checking whether sanitizers are fully functional... ">&2
$CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c} $CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c}

View File

@ -0,0 +1,8 @@
// This one should not come from libc because of -nostdinc
#include <stdio.h>
int main(int argc, char *argv[]) {
// provided by our own stdio.h
foo();
return 0;
}

View File

@ -0,0 +1 @@
static void foo(void) {}