Skip to content
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

[clangd] support the zig c++ compiler wrapper #100759

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mrexodia
Copy link

@mrexodia mrexodia commented Jul 26, 2024

When using zig c++ for cross-compiling clangd removes the zig command (c++/cc) from the command line. Because of this the system include extraction fails. This change detects that the driver executable is named zig and adds cc or c++ back into the command line.

I don't think there is infrastructure to test this (since it would involve executing the zig executable), so I did not add any tests. Screenshot of the feature working on Visual Studio Code on Windows:

image

Trivial example project that sets up the cross compilation with CMake and generates compile_commands.json: https://github.com/mrexodia/zig-cross. You also need to use --query-driver=**/zig.exe.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Jul 26, 2024

@llvm/pr-subscribers-clangd

@llvm/pr-subscribers-clang-tools-extra

Author: Duncan Ogilvie (mrexodia)

Changes

When using zig c++ for cross-compiling clangd removes the zig command from the command line. Because of this the system include extraction fails. This change detects that the driver executable is named zig and adds cc or c++ back into the command line.

I don't think there is infrastructure to test this (since it would involve executing the zig executable), so I did not add any tests.


Full diff: https://github.com/llvm/llvm-project/pull/100759.diff

1 Files Affected:

  • (modified) clang-tools-extra/clangd/SystemIncludeExtractor.cpp (+11-1)
diff --git a/clang-tools-extra/clangd/SystemIncludeExtractor.cpp b/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
index d4b9b173d149d..c01dbea9c0c12 100644
--- a/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
+++ b/clang-tools-extra/clangd/SystemIncludeExtractor.cpp
@@ -389,7 +389,17 @@ extractSystemIncludesAndTarget(const DriverArgs &InputArgs,
     return std::nullopt;
   }
 
-  llvm::SmallVector<llvm::StringRef> Args = {Driver, "-E", "-v"};
+  llvm::SmallVector<llvm::StringRef> Args = {Driver};
+  // Support the zig compiler wrapper
+  auto DriverExecutable = llvm::sys::path::stem(Driver);
+  if (DriverExecutable == "zig") {
+    if (InputArgs.Lang == "c") {
+      Args.push_back("cc");
+    } else {
+      Args.push_back("c++");
+    }
+  }
+  Args.append({"-E", "-v"});
   Args.append(InputArgs.render());
   // Input needs to go after Lang flags.
   Args.push_back("-");

@duk-37
Copy link
Contributor

duk-37 commented Aug 4, 2024

This is a simple and nice change. Improving the developer experience and having things "just work" is always good.

@mrexodia
Copy link
Author

mrexodia commented Aug 9, 2024

@kazutakahirata @kadircet @sam-mccall it looks like you were recently touching this file, any chance for a review?

@mrexodia
Copy link
Author

mrexodia commented Oct 3, 2024

Ping!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants