Skip to content

Commit ba486b3

Browse files
committed
feat(eda): bake FHS search paths into the cc-wrapper
Bake the search paths into the wrapper instead, so the compiler behaves like a system compiler regardless of the environment it is invoked from. Signed-off-by: Douglas Reis <doreis@lowrisc.org>
1 parent 1b7a992 commit ba486b3

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

‎lib/edaFhsPackages.nix‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@
3737
'';
3838
};
3939

40+
# The Nix cc-wrapper knows nothing about the FHS tree. buildFHSEnv teaches it
41+
# by exporting `NIX_CFLAGS_COMPILE=-idirafter /usr/include` and
42+
# `NIX_LDFLAGS=-L/usr/lib ...` from the profile, so any build that sanitises
43+
# the environment before invoking the compiler loses /usr/include and
44+
# /usr/lib. Bazel is the case in point: with `--incompatible_strict_action_env`
45+
# and an explicit action `env`, the compiler is handed nothing but PATH and
46+
# HOME, and a Bazel-driven Verilator build fails on `#include <libelf.h>` even
47+
# though libelf is right there in the sandbox.
48+
#
49+
# Bake the FHS search paths into the wrapper instead, so the compiler behaves
50+
# like a system compiler no matter what environment it is invoked from. The
51+
# wrapper's add-flags.sh reads `nix-support/cc-cflags` and `cc-ldflags`
52+
# unconditionally — unlike the generic `NIX_*` variables, which it only
53+
# consumes when the matching `NIX_{CC,BINTOOLS}_WRAPPER_TARGET_HOST_<salt>`
54+
# role variable is also set. `-idirafter` keeps /usr/include last in the
55+
# search order, after the wrapper's own libc headers and any package's -I.
56+
# Same intent as the pkg-config and aclocal wrappers below.
57+
cc-fhs = pkgs.stdenv.cc.override (old: {
58+
extraBuildCommands =
59+
(old.extraBuildCommands or "")
60+
+ ''
61+
echo "-idirafter /usr/include" >> $out/nix-support/cc-cflags
62+
echo "-L/usr/lib -L/usr/lib32" >> $out/nix-support/cc-ldflags
63+
'';
64+
});
65+
4066
# Bazel filters out all environment including PKG_CONFIG_PATH. Append this inside wrapper.
4167
pkg-config-patched = pkgs.pkg-config.override {
4268
extraBuildCommands = ''
@@ -82,7 +108,9 @@ in
82108
lsb-release # some tools probe the host OS even when unsupported
83109

84110
# Toolchain (tools invoke a compiler/linker for DPI, cosim models, etc.).
85-
stdenv.cc
111+
# Wrapped above so it finds /usr/include and /usr/lib without relying on
112+
# the profile's NIX_CFLAGS_COMPILE / NIX_LDFLAGS reaching the compiler.
113+
cc-fhs
86114
# A modern, *unwrapped* binutils so /usr/bin/{ld,as,ar,objdump,...} are
87115
# plain system-style tools. stdenv.cc alone provides a wrapped `ld` that
88116
# injects Nix-specific dynamic-linker/rpath flags — unwanted by vendor

0 commit comments

Comments
 (0)