clang-tools: Improve wrapper script, add tests#354755
clang-tools: Improve wrapper script, add tests#354755doronbehar merged 2 commits intoNixOS:stagingfrom
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/
clangdAs 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 :) |
alyssais
left a comment
There was a problem hiding this comment.
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
alyssais
left a comment
There was a problem hiding this comment.
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
|
clangd 21 has a new BuiltinHeaders option that, when used alongside the unwrapped package, forms a better solution than my previous
|
|
ping ping 👀 |
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: |
Hi, I'd like to nominate myself - I've been submitting pull requests since 2020, started by packaging [netris](NixOS/nixpkgs#85382) and then went on to improving [LXD](NixOS/nixpkgs#89540), [packaging](NixOS/nixpkgs#91182) and [maintaining](https://github.com/NixOS/nixpkgs/pulls?q=author%3APatryk27+pcloud) pCloud (which, amazingly enough, required fixing a bug in [patchelf](NixOS/patchelf#544) itself!), and overall I've been implementing various quality of live improvements [here](NixOS/nixpkgs#207466) and [there](NixOS/nixpkgs#354755). I've given [a talk about my Nix-driven blog](https://www.youtube.com/watch?v=_7wqXN-7ebw&t=6695s) during NixCon 2024 in Berlin, so it's possible we've already met irl! Outside of Nix, I'm the maintainer of Rust's [AVR backend](rust-lang/rust#131651), with commits and merging rights to [LLVM](https://github.com/llvm/llvm-project/pulls?q=author%3APatryk27) - just adding as an extra "in case it helps" mention 😇 Couple of weeks ago I decided to shift my priorities from working on custom projects to helping the communities I'm a part of more - having committer access to nixpkgs would allow me to make a greater impact as I intend to focus on reviewing and merging both ongoing and stale pull requests, since it seems there's always not enough people willing to review stuff! Thanks, Patryk :-)
There was a problem hiding this comment.
I'm not a clang user, but I came after the "PRs in distress post", so I cannot review the changes to wrapper script and I haven't read thoroughly the comments. However these are my 2 cents:
It could be easier to review this if the changes were a bit more decoupled to these commits:
clang-tools: use finalAttrsclang-tools: add passthru.testsclang-tools: improve wrapper script & installPhase
In the 1st commit, I'd also remove the usage of ${finalAttrs.unwrapped} and the unwrapped argument to mkDerivation, in favor of simply ${clang-unwrapped} - it will be easier to figure out how to override it by looking at the expression.
|
@doronbehar thanks for taking a look!
In that case what should exactly remain in that commit? 🤔 (i.e. what would use those |
Haha you are right :) If you have nothing to use |
1db896b to
c198984
Compare
|
Okie, done! Eventually I've decided to leave the |
doronbehar
left a comment
There was a problem hiding this comment.
Looks good I guess. Since there are many people who have reviewed this, and no body seems to have a strong objection, I think this should be good to go after the last small fix requested. Please write a message here if you think something in the PR is wrong.
c198984 to
6a3f89d
Compare
6a3f89d to
0951974
Compare
|
In the last force push I only made the |
No problemo, thank you so much for taking a look and merging this! |
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/dev-shell-flake-clangd-cant-find-gnu-stubs-32-h/51067/2 |
clang-tools invokes clangd through a wrapper script whose sole purpose is to extend
CPATHandCPLUS_INCLUDE_PATHenvs, so that clangd is later able to find built-in and stdlib headers.This script does two things wrong:
We extend
CPATHet al with entries from both@clang@/nix-support/libc-cflagsand@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
CPATHet al unconditionally, breaking the--query-driveroption. I'm not sure what's the perfect solution here, but skipping this logic when we detect--query-drivershould be good enough. I mean, ideally we wouldn't be playing withCPATHetc. whatsoever, but well.Closes #351962
Closes #348791
Closes #345704
Things done
nix.conf? (See Nix manual)sandbox = relaxedsandbox = truenix-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.