Skip to content

Commit

Permalink
gcc: provide both native and cross forms of gcc.libs libraries
Browse files Browse the repository at this point in the history
I would like to use --sysroot=/nix/store/does/not/exist hack
for both `gcc` and `clang` drivers to remove default include
(and library) search paths when we override them with libc.

For `gcc` it works as is. But for `clang` it also drops some of
`gcc` search paths`. Let'sconsider 2 lookups.

Successful lookup (no `--sysroot`):

    $ printf "int main(){}" | clang++ -x c++ - -Wl,--verbose |& grep -F stdc++
    attempt to open /nix/store/...-gcc-11.3.0/lib/gcc/x86_64-unknown-linux-gnu/11.3.0/libstdc++.so failed
    attempt to open /nix/store/...-gcc-11.3.0-lib/x86_64-unknown-linux-gnu/lib/libstdc++.so failed
    attempt to open /nix/store/...-clang-11.1.0-lib/lib/libstdc++.so failed
    attempt to open /nix/store/...-gcc-11.3.0/lib64/gcc/x86_64-unknown-linux-gnu/11.3.0/libstdc++.so failed
    attempt to open /nix/store/...-gcc-11.3.0/lib64/gcc/x86_64-unknown-linux-gnu/11.3.0/../../../../lib64/libstdc++.so failed
    /nix/store/...-gcc-11.3.0/lib64/gcc/x86_64-unknown-linux-gnu/11.3.0/../../../../lib64/libstdc++.a

Failed lookup (has `--sysroot`):

    $ printf "int main(){}" | clang++ --sysroot=/does/not/exist -x c++ - -Wl,--verbose |& grep -F stdc++
    attempt to open /nix/store/...-gcc-11.3.0/lib/gcc/x86_64-unknown-linux-gnu/11.3.0/libstdc++.so failed
    attempt to open /nix/store/...-gcc-11.3.0-lib/x86_64-unknown-linux-gnu/lib/libstdc++.so failed
    attempt to open /nix/store/...-clang-11.1.0-lib/lib/libstdc++.so failed
    attempt to open /nix/store/...-gcc-11.3.0/lib64/gcc/x86_64-unknown-linux-gnu/11.3.0/libstdc++.so failed
    /nix/store/...-binutils-2.39/bin/ld: cannot find -lstdc++: No such file or directory

Note how `clang` starts the search roughly from
`gcc-11.3.0-lib/x86_64-unknown-linux-gnu/lib` in both cases. I think
it's our preferred location for both native and cross cases.

The change adds such a symlink:

    `gcc-11.3.0-lib` -> `gcc-11.3.0-lib/x86_64-unknown-linux-gnu`
  • Loading branch information
trofi committed Jan 5, 2023
1 parent 4acd5a9 commit 2c931bd
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/10/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/11/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/12/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/4.8/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/4.9/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/6/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/7/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ stdenv.mkDerivation ({
;

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/8/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
1 change: 1 addition & 0 deletions pkgs/development/compilers/gcc/9/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ stdenv.mkDerivation ({
};

targetConfig = if targetPlatform != hostPlatform then targetPlatform.config else null;
targetPlatformConfig = targetPlatform.config;

buildFlags = optional
(targetPlatform == hostPlatform && hostPlatform == buildPlatform)
Expand Down
11 changes: 11 additions & 0 deletions pkgs/development/compilers/gcc/builder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ preInstall() {
ln -s lib "$out/${targetConfig}/lib32"
ln -s lib "${!outputLib}/${targetConfig}/lib32"
fi

# cc-wrappers uses --sysroot=/nix/store/does/not/exist as a way to
# drop default sysheaders search path. Unfortunately that switches
# clang++ into searching libraries in gcc in cross-compiler paths:
# from ${!outputLib}/lib (native)
# to ${!outputLib}/${targetPlatformConfig}/lib
# We create the symlink to make both native and cross paths
# available even if the toolchain is not the cross-compiler.
if [ ! -e ${!outputLib}/${targetPlatformConfig} ] ; then
ln -s . ${!outputLib}/${targetPlatformConfig}
fi
}


Expand Down

0 comments on commit 2c931bd

Please sign in to comment.