Skip to content

llvmPackages.clang: embed resource directory in Clang libraries#420382

Merged
alyssais merged 1 commit intoNixOS:masterfrom
pcc:clang-resource-dir
Jul 2, 2025
Merged

llvmPackages.clang: embed resource directory in Clang libraries#420382
alyssais merged 1 commit intoNixOS:masterfrom
pcc:clang-resource-dir

Conversation

@pcc
Copy link
Contributor

@pcc pcc commented Jun 27, 2025

When Clang is statically linked against other programs they are unable to find the headers in Clang's resource directory. Typically the resource directory is found by searching a path relative to argv[0] but this would only really work for Clang itself due to each binary having a separate prefix (and not in Nix because of the full resource directory being split between multiple derivations and assembled in the wrapper).

Because users of Clang as a library typically only need include in order for parsing to succeed, let's set that as the resource directory. The LLVM patch to make this work was sent upstream in llvm/llvm-project#145996, I intend to land it upstream and drop it from this PR.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • Nixpkgs 25.11 Release Notes (or backporting 24.11 and 25.05 Nixpkgs Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
  • NixOS 25.11 Release Notes (or backporting 24.11 and 25.05 NixOS Release notes)
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other contributing documentation in corresponding paths.

Add a 👍 reaction to pull requests you find important.

@nixpkgs-ci nixpkgs-ci bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. 6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related labels Jun 27, 2025
@alyssais
Copy link
Member

Great! So do we draft this until the patch is accepted upstream?

@pcc pcc marked this pull request as draft June 27, 2025 22:11
@pcc
Copy link
Contributor Author

pcc commented Jun 27, 2025

Great! So do we draft this until the patch is accepted upstream?

Makes sense, done

@pcc pcc force-pushed the clang-resource-dir branch from d63e649 to 791e6b2 Compare July 1, 2025 04:06
@pcc pcc marked this pull request as ready for review July 1, 2025 04:07
@pcc
Copy link
Contributor Author

pcc commented Jul 1, 2025

Upstream patch landed, ready for review.

I noticed one more issue with absolute resource directory paths and sent a fix: llvm/llvm-project#146449

I wouldn't expect landing this PR to regress anything in the meantime though because resource directory lookup via the API doesn't work anyway.

When Clang is statically linked against other programs they are unable to
find the headers in Clang's resource directory. Typically the resource
directory is found by searching a path relative to argv[0] but this
would only really work for Clang itself due to each binary having a
separate prefix (and not in Nix because of the full resource directory
being split between multiple derivations and assembled in the wrapper).

Because users of Clang as a library typically only need include in order
for parsing to succeed, let's set that as the resource directory. The LLVM
patch to make this work was sent upstream in llvm/llvm-project#145996,
I intend to land it upstream and drop it from this PR.
@pcc pcc force-pushed the clang-resource-dir branch from 791e6b2 to d432a3d Compare July 1, 2025 04:16
@alyssais alyssais merged commit 8dc663e into NixOS:master Jul 2, 2025
26 of 27 checks passed
@dramforever dramforever mentioned this pull request Oct 3, 2025
1 task
dramforever added a commit to dramforever/nixpkgs that referenced this pull request Oct 4, 2025
Since NixOS#420382, we set an absolute default CLANG_RESOURCE_DIR for LLVM
versions >= 21, which breaks the bootstrap tools because the absolute
path was removed by nuke-ref while building and isn't there while
bootstrapping.

Pass -resource-dir explicitly while testing Clang to make it work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: llvm/clang Issues related to llvmPackages, clangStdenv and related 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants