cc-wrapper: change cflags order from cc/crt1/libc to crt1/libc/cc

In https://github.com/NixOS/nixpkgs/issues/158042 I noticed order
mismatch as a bootstrap build failure when building x86_64-linux
against glibc-2.35 in nixpkgs (bootstrap libs has glibc-2.27):

expand-response-params> ld: /nix/store/p4s4jf7aq6v6z9iazll1aiqwb34aqxq9-bootstrap-tools/lib/crt1.o: in function `_start':
expand-response-params> /build/glibc-2.27/csu/../sysdeps/x86_64/start.S:101: undefined reference to `__libc_csu_fini'
expand-response-params> ld: /build/glibc-2.27/csu/../sysdeps/x86_64/start.S:102: undefined reference to `__libc_csu_init'
expand-response-params> collect2: error: ld returned 1 exit status

Here crt1.o from glibc-2.27 links against libc.so.6 from glibc-2.35.

This happens because ordering of `-L` (influences `libc.so` lookup) and
`-B` (influences `crt1.o` lookup) flags differs:

expand-response-params> -B/...-bootstrap-tools/lib
expand-response-params> -B/...-glibc-2.35/lib/
...
expand-response-params> -L/...-glibc-2.35/lib
expand-response-params> -L/...-bootstrap-tools/lib

The change makes consistent ordering of `-L`/`-B` and allows getting to
stage4 for `glibc-2.35` target.

+14 -4
+14 -4
pkgs/build-support/cc-wrapper/add-flags.sh
··· 33 # Export and assign separately in order that a failing $(..) will fail 34 # the script. 35 36 if [[ "$cInclude" = 1 ]] && [ -e @out@/nix-support/libc-cflags ]; then 37 NIX_CFLAGS_COMPILE_@suffixSalt@="$(< @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE_@suffixSalt@" 38 fi ··· 47 48 if [ -e @out@/nix-support/libcxx-ldflags ]; then 49 NIX_CXXSTDLIB_LINK_@suffixSalt@+=" $(< @out@/nix-support/libcxx-ldflags)" 50 - fi 51 - 52 - if [ -e @out@/nix-support/cc-cflags ]; then 53 - NIX_CFLAGS_COMPILE_@suffixSalt@="$(< @out@/nix-support/cc-cflags) $NIX_CFLAGS_COMPILE_@suffixSalt@" 54 fi 55 56 if [ -e @out@/nix-support/gnat-cflags ]; then
··· 33 # Export and assign separately in order that a failing $(..) will fail 34 # the script. 35 36 + # Currently bootstrap-tools does not split glibc, and gcc files into 37 + # separate directories. As a workaround we want resulting cflags to be 38 + # ordered as: crt1-cflags libc-cflags cc-cflags. Otherwise we mix crt/libc.so 39 + # from different libc as seen in 40 + # https://github.com/NixOS/nixpkgs/issues/158042 41 + # 42 + # Note that below has reverse ordering as we prepend flags one-by-one. 43 + # Once bootstrap-tools is split into different directories we can stop 44 + # relying on flag ordering below. 45 + 46 + if [ -e @out@/nix-support/cc-cflags ]; then 47 + NIX_CFLAGS_COMPILE_@suffixSalt@="$(< @out@/nix-support/cc-cflags) $NIX_CFLAGS_COMPILE_@suffixSalt@" 48 + fi 49 + 50 if [[ "$cInclude" = 1 ]] && [ -e @out@/nix-support/libc-cflags ]; then 51 NIX_CFLAGS_COMPILE_@suffixSalt@="$(< @out@/nix-support/libc-cflags) $NIX_CFLAGS_COMPILE_@suffixSalt@" 52 fi ··· 61 62 if [ -e @out@/nix-support/libcxx-ldflags ]; then 63 NIX_CXXSTDLIB_LINK_@suffixSalt@+=" $(< @out@/nix-support/libcxx-ldflags)" 64 fi 65 66 if [ -e @out@/nix-support/gnat-cflags ]; then