-
-
Notifications
You must be signed in to change notification settings - Fork 15k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clang-tools: Improve wrapper script, add tests #354755
base: staging
Are you sure you want to change the base?
Conversation
I'm not sure how great of a fix this is. My question is: why do we even need the wrapper's shenanigans in the first place? clangd is very good at finding out about system headers and compiler's resource directories given just a EDIT: Forgot to mention, the fix did work. |
And as mentioned in the other issue raised on this matter, a clangd dev does seem to agree that adding these implicit paths is at the very least unusual. |
Thanks for your work on this, @Patryk27. To me the Being ignorant of the circumstances, what's in Is there an easy way to test the PR? I have a copy of the clang-tools |
Well I copy-pasted the code from the PR and hacked at it until it "worked".
So IMO it'd be good to merge it, but a note explaining the workaround with the With the (hacked up) PR code, though, I'm not able to run $ clang-format
/nix/store/cb77k2bnkb1fvr25pb9chv3baj1x6x1x-clang-tools-18.1.8/bin/clang-format: line 44: /nix/store/nzyxg89vqxhdw884n50c2p5k1vrgibka-clang-unwrapped-with-lib/bin/clang-format: No such file or directory
$ which clang-format
/nix/store/cb77k2bnkb1fvr25pb9chv3baj1x6x1x-clang-tools-18.1.8/bin/clang-format
$ ls -l /nix/store/cb77k2bnkb1fvr25pb9chv3baj1x6x1x-clang-tools-18.1.8/bin/clang-format
lrwxrwxrwx 1 root root 6 led 1 1970 /nix/store/cb77k2bnkb1fvr25pb9chv3baj1x6x1x-clang-tools-18.1.8/bin/clang-format -> clangd
$ ls -l /nix/store/cb77k2bnkb1fvr25pb9chv3baj1x6x1x-clang-tools-18.1.8/bin/clangd
-r-xr-xr-x 1 root root 1499 led 1 1970 /nix/store/cb77k2bnkb1fvr25pb9chv3baj1x6x1x-clang-tools-18.1.8/bin/clangd
$ cat /nix/store/cb77k2bnkb1fvr25pb9chv3baj1x6x1x-clang-tools-18.1.8/bin/clangd
#!/nix/store/1xhds5s320nfp2022yjah1h7dpv8qqns-bash-5.2p32/bin/sh
buildcpath() {
local path after
while (( $# )); do
case $1 in
-isystem)
shift
path=$path${path:+':'}$1
;;
-idirafter)
shift
after=$after${after:+':'}$1
;;
esac
shift
done
echo $path${after:+':'}$after
}
# When user passes `--query-driver`, avoid extending `CPATH` et al, since we
# don't want to "infect" user-specified toolchain and headers with our stuff.
extendcpath=true
for arg in "$@"; do
if [[ "${arg}" == \-\-query\-driver* ]]; then
extendcpath=false
fi
done
if [ "$extendcpath" = true ]; then
export CPATH=${CPATH}${CPATH:+':'}$(buildcpath ${NIX_CFLAGS_COMPILE} \
$(</nix/store/nr95z75f3hj5z7a0bddqpx9wjm47mvbd-clang-wrapper-17.0.6/nix-support/libc-cflags))
export CPLUS_INCLUDE_PATH=${CPLUS_INCLUDE_PATH}${CPLUS_INCLUDE_PATH:+':'}$(buildcpath ${NIX_CFLAGS_COMPILE} \
$(</nix/store/nr95z75f3hj5z7a0bddqpx9wjm47mvbd-clang-wrapper-17.0.6/nix-support/libcxx-cxxflags) \
$(</nix/store/nr95z75f3hj5z7a0bddqpx9wjm47mvbd-clang-wrapper-17.0.6/nix-support/libc-cflags))
fi
exec -a "$0" /nix/store/nzyxg89vqxhdw884n50c2p5k1vrgibka-clang-unwrapped-with-lib/bin/$(basename $0) "$@"
$ ls /nix/store/nzyxg89vqxhdw884n50c2p5k1vrgibka-clang-unwrapped-with-lib/bin/
clangd As you can see, Also note that paths passed to the |
You're right, my patch breaks this (including other tools like As for the overall approach to the fix, I'm not 100% sold on it either - I just cobbled together something that works better than the current approach; I'm not sure why we need the |
Alright, I pushed a new commit that uses a bit less hacky approach - I'm also open to other approaches, just don't have any other idea myself. (btw, I'll squash the commits later - I've pushed it as a separate commit to make it easier to review) |
Just loaded the changes, both Regarding an overall better / "correct" approach, ideally you could work with |
I'm personally happy with the changes here. Would like to see this merged :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please squash the commits and update the commit message for the new approach? Otherwise this is quite hard to review.
d8d241b
to
3264fa9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit message doesn't explain anything at the moment. The details you put in the PR body would be a good start, but the addition of the lib symlink also requires explanation.
3264fa9
to
fd38da8
Compare
So glad to see progress being made here! Really useful change for embedded developers using nix :) |
081b1d7
to
6337c6a
Compare
6337c6a
to
248d843
Compare
clang-tools invokes clangd through a wrapper script whose sole purpose is to extend CPATH and CPLUS_INCLUDE_PATH envs, so that clangd is later able to find built-in headers. This script does two things wrong: - We extend CPATH et al with both `@clang@/nix-support/libc-cflags` and `@clang@/resource-root/include`, which is a self-made problem, since clangd already has header-detection logic. -We extend CPATH et al unconditionally, breaking the `--query-driver` option. I'm not sure what's the perfect solution here, but skipping this logic when we detect `--query-driver` should be good enough in practice.
248d843
to
7a5ea9f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite a lot of rebuilds so should go to staging.
Fair enough - changed! |
N.B. it's kinda weird it causes so many rebuilds imo |
clang-tools invokes clangd through a wrapper script whose sole purpose is to extend
CPATH
andCPLUS_INCLUDE_PATH
envs, so that clangd is later able to find built-in and stdlib headers.This script does two things wrong:
We extend
CPATH
et al with entries from both@clang@/nix-support/libc-cflags
and@clang@/resource-root/include
, which is kind of a self-made problem, since clangd already has some header auto-detection logic which we (used to) circumvent (for more details, see the diff).We extend
CPATH
et al unconditionally, breaking the--query-driver
option. I'm not sure what's the perfect solution here, but skipping this logic when we detect--query-driver
should be good enough. I mean, ideally we wouldn't be playing withCPATH
etc. whatsoever, but well.Closes #351962
Closes #348791
Closes #345704
Things done
nix.conf
? (See Nix manual)sandbox = relaxed
sandbox = true
nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)Add a 👍 reaction to pull requests you find important.